Open FFBoard
Open source force feedback firmware
osal_pico.h
Go to the documentation of this file.
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
36extern "C" {
37#endif
38
39//--------------------------------------------------------------------+
40// TASK API
41//--------------------------------------------------------------------+
42TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
43 sleep_ms(msec);
44}
45
46//--------------------------------------------------------------------+
47// Binary Semaphore API
48//--------------------------------------------------------------------+
49typedef struct semaphore osal_semaphore_def_t, * osal_semaphore_t;
50
51TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) {
52 sem_init(semdef, 0, 255);
53 return semdef;
54}
55
56TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) {
57 (void) semd_hdl;
58 return true; // nothing to do
59}
60
61TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
62 (void) in_isr;
63 sem_release(sem_hdl);
64 return true;
65}
66
67TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
68 return sem_acquire_timeout_ms(sem_hdl, msec);
69}
70
71TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) {
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 mutex_init(mdef);
83 return mdef;
84}
85
86TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) {
87 (void) mutex_hdl;
88 return true; // nothing to do
89}
90
91TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) {
92 return mutex_enter_timeout_ms(mutex_hdl, msec);
93}
94
95TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
96 mutex_exit(mutex_hdl);
97 return true;
98}
99
100//--------------------------------------------------------------------+
101// QUEUE API
102//--------------------------------------------------------------------+
103#include "common/tusb_fifo.h"
104
105typedef struct {
106 tu_fifo_t ff;
107 struct critical_section critsec; // osal_queue may be used in IRQs, so need critical section
109
111
112// role device/host is used by OS NONE for mutex (disable usb isr) only
113#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
114 uint8_t _name##_buf[_depth*sizeof(_type)]; \
115 osal_queue_def_t _name = { \
116 .ff = TU_FIFO_INIT(_name##_buf, _depth, _type, false) \
117 }
118
119TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) {
120 critical_section_init(&qdef->critsec);
121 tu_fifo_clear(&qdef->ff);
122 return (osal_queue_t) qdef;
123}
124
125TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) {
126 osal_queue_def_t* qdef = (osal_queue_def_t*) qhdl;
127 critical_section_deinit(&qdef->critsec);
128 return true;
129}
130
131TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
132 (void) msec; // not used, always behave as msec = 0
133
134 critical_section_enter_blocking(&qhdl->critsec);
135 bool success = tu_fifo_read(&qhdl->ff, data);
136 critical_section_exit(&qhdl->critsec);
137
138 return success;
139}
140
141TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const* data, bool in_isr) {
142 (void) in_isr;
143
144 critical_section_enter_blocking(&qhdl->critsec);
145 bool success = tu_fifo_write(&qhdl->ff, data);
146 critical_section_exit(&qhdl->critsec);
147
148 return success;
149}
150
151TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
152 // TODO: revisit; whether this is true or not currently, tu_fifo_empty is a single
153 // volatile read.
154
155 // Skip queue lock/unlock since this function is primarily called
156 // with interrupt disabled before going into low power mode
157 return tu_fifo_empty(&qhdl->ff);
158}
159
160#ifdef __cplusplus
161}
162#endif
163
164#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
struct semaphore * osal_semaphore_t
Definition: osal_pico.h:49
static TU_ATTR_ALWAYS_INLINE bool osal_queue_empty(osal_queue_t qhdl)
Definition: osal_pico.h:151
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_delete(osal_semaphore_t semd_hdl)
Definition: osal_pico.h:56
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
Definition: osal_pico.h:67
static TU_ATTR_ALWAYS_INLINE osal_queue_t osal_queue_create(osal_queue_def_t *qdef)
Definition: osal_pico.h:119
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
Definition: osal_pico.h:91
static TU_ATTR_ALWAYS_INLINE void osal_task_delay(uint32_t msec)
Definition: osal_pico.h:42
static TU_ATTR_ALWAYS_INLINE bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr)
Definition: osal_pico.h:141
osal_queue_def_t * osal_queue_t
Definition: osal_pico.h:110
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
Definition: osal_pico.h:95
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_delete(osal_mutex_t mutex_hdl)
Definition: osal_pico.h:86
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
Definition: osal_pico.h:61
static TU_ATTR_ALWAYS_INLINE bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec)
Definition: osal_pico.h:131
static TU_ATTR_ALWAYS_INLINE bool osal_queue_delete(osal_queue_t qhdl)
Definition: osal_pico.h:125
static TU_ATTR_ALWAYS_INLINE osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t *semdef)
Definition: osal_pico.h:51
struct mutex * osal_mutex_t
Definition: osal_pico.h:79
static TU_ATTR_ALWAYS_INLINE void osal_semaphore_reset(osal_semaphore_t sem_hdl)
Definition: osal_pico.h:71
static TU_ATTR_ALWAYS_INLINE osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef)
Definition: osal_pico.h:81
tu_fifo_t ff
Definition: osal_none.h:127
struct critical_section critsec
Definition: osal_pico.h:107
bool tu_fifo_read(tu_fifo_t *f, void *buffer)
Read one element out of the buffer.
Definition: tusb_fifo.c:699
bool tu_fifo_empty(tu_fifo_t *f)
Check if FIFO is empty.
Definition: tusb_fifo.c:608
bool tu_fifo_write(tu_fifo_t *f, const void *data)
Write one element into the buffer.
Definition: tusb_fifo.c:819
bool tu_fifo_clear(tu_fifo_t *f)
Clear the fifo read and write pointers.
Definition: tusb_fifo.c:896