mikroSDK Reference Manual
osal_rtx4.h
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{
42 uint16_t hi = msec >> 16;
43 uint16_t lo = msec;
44 while (hi--) {
45 os_dly_wait(0xFFFE);
46 }
47 os_dly_wait(lo);
48}
49
50TU_ATTR_ALWAYS_INLINE static inline uint16_t msec2wait(uint32_t msec) {
51 if (msec == OSAL_TIMEOUT_WAIT_FOREVER)
52 return 0xFFFF;
53 else if (msec >= 0xFFFE)
54 return 0xFFFE;
55 else
56 return msec;
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_post(osal_semaphore_t sem_hdl, bool in_isr) {
71 if ( !in_isr ) {
72 os_sem_send(sem_hdl);
73 } else {
74 isr_sem_send(sem_hdl);
75 }
76 return true;
77}
78
79TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) {
80 return os_sem_wait(sem_hdl, msec2wait(msec)) != OS_R_TMO;
81}
82
83TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
84 // TODO: implement
85}
86
87//--------------------------------------------------------------------+
88// MUTEX API (priority inheritance)
89//--------------------------------------------------------------------+
90typedef OS_MUT osal_mutex_def_t;
91typedef OS_ID osal_mutex_t;
92
93TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
94{
95 os_mut_init(mdef);
96 return mdef;
97}
98
99TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
100{
101 return os_mut_wait(mutex_hdl, msec2wait(msec)) != OS_R_TMO;
102}
103
104TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
105{
106 return os_mut_release(mutex_hdl) == OS_R_OK;
107}
108
109//--------------------------------------------------------------------+
110// QUEUE API
111//--------------------------------------------------------------------+
112
113// role device/host is used by OS NONE for mutex (disable usb isr) only
114#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
115 os_mbx_declare(_name##__mbox, _depth); \
116 _declare_box(_name##__pool, sizeof(_type), _depth); \
117 osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .pool = _name##__pool, .mbox = _name##__mbox };
118
119
120typedef struct
121{
122 uint16_t depth;
123 uint16_t item_sz;
124 U32* pool;
125 U32* mbox;
127
128typedef osal_queue_def_t* osal_queue_t;
129
130TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
131{
132 os_mbx_init(qdef->mbox, (qdef->depth + 4) * 4);
133 _init_box(qdef->pool, ((qdef->item_sz+3)/4)*(qdef->depth) + 3, qdef->item_sz);
134 return qdef;
135}
136
137TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
138{
139 void* buf;
140 os_mbx_wait(qhdl->mbox, &buf, msec2wait(msec));
141 memcpy(data, buf, qhdl->item_sz);
142 _free_box(qhdl->pool, buf);
143 return true;
144}
145
146TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
147{
148 void* buf = _alloc_box(qhdl->pool);
149 memcpy(buf, data, qhdl->item_sz);
150 if ( !in_isr )
151 {
152 os_mbx_send(qhdl->mbox, buf, 0xFFFF);
153 }
154 else
155 {
156 isr_mbx_send(qhdl->mbox, buf);
157 }
158 return true;
159}
160
161TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
162{
163 return os_mbx_check(qhdl->mbox) == qhdl->depth;
164}
165
166#ifdef __cplusplus
167 }
168#endif
169
170#endif
Definition osal_freertos.h:58
Definition osal_none.h:47