-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "mutex.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |