RTOS Wrapper
API reference
Loading...
Searching...
No Matches
os_cmsis.h
Go to the documentation of this file.
1#pragma once
17#include "cmsis_os2.h"
18
19#include <stddef.h>
20#include <stdint.h>
21#include <stdlib.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27typedef osThreadId_t OS_Thread;
28typedef osMutexId_t OS_Mutex;
29typedef osSemaphoreId_t OS_Semaphore;
30typedef osMessageQueueId_t OS_Queue;
31typedef osEventFlagsId_t OS_EventGroup;
32typedef osTimerId_t OS_Timer;
33typedef void* OS_StreamBuffer;
34
35typedef unsigned long OS_Ulong;
36typedef unsigned int OS_Uint;
37
38/* C++ `T x = {0}` only initializes the first member and trips
39 * -Wmissing-field-initializers. `{}` value-initializes every member.
40 * C keeps the classic zero initializer, which does not warn. */
41#ifdef __cplusplus
42#define OS_ZERO_INIT {}
43#else
44#define OS_ZERO_INIT {0}
45#endif
46
47enum {
48 OS_WAIT_FOREVER = (int)osWaitForever,
51 OS_AND = 1,
52 OS_OR = 2,
56 OS_INHERIT = 0
57};
58
59static inline OS_Ulong os_time_get(void)
60{
61 return (OS_Ulong)osKernelGetTickCount();
62}
63
64static inline int os_kernel_is_running(void)
65{
66 return osKernelGetState() == osKernelRunning;
67}
68
69static inline OS_Uint os_thread_yield(void)
70{
71 return (osThreadYield() == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
72}
73
74static inline OS_Uint os_thread_is_current(const OS_Thread *t)
75{
76 return (t != 0 && *t != 0 && osThreadGetId() == *t) ? 1u : 0u;
77}
78
84static inline OS_Uint os_thread_spawn(OS_Thread *t, const char *name,
85 void (*entry)(void *), void *arg,
86 void *stack, OS_Ulong stack_size,
87 OS_Uint priority, OS_Uint auto_start)
88{
89 if (t == 0 || entry == 0) {
90 return (OS_Uint)1;
91 }
92 osThreadAttr_t attr = OS_ZERO_INIT;
93 attr.name = name;
94 attr.stack_size = (uint32_t)stack_size;
95 attr.stack_mem = stack;
96 if (priority != 0u) {
97 attr.priority = (osPriority_t)priority;
98 }
99 osThreadId_t id = osThreadNew(entry, arg, &attr);
100 if (id == 0) {
101 return (OS_Uint)1;
102 }
103 *t = id;
104 if (!auto_start) {
105 (void)osThreadSuspend(id);
106 }
107 return OS_SUCCESS;
108}
109
110typedef struct {
111 void (*entry)(OS_Ulong);
114
115static inline void os_cmsis_generic_entry(void *p)
116{
117 os_cmsis_start_t params = *(os_cmsis_start_t *)p;
118 free(p);
119 params.entry(params.arg);
120}
121
123static inline OS_Uint os_thread_create(OS_Thread *t, const char *name,
124 void (*entry)(OS_Ulong),
125 OS_Ulong input, uint8_t *stack,
126 OS_Ulong stack_size, OS_Uint priority,
127 OS_Uint /*preempt*/, OS_Ulong /*slice*/,
128 OS_Uint auto_start)
129{
130 os_cmsis_start_t *params = (os_cmsis_start_t *)malloc(sizeof(os_cmsis_start_t));
131 if (params == 0) {
132 return (OS_Uint)1;
133 }
134 params->entry = entry;
135 params->arg = input;
136 const OS_Uint err = os_thread_spawn(t, name, os_cmsis_generic_entry, params,
137 stack, stack_size, priority, auto_start);
138 if (err != OS_SUCCESS) {
139 free(params);
140 }
141 return err;
142}
143
144static inline OS_Uint os_thread_create_pinned(OS_Thread *t, const char *name,
145 void (*entry)(OS_Ulong),
146 OS_Ulong input, uint8_t *stack,
147 OS_Ulong stack_size, OS_Uint priority,
148 OS_Uint preempt, OS_Ulong slice,
149 OS_Uint auto_start, int core_id)
150{
151 (void)core_id;
152 return os_thread_create(t, name, entry, input, stack, stack_size,
153 priority, preempt, slice, auto_start);
154}
155
156static inline int os_get_current_core_id(void) { return 0; }
157
159{
160 return (t && osThreadResume(*t) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
161}
163{
164 return (t && osThreadSuspend(*t) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
165}
167{
168 return (t && osThreadTerminate(*t) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
169}
171{
172 return os_thread_delete(t);
173}
175{
176 if (state && t && *t) {
177 *state = (OS_Uint)osThreadGetState(*t);
178 }
179 return OS_SUCCESS;
180}
181static inline OS_Uint os_thread_sleep(OS_Ulong ticks)
182{
183 return (osDelay((uint32_t)ticks) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
184}
185
186enum {
187 OS_THREAD_READY = osThreadReady,
188 OS_THREAD_SUSPENDED = osThreadBlocked,
189 OS_THREAD_BLOCKED = osThreadBlocked
191
192static inline int os_thread_is_live(const OS_Thread *t)
193{
194 return (t != 0) && (*t != 0);
195}
196static inline void os_thread_clear(OS_Thread *t)
197{
198 if (t) {
199 *t = 0;
200 }
201}
202static inline int os_mutex_is_live(const OS_Mutex *m)
203{
204 return (m != 0) && (*m != 0);
205}
206static inline int os_thread_state_is_suspended(OS_Uint state)
207{
208 return (state == (OS_Uint)osThreadBlocked) ||
209 (state == (OS_Uint)osThreadInactive);
210}
211
212static inline OS_Uint os_mutex_create(OS_Mutex *m, const char *name, OS_Uint inherit)
213{
214 osMutexAttr_t attr = OS_ZERO_INIT;
215 attr.name = name;
216 if (inherit) {
217 attr.attr_bits = osMutexPrioInherit;
218 }
219 *m = osMutexNew(&attr);
220 return (*m) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
221}
222static inline OS_Uint os_mutex_get(OS_Mutex *m, OS_Ulong wait)
223{
224 return (osMutexAcquire(*m, (uint32_t)wait) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
225}
227{
228 return (osMutexRelease(*m) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
229}
231{
232 return (osMutexDelete(*m) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
233}
234
235static inline OS_Uint os_recursive_mutex_create(OS_Mutex *m, const char *name)
236{
237 osMutexAttr_t attr = OS_ZERO_INIT;
238 attr.name = name;
239 attr.attr_bits = osMutexRecursive | osMutexPrioInherit;
240 *m = osMutexNew(&attr);
241 return (*m) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
242}
244{
245 return os_mutex_get(m, wait);
246}
248{
249 return os_mutex_put(m);
250}
252{
253 return os_mutex_delete(m);
254}
255
256static inline OS_Uint os_semaphore_create(OS_Semaphore *s, const char *name, OS_Uint initial)
257{
258 osSemaphoreAttr_t attr = OS_ZERO_INIT;
259 attr.name = name;
260 *s = osSemaphoreNew(0xFFFFu, initial, &attr);
261 return (*s) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
262}
264{
265 return (osSemaphoreDelete(*s) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
266}
268{
269 return (osSemaphoreRelease(*s) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
270}
272{
273 return (osSemaphoreAcquire(*s, (uint32_t)wait) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
274}
276{
277 if (count) {
278 *count = (OS_Ulong)osSemaphoreGetCount(*s);
279 }
280 return OS_SUCCESS;
281}
282
284#define OS_QUEUE_STORE_BYTES(item_words, length) ((size_t)0)
285
286static inline OS_Uint os_queue_create(OS_Queue *q, const char *name, OS_Uint item_words,
287 void * /*store*/, OS_Ulong length)
288{
289 osMessageQueueAttr_t attr = OS_ZERO_INIT;
290 attr.name = name;
291 *q = osMessageQueueNew((uint32_t)length, item_words * sizeof(uint32_t), &attr);
292 return (*q) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
293}
295{
296 return (osMessageQueueDelete(*q) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
297}
298static inline OS_Uint os_queue_send(OS_Queue *q, void *msg, OS_Ulong wait)
299{
300 return (osMessageQueuePut(*q, msg, 0, (uint32_t)wait) == osOK) ? (OS_Uint)OS_SUCCESS
301 : (OS_Uint)1;
302}
303static inline OS_Uint os_queue_receive(OS_Queue *q, void *msg, OS_Ulong wait)
304{
305 return (osMessageQueueGet(*q, msg, 0, (uint32_t)wait) == osOK) ? (OS_Uint)OS_SUCCESS
306 : (OS_Uint)1;
307}
308
309static inline OS_Uint os_event_group_create(OS_EventGroup *g, const char *name)
310{
311 osEventFlagsAttr_t attr = OS_ZERO_INIT;
312 attr.name = name;
313 *g = osEventFlagsNew(&attr);
314 return (*g) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
315}
317{
318 return (osEventFlagsDelete(*g) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
319}
321{
322 return (osEventFlagsSet(*g, (uint32_t)flags) != osFlagsError) ? (OS_Uint)OS_SUCCESS
323 : (OS_Uint)1;
324}
326{
327 return (osEventFlagsClear(*g, (uint32_t)flags) != osFlagsError) ? (OS_Uint)OS_SUCCESS
328 : (OS_Uint)1;
329}
331 OS_Ulong *actual, OS_Ulong wait)
332{
333 /* CMSIS clears the awaited flags unless told not to; clear only on OS_EVENT_CLEAR. */
334 const int all = ((option & ~(OS_Uint)OS_EVENT_CLEAR) == (OS_Uint)OS_AND);
335 const int clear = (option & (OS_Uint)OS_EVENT_CLEAR) != 0U;
336 const uint32_t opt = (all ? osFlagsWaitAll : osFlagsWaitAny) | (clear ? 0U : osFlagsNoClear);
337 const uint32_t got = osEventFlagsWait(*g, (uint32_t)flags, opt, (uint32_t)wait);
338 if (actual) {
339 *actual = (OS_Ulong)got;
340 }
341 return (got & osFlagsError) ? (OS_Uint)1 : (OS_Uint)OS_SUCCESS;
342}
343
344typedef struct {
345 void (*cb)(OS_Ulong);
348
349static inline void os_cmsis_timer_entry(void *arg)
350{
352 params->cb(params->id);
353}
354
355static inline OS_Uint os_timer_create(OS_Timer *t, const char *name,
356 void (*cb)(OS_Ulong), OS_Ulong id,
357 OS_Ulong initial, OS_Ulong reload,
358 OS_Uint auto_act)
359{
360 os_cmsis_timer_cb_t *params =
361 (os_cmsis_timer_cb_t *)malloc(sizeof(os_cmsis_timer_cb_t));
362 if (params == 0) {
363 return (OS_Uint)1;
364 }
365 params->cb = cb;
366 params->id = id;
367 osTimerAttr_t attr = OS_ZERO_INIT;
368 attr.name = name;
369 const osTimerType_t type = (reload != 0u) ? osTimerPeriodic : osTimerOnce;
370 *t = osTimerNew(os_cmsis_timer_entry, type, params, &attr);
371 if (*t == 0) {
372 free(params);
373 return (OS_Uint)1;
374 }
375 if (auto_act) {
376 return (osTimerStart(*t, (uint32_t)initial) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
377 }
378 (void)initial;
379 return OS_SUCCESS;
380}
382{
383 return (osTimerDelete(*t) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
384}
386{
387 return (osTimerStart(*t, 1u) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
388}
390{
391 return (osTimerStop(*t) == osOK) ? (OS_Uint)OS_SUCCESS : (OS_Uint)1;
392}
393
394static inline OS_Uint os_stream_buffer_create(OS_StreamBuffer *b, size_t capacity,
395 size_t trigger)
396{
397 (void)capacity;
398 (void)trigger;
399 *b = 0;
400 return OS_SUCCESS;
401}
403{
404 *b = 0;
405 return OS_SUCCESS;
406}
407static inline OS_Uint os_stream_buffer_send(OS_StreamBuffer *b, const void *data,
408 size_t len, OS_Ulong wait)
409{
410 (void)b;
411 (void)data;
412 (void)len;
413 (void)wait;
414 return (OS_Uint)1;
415}
417 size_t len, OS_Ulong wait)
418{
419 (void)b;
420 (void)data;
421 (void)len;
422 (void)wait;
423 return (OS_Uint)1;
424}
425
426typedef uint8_t OS_Critical;
428static inline void os_critical_enter(void)
429{
430 (void)osKernelLock();
431}
432static inline void os_critical_exit(void)
433{
434 (void)osKernelUnlock();
435}
436
437#ifdef __cplusplus
438}
439#endif
static void os_critical_enter(void)
Definition os_cmsis.h:428
@ OS_AUTO_START
Definition os_cmsis.h:54
@ OS_SUCCESS
Definition os_cmsis.h:50
@ OS_EVENT_CLEAR
Definition os_cmsis.h:53
@ OS_DONT_START
Definition os_cmsis.h:55
@ OS_INHERIT
Definition os_cmsis.h:56
@ OS_OR
Definition os_cmsis.h:52
@ OS_WAIT_FOREVER
Definition os_cmsis.h:48
@ OS_AND
Definition os_cmsis.h:51
@ OS_NO_WAIT
Definition os_cmsis.h:49
static OS_Uint os_semaphore_put(OS_Semaphore *s)
Definition os_cmsis.h:267
static void os_cmsis_generic_entry(void *p)
Definition os_cmsis.h:115
static OS_Uint os_queue_delete(OS_Queue *q)
Definition os_cmsis.h:294
static OS_Uint os_thread_create_pinned(OS_Thread *t, const char *name, void(*entry)(OS_Ulong), OS_Ulong input, uint8_t *stack, OS_Ulong stack_size, OS_Uint priority, OS_Uint preempt, OS_Ulong slice, OS_Uint auto_start, int core_id)
Definition os_cmsis.h:144
static OS_Uint os_thread_spawn(OS_Thread *t, const char *name, void(*entry)(void *), void *arg, void *stack, OS_Ulong stack_size, OS_Uint priority, OS_Uint auto_start)
Create a CMSIS thread. entry receives arg — no heap cookie.
Definition os_cmsis.h:84
static int os_mutex_is_live(const OS_Mutex *m)
Definition os_cmsis.h:202
static OS_Uint os_semaphore_get(OS_Semaphore *s, OS_Ulong wait)
Definition os_cmsis.h:271
#define OS_ZERO_INIT
Definition os_cmsis.h:44
void * OS_StreamBuffer
Definition os_cmsis.h:33
static OS_Uint os_thread_suspend(OS_Thread *t)
Definition os_cmsis.h:162
static OS_Uint os_thread_resume(OS_Thread *t)
Definition os_cmsis.h:158
static OS_Uint os_recursive_mutex_create(OS_Mutex *m, const char *name)
Definition os_cmsis.h:235
static OS_Uint os_event_group_set(OS_EventGroup *g, OS_Ulong flags)
Definition os_cmsis.h:320
static OS_Uint os_mutex_put(OS_Mutex *m)
Definition os_cmsis.h:226
static OS_Uint os_event_group_get(OS_EventGroup *g, OS_Ulong flags, OS_Uint option, OS_Ulong *actual, OS_Ulong wait)
Definition os_cmsis.h:330
OS_Critical os_critical_mux
Definition CriticalSection.cpp:11
static OS_Uint os_semaphore_create(OS_Semaphore *s, const char *name, OS_Uint initial)
Definition os_cmsis.h:256
static OS_Uint os_event_group_delete(OS_EventGroup *g)
Definition os_cmsis.h:316
static int os_kernel_is_running(void)
Definition os_cmsis.h:64
static OS_Uint os_mutex_get(OS_Mutex *m, OS_Ulong wait)
Definition os_cmsis.h:222
static OS_Uint os_mutex_delete(OS_Mutex *m)
Definition os_cmsis.h:230
static OS_Uint os_event_group_clear(OS_EventGroup *g, OS_Ulong flags)
Definition os_cmsis.h:325
static OS_Uint os_semaphore_info_get(OS_Semaphore *s, OS_Ulong *count)
Definition os_cmsis.h:275
static void os_critical_exit(void)
Definition os_cmsis.h:432
osEventFlagsId_t OS_EventGroup
Definition os_cmsis.h:31
static OS_Uint os_timer_create(OS_Timer *t, const char *name, void(*cb)(OS_Ulong), OS_Ulong id, OS_Ulong initial, OS_Ulong reload, OS_Uint auto_act)
Definition os_cmsis.h:355
static int os_thread_is_live(const OS_Thread *t)
Definition os_cmsis.h:192
static OS_Uint os_stream_buffer_send(OS_StreamBuffer *b, const void *data, size_t len, OS_Ulong wait)
Definition os_cmsis.h:407
static void os_cmsis_timer_entry(void *arg)
Definition os_cmsis.h:349
unsigned int OS_Uint
Definition os_cmsis.h:36
static void os_thread_clear(OS_Thread *t)
Definition os_cmsis.h:196
static OS_Uint os_queue_receive(OS_Queue *q, void *msg, OS_Ulong wait)
Definition os_cmsis.h:303
static OS_Uint os_thread_delete(OS_Thread *t)
Definition os_cmsis.h:166
static int os_get_current_core_id(void)
Definition os_cmsis.h:156
static OS_Uint os_timer_deactivate(OS_Timer *t)
Definition os_cmsis.h:389
osMessageQueueId_t OS_Queue
Definition os_cmsis.h:30
static OS_Uint os_mutex_create(OS_Mutex *m, const char *name, OS_Uint inherit)
Definition os_cmsis.h:212
unsigned long OS_Ulong
Definition os_cmsis.h:35
osThreadId_t OS_Thread
Definition os_cmsis.h:27
static OS_Uint os_recursive_mutex_put(OS_Mutex *m)
Definition os_cmsis.h:247
static OS_Uint os_recursive_mutex_delete(OS_Mutex *m)
Definition os_cmsis.h:251
static OS_Uint os_stream_buffer_receive(OS_StreamBuffer *b, void *data, size_t len, OS_Ulong wait)
Definition os_cmsis.h:416
osTimerId_t OS_Timer
Definition os_cmsis.h:32
uint8_t OS_Critical
Definition os_cmsis.h:426
static OS_Uint os_timer_activate(OS_Timer *t)
Definition os_cmsis.h:385
static OS_Uint os_stream_buffer_delete(OS_StreamBuffer *b)
Definition os_cmsis.h:402
static OS_Uint os_queue_create(OS_Queue *q, const char *name, OS_Uint item_words, void *, OS_Ulong length)
Definition os_cmsis.h:286
static OS_Uint os_thread_terminate(OS_Thread *t)
Definition os_cmsis.h:170
static OS_Uint os_thread_yield(void)
Definition os_cmsis.h:69
osSemaphoreId_t OS_Semaphore
Definition os_cmsis.h:29
static OS_Ulong os_time_get(void)
Definition os_cmsis.h:59
static OS_Uint os_event_group_create(OS_EventGroup *g, const char *name)
Definition os_cmsis.h:309
static OS_Uint os_queue_send(OS_Queue *q, void *msg, OS_Ulong wait)
Definition os_cmsis.h:298
static OS_Uint os_recursive_mutex_get(OS_Mutex *m, OS_Ulong wait)
Definition os_cmsis.h:243
static OS_Uint os_thread_is_current(const OS_Thread *t)
Definition os_cmsis.h:74
static OS_Uint os_thread_sleep(OS_Ulong ticks)
Definition os_cmsis.h:181
@ OS_THREAD_READY
Definition os_cmsis.h:187
@ OS_THREAD_BLOCKED
Definition os_cmsis.h:189
@ OS_THREAD_SUSPENDED
Definition os_cmsis.h:188
static int os_thread_state_is_suspended(OS_Uint state)
Definition os_cmsis.h:206
static OS_Uint os_timer_delete(OS_Timer *t)
Definition os_cmsis.h:381
static OS_Uint os_stream_buffer_create(OS_StreamBuffer *b, size_t capacity, size_t trigger)
Definition os_cmsis.h:394
static OS_Uint os_semaphore_delete(OS_Semaphore *s)
Definition os_cmsis.h:263
osMutexId_t OS_Mutex
Definition os_cmsis.h:28
static OS_Uint os_thread_create(OS_Thread *t, const char *name, void(*entry)(OS_Ulong), OS_Ulong input, uint8_t *stack, OS_Ulong stack_size, OS_Uint priority, OS_Uint, OS_Ulong, OS_Uint auto_start)
Definition os_cmsis.h:123
static OS_Uint os_thread_info_get(OS_Thread *t, OS_Uint *state)
Definition os_cmsis.h:174
Definition os_cmsis.h:110
void(* entry)(OS_Ulong)
Definition os_cmsis.h:111
OS_Ulong arg
Definition os_cmsis.h:112
Definition os_cmsis.h:344
OS_Ulong id
Definition os_cmsis.h:346
void(* cb)(OS_Ulong)
Definition os_cmsis.h:345