Skip to content

Commit

Permalink
[QUEUE/MUTEX] header file setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Akashem06 committed Jun 2, 2024
1 parent e01dca5 commit a493ab6
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 1 deletion.
16 changes: 16 additions & 0 deletions inc/mutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef MUTEX_H
#define MUTEX_H

#include "task.h"

typedef struct {
bool locked;
bool original_polarity;
TaskControlBlock *owner;
} Mutex;

void init_mutex(Mutex* mutex);
bool lock_mutex(Mutex* mutex);
void unlock_mutex(Mutex* mutex);

#endif
19 changes: 19 additions & 0 deletions inc/queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef QUEUE_H
#define QUEUE_H

#include <cstdint>
#include <stdbool.h>

typedef struct {
uint8_t *buffer;
uint32_t head;
uint32_t tail;
uint32_t maxLen;
bool full;
} Queue;

void init_queue(Queue *q, uint8_t *buffer, uint32_t size);
bool enqueue(Queue *q, uint8_t data);
bool dequeue(Queue *q, uint8_t *data);

#endif
10 changes: 10 additions & 0 deletions inc/scheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef SCHEDULER_H
#define SCHEDULER_H

#include "task.h"

void init_scheduler();
void start_scheduler();
void create_task(TaskFunction taskFunction, uint8_t priority, const char *name);

#endif
27 changes: 27 additions & 0 deletions inc/task.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef TASK_H
#define TASK_H

#include <cstdint>

typedef enum {
TASK_READY,
TASK_RUNNING,
TASK_BLOCKED,
TASK_SUSPENDED
} TaskState;

typedef void (*TaskFunction)(void);

typedef struct {
TaskFunction taskFunction; // Task function pointer
uint32_t *stackPointer; // Pointer to the task's stack
TaskState state; // Current state of the task
uint8_t priority; // Task priority
uint32_t delay; // Delay for task to unblock (for time-delayed tasks)
char taskName[16]; // Task name for debugging
} TaskControlBlock;

#define MAX_TASKS 10
#define STACK_SIZE 1024

#endif
2 changes: 1 addition & 1 deletion scripts/check_format.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

FILES=$(find . -name "*.c" -o -name "*.h")
FILES=$(find . -name "*.c" -o -name "*.h" -not -path "./cpputest/*")

format_check=0

Expand Down
1 change: 1 addition & 0 deletions src/mutex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "mutex.h"
33 changes: 33 additions & 0 deletions src/queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "queue.h"

void init_queue(Queue *q, uint8_t *buffer, uint32_t size) {
q->buffer = buffer;
q->maxLen = size;
q->head = 0;
q->tail = 0;
q->full = false;
}

bool enqueue(Queue *q, uint8_t data) {
if (q->full) {
return false;
}

q->buffer[q->head] = data;
q->head = (q->head + 1) % q->maxLen;
q->full = (q->head == q->tail);
}


bool dequeue(Queue *q, uint8_t *data) {
if (q->head == q->tail && !q->full) {
enableInterrupts();
return false;
}

*data = q->buffer[q->tail];
q->tail = (q->tail + 1) % q->maxLen;
q->full = false;

return true;
}
28 changes: 28 additions & 0 deletions src/scheduler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "scheduler.h"

TaskControlBlock task_list[MAX_TASKS];
uint8_t task_stacks[MAX_TASKS][STACK_SIZE];

void init_scheduler() {
for (int i = 0; i < MAX_TASKS; i++) {
task_list[i].state = TASK_SUSPENDED;
task_list[i].stackPointer = &task_stacks[i][STACK_SIZE - 1];
}
}

void create_task(TaskFunction task_function, uint8_t priority, const char *name) {
for (int i = 0; i < MAX_TASKS; i++) {
if (task_list[i].state == TASK_SUSPENDED) {
task_list[i].taskFunction = task_function;
task_list[i].priority = priority;
task_list[i].state = TASK_READY;
// Copies over name into taskName and states string end using \0
strncpy(task_list[i].taskName, name, sizeof(task_list[i].taskName));
task_list[i].taskName[sizeof(task_list[i].taskName) - 1] = '\0';

task_list[i].stackPointer = &task_stacks[i][STACK_SIZE - 1];

break;
}
}
}

0 comments on commit a493ab6

Please sign in to comment.