From 3b4b045db7aa1dd27c422aee74826882a456e8e5 Mon Sep 17 00:00:00 2001 From: NArmistead Date: Wed, 1 Jul 2026 22:26:34 -0700 Subject: [PATCH 1/4] checkpoint: working schedler with fixed table --- pool_allocator/pool_allocator.c | 96 +++++++++++++++++++++++++ pool_allocator/pool_allocator.h | 80 +++++++++++++++++++++ scheduler/scheduler.c | 121 ++++++++++++++++++++++++++++++++ scheduler/scheduler.h | 115 ++++++++++++++++++++++++++++++ 4 files changed, 412 insertions(+) create mode 100644 pool_allocator/pool_allocator.c create mode 100644 pool_allocator/pool_allocator.h create mode 100644 scheduler/scheduler.c create mode 100644 scheduler/scheduler.h diff --git a/pool_allocator/pool_allocator.c b/pool_allocator/pool_allocator.c new file mode 100644 index 0000000..009ef97 --- /dev/null +++ b/pool_allocator/pool_allocator.c @@ -0,0 +1,96 @@ +/** + ****************************************************************************** + * @file : pool_allocator.c + * @brief : TODO + ****************************************************************************** + * @copyright + * + * Copyright (c) 2025 Sun Devil Rocketry. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is covered under the + * BSD-3-Clause. + * + * https://opensource.org/license/bsd-3-clause + * + ****************************************************************************** + */ + +/*------------------------------------------------------------------------------ + Standard Includes +------------------------------------------------------------------------------*/ + + +/*------------------------------------------------------------------------------ + Project Includes +------------------------------------------------------------------------------*/ +#include "main.h" +#include "pool_allocator.h" +#include "error_sdr.h" +#include "debug_sdr.h" + + + +/*------------------------------------------------------------------------------ + Global Variables +------------------------------------------------------------------------------*/ +uint8_t pool_test[POOL_SIZE]; + + +/*------------------------------------------------------------------------------ + API Functions +------------------------------------------------------------------------------*/ +Pool pool_init + ( + uint8_t* pool_space, + size_t size + ) +{ +Pool pool; +pool.free_chunk = (Chunk*)pool_space; +pool.chunk_arr = pool.free_chunk; + +for ( size_t i = 0; i < size - 1; i++ ) + { + pool.chunk_arr[i].next = &pool.chunk_arr[i + 1]; + } +pool.chunk_arr[size - 1].next = NULL; + +return pool; +} + + +void* pool_alloc + ( + Pool* pool + ) +{ +if ( pool == NULL || pool->free_chunk == NULL) return NULL; + +Chunk* next_free = pool->free_chunk; +pool->free_chunk = pool->free_chunk->next; + +return next_free; + +} + + +void pool_free + ( + Pool* pool, + void* ptr + ) +{ +debug_assert(pool == NULL || ptr == NULL, ERROR_UNKNOWN_FATAL_ERROR); +Chunk* chunk = ptr; +chunk->next = pool->free_chunk; +pool->free_chunk = chunk; + +} + + +/******************************************************************************* +* END OF FILE * +*******************************************************************************/ \ No newline at end of file diff --git a/pool_allocator/pool_allocator.h b/pool_allocator/pool_allocator.h new file mode 100644 index 0000000..c3eba3d --- /dev/null +++ b/pool_allocator/pool_allocator.h @@ -0,0 +1,80 @@ +/** + ****************************************************************************** + * @file : pool_allocator.h + * @brief : TODO + ****************************************************************************** + * @copyright + * + * Copyright (c) 2025 Sun Devil Rocketry. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is covered under the + * BSD-3-Clause. + * + * https://opensource.org/license/bsd-3-clause + * + ****************************************************************************** + @verbatim + ============================================================================== + ##### Pool allocator features ##### + ============================================================================== + TODO + ****************************************************************************** + @endverbatim + */ + + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef POOL_ALLOCATOR_H +#define POOL_ALLOCATOR_H + +#ifdef __cplusplus +extern "C" { +#endif + + +/*------------------------------------------------------------------------------ + Includes +------------------------------------------------------------------------------*/ +#include +#include + + +/*------------------------------------------------------------------------------ + Macros +------------------------------------------------------------------------------*/ +#define CHUNK_SIZE 12 /* Unofficially the size of SHEDULED_TASK for now */ +#define POOL_SIZE ( CHUNK_SIZE * 16 ) + +/*------------------------------------------------------------------------------ + Typedefs +------------------------------------------------------------------------------*/ + +typedef union + { + struct Chunk* next; + uint8_t arr[CHUNK_SIZE]; + } Chunk; + +typedef struct + { + Chunk* free_chunk; + Chunk* chunk_arr; + } Pool; + + +/*------------------------------------------------------------------------------ + Function Prototypes +------------------------------------------------------------------------------*/ + + +#ifdef __cplusplus +} +#endif +#endif /* POOL_ALLOCATOR_H */ + +/******************************************************************************* +* END OF FILE * +*******************************************************************************/ \ No newline at end of file diff --git a/scheduler/scheduler.c b/scheduler/scheduler.c new file mode 100644 index 0000000..356cf4c --- /dev/null +++ b/scheduler/scheduler.c @@ -0,0 +1,121 @@ +/** + ****************************************************************************** + * @file : scheduler.c + * @brief : TODO + ****************************************************************************** + * @copyright + * + * Copyright (c) 2025 Sun Devil Rocketry. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is covered under the + * BSD-3-Clause. + * + * https://opensource.org/license/bsd-3-clause + * + ****************************************************************************** + */ + +/*------------------------------------------------------------------------------ + Standard Includes +------------------------------------------------------------------------------*/ + + +/*------------------------------------------------------------------------------ + Project Includes +------------------------------------------------------------------------------*/ +#include "main.h" +#include "scheduler.h" +#include "error_sdr.h" +#include "pool_allocator.h" + +/*------------------------------------------------------------------------------ + Global Variables +------------------------------------------------------------------------------*/ +extern SCHEDULED_TASK task_scheduler_table[]; +extern uint8_t task_scheduler_table_size; + + +/*------------------------------------------------------------------------------ + API Functions +------------------------------------------------------------------------------*/ + +TASK_STATUS schedule_task + ( + TASK_TYPE task, + uint32_t scheduled_systick + ) +{ + +/* CRITICAL SECTION BEGIN ?*/ + +for ( uint8_t i = 0; i < task_scheduler_table_size; i++ ) + { + SCHEDULED_TASK* tabled_task = &task_scheduler_table[i]; + if ( tabled_task->task == task ) + { + if ( tabled_task->scheduled_systick != 0 ) + { + return TASK_SCHEDULER_OCCUPIED; + } + + tabled_task->scheduled_systick = scheduled_systick; + return TASK_SCHEDULER_OK; + } + } + +/* CRITICAL SECTION END */ + +return TASK_SCHEDULER_NOT_FOUND; +} + + +TASK_STATUS task_check_and_execute + ( + void + ) +{ +uint32_t systick_now = HAL_GetTick(); +uint16_t status = TASK_SCHEDULER_OK; + +for ( uint8_t i = 0; i < task_scheduler_table_size; i++ ) + { + SCHEDULED_TASK* tabled_task = &task_scheduler_table[i]; + if ( tabled_task->scheduled_systick <= systick_now && tabled_task->scheduled_systick != 0 ) + { + /* At or passed scheduled tick, so execute callback */ + uint16_t callback_return = tabled_task->callback(); + + /* Reset systick*/ + tabled_task->scheduled_systick = 0; + + if ( callback_return ) + { + status = TASK_SCHEDULER_CALLBACK_ERROR; + } + } + } + +return status; +} + + +void task_scheduler_IT_handler + ( + void + ) +{ +TASK_STATUS status = task_check_and_execute(); +if ( status == TASK_SCHEDULER_CALLBACK_ERROR ) + { + error_fail_fast( ERROR_UNKNOWN_FATAL_ERROR ); // TODO + } +} + + + +/******************************************************************************* +* END OF FILE * +*******************************************************************************/ \ No newline at end of file diff --git a/scheduler/scheduler.h b/scheduler/scheduler.h new file mode 100644 index 0000000..4345087 --- /dev/null +++ b/scheduler/scheduler.h @@ -0,0 +1,115 @@ +/** + ****************************************************************************** + * @file : scheduler.h + * @brief : TODO + ****************************************************************************** + * @copyright + * + * Copyright (c) 2025 Sun Devil Rocketry. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is covered under the + * BSD-3-Clause. + * + * https://opensource.org/license/bsd-3-clause + * + ****************************************************************************** + @verbatim + ============================================================================== + ##### Task scheduler features ##### + ============================================================================== + TODO + ****************************************************************************** + @endverbatim + */ + + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef SCHEDULER_H +#define SCHEDULER_H + +#ifdef __cplusplus +extern "C" { +#endif + + +/*------------------------------------------------------------------------------ + Includes +------------------------------------------------------------------------------*/ +#include +#include + + +/*------------------------------------------------------------------------------ + Typedefs +------------------------------------------------------------------------------*/ + +typedef enum + { + TASK_START_BUZZ, + TASK_STOP_BUZZ + } TASK_TYPE; + +typedef enum + { + TASK_SCHEDULER_OK, + TASK_SCHEDULER_PASS, + TASK_SCHEDULER_FAIL, + TASK_SCHEDULER_INVALID_SYSTICK, + TASK_SCHEDULER_NOT_FOUND, + TASK_SCHEDULER_OCCUPIED, + TASK_SCHEDULER_CALLBACK_ERROR + } TASK_STATUS; + +typedef uint16_t (*task_callback)(void); /* NA TODO: error handling (any negative returns?)*/ + +typedef struct + { + const TASK_TYPE task; + uint32_t scheduled_systick; + const task_callback callback; + } SCHEDULED_TASK; + +typedef struct + { + SCHEDULED_TASK task; + struct TASK_LLIST* next; + } TASK_LLIST; + +/*------------------------------------------------------------------------------ + Macros +------------------------------------------------------------------------------*/ + + + +/*------------------------------------------------------------------------------ + Function Prototypes +------------------------------------------------------------------------------*/ + +TASK_STATUS schedule_task + ( + TASK_TYPE task, + uint32_t scheduled_systick + ); + + +TASK_STATUS task_check_and_execute + ( + void + ); + +void task_scheduler_IT_handler + ( + void + ); + +#ifdef __cplusplus +} +#endif +#endif /* SCHEDULER_H */ + +/******************************************************************************* +* END OF FILE * +*******************************************************************************/ \ No newline at end of file From 9565782b629715f4c414a212f40282183f8ec37a Mon Sep 17 00:00:00 2001 From: NArmistead Date: Thu, 2 Jul 2026 21:39:56 -0700 Subject: [PATCH 2/4] Linked list scheduler, pool allocator fixes --- error_sdr/error_sdr.c | 1 + pool_allocator/pool_allocator.c | 19 ++++--- pool_allocator/pool_allocator.h | 27 +++++++--- scheduler/scheduler.c | 91 ++++++++++++++++++++------------- scheduler/scheduler.h | 50 +++++++----------- 5 files changed, 107 insertions(+), 81 deletions(-) diff --git a/error_sdr/error_sdr.c b/error_sdr/error_sdr.c index 064640f..d12da36 100644 --- a/error_sdr/error_sdr.c +++ b/error_sdr/error_sdr.c @@ -322,6 +322,7 @@ static void dflt_error_handler volatile ERROR_CODE error_code ) { +__disable_irq(); led_set_color( LED_RED ); while(1); /* Control flow trap */ diff --git a/pool_allocator/pool_allocator.c b/pool_allocator/pool_allocator.c index 009ef97..010803c 100644 --- a/pool_allocator/pool_allocator.c +++ b/pool_allocator/pool_allocator.c @@ -32,11 +32,9 @@ #include "debug_sdr.h" - /*------------------------------------------------------------------------------ Global Variables ------------------------------------------------------------------------------*/ -uint8_t pool_test[POOL_SIZE]; /*------------------------------------------------------------------------------ @@ -44,19 +42,20 @@ uint8_t pool_test[POOL_SIZE]; ------------------------------------------------------------------------------*/ Pool pool_init ( - uint8_t* pool_space, + uint8_t* pool_memory, size_t size ) { Pool pool; -pool.free_chunk = (Chunk*)pool_space; +size_t block_count = size / sizeof(Chunk); +pool.free_chunk = (Chunk*)pool_memory; pool.chunk_arr = pool.free_chunk; -for ( size_t i = 0; i < size - 1; i++ ) +for ( size_t i = 0; i < block_count - 1; i++ ) { pool.chunk_arr[i].next = &pool.chunk_arr[i + 1]; } -pool.chunk_arr[size - 1].next = NULL; +pool.chunk_arr[block_count - 1].next = NULL; return pool; } @@ -67,7 +66,10 @@ void* pool_alloc Pool* pool ) { -if ( pool == NULL || pool->free_chunk == NULL) return NULL; +if ( pool == NULL || pool->free_chunk == NULL) + { + return NULL; + } Chunk* next_free = pool->free_chunk; pool->free_chunk = pool->free_chunk->next; @@ -83,7 +85,8 @@ void pool_free void* ptr ) { -debug_assert(pool == NULL || ptr == NULL, ERROR_UNKNOWN_FATAL_ERROR); +/* ptr can be NULL, but I want to check for now */ +debug_assert(pool != NULL && ptr != NULL, ERROR_UNKNOWN_FATAL_ERROR); Chunk* chunk = ptr; chunk->next = pool->free_chunk; pool->free_chunk = chunk; diff --git a/pool_allocator/pool_allocator.h b/pool_allocator/pool_allocator.h index c3eba3d..7715ea4 100644 --- a/pool_allocator/pool_allocator.h +++ b/pool_allocator/pool_allocator.h @@ -45,18 +45,18 @@ extern "C" { /*------------------------------------------------------------------------------ Macros ------------------------------------------------------------------------------*/ -#define CHUNK_SIZE 12 /* Unofficially the size of SHEDULED_TASK for now */ -#define POOL_SIZE ( CHUNK_SIZE * 16 ) +#define CHUNK_SIZE 16 // TODO variable chunk size /*------------------------------------------------------------------------------ Typedefs ------------------------------------------------------------------------------*/ -typedef union +typedef union Chunk Chunk; +union Chunk { - struct Chunk* next; + Chunk* next; uint8_t arr[CHUNK_SIZE]; - } Chunk; + }; typedef struct { @@ -68,8 +68,23 @@ typedef struct /*------------------------------------------------------------------------------ Function Prototypes ------------------------------------------------------------------------------*/ +Pool pool_init + ( + uint8_t* pool_memory, + size_t size + ); + +void* pool_alloc + ( + Pool* pool + ); + +void pool_free + ( + Pool* pool, + void* ptr + ); - #ifdef __cplusplus } #endif diff --git a/scheduler/scheduler.c b/scheduler/scheduler.c index 356cf4c..4d0a3cc 100644 --- a/scheduler/scheduler.c +++ b/scheduler/scheduler.c @@ -29,75 +29,94 @@ #include "main.h" #include "scheduler.h" #include "error_sdr.h" +#include "debug_sdr.h" #include "pool_allocator.h" /*------------------------------------------------------------------------------ Global Variables ------------------------------------------------------------------------------*/ -extern SCHEDULED_TASK task_scheduler_table[]; -extern uint8_t task_scheduler_table_size; +/* Project defined scheduler pool */ +extern Pool scheduler_pool; +TASK_LIST task_list_head = { 0 }; /*------------------------------------------------------------------------------ API Functions ------------------------------------------------------------------------------*/ -TASK_STATUS schedule_task +SCHEDULER_STATUS schedule_task ( - TASK_TYPE task, + task_callback task, uint32_t scheduled_systick ) { +if ( scheduled_systick < HAL_GetTick() ) + { + return SCHEDULER_INVALID_SYSTICK; + } + +TASK_LIST* new_task = pool_alloc( &scheduler_pool ); +if ( new_task == NULL ) + { + return SCHEDULER_FAIL; + } +new_task->task = task; +new_task->scheduled_systick = scheduled_systick; +new_task->next = NULL; -/* CRITICAL SECTION BEGIN ?*/ +TASK_LIST* previous = &task_list_head; -for ( uint8_t i = 0; i < task_scheduler_table_size; i++ ) +// potential race condition + +/* Traverse to end of linked list */ +while ( previous->next != NULL ) { - SCHEDULED_TASK* tabled_task = &task_scheduler_table[i]; - if ( tabled_task->task == task ) - { - if ( tabled_task->scheduled_systick != 0 ) - { - return TASK_SCHEDULER_OCCUPIED; - } - - tabled_task->scheduled_systick = scheduled_systick; - return TASK_SCHEDULER_OK; - } + previous = previous->next; + } + +previous->next = new_task; + +if ( !previous->next ) + { + return SCHEDULER_FAIL; } -/* CRITICAL SECTION END */ -return TASK_SCHEDULER_NOT_FOUND; +return SCHEDULER_OK; } -TASK_STATUS task_check_and_execute +SCHEDULER_STATUS task_check_and_execute ( void ) { uint32_t systick_now = HAL_GetTick(); -uint16_t status = TASK_SCHEDULER_OK; +uint16_t status = SCHEDULER_OK; -for ( uint8_t i = 0; i < task_scheduler_table_size; i++ ) +TASK_LIST* previous = &task_list_head; +TASK_LIST* current = task_list_head.next; + +while ( current != NULL ) { - SCHEDULED_TASK* tabled_task = &task_scheduler_table[i]; - if ( tabled_task->scheduled_systick <= systick_now && tabled_task->scheduled_systick != 0 ) + if ( current->scheduled_systick <= systick_now ) { - /* At or passed scheduled tick, so execute callback */ - uint16_t callback_return = tabled_task->callback(); - - /* Reset systick*/ - tabled_task->scheduled_systick = 0; - - if ( callback_return ) - { - status = TASK_SCHEDULER_CALLBACK_ERROR; - } + current->task(); + + previous->next = current->next; + TASK_LIST* next = current->next; + + pool_free(&scheduler_pool, current); + current = next; + } + else + { + previous = current; + current = current->next; } } + return status; } @@ -107,8 +126,8 @@ void task_scheduler_IT_handler void ) { -TASK_STATUS status = task_check_and_execute(); -if ( status == TASK_SCHEDULER_CALLBACK_ERROR ) +SCHEDULER_STATUS status = task_check_and_execute(); +if ( status == SCHEDULER_CALLBACK_ERROR ) { error_fail_fast( ERROR_UNKNOWN_FATAL_ERROR ); // TODO } diff --git a/scheduler/scheduler.h b/scheduler/scheduler.h index 4345087..2c20485 100644 --- a/scheduler/scheduler.h +++ b/scheduler/scheduler.h @@ -45,38 +45,26 @@ extern "C" { /*------------------------------------------------------------------------------ Typedefs ------------------------------------------------------------------------------*/ - -typedef enum - { - TASK_START_BUZZ, - TASK_STOP_BUZZ - } TASK_TYPE; - typedef enum { - TASK_SCHEDULER_OK, - TASK_SCHEDULER_PASS, - TASK_SCHEDULER_FAIL, - TASK_SCHEDULER_INVALID_SYSTICK, - TASK_SCHEDULER_NOT_FOUND, - TASK_SCHEDULER_OCCUPIED, - TASK_SCHEDULER_CALLBACK_ERROR - } TASK_STATUS; - -typedef uint16_t (*task_callback)(void); /* NA TODO: error handling (any negative returns?)*/ - -typedef struct - { - const TASK_TYPE task; - uint32_t scheduled_systick; - const task_callback callback; - } SCHEDULED_TASK; + SCHEDULER_OK, + SCHEDULER_PASS, + SCHEDULER_FAIL, + SCHEDULER_INVALID_SYSTICK, + SCHEDULER_TASK_NOT_FOUND, + SCHEDULER_CALLBACK_ERROR + } SCHEDULER_STATUS; + +typedef void (*task_callback)(void); /* NA TODO: error handling */ -typedef struct + +typedef struct TASK_LIST TASK_LIST; +struct TASK_LIST { - SCHEDULED_TASK task; - struct TASK_LLIST* next; - } TASK_LLIST; + TASK_LIST* next; + uint32_t scheduled_systick; + task_callback task; + }; /*------------------------------------------------------------------------------ Macros @@ -88,14 +76,14 @@ typedef struct Function Prototypes ------------------------------------------------------------------------------*/ -TASK_STATUS schedule_task +SCHEDULER_STATUS schedule_task ( - TASK_TYPE task, + task_callback task, uint32_t scheduled_systick ); -TASK_STATUS task_check_and_execute +SCHEDULER_STATUS task_check_and_execute ( void ); From 427b355016ea4dce8e23be928bb55012699c29bd Mon Sep 17 00:00:00 2001 From: NArmistead Date: Sun, 5 Jul 2026 12:08:55 -0700 Subject: [PATCH 3/4] Better scheduler encapsulation, add comments --- error_sdr/error_sdr.h | 1 + pool_allocator/pool_allocator.c | 18 +++---- pool_allocator/pool_allocator.h | 20 ++++---- scheduler/scheduler.c | 85 +++++++++++++++++---------------- scheduler/scheduler.h | 41 ++++++---------- 5 files changed, 77 insertions(+), 88 deletions(-) diff --git a/error_sdr/error_sdr.h b/error_sdr/error_sdr.h index 1fdb8b7..8c1d17a 100644 --- a/error_sdr/error_sdr.h +++ b/error_sdr/error_sdr.h @@ -115,6 +115,7 @@ typedef enum _ERROR_CODE ERROR_LORA_CMD_ERROR , /* Error with LoRa command */ ERROR_IMU_I2C_ERROR , /* Error with IMU I2C handle */ ERROR_BARO_I2C_ERROR , /* Error with Baro I2C handle */ + ERROR_NULL_PTR_ERROR , /* Error trying to use NULL pointer */ } ERROR_CODE; /* Error callback table entry */ diff --git a/pool_allocator/pool_allocator.c b/pool_allocator/pool_allocator.c index 010803c..0c2ab1b 100644 --- a/pool_allocator/pool_allocator.c +++ b/pool_allocator/pool_allocator.c @@ -40,15 +40,15 @@ /*------------------------------------------------------------------------------ API Functions ------------------------------------------------------------------------------*/ -Pool pool_init +POOL pool_init ( uint8_t* pool_memory, size_t size ) { -Pool pool; -size_t block_count = size / sizeof(Chunk); -pool.free_chunk = (Chunk*)pool_memory; +POOL pool; +size_t block_count = size / sizeof(POOL_CHUNK); +pool.free_chunk = (POOL_CHUNK*)pool_memory; pool.chunk_arr = pool.free_chunk; for ( size_t i = 0; i < block_count - 1; i++ ) @@ -63,7 +63,7 @@ return pool; void* pool_alloc ( - Pool* pool + POOL* pool ) { if ( pool == NULL || pool->free_chunk == NULL) @@ -71,7 +71,7 @@ if ( pool == NULL || pool->free_chunk == NULL) return NULL; } -Chunk* next_free = pool->free_chunk; +POOL_CHUNK* next_free = pool->free_chunk; pool->free_chunk = pool->free_chunk->next; return next_free; @@ -81,13 +81,13 @@ return next_free; void pool_free ( - Pool* pool, + POOL* pool, void* ptr ) { /* ptr can be NULL, but I want to check for now */ -debug_assert(pool != NULL && ptr != NULL, ERROR_UNKNOWN_FATAL_ERROR); -Chunk* chunk = ptr; +debug_assert(pool != NULL && ptr != NULL, ERROR_NULL_PTR_ERROR); +POOL_CHUNK* chunk = ptr; chunk->next = pool->free_chunk; pool->free_chunk = chunk; diff --git a/pool_allocator/pool_allocator.h b/pool_allocator/pool_allocator.h index 7715ea4..c464bee 100644 --- a/pool_allocator/pool_allocator.h +++ b/pool_allocator/pool_allocator.h @@ -45,30 +45,30 @@ extern "C" { /*------------------------------------------------------------------------------ Macros ------------------------------------------------------------------------------*/ -#define CHUNK_SIZE 16 // TODO variable chunk size +#define CHUNK_SIZE 16 /*------------------------------------------------------------------------------ Typedefs ------------------------------------------------------------------------------*/ -typedef union Chunk Chunk; -union Chunk +typedef union POOL_CHUNK POOL_CHUNK; +union POOL_CHUNK { - Chunk* next; + POOL_CHUNK* next; uint8_t arr[CHUNK_SIZE]; }; typedef struct { - Chunk* free_chunk; - Chunk* chunk_arr; - } Pool; + POOL_CHUNK* free_chunk; + POOL_CHUNK* chunk_arr; + } POOL; /*------------------------------------------------------------------------------ Function Prototypes ------------------------------------------------------------------------------*/ -Pool pool_init +POOL pool_init ( uint8_t* pool_memory, size_t size @@ -76,12 +76,12 @@ Pool pool_init void* pool_alloc ( - Pool* pool + POOL* pool ); void pool_free ( - Pool* pool, + POOL* pool, void* ptr ); diff --git a/scheduler/scheduler.c b/scheduler/scheduler.c index 4d0a3cc..8541853 100644 --- a/scheduler/scheduler.c +++ b/scheduler/scheduler.c @@ -1,7 +1,7 @@ /** ****************************************************************************** * @file : scheduler.c - * @brief : TODO + * @brief : Asynchronous task scheduler ****************************************************************************** * @copyright * @@ -18,32 +18,45 @@ ****************************************************************************** */ -/*------------------------------------------------------------------------------ - Standard Includes -------------------------------------------------------------------------------*/ - - -/*------------------------------------------------------------------------------ - Project Includes -------------------------------------------------------------------------------*/ +/* Includes ------------------------------------------------------------------*/ #include "main.h" #include "scheduler.h" #include "error_sdr.h" #include "debug_sdr.h" #include "pool_allocator.h" -/*------------------------------------------------------------------------------ - Global Variables -------------------------------------------------------------------------------*/ -/* Project defined scheduler pool */ -extern Pool scheduler_pool; -TASK_LIST task_list_head = { 0 }; +/* Static Variables ----------------------------------------------------------*/ + +/* Memory to be used by scheduler*/ +static uint8_t scheduler_pool_mem[SCHEDULER_POOL_SIZE]; +static POOL scheduler_pool; + +/* Dummy head for task list */ +static TASK_LIST task_list_head = { 0 }; -/*------------------------------------------------------------------------------ - API Functions -------------------------------------------------------------------------------*/ +/* Procedures ----------------------------------------------------------------*/ +/** + * @brief Initializes the scheduler's memory pool + */ +void scheduler_init + ( + void + ) +{ +scheduler_pool = pool_init(scheduler_pool_mem, SCHEDULER_POOL_SIZE); +} + + +/** + * @brief Schedules a task. + * + * @param task The task to perform. + * @param scheduled_systick The systick at which the task will be run. + * + * @return The status of the scheduler. + */ SCHEDULER_STATUS schedule_task ( task_callback task, @@ -55,34 +68,29 @@ if ( scheduled_systick < HAL_GetTick() ) return SCHEDULER_INVALID_SYSTICK; } -TASK_LIST* new_task = pool_alloc( &scheduler_pool ); +TASK_LIST* new_task = pool_alloc(&scheduler_pool); if ( new_task == NULL ) { return SCHEDULER_FAIL; } new_task->task = task; new_task->scheduled_systick = scheduled_systick; -new_task->next = NULL; - -TASK_LIST* previous = &task_list_head; +/* Insert new task into the front of the list */ // potential race condition - -/* Traverse to end of linked list */ -while ( previous->next != NULL ) +if ( task_list_head.next != NULL ) { - previous = previous->next; + new_task->next = task_list_head.next; } - -previous->next = new_task; - -if ( !previous->next ) +else { - return SCHEDULER_FAIL; + new_task->next = NULL; } +task_list_head.next = new_task; return SCHEDULER_OK; + } @@ -116,7 +124,6 @@ while ( current != NULL ) } } - return status; } @@ -127,14 +134,8 @@ void task_scheduler_IT_handler ) { SCHEDULER_STATUS status = task_check_and_execute(); -if ( status == SCHEDULER_CALLBACK_ERROR ) - { - error_fail_fast( ERROR_UNKNOWN_FATAL_ERROR ); // TODO - } +// if ( status == SCHEDULER_CALLBACK_ERROR ) +// { +// error_fail_fast( ERROR_UNKNOWN_FATAL_ERROR ); // TODO +// } } - - - -/******************************************************************************* -* END OF FILE * -*******************************************************************************/ \ No newline at end of file diff --git a/scheduler/scheduler.h b/scheduler/scheduler.h index 2c20485..91812db 100644 --- a/scheduler/scheduler.h +++ b/scheduler/scheduler.h @@ -1,7 +1,7 @@ /** ****************************************************************************** * @file : scheduler.h - * @brief : TODO + * @brief : Asynchronous task scheduler ****************************************************************************** * @copyright * @@ -20,7 +20,7 @@ ============================================================================== ##### Task scheduler features ##### ============================================================================== - TODO + - Schedule `task_callback` functions to be run at a specified systick ****************************************************************************** @endverbatim */ @@ -34,30 +34,19 @@ extern "C" { #endif +/* Includes ------------------------------------------------------------------*/ +#include "main.h" -/*------------------------------------------------------------------------------ - Includes -------------------------------------------------------------------------------*/ -#include -#include - - -/*------------------------------------------------------------------------------ - Typedefs -------------------------------------------------------------------------------*/ +/* Types ---------------------------------------------------------------------*/ typedef enum { SCHEDULER_OK, - SCHEDULER_PASS, SCHEDULER_FAIL, - SCHEDULER_INVALID_SYSTICK, - SCHEDULER_TASK_NOT_FOUND, - SCHEDULER_CALLBACK_ERROR + SCHEDULER_INVALID_SYSTICK } SCHEDULER_STATUS; typedef void (*task_callback)(void); /* NA TODO: error handling */ - typedef struct TASK_LIST TASK_LIST; struct TASK_LIST { @@ -66,15 +55,17 @@ struct TASK_LIST task_callback task; }; -/*------------------------------------------------------------------------------ - Macros -------------------------------------------------------------------------------*/ +/* Macros --------------------------------------------------------------------*/ +/* Size defined as a macro by the project */ +#define SCHEDULER_POOL_SIZE ( SCHEDULER_POOL_MAX_TASKS * sizeof(POOL_CHUNK) ) +/* Exported functions prototypes ---------------------------------------------*/ -/*------------------------------------------------------------------------------ - Function Prototypes -------------------------------------------------------------------------------*/ +void scheduler_init + ( + void + ); SCHEDULER_STATUS schedule_task ( @@ -97,7 +88,3 @@ void task_scheduler_IT_handler } #endif #endif /* SCHEDULER_H */ - -/******************************************************************************* -* END OF FILE * -*******************************************************************************/ \ No newline at end of file From 66bc8cde11225320e63cf7667417104309c0bc70 Mon Sep 17 00:00:00 2001 From: NArmistead Date: Wed, 8 Jul 2026 20:12:40 -0700 Subject: [PATCH 4/4] Pool chunk adjustment and coments --- pool_allocator/pool_allocator.h | 2 +- scheduler/scheduler.c | 4 ++-- scheduler/scheduler.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pool_allocator/pool_allocator.h b/pool_allocator/pool_allocator.h index c464bee..84d18fa 100644 --- a/pool_allocator/pool_allocator.h +++ b/pool_allocator/pool_allocator.h @@ -45,7 +45,7 @@ extern "C" { /*------------------------------------------------------------------------------ Macros ------------------------------------------------------------------------------*/ -#define CHUNK_SIZE 16 +#define CHUNK_SIZE 12 /* Defined as the size of a scheduler task for now */ /*------------------------------------------------------------------------------ Typedefs diff --git a/scheduler/scheduler.c b/scheduler/scheduler.c index 8541853..6d537a0 100644 --- a/scheduler/scheduler.c +++ b/scheduler/scheduler.c @@ -77,7 +77,7 @@ new_task->task = task; new_task->scheduled_systick = scheduled_systick; /* Insert new task into the front of the list */ -// potential race condition +// potential race condition, but I've been having trouble disabling the TIM6 IRQ if ( task_list_head.next != NULL ) { new_task->next = task_list_head.next; @@ -109,7 +109,7 @@ while ( current != NULL ) { if ( current->scheduled_systick <= systick_now ) { - current->task(); + current->task(); // this needs error handling somehow previous->next = current->next; TASK_LIST* next = current->next; diff --git a/scheduler/scheduler.h b/scheduler/scheduler.h index 91812db..1372039 100644 --- a/scheduler/scheduler.h +++ b/scheduler/scheduler.h @@ -57,7 +57,7 @@ struct TASK_LIST /* Macros --------------------------------------------------------------------*/ -/* Size defined as a macro by the project */ +/* SCHEDULER_POOL_MAX_TASKS defined by the project */ #define SCHEDULER_POOL_SIZE ( SCHEDULER_POOL_MAX_TASKS * sizeof(POOL_CHUNK) ) /* Exported functions prototypes ---------------------------------------------*/