BSB Developer Docs
Loading...
Searching...
No Matches
cmsis_os2.h
1// /*
2// * Copyright (c) 2013-2020 Arm Limited. All rights reserved.
3// *
4// * SPDX-License-Identifier: Apache-2.0
5// *
6// * Licensed under the Apache License, Version 2.0 (the License); you may
7// * not use this file except in compliance with the License.
8// * You may obtain a copy of the License at
9// *
10// * www.apache.org/licenses/LICENSE-2.0
11// *
12// * Unless required by applicable law or agreed to in writing, software
13// * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14// * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// * See the License for the specific language governing permissions and
16// * limitations under the License.
17// *
18// * ----------------------------------------------------------------------
19// *
20// * $Date: 12. June 2020
21// * $Revision: V2.1.3
22// *
23// * Project: CMSIS-RTOS2 API
24// * Title: cmsis_os2.h header file
25// *
26// * Version 2.1.3
27// * Additional functions allowed to be called from Interrupt Service Routines:
28// * - osThreadGetId
29// * Version 2.1.2
30// * Additional functions allowed to be called from Interrupt Service Routines:
31// * - osKernelGetInfo, osKernelGetState
32// * Version 2.1.1
33// * Additional functions allowed to be called from Interrupt Service Routines:
34// * - osKernelGetTickCount, osKernelGetTickFreq
35// * Changed Kernel Tick type to uint32_t:
36// * - updated: osKernelGetTickCount, osDelayUntil
37// * Version 2.1.0
38// * Support for critical and uncritical sections (nesting safe):
39// * - updated: osKernelLock, osKernelUnlock
40// * - added: osKernelRestoreLock
41// * Updated Thread and Event Flags:
42// * - changed flags parameter and return type from int32_t to uint32_t
43// * Version 2.0.0
44// * Initial Release
45// *---------------------------------------------------------------------------*/
46
47#ifndef CMSIS_OS2_H_
48#define CMSIS_OS2_H_
49
50// #ifndef __NO_RETURN
51// #if defined(__CC_ARM)
52// #define __NO_RETURN __declspec(noreturn)
53// #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
54// #define __NO_RETURN __attribute__((__noreturn__))
55// #elif defined(__GNUC__)
56// #define __NO_RETURN __attribute__((__noreturn__))
57// #elif defined(__ICCARM__)
58// #define __NO_RETURN __noreturn
59// #else
60// #define __NO_RETURN
61// #endif
62// #endif
63
64#include <stdint.h>
65#include <stddef.h>
66
67//#include <furi.h>
68#include <stdlib.h>
69#include <stdio.h>
70#include <furi/core/base.h>
71#include <furi/core/common_defines.h>
72//#include <furi/core/check.h> //ToDo: Fix m-lib
73#include <furi/core/thread.h>
74#include <furi/core/event_loop.h>
75#include <furi/core/semaphore.h>
76#include <furi/core/mutex.h>
77#include <furi/core/event_flag.h>
78#include <furi/core/kernel.h>
79#include <furi/core/message_queue.h>
80#include <furi/core/timer.h>
81#include <furi/core/event_flag.h>
82
83#ifdef __cplusplus
84extern "C" {
85#endif
86
87// // ==== Enumerations, structures, defines ====
88
89// /// Version information.
90// typedef struct {
91// uint32_t api; ///< API version (major.minor.rev: mmnnnrrrr dec).
92// uint32_t kernel; ///< Kernel version (major.minor.rev: mmnnnrrrr dec).
93// } osVersion_t;
94
96typedef enum {
97 osKernelInactive = 0,
98 osKernelReady = 1,
99 osKernelRunning = 2,
100 osKernelLocked = 3,
101 osKernelSuspended = 4,
102 osKernelError = -1,
103 osKernelReserved = 0x7FFFFFFF
104} osKernelState_t;
105
106// /// Thread state.
107// typedef enum {
108// osThreadInactive = 0, ///< Inactive.
109// osThreadReady = 1, ///< Ready.
110// osThreadRunning = 2, ///< Running.
111// osThreadBlocked = 3, ///< Blocked.
112// osThreadTerminated = 4, ///< Terminated.
113// osThreadError = -1, ///< Error.
114// osThreadReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
115// } osThreadState_t;
116
118typedef enum {
119 osPriorityNone = 66, //not use //0, ///< No priority (not initialized).
120 osPriorityIdle = FuriThreadPriorityIdle, // 1, ///< Reserved for Idle thread.
121 osPriorityLow = 1,
122 osPriorityLow1 = 1 + 1,
123 osPriorityLow2 = 1 + 2,
124 osPriorityLow3 = 1 + 3,
125 osPriorityLow4 = 1 + 4,
126 osPriorityLow5 = 1 + 4,
127 osPriorityLow6 = 1 + 4, //8 + 6, ///< Priority: low + 6
128 osPriorityLow7 = 1 + 4, //8 + 7, ///< Priority: low + 7
129 osPriorityBelowNormal = 6, //16, ///< Priority: below normal
130 osPriorityBelowNormal1 = 6 + 1, //16 + 1, ///< Priority: below normal + 1
131 osPriorityBelowNormal2 = 6 + 2, //16 + 2, ///< Priority: below normal + 2
132 osPriorityBelowNormal3 = 6 + 3, //16 + 3, ///< Priority: below normal + 3
133 osPriorityBelowNormal4 = 6 + 4, //16 + 4, ///< Priority: below normal + 4
134 osPriorityBelowNormal5 = 6 + 4, //16 + 5, ///< Priority: below normal + 5
135 osPriorityBelowNormal6 = 6 + 4, //16 + 6, ///< Priority: below normal + 6
136 osPriorityBelowNormal7 = 6 + 4, //16 + 7, ///< Priority: below normal + 7
137 osPriorityNormal = 11, //24, ///< Priority: normal
138 osPriorityNormal1 = 11 + 1, //24 + 1, ///< Priority: normal + 1
139 osPriorityNormal2 = 11 + 2, //24 + 2, ///< Priority: normal + 2
140 osPriorityNormal3 = 11 + 3, //24 + 3, ///< Priority: normal + 3
141 osPriorityNormal4 = 11 + 4, //24 + 4, ///< Priority: normal + 4
142 osPriorityNormal5 = 11 + 4, //24 + 5, ///< Priority: normal + 5
143 osPriorityNormal6 = 11 + 4, //24 + 6, ///< Priority: normal + 6
144 osPriorityNormal7 = 11 + 4, //24 + 7, ///< Priority: normal + 7
145 osPriorityAboveNormal = 16, //32, ///< Priority: above normal
146 osPriorityAboveNormal1 = 16 + 1, //32 + 1, ///< Priority: above normal + 1
147 osPriorityAboveNormal2 = 16 + 2, //32 + 2, ///< Priority: above normal + 2
148 osPriorityAboveNormal3 = 16 + 3, //32 + 3, ///< Priority: above normal + 3
149 osPriorityAboveNormal4 = 16 + 4, //32 + 4, ///< Priority: above normal + 4
150 osPriorityAboveNormal5 = 16 + 4, //32 + 5, ///< Priority: above normal + 5
151 osPriorityAboveNormal6 = 16 + 4, //32 + 6, ///< Priority: above normal + 6
152 osPriorityAboveNormal7 = 16 + 4, //32 + 7, ///< Priority: above normal + 7
153 osPriorityHigh = 21, //40, ///< Priority: high
154 osPriorityHigh1 = 21 + 1, //40 + 1, ///< Priority: high + 1
155 osPriorityHigh2 = 21 + 2, //40 + 2, ///< Priority: high + 2
156 osPriorityHigh3 = 21 + 3, //40 + 3, ///< Priority: high + 3
157 osPriorityHigh4 = 21 + 4, //40 + 4, ///< Priority: high + 4
158 osPriorityHigh5 = 21 + 4, //40 + 5, ///< Priority: high + 5
159 osPriorityHigh6 = 21 + 4, //40 + 6, ///< Priority: high + 6
160 osPriorityHigh7 = 21 + 4, //40 + 7, ///< Priority: high + 7
161 osPriorityRealtime = 26, //48, ///< Priority: realtime
162 osPriorityRealtime1 = 26 + 1, //48 + 1, ///< Priority: realtime + 1
163 osPriorityRealtime2 = 26 + 2, //48 + 2, ///< Priority: realtime + 2
164 osPriorityRealtime3 = 26 + 3, //48 + 3, ///< Priority: realtime + 3
165 osPriorityRealtime4 = 26 + 3, //48 + 4, ///< Priority: realtime + 4
166 osPriorityRealtime5 = 26 + 3, //48 + 5, ///< Priority: realtime + 5
167 osPriorityRealtime6 = 26 + 3, //48 + 6, ///< Priority: realtime + 6
168 osPriorityRealtime7 = 26 + 3, //48 + 7, ///< Priority: realtime + 7
169 osPriorityISR = FuriThreadPriorityIsr, //56, ///< Reserved for ISR deferred thread.
170 osPriorityError = -1,
171 osPriorityReserved = 0x7FFFFFFF
172} osPriority_t;
173
174// // Minimum number of priorities required by CMSIS-RTOS2
175// #define osMinNumPriority 56
176
178typedef void (*osThreadFunc_t)(void* argument);
179
180// /// Timer callback function.
181typedef void (*osTimerFunc_t)(void* argument);
182
184typedef enum {
185 osTimerOnce = 0,
186 osTimerPeriodic = 1
187} osTimerType_t;
188
189// Timeout value.
190//#define osWaitForever 0xFFFFFFFFU ///< Wait forever timeout value.
191#define osWaitForever ((uint32_t)FuriWaitForever)
192
193// // Flags options (\ref osThreadFlagsWait and \ref osEventFlagsWait).
194// #define osFlagsWaitAny 0x00000000U ///< Wait for any flag (default).
195#define osFlagsWaitAny ((uint32_t)FuriFlagWaitAny)
196// #define osFlagsWaitAll 0x00000001U ///< Wait for all flags.
197#define osFlagsWaitAll ((uint32_t)FuriFlagWaitAll)
198// #define osFlagsNoClear 0x00000002U ///< Do not clear flags which have been specified to wait for.
199#define osFlagsNoClear ((uint32_t)FuriFlagNoClear)
200
201// // Flags errors (returned by osThreadFlagsXxxx and osEventFlagsXxxx).
202// #define osFlagsError 0x80000000U ///< Error indicator.
203#define osFlagsError ((uint32_t)FuriFlagError)
204// #define osFlagsErrorUnknown 0xFFFFFFFFU ///< osError (-1).
205#define osFlagsErrorUnknown ((uint32_t)FuriFlagErrorUnknown)
206// #define osFlagsErrorTimeout 0xFFFFFFFEU ///< osErrorTimeout (-2).
207#define osFlagsErrorTimeout ((uint32_t)FuriFlagErrorTimeout)
208// #define osFlagsErrorResource 0xFFFFFFFDU ///< osErrorResource (-3).
209#define osFlagsErrorResource ((uint32_t)FuriFlagErrorResource)
210// #define osFlagsErrorParameter 0xFFFFFFFCU ///< osErrorParameter (-4).
211#define osFlagsErrorParameter ((uint32_t)FuriFlagErrorParameter)
212// #define osFlagsErrorISR 0xFFFFFFFAU ///< osErrorISR (-6).
213#define osFlagsErrorISR ((uint32_t)FuriFlagErrorISR)
214
215// // Thread attributes (attr_bits in \ref osThreadAttr_t).
216#define osThreadDetached 0x00000000U
217#define osThreadJoinable 0x00000001U
218
219// Mutex attributes (attr_bits in \ref osMutexAttr_t).
220#define osMutexRecursive 0x00000001U
221#define osMutexPrioInherit 0x00000002U
222#define osMutexRobust 0x00000008U
223
225typedef enum {
226 osOK = FuriStatusOk,
227 osError =
228 FuriStatusError,
229 osErrorTimeout =
230 FuriStatusErrorTimeout,
231 osErrorResource = FuriStatusErrorResource,
232 osErrorParameter = FuriStatusErrorParameter,
233 osErrorNoMemory =
234 FuriStatusErrorNoMemory,
235 osErrorISR =
236 FuriStatusErrorISR,
237 osStatusReserved = FuriStatusReserved
238} osStatus_t;
239
241typedef void* osThreadId_t;
242
243// /// \details Timer ID identifies the timer.
244typedef void* osTimerId_t;
245
247typedef void* osEventFlagsId_t;
248
250typedef void* osMutexId_t;
251
252// /// \details Semaphore ID identifies the semaphore.
253typedef void* osSemaphoreId_t;
254
255// /// \details Memory Pool ID identifies the memory pool.
256// typedef void *osMemoryPoolId_t;
257
259typedef void* osMessageQueueId_t;
260
261#ifndef TZ_MODULEID_T
262#define TZ_MODULEID_T
264typedef uint32_t TZ_ModuleId_t;
265#endif
266
268typedef struct {
269 const char* name;
270 uint32_t attr_bits;
271 void* cb_mem;
272 uint32_t cb_size;
273 void* stack_mem;
274 uint32_t stack_size;
275 osPriority_t priority;
276 TZ_ModuleId_t tz_module;
277 uint32_t reserved;
279
280typedef struct {
281 FuriThread* thread;
282 osThreadFunc_t callback;
283 void* context;
285
286static FURI_ALWAYS_INLINE int32_t furi_cmsis_thread_callback(void* context) {
287 FuriThreadAdapter* adapter = (FuriThreadAdapter*)context;
288 adapter->callback(adapter->context);
289 return 0;
290}
291
293typedef struct {
294 const char* name;
295 uint32_t attr_bits;
296 void* cb_mem;
297 uint32_t cb_size;
299
301typedef struct {
302 const char* name;
303 uint32_t attr_bits;
304 void* cb_mem;
305 uint32_t cb_size;
307
309typedef struct {
310 const char* name;
311 uint32_t attr_bits;
312 void* cb_mem;
313 uint32_t cb_size;
315
317typedef struct {
318 const char* name;
319 uint32_t attr_bits;
320 void* cb_mem;
321 uint32_t cb_size;
323
324// /// Attributes structure for memory pool.
325// typedef struct {
326// const char *name; ///< name of the memory pool
327// uint32_t attr_bits; ///< attribute bits
328// void *cb_mem; ///< memory for control block
329// uint32_t cb_size; ///< size of provided memory for control block
330// void *mp_mem; ///< memory for data storage
331// uint32_t mp_size; ///< size of provided memory for data storage
332// } osMemoryPoolAttr_t;
333
335typedef struct {
336 const char* name;
337 uint32_t attr_bits;
338 void* cb_mem;
339 uint32_t cb_size;
340 void* mq_mem;
341 uint32_t mq_size;
343
344// // ==== Kernel Management Functions ====
345
348//osStatus_t osKernelInitialize (void);
349static FURI_ALWAYS_INLINE osStatus_t osKernelInitialize(void) {
350 return osOK;
351}
352
353// /// Get RTOS Kernel Information.
354// /// \param[out] version pointer to buffer for retrieving version information.
355// /// \param[out] id_buf pointer to buffer for retrieving kernel identification string.
356// /// \param[in] id_size size of buffer for kernel identification string.
357// /// \return status code that indicates the execution status of the function.
358// osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size);
359
362//osKernelState_t osKernelGetState (void);
363static FURI_ALWAYS_INLINE osKernelState_t osKernelGetState(void) {
364 osKernelState_t state;
365 uint32_t KernelState = furi_kernel_is_running();
366
367 switch(KernelState) {
368 case 1:
369 state = osKernelRunning;
370 break;
371 case 0:
372 state = osKernelLocked;
373 break;
374 default:
375 state = osKernelError;
376 break;
377 }
378 return (state);
379}
380
381// /// Start the RTOS Kernel scheduler.
382// /// \return status code that indicates the execution status of the function.
383// osStatus_t osKernelStart (void);
384
385// /// Lock the RTOS Kernel scheduler.
386// /// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
387//int32_t osKernelLock (void);
388static FURI_ALWAYS_INLINE int32_t osKernelLock(void) {
389 return furi_kernel_lock();
390}
391
392// /// Unlock the RTOS Kernel scheduler.
393// /// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
394//int32_t osKernelUnlock (void);
395static FURI_ALWAYS_INLINE int32_t osKernelUnlock(void) {
396 return furi_kernel_unlock();
397}
398
399// /// Restore the RTOS Kernel scheduler lock state.
400// /// \param[in] lock lock state obtained by \ref osKernelLock or \ref osKernelUnlock.
401// /// \return new lock state (1 - locked, 0 - not locked, error code if negative).
402//int32_t osKernelRestoreLock (int32_t lock);
403static FURI_ALWAYS_INLINE int32_t osKernelRestoreLock(int32_t lock) {
404 return furi_kernel_restore_lock(lock);
405}
406
407// /// Suspend the RTOS Kernel scheduler.
408// /// \return time in ticks, for how long the system can sleep or power-down.
409// uint32_t osKernelSuspend (void);
410
411// /// Resume the RTOS Kernel scheduler.
412// /// \param[in] sleep_ticks time in ticks for how long the system was in sleep or power-down mode.
413// void osKernelResume (uint32_t sleep_ticks);
414
417//uint32_t osKernelGetTickCount (void);
418static FURI_ALWAYS_INLINE uint32_t osKernelGetTickCount(void) {
419 return furi_get_tick();
420}
421
422// /// Get the RTOS kernel tick frequency.
423// /// \return frequency of the kernel tick in hertz, i.e. kernel ticks per second.
424static FURI_ALWAYS_INLINE uint32_t osKernelGetTickFreq (void) {
425 return furi_kernel_get_tick_frequency();
426}
427
428// /// Get the RTOS kernel system timer count.
429// /// \return RTOS kernel current system timer count as 32-bit value.
430// uint32_t osKernelGetSysTimerCount (void);
431
432// /// Get the RTOS kernel system timer frequency.
433// /// \return frequency of the system timer in hertz, i.e. timer ticks per second.
434// uint32_t osKernelGetSysTimerFreq (void);
435
436// // ==== Thread Management Functions ====
437
443//osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);
444static FURI_ALWAYS_INLINE osThreadId_t
445 osThreadNew(osThreadFunc_t func, void* argument, const osThreadAttr_t* attr) {
446 FuriThreadAdapter* adapter = (FuriThreadAdapter*)malloc(sizeof(FuriThreadAdapter));
447
448 adapter->callback = func;
449 adapter->context = argument;
450
451 adapter->thread =
452 furi_thread_alloc_ex(attr->name, attr->stack_size, furi_cmsis_thread_callback, adapter);
453
454 furi_thread_set_priority(adapter->thread, (FuriThreadPriority)attr->priority);
455 if(attr->attr_bits & osThreadJoinable) {
456 //ToDo: Implement joinable threads ??????
457 } else {
458 furi_thread_start(adapter->thread);
459 }
460
461 return (osThreadId_t)adapter;
462}
463
467//const char *osThreadGetName (osThreadId_t thread_id);
468static FURI_ALWAYS_INLINE const char* osThreadGetName(osThreadId_t thread_id) {
469 FuriThreadAdapter* adapter = (FuriThreadAdapter*)thread_id;
470 return furi_thread_get_name((FuriThreadId)adapter->thread);
471}
472
475//osThreadId_t osThreadGetId (void);
476static FURI_ALWAYS_INLINE osThreadId_t osThreadGetId(void) {
477 return (osThreadId_t)furi_thread_get_current_id();
478}
479
483//osThreadState_t osThreadGetState (osThreadId_t thread_id);
484
485// /// Get stack size of a thread.
486// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
487// /// \return stack size in bytes.
488// uint32_t osThreadGetStackSize (osThreadId_t thread_id);
489
490// /// Get available stack space of a thread based on stack watermark recording during execution.
491// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
492// /// \return remaining stack space in bytes.
493// uint32_t osThreadGetStackSpace (osThreadId_t thread_id);
494
495// /// Change priority of a thread.
496// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
497// /// \param[in] priority new priority value for the thread function.
498// /// \return status code that indicates the execution status of the function.
499// osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority);
500
501// /// Get current priority of a thread.
502// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
503// /// \return current priority value of the specified thread.
504// osPriority_t osThreadGetPriority (osThreadId_t thread_id);
505
508// osStatus_t osThreadYield (void);
509static FURI_ALWAYS_INLINE osStatus_t osThreadYield(void) {
510 furi_thread_yield();
511 return osOK;
512}
513
514// /// Suspend execution of a thread.
515// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
516// /// \return status code that indicates the execution status of the function.
517static FURI_ALWAYS_INLINE osStatus_t osThreadSuspend (osThreadId_t thread_id) {
518 FuriThreadAdapter* adapter = (FuriThreadAdapter*)thread_id;
519 furi_thread_suspend(adapter->thread);
520 return osOK;
521}
522
523// /// Resume execution of a thread.
524// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
525// /// \return status code that indicates the execution status of the function.
526// osStatus_t osThreadResume (osThreadId_t thread_id);
527
528// /// Detach a thread (thread storage can be reclaimed when thread terminates).
529// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
530// /// \return status code that indicates the execution status of the function.
531// osStatus_t osThreadDetach (osThreadId_t thread_id);
532
533// /// Wait for specified thread to terminate.
534// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
535// /// \return status code that indicates the execution status of the function.
536//osStatus_t osThreadJoin (osThreadId_t thread_id);
537static FURI_ALWAYS_INLINE osStatus_t osThreadJoin(osThreadId_t thread_id) {
538 FuriThreadAdapter* adapter = (FuriThreadAdapter*)thread_id;
539 furi_thread_join(adapter->thread);
540 //furi_thread_free(adapter->thread);
541 //free(adapter);
542 return osOK;
543}
544
545// /// Terminate execution of current running thread.
546// __NO_RETURN void osThreadExit (void);
547
551//osStatus_t osThreadTerminate (osThreadId_t thread_id);
552static FURI_ALWAYS_INLINE osStatus_t osThreadTerminate(osThreadId_t thread_id) {
553 FuriThreadAdapter* adapter = (FuriThreadAdapter*)thread_id;
554 furi_thread_join(adapter->thread);
555 furi_thread_free(adapter->thread);
556 free(adapter);
557 return osOK;
558}
559
560// /// Get number of active threads.
561// /// \return number of active threads.
562// uint32_t osThreadGetCount (void);
563
564// /// Enumerate active threads.
565// /// \param[out] thread_array pointer to array for retrieving thread IDs.
566// /// \param[in] array_items maximum number of items in array for retrieving thread IDs.
567// /// \return number of enumerated threads.
568// uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items);
569
570// // ==== Thread Flags Functions ====
571
572// /// Set the specified Thread Flags of a thread.
573// /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
574// /// \param[in] flags specifies the flags of the thread that shall be set.
575// /// \return thread flags after setting or error code if highest bit set.
576// uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags);
577static FURI_ALWAYS_INLINE uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags) {
578 FuriThreadAdapter* adapter = (FuriThreadAdapter*)thread_id;
579 return furi_thread_flags_set(adapter->thread, flags);
580}
581
582// /// Clear the specified Thread Flags of current running thread.
583// /// \param[in] flags specifies the flags of the thread that shall be cleared.
584// /// \return thread flags before clearing or error code if highest bit set.
585// uint32_t osThreadFlagsClear (uint32_t flags);
586
587// /// Get the current Thread Flags of current running thread.
588// /// \return current thread flags.
589// uint32_t osThreadFlagsGet (void);
590
591// /// Wait for one or more Thread Flags of the current running thread to become signaled.
592// /// \param[in] flags specifies the flags to wait for.
593// /// \param[in] options specifies flags options (osFlagsXxxx).
594// /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
595// /// \return thread flags before clearing or error code if highest bit set.
596//uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout);
597static FURI_ALWAYS_INLINE uint32_t
598 osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout) {
599 return furi_thread_flags_wait(flags, options, timeout);
600}
601
602// // ==== Generic Wait Functions ====
603
607//osStatus_t osDelay (uint32_t ticks);
608static FURI_ALWAYS_INLINE osStatus_t osDelay(uint32_t ticks) {
609 furi_delay_tick(ticks);
610 return osOK;
611}
612
616//osStatus_t osDelayUntil (uint32_t ticks);
617static FURI_ALWAYS_INLINE osStatus_t osDelayUntil(uint32_t ticks) {
618 return (osStatus_t)furi_delay_until_tick(ticks);
619}
620
621// // ==== Timer Management Functions ====
622
623// /// Create and Initialize a timer.
624// /// \param[in] func function pointer to callback function.
625// /// \param[in] type \ref osTimerOnce for one-shot or \ref osTimerPeriodic for periodic behavior.
626// /// \param[in] argument argument to the timer callback function.
627// /// \param[in] attr timer attributes; NULL: default values.
628// /// \return timer ID for reference by other functions or NULL in case of error.
629// osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);
630static FURI_ALWAYS_INLINE osTimerId_t
631 osTimerNew(osTimerFunc_t func, osTimerType_t type, void* argument, const osTimerAttr_t* attr) {
632 UNUSED(attr);
633 //furi_check((attr == NULL), "osTimerNew: attr must be NULL");
634 return (osTimerId_t)furi_timer_alloc((FuriTimerCallback)func, (FuriTimerType)type, argument);
635}
636
637// /// Get name of a timer.
638// /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
639// /// \return name as null-terminated string.
640// const char *osTimerGetName (osTimerId_t timer_id);
641
642// /// Start or restart a timer.
643// /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
644// /// \param[in] ticks \ref CMSIS_RTOS_TimeOutValue "time ticks" value of the timer.
645// /// \return status code that indicates the execution status of the function.
646//osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks);
647static FURI_ALWAYS_INLINE osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks) {
648 FuriStatus status = furi_timer_start((FuriTimer*)timer_id, ticks);
649 return (osStatus_t)status;
650}
651
652// /// Stop a timer.
653// /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
654// /// \return status code that indicates the execution status of the function.
655//osStatus_t osTimerStop (osTimerId_t timer_id);
656static FURI_ALWAYS_INLINE osStatus_t osTimerStop(osTimerId_t timer_id) {
657 FuriStatus status = furi_timer_stop((FuriTimer*)timer_id);
658 return (osStatus_t)status;
659}
660
661// /// Check if a timer is running.
662// /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
663// /// \return 0 not running, 1 running.
664//uint32_t osTimerIsRunning (osTimerId_t timer_id);
665static FURI_ALWAYS_INLINE uint32_t osTimerIsRunning(osTimerId_t timer_id) {
666 return furi_timer_is_running((FuriTimer*)timer_id);
667}
668
669// /// Delete a timer.
670// /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
671// /// \return status code that indicates the execution status of the function.
672//osStatus_t osTimerDelete (osTimerId_t timer_id);
673static FURI_ALWAYS_INLINE osStatus_t osTimerDelete(osTimerId_t timer_id) {
674 furi_timer_free((FuriTimer*)timer_id);
675 return osOK;
676}
677
678// // ==== Event Flags Management Functions ====
679
683//osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr);
684static FURI_ALWAYS_INLINE osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t* attr) {
685 UNUSED(attr);
686 //furi_check((attr == NULL), "osEventFlagsNew: attr must be NULL");
687 return (osEventFlagsId_t)furi_event_flag_alloc();
688}
689
690// /// Get name of an Event Flags object.
691// /// \param[in] ef_id event flags ID obtained by \ref osEventFlagsNew.
692// /// \return name as null-terminated string.
693// const char *osEventFlagsGetName (osEventFlagsId_t ef_id);
694
699//uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags);
700static FURI_ALWAYS_INLINE uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags) {
701 return furi_event_flag_set((FuriEventFlag*)ef_id, flags);
702}
703
708//uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags);
709static FURI_ALWAYS_INLINE uint32_t osEventFlagsClear(osEventFlagsId_t ef_id, uint32_t flags) {
710 return furi_event_flag_clear((FuriEventFlag*)ef_id, flags);
711}
712
716//uint32_t osEventFlagsGet (osEventFlagsId_t ef_id);
717static FURI_ALWAYS_INLINE uint32_t osEventFlagsGet(osEventFlagsId_t ef_id) {
718 return furi_event_flag_get((FuriEventFlag*)ef_id);
719}
720
727// uint32_t
728// osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout);
729
730static inline uint32_t
731 osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout) {
732 return furi_event_flag_wait((FuriEventFlag*)ef_id, flags, options, timeout);
733}
734
738//osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id);
739static FURI_ALWAYS_INLINE osStatus_t osEventFlagsDelete(osEventFlagsId_t ef_id) {
740 furi_event_flag_free((FuriEventFlag*)ef_id);
741 return osOK;
742}
743
744// // ==== Mutex Management Functions ====
745
749static FURI_ALWAYS_INLINE osMutexId_t osMutexNew(const osMutexAttr_t* attr) {
750 const uint32_t attr_bits = attr ? attr->attr_bits : 0;
751 const FuriMutexType type = attr_bits & osMutexRecursive ? FuriMutexTypeRecursive : FuriMutexTypeNormal;
752 return (osMutexId_t)furi_mutex_alloc(type);
753}
754
755// /// Get name of a Mutex object.
756// /// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
757// /// \return name as null-terminated string.
758// const char *osMutexGetName (osMutexId_t mutex_id);
759
764// osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout);
765static FURI_ALWAYS_INLINE osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout) {
766 return (osStatus_t)furi_mutex_acquire((FuriMutex*)mutex_id, timeout);
767}
768
772// osStatus_t osMutexRelease (osMutexId_t mutex_id);
773static FURI_ALWAYS_INLINE osStatus_t osMutexRelease(osMutexId_t mutex_id) {
774 return (osStatus_t)furi_mutex_release((FuriMutex*)mutex_id);
775}
776
777// /// Get Thread which owns a Mutex object.
778// /// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
779// /// \return thread ID of owner thread or NULL when mutex was not acquired.
780// osThreadId_t osMutexGetOwner (osMutexId_t mutex_id);
781static FURI_ALWAYS_INLINE osThreadId_t osMutexGetOwner(osMutexId_t mutex_id) {
782 return (osThreadId_t)furi_mutex_get_owner((FuriMutex*)mutex_id);
783}
784
788// osStatus_t osMutexDelete (osMutexId_t mutex_id);
789static FURI_ALWAYS_INLINE osStatus_t osMutexDelete(osMutexId_t mutex_id) {
790 furi_mutex_free((FuriMutex*)mutex_id);
791 return osOK;
792}
793
794// // ==== Semaphore Management Functions ====
795
796// /// Create and Initialize a Semaphore object.
797// /// \param[in] max_count maximum number of available tokens.
798// /// \param[in] initial_count initial number of available tokens.
799// /// \param[in] attr semaphore attributes; NULL: default values.
800// /// \return semaphore ID for reference by other functions or NULL in case of error.
801// osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr);
802static FURI_ALWAYS_INLINE osSemaphoreId_t
803 osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t* attr) {
804 UNUSED(attr);
805 return (osSemaphoreId_t)furi_semaphore_alloc(max_count, initial_count);
806}
807
808// /// Get name of a Semaphore object.
809// /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
810// /// \return name as null-terminated string.
811// const char *osSemaphoreGetName (osSemaphoreId_t semaphore_id);
812
813// /// Acquire a Semaphore token or timeout if no tokens are available.
814// /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
815// /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
816// /// \return status code that indicates the execution status of the function.
817// osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout);
818static FURI_ALWAYS_INLINE osStatus_t
819 osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout) {
820 return (osStatus_t)furi_semaphore_acquire((FuriSemaphore*)semaphore_id, timeout);
821}
822
823// /// Release a Semaphore token up to the initial maximum count.
824// /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
825// /// \return status code that indicates the execution status of the function.
826// osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id);
827static FURI_ALWAYS_INLINE osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id) {
828 return (osStatus_t)furi_semaphore_release((FuriSemaphore*)semaphore_id);
829}
830
831// /// Get current Semaphore token count.
832// /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
833// /// \return number of tokens available.
834// uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id);
835
836// /// Delete a Semaphore object.
837// /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
838// /// \return status code that indicates the execution status of the function.
839// osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id);
840static FURI_ALWAYS_INLINE osStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id) {
841 furi_semaphore_free((FuriSemaphore*)semaphore_id);
842 return osOK;
843}
844
845// // ==== Memory Pool Management Functions ====
846
847// /// Create and Initialize a Memory Pool object.
848// /// \param[in] block_count maximum number of memory blocks in memory pool.
849// /// \param[in] block_size memory block size in bytes.
850// /// \param[in] attr memory pool attributes; NULL: default values.
851// /// \return memory pool ID for reference by other functions or NULL in case of error.
852// osMemoryPoolId_t osMemoryPoolNew (uint32_t block_count, uint32_t block_size, const osMemoryPoolAttr_t *attr);
853
854// /// Get name of a Memory Pool object.
855// /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
856// /// \return name as null-terminated string.
857// const char *osMemoryPoolGetName (osMemoryPoolId_t mp_id);
858
859// /// Allocate a memory block from a Memory Pool.
860// /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
861// /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
862// /// \return address of the allocated memory block or NULL in case of no memory is available.
863// void *osMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout);
864
865// /// Return an allocated memory block back to a Memory Pool.
866// /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
867// /// \param[in] block address of the allocated memory block to be returned to the memory pool.
868// /// \return status code that indicates the execution status of the function.
869// osStatus_t osMemoryPoolFree (osMemoryPoolId_t mp_id, void *block);
870
871// /// Get maximum number of memory blocks in a Memory Pool.
872// /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
873// /// \return maximum number of memory blocks.
874// uint32_t osMemoryPoolGetCapacity (osMemoryPoolId_t mp_id);
875
876// /// Get memory block size in a Memory Pool.
877// /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
878// /// \return memory block size in bytes.
879// uint32_t osMemoryPoolGetBlockSize (osMemoryPoolId_t mp_id);
880
881// /// Get number of memory blocks used in a Memory Pool.
882// /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
883// /// \return number of memory blocks used.
884// uint32_t osMemoryPoolGetCount (osMemoryPoolId_t mp_id);
885
886// /// Get number of memory blocks available in a Memory Pool.
887// /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
888// /// \return number of memory blocks available.
889// uint32_t osMemoryPoolGetSpace (osMemoryPoolId_t mp_id);
890
891// /// Delete a Memory Pool object.
892// /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
893// /// \return status code that indicates the execution status of the function.
894// osStatus_t osMemoryPoolDelete (osMemoryPoolId_t mp_id);
895
896// ==== Message Queue Management Functions ====
897
903//osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);
904static FURI_ALWAYS_INLINE osMessageQueueId_t
905 osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t* attr) {
906 UNUSED(attr);
907 return (osMessageQueueId_t)furi_message_queue_alloc(msg_count, msg_size);
908}
909
910// /// Get name of a Message Queue object.
911// /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
912// /// \return name as null-terminated string.
913// const char *osMessageQueueGetName (osMessageQueueId_t mq_id);
914
921// osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout);
922static FURI_ALWAYS_INLINE osStatus_t osMessageQueuePut(
923 osMessageQueueId_t mq_id,
924 const void* msg_ptr,
925 uint8_t msg_prio,
926 uint32_t timeout) {
927 UNUSED(msg_prio);
928 return (osStatus_t)furi_message_queue_put((FuriMessageQueue*)mq_id, msg_ptr, timeout);
929}
930
937// osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);
938static FURI_ALWAYS_INLINE osStatus_t osMessageQueueGet(
939 osMessageQueueId_t mq_id,
940 void* msg_ptr,
941 uint8_t* msg_prio,
942 uint32_t timeout) {
943 UNUSED(msg_prio);
944 return (osStatus_t)furi_message_queue_get((FuriMessageQueue*)mq_id, msg_ptr, timeout);
945}
946
947// /// Get maximum number of messages in a Message Queue.
948// /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
949// /// \return maximum number of messages.
950// uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id);
951
952// /// Get maximum message size in a Message Queue.
953// /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
954// /// \return maximum message size in bytes.
955// uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id);
956
957// /// Get number of queued messages in a Message Queue.
958// /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
959// /// \return number of queued messages.
960// uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id);
961
962// /// Get number of available slots for messages in a Message Queue.
963// /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
964// /// \return number of available slots for messages.
965// uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id);
966
967// /// Reset a Message Queue to initial empty state.
968// /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
969// /// \return status code that indicates the execution status of the function.
970// osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id);
971
975// osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id);
976static FURI_ALWAYS_INLINE osStatus_t osMessageQueueDelete(osMessageQueueId_t mq_id) {
977 furi_message_queue_free((FuriMessageQueue*)mq_id);
978 return osOK;
979}
980
981#ifdef __cplusplus
982}
983#endif
984
985#endif // CMSIS_OS2_H_
Definition cmsis_os2.h:280
Attributes structure for event flags.
Definition cmsis_os2.h:301
uint32_t attr_bits
attribute bits
Definition cmsis_os2.h:303
uint32_t cb_size
size of provided memory for control block
Definition cmsis_os2.h:305
const char * name
name of the event flags
Definition cmsis_os2.h:302
void * cb_mem
memory for control block
Definition cmsis_os2.h:304
Attributes structure for message queue.
Definition cmsis_os2.h:335
const char * name
name of the message queue
Definition cmsis_os2.h:336
uint32_t attr_bits
attribute bits
Definition cmsis_os2.h:337
void * cb_mem
memory for control block
Definition cmsis_os2.h:338
uint32_t mq_size
size of provided memory for data storage
Definition cmsis_os2.h:341
uint32_t cb_size
size of provided memory for control block
Definition cmsis_os2.h:339
void * mq_mem
memory for data storage
Definition cmsis_os2.h:340
Attributes structure for mutex.
Definition cmsis_os2.h:309
uint32_t cb_size
size of provided memory for control block
Definition cmsis_os2.h:313
uint32_t attr_bits
attribute bits
Definition cmsis_os2.h:311
const char * name
name of the mutex
Definition cmsis_os2.h:310
void * cb_mem
memory for control block
Definition cmsis_os2.h:312
Attributes structure for semaphore.
Definition cmsis_os2.h:317
const char * name
name of the semaphore
Definition cmsis_os2.h:318
uint32_t attr_bits
attribute bits
Definition cmsis_os2.h:319
uint32_t cb_size
size of provided memory for control block
Definition cmsis_os2.h:321
void * cb_mem
memory for control block
Definition cmsis_os2.h:320
Attributes structure for thread.
Definition cmsis_os2.h:268
void * cb_mem
memory for control block
Definition cmsis_os2.h:271
uint32_t reserved
reserved (must be 0)
Definition cmsis_os2.h:277
TZ_ModuleId_t tz_module
TrustZone module identifier.
Definition cmsis_os2.h:276
uint32_t attr_bits
attribute bits
Definition cmsis_os2.h:270
uint32_t cb_size
size of provided memory for control block
Definition cmsis_os2.h:272
void * stack_mem
memory for stack
Definition cmsis_os2.h:273
osPriority_t priority
initial thread priority (default: osPriorityNormal)
Definition cmsis_os2.h:275
uint32_t stack_size
size of stack
Definition cmsis_os2.h:274
const char * name
name of the thread
Definition cmsis_os2.h:269
Attributes structure for timer.
Definition cmsis_os2.h:293
void * cb_mem
memory for control block
Definition cmsis_os2.h:296
const char * name
name of the timer
Definition cmsis_os2.h:294
uint32_t attr_bits
attribute bits
Definition cmsis_os2.h:295
uint32_t cb_size
size of provided memory for control block
Definition cmsis_os2.h:297