diff --git a/main.cpp b/main.cpp index 09cca21..d259991 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,15 @@ #include "queue.h" #include "semphr.h" +#define BASE_TASK_PRIO 1 +#define SERIAL_PROCESSOR_TASK_PRIO 2 +#define COMMAND_HANDLER_TASK_PRIO 3 + +#define MAX_SERIAL_QUEUE_COUNT 16 +#define COMMAND_HANDLER_TASK_COUNT 10 + +#define DEFAULT_TASK_STACK_SIZE 512 + #define PING_COMMAND "ping" #define PING_RESPONSE "PONG!" @@ -17,8 +26,9 @@ #define distilleCore(core) (1 << (core)) #define is_digit_char(num) (num >= '0' && num <= '9') -QueueHandle_t serial_queue; -QueueHandle_t command_handler_task_queue; +static QueueHandle_t serial_queue; +static QueueHandle_t command_handler_task_queue; +static SemaphoreHandle_t serial_out_mutex; typedef struct { int id; @@ -26,8 +36,6 @@ typedef struct { TaskHandle_t task_handle; } CommandTask; -static SemaphoreHandle_t serial_out_mutex; - static void printf_sync(const char *fmt, ...){ va_list args; va_start(args, fmt); @@ -82,11 +90,11 @@ static void read_serial_line_task(void *params){ } } -bool is_valid_command(const char *command){ +static bool is_valid_command(const char *command){ return strstr(command, PING_COMMAND) == command; } -void parse_command(CommandTask *dest, const char *command){ +static void parse_command(CommandTask *dest, const char *command){ char *delim = strchr(command, ':'); if(delim == nullptr || delim == command){ dest->id = -1; @@ -108,12 +116,12 @@ void parse_command(CommandTask *dest, const char *command){ return; } -void clear_command_task(CommandTask *cmd_task){ +static void clear_command_task(CommandTask *cmd_task){ cmd_task->id = -1; cmd_task->command[0] = '\0'; } -void command_handler_task(void *params){ +static void command_handler_task(void *params){ CommandTask *cmd_task = (CommandTask*)params; while(true){ @@ -183,8 +191,8 @@ extern "C" void vApplicationStackOverflowHook(TaskHandle_t task, char *task_name } } -void init_serial_queue(){ - serial_queue = xQueueCreate(10, sizeof(char[64])); +static void init_serial_queue(){ + serial_queue = xQueueCreate(MAX_SERIAL_QUEUE_COUNT, sizeof(char[64])); if (serial_queue == NULL) { printf("Failed to create serial queue\n"); while(true){ @@ -193,8 +201,8 @@ void init_serial_queue(){ } } -void init_command_handler(){ - command_handler_task_queue = xQueueCreate(10, sizeof(CommandTask*)); +static void init_command_handler(){ + command_handler_task_queue = xQueueCreate(COMMAND_HANDLER_TASK_COUNT, sizeof(CommandTask*)); if (command_handler_task_queue == NULL) { printf("Failed to create command handler task queue\n"); while(true){ @@ -202,7 +210,7 @@ void init_command_handler(){ } } - for(int i = 0; i < 10; i++){ + for(int i = 0; i < COMMAND_HANDLER_TASK_COUNT; i++){ CommandTask *cmd_task = (CommandTask*)malloc(sizeof(CommandTask)); if(cmd_task == nullptr){ printf("Failed to allocate memory for command task\n"); @@ -211,7 +219,7 @@ void init_command_handler(){ } } - if(xTaskCreateAffinitySet(command_handler_task, "CmdHandler", 512, cmd_task, 2, distilleCore(0), &cmd_task->task_handle) != pdPASS){ + if(xTaskCreateAffinitySet(command_handler_task, "CmdHandler", DEFAULT_TASK_STACK_SIZE, cmd_task, COMMAND_HANDLER_TASK_PRIO, distilleCore(0), &cmd_task->task_handle) != pdPASS){ printf("Failed to create command handler task\n"); while(true){ tight_loop_contents(); @@ -223,7 +231,7 @@ void init_command_handler(){ } } -void init_serial_out_mutex(){ +static void init_serial_out_mutex(){ serial_out_mutex = xSemaphoreCreateMutex(); if (serial_out_mutex == NULL) { printf("Failed to create serial output mutex\n"); @@ -240,9 +248,9 @@ int main(){ init_command_handler(); init_serial_out_mutex(); - xTaskCreateAffinitySet(led_task, "LED", 512, nullptr, 1, distilleCore(0), nullptr); - xTaskCreateAffinitySet(serial_processing_task, "Serial Processing", 512, nullptr, 1, distilleCore(0), nullptr); - xTaskCreateAffinitySet(read_serial_line_task, "Serial Receiver", 512, nullptr, 1, distilleCore(0), nullptr); + xTaskCreateAffinitySet(led_task, "LED", DEFAULT_TASK_STACK_SIZE, nullptr, BASE_TASK_PRIO, distilleCore(0), nullptr); + xTaskCreateAffinitySet(serial_processing_task, "Serial Processing", DEFAULT_TASK_STACK_SIZE, nullptr, SERIAL_PROCESSOR_TASK_PRIO, distilleCore(1), nullptr); + xTaskCreateAffinitySet(read_serial_line_task, "Serial Receiver", DEFAULT_TASK_STACK_SIZE, nullptr, BASE_TASK_PRIO, distilleCore(0), nullptr); vTaskStartScheduler();