Open FFBoard
Open source force feedback firmware
osal_mynewt.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 OSAL_MYNEWT_H_
28#define OSAL_MYNEWT_H_
29
30#include "os/os.h"
31
32#ifdef __cplusplus
33 extern "C" {
34#endif
35
36//--------------------------------------------------------------------+
37// TASK API
38//--------------------------------------------------------------------+
39TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
40 os_time_delay( os_time_ms_to_ticks32(msec) );
41}
42
43//--------------------------------------------------------------------+
44// Semaphore API
45//--------------------------------------------------------------------+
46typedef struct os_sem osal_semaphore_def_t;
47typedef struct os_sem* osal_semaphore_t;
48
49TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) {
50 return (os_sem_init(semdef, 0) == OS_OK) ? (osal_semaphore_t) semdef : NULL;
51}
52
53TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) {
54 (void) semd_hdl;
55 return true; // nothing to do
56}
57
58TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
59 (void) in_isr;
60 return os_sem_release(sem_hdl) == OS_OK;
61}
62
63TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
64 uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec);
65 return os_sem_pend(sem_hdl, ticks) == OS_OK;
66}
67
68static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) {
69 // TODO implement later
70}
71
72//--------------------------------------------------------------------+
73// MUTEX API (priority inheritance)
74//--------------------------------------------------------------------+
75typedef struct os_mutex osal_mutex_def_t;
76typedef struct os_mutex* osal_mutex_t;
77
78TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) {
79 return (os_mutex_init(mdef) == OS_OK) ? (osal_mutex_t) mdef : NULL;
80}
81
82TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) {
83 (void) mutex_hdl;
84 return true; // nothing to do
85}
86
87TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) {
88 uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec);
89 return os_mutex_pend(mutex_hdl, ticks) == OS_OK;
90}
91
92TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
93 return os_mutex_release(mutex_hdl) == OS_OK;
94}
95
96//--------------------------------------------------------------------+
97// QUEUE API
98//--------------------------------------------------------------------+
99
100// role device/host is used by OS NONE for mutex (disable usb isr) only
101#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
102 static _type _name##_##buf[_depth];\
103 static struct os_event _name##_##evbuf[_depth];\
104 osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf, .evbuf = _name##_##evbuf};\
105
106typedef struct {
107 uint16_t depth;
108 uint16_t item_sz;
109 void* buf;
110 void* evbuf;
111
112 struct os_mempool mpool;
113 struct os_mempool epool;
114
115 struct os_eventq evq;
117
119
120TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) {
121 if ( OS_OK != os_mempool_init(&qdef->mpool, qdef->depth, qdef->item_sz, qdef->buf, "usb queue") ) return NULL;
122 if ( OS_OK != os_mempool_init(&qdef->epool, qdef->depth, sizeof(struct os_event), qdef->evbuf, "usb evqueue") ) return NULL;
123
124 os_eventq_init(&qdef->evq);
125 return (osal_queue_t) qdef;
126}
127
128TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) {
129 (void) qhdl;
130 return true; // nothing to do
131}
132
133TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
134 (void) msec; // os_eventq_get() does not take timeout, always behave as msec = WAIT_FOREVER
135
136 struct os_event* ev;
137 ev = os_eventq_get(&qhdl->evq);
138
139 memcpy(data, ev->ev_arg, qhdl->item_sz); // copy message
140 os_memblock_put(&qhdl->mpool, ev->ev_arg); // put back mem block
141 os_memblock_put(&qhdl->epool, ev); // put back ev block
142
143 return true;
144}
145
146static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) {
147 (void) in_isr;
148
149 // get a block from mem pool for data
150 void* ptr = os_memblock_get(&qhdl->mpool);
151 if (!ptr) return false;
152 memcpy(ptr, data, qhdl->item_sz);
153
154 // get a block from event pool to put into queue
155 struct os_event* ev = (struct os_event*) os_memblock_get(&qhdl->epool);
156 if (!ev) {
157 os_memblock_put(&qhdl->mpool, ptr);
158 return false;
159 }
160 tu_memclr(ev, sizeof(struct os_event));
161 ev->ev_arg = ptr;
162
163 os_eventq_put(&qhdl->evq, ev);
164
165 return true;
166}
167
168TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
169 return STAILQ_EMPTY(&qhdl->evq.evq_list);
170}
171
172
173#ifdef __cplusplus
174 }
175#endif
176
177#endif /* OSAL_MYNEWT_H_ */
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
static TU_ATTR_ALWAYS_INLINE bool osal_queue_empty(osal_queue_t qhdl)
Definition: osal_mynewt.h:168
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_delete(osal_semaphore_t semd_hdl)
Definition: osal_mynewt.h:53
static void osal_semaphore_reset(osal_semaphore_t sem_hdl)
Definition: osal_mynewt.h:68
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
Definition: osal_mynewt.h:63
static TU_ATTR_ALWAYS_INLINE osal_queue_t osal_queue_create(osal_queue_def_t *qdef)
Definition: osal_mynewt.h:120
struct os_sem * osal_semaphore_t
Definition: osal_mynewt.h:47
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
Definition: osal_mynewt.h:87
osal_queue_def_t * osal_queue_t
Definition: osal_mynewt.h:118
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
Definition: osal_mynewt.h:92
static TU_ATTR_ALWAYS_INLINE bool osal_mutex_delete(osal_mutex_t mutex_hdl)
Definition: osal_mynewt.h:82
static TU_ATTR_ALWAYS_INLINE bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
Definition: osal_mynewt.h:58
static TU_ATTR_ALWAYS_INLINE bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec)
Definition: osal_mynewt.h:133
static TU_ATTR_ALWAYS_INLINE bool osal_queue_delete(osal_queue_t qhdl)
Definition: osal_mynewt.h:128
static TU_ATTR_ALWAYS_INLINE osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t *semdef)
Definition: osal_mynewt.h:49
struct os_mutex * osal_mutex_t
Definition: osal_mynewt.h:76
static bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr)
Definition: osal_mynewt.h:146
static TU_ATTR_ALWAYS_INLINE osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef)
Definition: osal_mynewt.h:78
TU_ATTR_WEAK void osal_task_delay(uint32_t msec)
static void * memcpy(void *dst, const void *src, size_t n)
Definition: ringbuffer.c:8
struct os_mempool mpool
Definition: osal_mynewt.h:112
struct os_eventq evq
Definition: osal_mynewt.h:115
struct os_mempool epool
Definition: osal_mynewt.h:113