Open FFBoard
Open source force feedback firmware
osal_rtx4.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2021 Tian Yunhao (t123yh)
5 * Copyright (c) 2019 Ha Thach (tinyusb.org)
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 * This file is part of the TinyUSB stack.
26 */
27
28#ifndef TUSB_OSAL_RTX4_H_
29#define TUSB_OSAL_RTX4_H_
30
31#include <rtl.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37//--------------------------------------------------------------------+
38// TASK API
39//--------------------------------------------------------------------+
40TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
41 uint16_t hi = msec >> 16;
42 uint16_t lo = msec;
43 while (hi--) {
44 os_dly_wait(0xFFFE);
45 }
46 os_dly_wait(lo);
47}
48
49TU_ATTR_ALWAYS_INLINE static inline uint16_t msec2wait(uint32_t msec) {
50 if (msec == OSAL_TIMEOUT_WAIT_FOREVER) {
51 return 0xFFFF;
52 } else if (msec >= 0xFFFE) {
53 return 0xFFFE;
54 } else {
55 return msec;
56 }
57}
58
59//--------------------------------------------------------------------+
60// Semaphore API
61//--------------------------------------------------------------------+
62typedef OS_SEM osal_semaphore_def_t;
63typedef OS_ID osal_semaphore_t;
64
65TU_ATTR_ALWAYS_INLINE static inline OS_ID osal_semaphore_create(osal_semaphore_def_t* semdef) {
66 os_sem_init(semdef, 0);
67 return semdef;
68}
69
70TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) {
71 (void) semd_hdl;
72 return true; // nothing to do
73}
74
75TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
76 if ( !in_isr ) {
77 os_sem_send(sem_hdl);
78 } else {
79 isr_sem_send(sem_hdl);
80 }
81 return true;
82}
83
84TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) {
85 return os_sem_wait(sem_hdl, msec2wait(msec)) != OS_R_TMO;
86}
87
88TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
89 // TODO: implement
90}
91
92//--------------------------------------------------------------------+
93// MUTEX API (priority inheritance)
94//--------------------------------------------------------------------+
95typedef OS_MUT osal_mutex_def_t;
96typedef OS_ID osal_mutex_t;
97
98TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) {
99 os_mut_init(mdef);
100 return mdef;
101}
102
103TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) {
104 (void) mutex_hdl;
105 return true; // nothing to do
106}
107
108TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) {
109 return os_mut_wait(mutex_hdl, msec2wait(msec)) != OS_R_TMO;
110}
111
112TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
113 return os_mut_release(mutex_hdl) == OS_R_OK;
114}
115
116//--------------------------------------------------------------------+
117// QUEUE API
118//--------------------------------------------------------------------+
119
120// role device/host is used by OS NONE for mutex (disable usb isr) only
121#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
122 os_mbx_declare(_name##__mbox, _depth); \
123 _declare_box(_name##__pool, sizeof(_type), _depth); \
124 osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .pool = _name##__pool, .mbox = _name##__mbox };
125
126typedef struct {
127 uint16_t depth;
128 uint16_t item_sz;
129 U32* pool;
130 U32* mbox;
132
134
135TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) {
136 os_mbx_init(qdef->mbox, (qdef->depth + 4) * 4);
137 _init_box(qdef->pool, ((qdef->item_sz+3)/4)*(qdef->depth) + 3, qdef->item_sz);
138 return qdef;
139}
140
141TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
142 void* buf;
143 os_mbx_wait(qhdl->mbox, &buf, msec2wait(msec));
144 memcpy(data, buf, qhdl->item_sz);
145 _free_box(qhdl->pool, buf);
146 return true;
147}
148
149TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) {
150 (void) qhdl;
151 return true; // nothing to do ?
152}
153
154TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) {
155 void* buf = _alloc_box(qhdl->pool);
156 memcpy(buf, data, qhdl->item_sz);
157 if ( !in_isr ) {
158 os_mbx_send(qhdl->mbox, buf, 0xFFFF);
159 } else {
160 isr_mbx_send(qhdl->mbox, buf);
161 }
162 return true;
163}
164
165TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
166 return os_mbx_check(qhdl->mbox) == qhdl->depth;
167}
168
169#ifdef __cplusplus
170 }
171#endif
172
173#endif
static struct @612 data
static bool in_isr
StaticSemaphore_t osal_mutex_def_t
Definition: osal_freertos.h:46
QueueHandle_t osal_queue_t
Definition: osal_freertos.h:55
SemaphoreHandle_t osal_semaphore_t
Definition: osal_freertos.h:53
SemaphoreHandle_t osal_mutex_t
Definition: osal_freertos.h:54
OS_SEM osal_semaphore_def_t
Definition: osal_rtx4.h:62
static TU_ATTR_ALWAYS_INLINE bool osal_queue_empty(osal_queue_t qhdl)
Definition: osal_rtx4.h:165
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_delete(osal_semaphore_t semd_hdl)
Definition: osal_rtx4.h:70
OS_MUT osal_mutex_def_t
Definition: osal_rtx4.h:95
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
Definition: osal_rtx4.h:84
static TU_ATTR_ALWAYS_INLINE uint16_t msec2wait(uint32_t msec)
Definition: osal_rtx4.h:49
static TU_ATTR_ALWAYS_INLINE osal_queue_t osal_queue_create(osal_queue_def_t *qdef)
Definition: osal_rtx4.h:135
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
Definition: osal_rtx4.h:108
static TU_ATTR_ALWAYS_INLINE void osal_task_delay(uint32_t msec)
Definition: osal_rtx4.h:40
static TU_ATTR_ALWAYS_INLINE bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr)
Definition: osal_rtx4.h:154
static TU_ATTR_ALWAYS_INLINE OS_ID osal_semaphore_create(osal_semaphore_def_t *semdef)
Definition: osal_rtx4.h:65
osal_queue_def_t * osal_queue_t
Definition: osal_rtx4.h:133
OS_ID osal_mutex_t
Definition: osal_rtx4.h:96
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
Definition: osal_rtx4.h:112
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_delete(osal_mutex_t mutex_hdl)
Definition: osal_rtx4.h:103
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
Definition: osal_rtx4.h:75
static TU_ATTR_ALWAYS_INLINE bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec)
Definition: osal_rtx4.h:141
static TU_ATTR_ALWAYS_INLINE bool osal_queue_delete(osal_queue_t qhdl)
Definition: osal_rtx4.h:149
static TU_ATTR_ALWAYS_INLINE void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
Definition: osal_rtx4.h:88
OS_ID osal_semaphore_t
Definition: osal_rtx4.h:63
static TU_ATTR_ALWAYS_INLINE osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef)
Definition: osal_rtx4.h:98
static void * memcpy(void *dst, const void *src, size_t n)
Definition: ringbuffer.c:8