working on queues

This commit is contained in:
2023-11-15 15:15:37 +01:00
parent 15478854f5
commit 1c3ba9e20f
2 changed files with 89 additions and 11 deletions

View File

@ -2,27 +2,40 @@
#include "semphr.h"
#include "task.h"
#define NUMBER_OF_TASKS 1
/**
* Task descriptors
*/
#define NUMBER_OF_TASKS 100
SemaphoreHandle_t taskMutex = NULL;
StaticSemaphore_t taskMutexDescriptor;
size_t nextFreeTaskDescriptor = 0;
StaticTask_t taskDescriptors[NUMBER_OF_TASKS];
/**
* Queue descriptors
*/
#define NUMBER_OF_QUEUES 300
SemaphoreHandle_t queueMutex = NULL;
StaticSemaphore_t queueMutexDescriptor;
size_t nextFreeQueueDescriptor = 0;
StaticQueue_t queueDescriptors[NUMBER_OF_QUEUES];
// TODO check and return
void initFreeRTOSHelper() {
taskMutex = xSemaphoreCreateRecursiveMutexStatic(&taskMutexDescriptor);
queueMutex = xSemaphoreCreateRecursiveMutexStatic(&queueMutexDescriptor);
}
const char * getTaskName() {
const char * get_task_name() {
return pcTaskGetName( NULL );
}
void stopIt() {
void stop_it() {
taskENTER_CRITICAL();
}
// TODO return some error code?
void *createTask(TaskFunction_t taskFunction, void *parameter, void *buffer,
void *create_task(TaskFunction_t taskFunction, void *parameter, void *buffer,
size_t buffersize) {
if (xSemaphoreTakeRecursive(taskMutex, portMAX_DELAY) != pdTRUE) {
return NULL;
@ -45,4 +58,19 @@ void *createTask(TaskFunction_t taskFunction, void *parameter, void *buffer,
void task_delay(uint32_t milliseconds) {
vTaskDelay(pdMS_TO_TICKS(milliseconds));
}
void * create_queue(size_t length, size_t element_size, uint8_t * buffer) {
if (xSemaphoreTakeRecursive(queueMutex, portMAX_DELAY) != pdTRUE) {
return NULL;
}
// we hold the queue mutex and are now allowed to access the taskDescriptors
if (nextFreeQueueDescriptor >=
sizeof(queueDescriptors) / sizeof(*queueDescriptors)) {
return NULL;
}
QueueHandle_t newQueue = xQueueCreateStatic(length, element_size, buffer, queueDescriptors + nextFreeQueueDescriptor);
nextFreeQueueDescriptor++;
xSemaphoreGiveRecursive(queueMutex);
return newQueue;
}