Open FFBoard
Open source force feedback firmware
osal_none.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
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_NONE_H_
28#define TUSB_OSAL_NONE_H_
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34//--------------------------------------------------------------------+
35// TASK API
36//--------------------------------------------------------------------+
37
38#if CFG_TUH_ENABLED
39// currently only needed/available in host mode
40TU_ATTR_WEAK void osal_task_delay(uint32_t msec);
41#endif
42
43//--------------------------------------------------------------------+
44// Binary Semaphore API
45//--------------------------------------------------------------------+
46typedef struct {
47 volatile uint16_t count;
49
51
52TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) {
53 semdef->count = 0;
54 return semdef;
55}
56
57TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) {
58 (void) semd_hdl;
59 return true; // nothing to do
60}
61
62
63TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
64 (void) in_isr;
65 sem_hdl->count++;
66 return true;
67}
68
69// TODO blocking for now
70TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
71 (void) msec;
72
73 while (sem_hdl->count == 0) {}
74 sem_hdl->count--;
75
76 return true;
77}
78
79TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) {
80 sem_hdl->count = 0;
81}
82
83//--------------------------------------------------------------------+
84// MUTEX API
85// Within tinyusb, mutex is never used in ISR context
86//--------------------------------------------------------------------+
89
90#if OSAL_MUTEX_REQUIRED
91// Note: multiple cores MCUs usually do provide IPC API for mutex
92// or we can use std atomic function
93
94TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) {
95 mdef->count = 1;
96 return mdef;
97}
98
99TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) {
100 (void) mutex_hdl;
101 return true; // nothing to do
102}
103
104TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) {
105 return osal_semaphore_wait(mutex_hdl, msec);
106}
107
108TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
109 return osal_semaphore_post(mutex_hdl, false);
110}
111
112#else
113
114#define osal_mutex_create(_mdef) (NULL)
115#define osal_mutex_lock(_mutex_hdl, _ms) (true)
116#define osal_mutex_unlock(_mutex_hdl) (true)
117
118#endif
119
120//--------------------------------------------------------------------+
121// QUEUE API
122//--------------------------------------------------------------------+
123#include "common/tusb_fifo.h"
124
125typedef struct {
126 void (* interrupt_set)(bool);
129
131
132// _int_set is used as mutex in OS NONE (disable/enable USB ISR)
133#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
134 uint8_t _name##_buf[_depth*sizeof(_type)]; \
135 osal_queue_def_t _name = { \
136 .interrupt_set = _int_set, \
137 .ff = TU_FIFO_INIT(_name##_buf, _depth, _type, false) \
138 }
139
140// lock queue by disable USB interrupt
141TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl) {
142 // disable dcd/hcd interrupt
143 qhdl->interrupt_set(false);
144}
145
146// unlock queue
147TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl) {
148 // enable dcd/hcd interrupt
149 qhdl->interrupt_set(true);
150}
151
152TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) {
153 tu_fifo_clear(&qdef->ff);
154 return (osal_queue_t) qdef;
155}
156
157TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) {
158 (void) qhdl;
159 return true; // nothing to do
160}
161
162TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
163 (void) msec; // not used, always behave as msec = 0
164
165 _osal_q_lock(qhdl);
166 bool success = tu_fifo_read(&qhdl->ff, data);
167 _osal_q_unlock(qhdl);
168
169 return success;
170}
171
172TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const* data, bool in_isr) {
173 if (!in_isr) {
174 _osal_q_lock(qhdl);
175 }
176
177 bool success = tu_fifo_write(&qhdl->ff, data);
178
179 if (!in_isr) {
180 _osal_q_unlock(qhdl);
181 }
182
183 return success;
184}
185
186TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
187 // Skip queue lock/unlock since this function is primarily called
188 // with interrupt disabled before going into low power mode
189 return tu_fifo_empty(&qhdl->ff);
190}
191
192#ifdef __cplusplus
193}
194#endif
195
196#endif
static struct @612 data
static bool in_isr
StaticSemaphore_t osal_semaphore_def_t
Definition: osal_freertos.h:45
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
osal_semaphore_def_t osal_mutex_def_t
Definition: osal_none.h:87
static TU_ATTR_ALWAYS_INLINE bool osal_queue_empty(osal_queue_t qhdl)
Definition: osal_none.h:186
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_delete(osal_semaphore_t semd_hdl)
Definition: osal_none.h:57
static TU_ATTR_ALWAYS_INLINE void _osal_q_lock(osal_queue_t qhdl)
Definition: osal_none.h:141
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
Definition: osal_none.h:70
static TU_ATTR_ALWAYS_INLINE osal_queue_t osal_queue_create(osal_queue_def_t *qdef)
Definition: osal_none.h:152
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
Definition: osal_none.h:104
static TU_ATTR_ALWAYS_INLINE bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr)
Definition: osal_none.h:172
osal_semaphore_t osal_mutex_t
Definition: osal_none.h:88
osal_queue_def_t * osal_queue_t
Definition: osal_none.h:130
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
Definition: osal_none.h:108
TU_ATTR_WEAK void osal_task_delay(uint32_t msec)
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_delete(osal_mutex_t mutex_hdl)
Definition: osal_none.h:99
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
Definition: osal_none.h:63
static TU_ATTR_ALWAYS_INLINE bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec)
Definition: osal_none.h:162
static TU_ATTR_ALWAYS_INLINE bool osal_queue_delete(osal_queue_t qhdl)
Definition: osal_none.h:157
static TU_ATTR_ALWAYS_INLINE void _osal_q_unlock(osal_queue_t qhdl)
Definition: osal_none.h:147
static TU_ATTR_ALWAYS_INLINE osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t *semdef)
Definition: osal_none.h:52
static TU_ATTR_ALWAYS_INLINE void osal_semaphore_reset(osal_semaphore_t sem_hdl)
Definition: osal_none.h:79
osal_semaphore_def_t * osal_semaphore_t
Definition: osal_none.h:50
static TU_ATTR_ALWAYS_INLINE osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef)
Definition: osal_none.h:94
tu_fifo_t ff
Definition: osal_none.h:127
volatile uint16_t count
Definition: osal_none.h:47
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