19 #ifndef TUSB_OS_CUSTOM_H
20 #define TUSB_OS_CUSTOM_H
33 #ifndef TINYUSB_OSAL_MUTEX_CANCELABLE
34 #define TINYUSB_OSAL_MUTEX_CANCELABLE 0
41 TU_ATTR_ALWAYS_INLINE
static inline void osal_task_delay(uint32_t msec)
52 typedef sema_t osal_semaphore_def_t;
53 typedef sema_t *osal_semaphore_t;
56 static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
59 return (osal_semaphore_t)semdef;
63 static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl,
bool in_isr)
70 static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
76 TU_ATTR_ALWAYS_INLINE
static inline void osal_semaphore_reset(osal_semaphore_t
const sem_hdl)
83 typedef mutex_t osal_mutex_def_t;
87 static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
91 return (osal_mutex_t)mdef;
94 #if TINYUSB_OSAL_MUTEX_CANCELABLE
95 static void _osal_mutex_lock_timeout(
void *arg)
101 TU_ATTR_ALWAYS_INLINE
102 static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
105 #if TINYUSB_OSAL_MUTEX_CANCELABLE
114 assert(msec == OSAL_TIMEOUT_WAIT_FOREVER);
120 TU_ATTR_ALWAYS_INLINE
121 static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
132 #define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
134 static _type _name##_##q_items[_depth]; \
135 osal_queue_def_t _name = { \
136 .buffer = _name##_##q_items, \
137 .size = sizeof(_type), \
155 typedef osal_queue_def_t *osal_queue_t;
157 TU_ATTR_ALWAYS_INLINE
static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
166 return (osal_queue_t)qdef;
169 TU_ATTR_ALWAYS_INLINE
170 static inline bool osal_queue_send(osal_queue_t qhdl,
void const * data,
bool in_isr)
200 memcpy(qhdl->buffer + (qhdl->tail * (qhdl->size)), data, qhdl->size);
202 qhdl->tail = (qhdl->tail + 1) % qhdl->depth;
210 #if TINYUSB_OSAL_MUTEX_CANCELABLE
211 static void _osal_queue_lock_timeout(
void *arg)
217 TU_ATTR_ALWAYS_INLINE
218 static inline bool osal_queue_receive(osal_queue_t qhdl,
void* data, uint32_t msec)
234 assert(qhdl->front != qhdl->tail);
237 memcpy(data, qhdl->buffer + (qhdl->front * (qhdl->size)), qhdl->size);
239 qhdl->front = (qhdl->front + 1) % qhdl->depth;
244 TU_ATTR_ALWAYS_INLINE
static inline bool osal_queue_empty(osal_queue_t qhdl)
#define assert(cond)
abort the program if assertion is false
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
void mutex_cancel(mutex_cancel_t *mc)
Cancels a call to mutex_lock_cancelable.
static mutex_cancel_t mutex_cancel_init(mutex_t *mutex)
Initialize a mutex cancellation structure.
int mutex_lock_cancelable(mutex_cancel_t *mc)
Locks a mutex, blocking.
static void mutex_init(mutex_t *mutex)
Initializes a mutex object.
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
void sema_create(sema_t *sema, unsigned int value)
Creates semaphore dynamically.
static int sema_wait(sema_t *sema)
Wait for a semaphore being posted (without timeout).
static int sema_wait_timed_ztimer(sema_t *sema, ztimer_clock_t *clock, uint32_t timeout)
Wait for a semaphore being posted, using ztimer as backend.
static unsigned sema_get_value(const sema_t *sema)
Get a semaphore's current value.
int sema_post(sema_t *sema)
Signal semaphore.
uint32_t ztimer_set(ztimer_clock_t *clock, ztimer_t *timer, uint32_t val)
Set a timer on a clock.
void ztimer_sleep(ztimer_clock_t *clock, uint32_t duration)
Put the calling thread to sleep for the specified number of ticks.
ztimer_clock_t *const ZTIMER_MSEC
Default ztimer millisecond clock.
Mutex for thread synchronization.
A cancellation structure for use with mutex_lock_cancelable and mutex_cancel.
ztimer_callback_t callback
timer callback function pointer