#include "FreeRTOS.h" #include "semphr.h" #include "task.h" #define NUMBER_OF_TASKS 300 SemaphoreHandle_t taskMutex = NULL; StaticSemaphore_t taskMutexDescriptor; size_t nextFreeTaskDescriptor = 0; StaticTask_t taskDescriptors[NUMBER_OF_TASKS]; // TODO check and return void initFreeRTOSHelper() { taskMutex = xSemaphoreCreateRecursiveMutexStatic(&taskMutexDescriptor); } // TODO return some error code? void *createTask(TaskFunction_t taskFunction, void *parameter, void *buffer, size_t buffersize) { if (xSemaphoreTakeRecursive(taskMutex, portMAX_DELAY) != pdTRUE) { return NULL; } // we hold the task mutex and are now allowed to access the taskDescriptors if (nextFreeTaskDescriptor >= sizeof(taskDescriptors) / sizeof(*taskDescriptors)) { return NULL; } TaskHandle_t newTask = xTaskCreateStatic( taskFunction, "rust", buffersize / sizeof(StackType_t), parameter, 4, buffer, taskDescriptors + nextFreeTaskDescriptor); nextFreeTaskDescriptor++; xSemaphoreGiveRecursive(taskMutex); return newTask; } void task_delay(uint32_t milliseconds) { vTaskDelay(pdMS_TO_TICKS(milliseconds)); }