mikroSDK Reference Manual
osal_mynewt.h
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{
41 os_time_delay( os_time_ms_to_ticks32(msec) );
42}
43
44//--------------------------------------------------------------------+
45// Semaphore API
46//--------------------------------------------------------------------+
47typedef struct os_sem osal_semaphore_def_t;
48typedef struct os_sem* osal_semaphore_t;
49
50TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
51{
52 return (os_sem_init(semdef, 0) == OS_OK) ? (osal_semaphore_t) semdef : NULL;
53}
54
55TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
56{
57 (void) in_isr;
58 return os_sem_release(sem_hdl) == OS_OK;
59}
60
61TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
62{
63 uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec);
64 return os_sem_pend(sem_hdl, ticks) == OS_OK;
65}
66
67static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
68{
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{
80 return (os_mutex_init(mdef) == OS_OK) ? (osal_mutex_t) mdef : NULL;
81}
82
83TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
84{
85 uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec);
86 return os_mutex_pend(mutex_hdl, ticks) == OS_OK;
87}
88
89TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
90{
91 return os_mutex_release(mutex_hdl) == OS_OK;
92}
93
94//--------------------------------------------------------------------+
95// QUEUE API
96//--------------------------------------------------------------------+
97
98// role device/host is used by OS NONE for mutex (disable usb isr) only
99#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
100 static _type _name##_##buf[_depth];\
101 static struct os_event _name##_##evbuf[_depth];\
102 osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf, .evbuf = _name##_##evbuf};\
103
104typedef struct
105{
106 uint16_t depth;
107 uint16_t item_sz;
108 void* buf;
109 void* evbuf;
110
111 struct os_mempool mpool;
112 struct os_mempool epool;
113
114 struct os_eventq evq;
116
117typedef osal_queue_def_t* osal_queue_t;
118
119TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
120{
121 if ( OS_OK != os_mempool_init(&qdef->mpool, qdef->depth, qdef->item_sz, qdef->buf, "usbd queue") ) return NULL;
122 if ( OS_OK != os_mempool_init(&qdef->epool, qdef->depth, sizeof(struct os_event), qdef->evbuf, "usbd 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_receive(osal_queue_t qhdl, void* data, uint32_t msec)
129{
130 (void) msec; // os_eventq_get() does not take timeout, always behave as msec = WAIT_FOREVER
131
132 struct os_event* ev;
133 ev = os_eventq_get(&qhdl->evq);
134
135 memcpy(data, ev->ev_arg, qhdl->item_sz); // copy message
136 os_memblock_put(&qhdl->mpool, ev->ev_arg); // put back mem block
137 os_memblock_put(&qhdl->epool, ev); // put back ev block
138
139 return true;
140}
141
142static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
143{
144 (void) in_isr;
145
146 // get a block from mem pool for data
147 void* ptr = os_memblock_get(&qhdl->mpool);
148 if (!ptr) return false;
149 memcpy(ptr, data, qhdl->item_sz);
150
151 // get a block from event pool to put into queue
152 struct os_event* ev = (struct os_event*) os_memblock_get(&qhdl->epool);
153 if (!ev)
154 {
155 os_memblock_put(&qhdl->mpool, ptr);
156 return false;
157 }
158 tu_memclr(ev, sizeof(struct os_event));
159 ev->ev_arg = ptr;
160
161 os_eventq_put(&qhdl->evq, ev);
162
163 return true;
164}
165
166TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
167{
168 return STAILQ_EMPTY(&qhdl->evq.evq_list);
169}
170
171
172#ifdef __cplusplus
173 }
174#endif
175
176#endif /* OSAL_MYNEWT_H_ */
Definition osal_freertos.h:58
Definition osal_none.h:47