mikroSDK Reference Manual
osal_pico.h
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#ifndef _TUSB_OSAL_PICO_H_
28#define _TUSB_OSAL_PICO_H_
29
30#include "pico/time.h"
31#include "pico/sem.h"
32#include "pico/mutex.h"
33#include "pico/critical_section.h"
34
35#ifdef __cplusplus
36 extern "C" {
37#endif
38
39//--------------------------------------------------------------------+
40// TASK API
41//--------------------------------------------------------------------+
42TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
43{
44 sleep_ms(msec);
45}
46
47//--------------------------------------------------------------------+
48// Binary Semaphore API
49//--------------------------------------------------------------------+
50typedef struct semaphore osal_semaphore_def_t, *osal_semaphore_t;
51
52TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
53{
54 sem_init(semdef, 0, 255);
55 return semdef;
56}
57
58TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
59{
60 (void) in_isr;
61 sem_release(sem_hdl);
62 return true;
63}
64
65TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
66{
67 return sem_acquire_timeout_ms(sem_hdl, msec);
68}
69
70TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
71{
72 sem_reset(sem_hdl, 0);
73}
74
75//--------------------------------------------------------------------+
76// MUTEX API
77// Within tinyusb, mutex is never used in ISR context
78//--------------------------------------------------------------------+
79typedef struct mutex osal_mutex_def_t, *osal_mutex_t;
80
81TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
82{
83 mutex_init(mdef);
84 return mdef;
85}
86
87TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
88{
89 return mutex_enter_timeout_ms(mutex_hdl, msec);
90}
91
92TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
93{
94 mutex_exit(mutex_hdl);
95 return true;
96}
97
98//--------------------------------------------------------------------+
99// QUEUE API
100//--------------------------------------------------------------------+
101#include "common/tusb_fifo.h"
102
103typedef struct
104{
105 tu_fifo_t ff;
106 struct critical_section critsec; // osal_queue may be used in IRQs, so need critical section
108
109typedef osal_queue_def_t* osal_queue_t;
110
111// role device/host is used by OS NONE for mutex (disable usb isr) only
112#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
113 uint8_t _name##_buf[_depth*sizeof(_type)]; \
114 osal_queue_def_t _name = { \
115 .ff = TU_FIFO_INIT(_name##_buf, _depth, _type, false) \
116 }
117
118// lock queue by disable USB interrupt
119TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl)
120{
121 critical_section_enter_blocking(&qhdl->critsec);
122}
123
124// unlock queue
125TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl)
126{
127 critical_section_exit(&qhdl->critsec);
128}
129
130TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
131{
132 critical_section_init(&qdef->critsec);
133 tu_fifo_clear(&qdef->ff);
134 return (osal_queue_t) 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) msec; // not used, always behave as msec = 0
140
141 // TODO: revisit... docs say that mutexes are never used from IRQ context,
142 // however osal_queue_recieve may be. therefore my assumption is that
143 // the fifo mutex is not populated for queues used from an IRQ context
144 //assert(!qhdl->ff.mutex);
145
146 _osal_q_lock(qhdl);
147 bool success = tu_fifo_read(&qhdl->ff, data);
148 _osal_q_unlock(qhdl);
149
150 return success;
151}
152
153TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
154{
155 // TODO: revisit... docs say that mutexes are never used from IRQ context,
156 // however osal_queue_recieve may be. therefore my assumption is that
157 // the fifo mutex is not populated for queues used from an IRQ context
158 //assert(!qhdl->ff.mutex);
159 (void) in_isr;
160
161 _osal_q_lock(qhdl);
162 bool success = tu_fifo_write(&qhdl->ff, data);
163 _osal_q_unlock(qhdl);
164
165 TU_ASSERT(success);
166
167 return success;
168}
169
170TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
171{
172 // TODO: revisit; whether this is true or not currently, tu_fifo_empty is a single
173 // volatile read.
174
175 // Skip queue lock/unlock since this function is primarily called
176 // with interrupt disabled before going into low power mode
177 return tu_fifo_empty(&qhdl->ff);
178}
179
180#ifdef __cplusplus
181 }
182#endif
183
184#endif /* _TUSB_OSAL_PICO_H_ */
Definition osal_freertos.h:58
Definition osal_none.h:47
Definition tusb_fifo.h:108