blob: 1c3366e0194f6cb9d1c9f521900323de2ed789a4 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
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//--------------------------------------------------------------------+
42static 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
52static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
53{
54 sem_init(semdef, 0, 255);
55 return semdef;
56}
57
58static 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
65static 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
70static 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
81static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
82{
83 mutex_init(mdef);
84 return mdef;
85}
86
87static 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
92static 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
103#if TUSB_OPT_HOST_ENABLED
104extern void hcd_int_disable(uint8_t rhport);
105extern void hcd_int_enable(uint8_t rhport);
106#endif
107
108typedef struct
109{
110 tu_fifo_t ff;
111 struct critical_section critsec; // osal_queue may be used in IRQs, so need critical section
112} osal_queue_def_t;
113
114typedef osal_queue_def_t* osal_queue_t;
115
116// role device/host is used by OS NONE for mutex (disable usb isr) only
117#define OSAL_QUEUE_DEF(_role, _name, _depth, _type) \
118 uint8_t _name##_buf[_depth*sizeof(_type)]; \
119 osal_queue_def_t _name = { \
120 .ff = TU_FIFO_INIT(_name##_buf, _depth, _type, false) \
121 }
122
123// lock queue by disable USB interrupt
124static inline void _osal_q_lock(osal_queue_t qhdl)
125{
126 critical_section_enter_blocking(&qhdl->critsec);
127}
128
129// unlock queue
130static inline void _osal_q_unlock(osal_queue_t qhdl)
131{
132 critical_section_exit(&qhdl->critsec);
133}
134
135static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
136{
137 critical_section_init(&qdef->critsec);
138 tu_fifo_clear(&qdef->ff);
139 return (osal_queue_t) qdef;
140}
141
142static inline bool osal_queue_receive(osal_queue_t qhdl, void* data)
143{
144 // TODO: revisit... docs say that mutexes are never used from IRQ context,
145 // however osal_queue_recieve may be. therefore my assumption is that
146 // the fifo mutex is not populated for queues used from an IRQ context
147 //assert(!qhdl->ff.mutex);
148
149 _osal_q_lock(qhdl);
150 bool success = tu_fifo_read(&qhdl->ff, data);
151 _osal_q_unlock(qhdl);
152
153 return success;
154}
155
156static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
157{
158 // TODO: revisit... docs say that mutexes are never used from IRQ context,
159 // however osal_queue_recieve may be. therefore my assumption is that
160 // the fifo mutex is not populated for queues used from an IRQ context
161 //assert(!qhdl->ff.mutex);
162 (void) in_isr;
163
164 _osal_q_lock(qhdl);
165 bool success = tu_fifo_write(&qhdl->ff, data);
166 _osal_q_unlock(qhdl);
167
168 TU_ASSERT(success);
169
170 return success;
171}
172
173static inline bool osal_queue_empty(osal_queue_t qhdl)
174{
175 // TODO: revisit; whether this is true or not currently, tu_fifo_empty is a single
176 // volatile read.
177
178 // Skip queue lock/unlock since this function is primarily called
179 // with interrupt disabled before going into low power mode
180 return tu_fifo_empty(&qhdl->ff);
181}
182
183#ifdef __cplusplus
184 }
185#endif
186
187#endif /* _TUSB_OSAL_PICO_H_ */