#ifndef _TUSB_OSAL_RTX4_H_
#define _TUSB_OSAL_RTX4_H_
#include <rtl.h>
#ifdef __cplusplus
extern "C" {
#endif
static inline void osal_task_delay(uint32_t msec)
{
uint16_t hi = msec >> 16;
uint16_t lo = msec;
while (hi--) {
os_dly_wait(0xFFFE);
}
os_dly_wait(lo);
}
static inline uint16_t msec2wait(uint32_t msec) {
if (msec == OSAL_TIMEOUT_WAIT_FOREVER)
return 0xFFFF;
else if (msec >= 0xFFFE)
return 0xFFFE;
else
return msec;
}
typedef OS_SEM osal_semaphore_def_t;
typedef OS_ID osal_semaphore_t;
static inline OS_ID osal_semaphore_create(osal_semaphore_def_t* semdef) {
os_sem_init(semdef, 0);
return semdef;
}
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
if ( !in_isr ) {
os_sem_send(sem_hdl);
} else {
isr_sem_send(sem_hdl);
}
return true;
}
static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) {
return os_sem_wait(sem_hdl, msec2wait(msec)) != OS_R_TMO;
}
static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
}
typedef OS_MUT osal_mutex_def_t;
typedef OS_ID osal_mutex_t;
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
{
os_mut_init(mdef);
return mdef;
}
static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
{
return os_mut_wait(mutex_hdl, msec2wait(msec)) != OS_R_TMO;
}
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
{
return os_mut_release(mutex_hdl) == OS_R_OK;
}
#define OSAL_QUEUE_DEF(_role, _name, _depth, _type) \
os_mbx_declare(_name##__mbox, _depth); \
_declare_box(_name##__pool, sizeof(_type), _depth); \
osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .pool = _name##__pool, .mbox = _name##__mbox };
typedef struct
{
uint16_t depth;
uint16_t item_sz;
U32* pool;
U32* mbox;
}osal_queue_def_t;
typedef osal_queue_def_t* osal_queue_t;
static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
{
os_mbx_init(qdef->mbox, (qdef->depth + 4) * 4);
_init_box(qdef->pool, ((qdef->item_sz+3)/4)*(qdef->depth) + 3, qdef->item_sz);
return qdef;
}
static inline bool osal_queue_receive(osal_queue_t qhdl, void* data)
{
void* buf;
os_mbx_wait(qhdl->mbox, &buf, 0xFFFF);
memcpy(data, buf, qhdl->item_sz);
_free_box(qhdl->pool, buf);
return true;
}
static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
{
void* buf = _alloc_box(qhdl->pool);
memcpy(buf, data, qhdl->item_sz);
if ( !in_isr )
{
os_mbx_send(qhdl->mbox, buf, 0xFFFF);
}
else
{
isr_mbx_send(qhdl->mbox, buf);
}
return true;
}
static inline bool osal_queue_empty(osal_queue_t qhdl)
{
return os_mbx_check(qhdl->mbox) == qhdl->depth;
}
#ifdef __cplusplus
}
#endif
#endif