aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/os/various/tinyusb_bindings
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/os/various/tinyusb_bindings')
-rw-r--r--lib/chibios-contrib/os/various/tinyusb_bindings/tinyusb.mk9
-rw-r--r--lib/chibios-contrib/os/various/tinyusb_bindings/tusb_os_custom.h195
2 files changed, 204 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/various/tinyusb_bindings/tinyusb.mk b/lib/chibios-contrib/os/various/tinyusb_bindings/tinyusb.mk
new file mode 100644
index 000000000..b631eb3d8
--- /dev/null
+++ b/lib/chibios-contrib/os/various/tinyusb_bindings/tinyusb.mk
@@ -0,0 +1,9 @@
1
2TINYUSBSRC = $(shell find $(TINYUSB)/src/ -type f -name "*.c")
3
4TINYUSBINC = \
5 $(CHIBIOS_CONTRIB)/os/various/tinyusb_bindings \
6 $(shell find $(TINYUSB)/src/ -type d)
7
8ALLCSRC += $(TINYUSBSRC)
9ALLINC += $(TINYUSBINC)
diff --git a/lib/chibios-contrib/os/various/tinyusb_bindings/tusb_os_custom.h b/lib/chibios-contrib/os/various/tinyusb_bindings/tusb_os_custom.h
new file mode 100644
index 000000000..0da286e58
--- /dev/null
+++ b/lib/chibios-contrib/os/various/tinyusb_bindings/tusb_os_custom.h
@@ -0,0 +1,195 @@
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_CHIBIOS_H_
28#define _TUSB_OSAL_CHIBIOS_H_
29
30// ChibiOS Headers
31#include "ch.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37//--------------------------------------------------------------------+
38// TASK API
39//--------------------------------------------------------------------+
40static inline void osal_task_delay(uint32_t msec)
41{
42 chThdSleepMilliseconds(msec);
43}
44
45//--------------------------------------------------------------------+
46// Semaphore API
47//--------------------------------------------------------------------+
48typedef struct {
49 uint16_t size;
50 semaphore_t sem;
51} osal_semaphore_def_t;
52typedef osal_semaphore_def_t * osal_semaphore_t;
53
54static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
55{
56 chSemObjectInit(&semdef->sem, semdef->size);
57 return semdef;
58}
59
60static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
61{
62 if ( !in_isr )
63 {
64 chSemSignal(&sem_hdl->sem);
65 return true;
66 }
67 else
68 {
69 chSysLockFromISR();
70 chSemSignalI(&sem_hdl->sem);
71 chSysUnlockFromISR();
72 return true;
73 }
74}
75
76static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
77{
78 const sysinterval_t ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? TIME_INFINITE : TIME_MS2I(msec);
79 return chSemWaitTimeout(&sem_hdl->sem, ticks) == MSG_OK;
80}
81
82static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
83{
84 chSemReset(&sem_hdl->sem, sem_hdl->size);
85}
86
87//--------------------------------------------------------------------+
88// MUTEX API (priority inheritance)
89//--------------------------------------------------------------------+
90typedef osal_semaphore_def_t osal_mutex_def_t;
91typedef osal_semaphore_t osal_mutex_t;
92
93static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
94{
95 chSemObjectInit(&mdef->sem, mdef->size);
96 return mdef;
97}
98
99static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
100{
101 uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? TIME_INFINITE : TIME_MS2I(msec);
102 return chSemWaitTimeout(&mutex_hdl->sem, ticks) == MSG_OK;
103}
104
105static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
106{
107 chSemSignal(&mutex_hdl->sem);
108 return true;
109}
110
111//--------------------------------------------------------------------+
112// QUEUE API
113//--------------------------------------------------------------------+
114
115// role device/host is used by OS NONE for mutex (disable usb isr) only
116#define OSAL_QUEUE_DEF(_role, _name, _depth, _type) \
117 static _type _name##_##objbuf[_depth];\
118 static msg_t _name##_##msgbuf[_depth];\
119 osal_queue_def_t _name = { .depth = _depth, .obj_sz = sizeof(_type), .objbuf = _name##_##objbuf, .msgbuf = _name##_##msgbuf };
120
121// Use FIFO as queue (mailbox + memory pool)
122typedef struct
123{
124 uint16_t depth;
125 uint16_t obj_sz;
126 void* objbuf;
127 msg_t* msgbuf;
128
129 objects_fifo_t fifo;
130} osal_queue_def_t;
131typedef osal_queue_def_t * osal_queue_t;
132
133static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
134{
135 chFifoObjectInit(&qdef->fifo, qdef->obj_sz, qdef->depth, qdef->objbuf, qdef->msgbuf);
136 return qdef;
137}
138
139static inline bool osal_queue_receive(osal_queue_t qhdl, void* data)
140{
141 void* objpp; // pointer to the object
142
143 bool r = chFifoReceiveObjectTimeout(&qhdl->fifo, &objpp, TIME_MS2I(10000)) != MSG_OK;
144 if (r)
145 return false;
146
147 memcpy(data, objpp, qhdl->obj_sz);
148 chFifoReturnObject(&qhdl->fifo, objpp);
149 return true;
150}
151
152static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
153{
154 void* obj;
155
156 if ( !in_isr )
157 {
158 obj = chFifoTakeObjectTimeout(&qhdl->fifo, TIME_MS2I(10000));
159 if (obj == NULL) // Allocation failed
160 return false;
161 memcpy(obj, data, qhdl->obj_sz);
162 chFifoSendObject(&qhdl->fifo, obj);
163 return true;
164 }
165 else
166 {
167 chSysLockFromISR();
168 obj = chFifoTakeObjectI(&qhdl->fifo);
169 chSysUnlockFromISR();
170
171 if (obj == NULL) // Allocation failed
172 return false;
173 memcpy(obj, data, qhdl->obj_sz);
174
175 chSysLockFromISR();
176 chFifoSendObjectI(&qhdl->fifo, obj);
177 chSysUnlockFromISR();
178 return true;
179 }
180}
181
182static inline bool osal_queue_empty(osal_queue_t qhdl)
183{
184 uint16_t cnt;
185 chSysLock();
186 cnt = chMBGetFreeCountI(&qhdl->fifo.mbx);
187 chSysUnlock();
188 return cnt == 0;
189}
190
191#ifdef __cplusplus
192 }
193#endif
194
195#endif /* _TUSB_OSAL_FREERTOS_H_ */