From a493ab687e96ca1cbb5270ea78d6668105ebba32 Mon Sep 17 00:00:00 2001 From: Aryan Kashem Date: Sun, 2 Jun 2024 00:59:49 -0400 Subject: [PATCH] [QUEUE/MUTEX] header file setup --- inc/mutex.h | 16 ++++++++++++++++ inc/queue.h | 19 +++++++++++++++++++ inc/scheduler.h | 10 ++++++++++ inc/task.h | 27 +++++++++++++++++++++++++++ scripts/check_format.sh | 2 +- src/mutex.c | 1 + src/queue.c | 33 +++++++++++++++++++++++++++++++++ src/scheduler.c | 28 ++++++++++++++++++++++++++++ 8 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 inc/mutex.h create mode 100644 inc/queue.h create mode 100644 inc/scheduler.h create mode 100644 inc/task.h create mode 100644 src/mutex.c create mode 100644 src/queue.c create mode 100644 src/scheduler.c diff --git a/inc/mutex.h b/inc/mutex.h new file mode 100644 index 0000000..ccd2dee --- /dev/null +++ b/inc/mutex.h @@ -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 \ No newline at end of file diff --git a/inc/queue.h b/inc/queue.h new file mode 100644 index 0000000..68d418e --- /dev/null +++ b/inc/queue.h @@ -0,0 +1,19 @@ +#ifndef QUEUE_H +#define QUEUE_H + +#include +#include + +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 \ No newline at end of file diff --git a/inc/scheduler.h b/inc/scheduler.h new file mode 100644 index 0000000..807baa0 --- /dev/null +++ b/inc/scheduler.h @@ -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 \ No newline at end of file diff --git a/inc/task.h b/inc/task.h new file mode 100644 index 0000000..aa2deeb --- /dev/null +++ b/inc/task.h @@ -0,0 +1,27 @@ +#ifndef TASK_H +#define TASK_H + +#include + +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 \ No newline at end of file diff --git a/scripts/check_format.sh b/scripts/check_format.sh index 8e9d5ce..61757af 100644 --- a/scripts/check_format.sh +++ b/scripts/check_format.sh @@ -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 diff --git a/src/mutex.c b/src/mutex.c new file mode 100644 index 0000000..008933e --- /dev/null +++ b/src/mutex.c @@ -0,0 +1 @@ +#include "mutex.h" \ No newline at end of file diff --git a/src/queue.c b/src/queue.c new file mode 100644 index 0000000..8510720 --- /dev/null +++ b/src/queue.c @@ -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; +} \ No newline at end of file diff --git a/src/scheduler.c b/src/scheduler.c new file mode 100644 index 0000000..70f47d0 --- /dev/null +++ b/src/scheduler.c @@ -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; + } + } +}