PER Firmware
Loading...
Searching...
No Matches
rtos.h
Go to the documentation of this file.
1#ifndef RTOS_H
2#define RTOS_H
3
14#include "FreeRTOS.h"
15#include "list.h"
16#include "queue.h"
17#include "semphr.h"
18#include "task.h"
19#include "timers.h"
20
21#include <stdint.h>
22
24typedef enum : size_t {
25 STACK_256 = 256,
26 STACK_512 = 512,
27 STACK_1024 = 1024,
28 STACK_2048 = 2048,
29 STACK_4096 = 4096,
31
38
39
48[[gnu::always_inline]]
49static inline void RTOS_delay_ms(uint32_t ms) {
50 vTaskDelay(pdMS_TO_TICKS(ms));
51}
52
53typedef struct {
54 void (*taskFunction)(void);
55 uint32_t period_ms;
56 UBaseType_t priority;
58
59// This is the function actually scheduled by freertos
60// Arg is of type RTOS_periodic_task_params_t*
61void RTOS_periodic_task_runner(void* arg);
62
63
79#define RTOS_DEFINE_TASK(NAME, PERIOD_MS, PRIORITY, STACK_SIZE) \
80 static_assert( \
81 (STACK_SIZE) % sizeof(StackType_t) == 0, \
82 "Stack size must be a multiple of StackType_t" \
83 ); \
84 \
85 static StaticTask_t NAME##_tcb; \
86 static StackType_t NAME##_stack[(STACK_SIZE) / sizeof(StackType_t)]; \
87 \
88 static RTOS_periodic_task_params_t NAME##_params = { \
89 .taskFunction = (void (*)(void))NAME, \
90 .period_ms = (PERIOD_MS), \
91 .priority = (PRIORITY), \
92 }; \
93 \
94 TaskHandle_t NAME##_handle
95
109#define RTOS_START_TASK(NAME) \
110 (NAME##_handle = xTaskCreateStatic( \
111 RTOS_periodic_task_runner, \
112 #NAME, \
113 sizeof(NAME##_stack) / sizeof(StackType_t), \
114 &NAME##_params, \
115 NAME##_params.priority, \
116 NAME##_stack, \
117 &NAME##_tcb))
118
119
123#define RTOS_HANDLE_OF(NAME) (NAME##_handle)
124
125
126typedef struct {
127 size_t item_size;
130
134#define RTOS_DEFINE_QUEUE(NAME, ITEM, COUNT) \
135 static_assert(COUNT > 0, "Queue count must be greater than 0"); \
136 \
137 static StaticQueue_t NAME##_cb; \
138 static uint8_t NAME##_data[sizeof(ITEM) * (COUNT)]; \
139 \
140 static RTOS_queue_params_t NAME##_params = { \
141 .item_size = sizeof(ITEM), \
142 .item_count = (COUNT), \
143 }; \
144 \
145 QueueHandle_t NAME; \
146
150#define RTOS_INIT_QUEUE(NAME) \
151 (NAME = xQueueCreateStatic( \
152 NAME##_params.item_count, \
153 NAME##_params.item_size, \
154 NAME##_data, \
155 &NAME##_cb))
156
160#define RTOS_DEFINE_SEMAPHORE(NAME) \
161 SemaphoreHandle_t NAME; \
162 static StaticSemaphore_t NAME##_cb;
163
164// Aliases for clarity
165#define RTOS_DEFINE_MUTEX(NAME) RTOS_DEFINE_SEMAPHORE(NAME)
166#define RTOS_DEFINE_COUNTING_SEMAPHORE(NAME) RTOS_DEFINE_SEMAPHORE(NAME)
167#define RTOS_DEFINE_BINARY_SEMAPHORE(NAME) RTOS_DEFINE_SEMAPHORE(NAME)
168
172#define RTOS_INIT_MUTEX(NAME) \
173 (NAME = xSemaphoreCreateMutexStatic(&(NAME##_cb)))
174
179#define RTOS_INIT_COUNTING_SEMAPHORE(NAME, MAX_COUNT) \
180 (NAME = xSemaphoreCreateCountingStatic((MAX_COUNT), 0, &(NAME##_cb)))
181
185#define RTOS_INIT_BINARY_SEMAPHORE(NAME) \
186 (NAME = xSemaphoreCreateBinaryStatic(&(NAME##_cb)))
187
188#endif // RTOS_H
RTOS_task_priority_t
Priority aliases.
Definition rtos.h:33
@ TASK_PRIORITY_LOW
Definition rtos.h:34
@ TASK_PRIORITY_HIGH
Definition rtos.h:36
@ TASK_PRIORITY_NORMAL
Definition rtos.h:35
void RTOS_periodic_task_runner(void *arg)
Definition rtos.c:13
static void RTOS_delay_ms(uint32_t ms)
Delays the current task for the specified number of milliseconds.
Definition rtos.h:49
RTOS_stack_size_t
Stack size aliases.
Definition rtos.h:24
@ STACK_4096
Definition rtos.h:29
@ STACK_256
Definition rtos.h:25
@ STACK_1024
Definition rtos.h:27
@ STACK_2048
Definition rtos.h:28
@ STACK_512
Definition rtos.h:26
Definition rtos.h:53
uint32_t period_ms
Definition rtos.h:55
UBaseType_t priority
Definition rtos.h:56
Definition rtos.h:126
size_t item_size
Definition rtos.h:127
size_t item_count
Definition rtos.h:128