BSB Developer Docs
Loading...
Searching...
No Matches
tusb_os_custom.h
1#pragma once
2
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7#include <furi.h>
8#include <FreeRTOS.h>
9
10// not used therefore defined to smallest possible type to save space
11typedef uint8_t osal_semaphore_def_t;
12typedef uint8_t osal_mutex_def_t;
13
14typedef FuriSemaphore* osal_semaphore_t;
15typedef FuriMutex* osal_mutex_t;
16typedef FuriMessageQueue* osal_queue_t;
17
18typedef struct {
19 uint16_t depth;
20 uint16_t item_sz;
22
23#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
24 osal_queue_def_t _name = {.depth = _depth, .item_sz = sizeof(_type)}
25
26FURI_ALWAYS_INLINE static inline uint32_t _osal_ms2tick(uint32_t msec) {
27 if(msec == OSAL_TIMEOUT_WAIT_FOREVER) {
28 return FuriWaitForever;
29 }
30 if(msec == 0) {
31 return 0;
32 }
33
34 uint32_t ticks = furi_ms_to_ticks(msec);
35
36 // we still need to delay at least 1 tick
37 if(ticks == 0) {
38 ticks = 1;
39 }
40
41 return ticks;
42}
43
44FURI_ALWAYS_INLINE static inline uint32_t osal_time_millis(void) {
45 return furi_get_tick();
46}
47
48FURI_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
49 furi_delay_ms(msec);
50}
51
52#define OSAL_SPINLOCK_DEF(_name, _int_set) osal_spinlock_t _name
53
54typedef UBaseType_t osal_spinlock_t;
55
56FURI_ALWAYS_INLINE static inline void osal_spin_init(osal_spinlock_t* ctx) {
57 UNUSED(ctx);
58}
59
60FURI_ALWAYS_INLINE static inline void osal_spin_lock(osal_spinlock_t* ctx, bool in_isr) {
61 if(in_isr) {
62 return; // single core MCU does not need to lock in ISR
63 }
64 UNUSED(ctx);
65 // FURI_CRITICAL_ENTER is not usable here
66 vPortEnterCritical();
67}
68
69FURI_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t* ctx, bool in_isr) {
70 if(in_isr) {
71 return; // single core MCU does not need to lock in ISR
72 }
73 UNUSED(ctx);
74 // FURI_CRITICAL_EXIT is not usable here
75 vPortExitCritical();
76}
77
78FURI_ALWAYS_INLINE static inline osal_semaphore_t
79 osal_semaphore_create(osal_semaphore_def_t* semdef) {
80 UNUSED(semdef);
81 return furi_semaphore_alloc(1, 0);
82}
83
84FURI_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) {
85 furi_semaphore_free(semd_hdl);
86 return true;
87}
88
89FURI_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
90 UNUSED(in_isr);
91 FuriStatus stat = furi_semaphore_release(sem_hdl);
92 return (stat == FuriStatusOk);
93}
94
95FURI_ALWAYS_INLINE static inline bool
96 osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
97 FuriStatus stat = furi_semaphore_acquire(sem_hdl, _osal_ms2tick(msec));
98 return (stat == FuriStatusOk);
99}
100
101FURI_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
102 furi_semaphore_acquire(sem_hdl, 0);
103}
104
105FURI_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) {
106 UNUSED(mdef);
107 return furi_mutex_alloc(FuriMutexTypeNormal);
108}
109
110FURI_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) {
111 furi_mutex_free(mutex_hdl);
112 return true;
113}
114
115FURI_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) {
116 FuriStatus stat = furi_mutex_acquire(mutex_hdl, _osal_ms2tick(msec));
117 return (stat == FuriStatusOk);
118}
119
120FURI_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
121 FuriStatus stat = furi_mutex_release(mutex_hdl);
122 return (stat == FuriStatusOk);
123}
124
125FURI_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) {
126 return furi_message_queue_alloc(qdef->depth, qdef->item_sz);
127}
128
129FURI_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) {
130 furi_message_queue_free(qhdl);
131 return true;
132}
133
134FURI_ALWAYS_INLINE static inline bool
135 osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
136 FuriStatus stat = furi_message_queue_get(qhdl, data, _osal_ms2tick(msec));
137 return (stat == FuriStatusOk);
138}
139
140FURI_ALWAYS_INLINE static inline bool
141 osal_queue_send(osal_queue_t qhdl, void const* data, bool in_isr) {
142 UNUSED(in_isr);
143 FuriStatus stat = furi_message_queue_put(qhdl, data, in_isr ? 0 : FuriWaitForever);
144 return (stat == FuriStatusOk);
145}
146
147FURI_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
148 return (furi_message_queue_get_count(qhdl) == 0);
149}
150
151#ifdef __cplusplus
152}
153#endif
Definition tusb_os_custom.h:18