Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 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 |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | //--------------------------------------------------------------------+ |
| 35 | // TASK API |
| 36 | //--------------------------------------------------------------------+ |
| 37 | |
| 38 | |
| 39 | //--------------------------------------------------------------------+ |
| 40 | // Binary Semaphore API |
| 41 | //--------------------------------------------------------------------+ |
| 42 | typedef struct |
| 43 | { |
| 44 | volatile uint16_t count; |
| 45 | }osal_semaphore_def_t; |
| 46 | |
| 47 | typedef osal_semaphore_def_t* osal_semaphore_t; |
| 48 | |
| 49 | static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) |
| 50 | { |
| 51 | semdef->count = 0; |
| 52 | return semdef; |
| 53 | } |
| 54 | |
| 55 | static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) |
| 56 | { |
| 57 | (void) in_isr; |
| 58 | sem_hdl->count++; |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | // TODO blocking for now |
| 63 | static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) |
| 64 | { |
| 65 | (void) msec; |
| 66 | |
| 67 | while (sem_hdl->count == 0) { } |
| 68 | sem_hdl->count--; |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) |
| 74 | { |
| 75 | sem_hdl->count = 0; |
| 76 | } |
| 77 | |
| 78 | //--------------------------------------------------------------------+ |
| 79 | // MUTEX API |
| 80 | // Within tinyusb, mutex is never used in ISR context |
| 81 | //--------------------------------------------------------------------+ |
| 82 | typedef osal_semaphore_def_t osal_mutex_def_t; |
| 83 | typedef osal_semaphore_t osal_mutex_t; |
| 84 | |
| 85 | static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) |
| 86 | { |
| 87 | mdef->count = 1; |
| 88 | return mdef; |
| 89 | } |
| 90 | |
| 91 | static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) |
| 92 | { |
| 93 | return osal_semaphore_wait(mutex_hdl, msec); |
| 94 | } |
| 95 | |
| 96 | static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) |
| 97 | { |
| 98 | return osal_semaphore_post(mutex_hdl, false); |
| 99 | } |
| 100 | |
| 101 | //--------------------------------------------------------------------+ |
| 102 | // QUEUE API |
| 103 | //--------------------------------------------------------------------+ |
| 104 | #include "common/tusb_fifo.h" |
| 105 | |
| 106 | // extern to avoid including dcd.h and hcd.h |
| 107 | #if TUSB_OPT_DEVICE_ENABLED |
| 108 | extern void dcd_int_disable(uint8_t rhport); |
| 109 | extern void dcd_int_enable(uint8_t rhport); |
| 110 | #endif |
| 111 | |
| 112 | #if TUSB_OPT_HOST_ENABLED |
| 113 | extern void hcd_int_disable(uint8_t rhport); |
| 114 | extern void hcd_int_enable(uint8_t rhport); |
| 115 | #endif |
| 116 | |
| 117 | typedef struct |
| 118 | { |
| 119 | uint8_t role; // device or host |
| 120 | tu_fifo_t ff; |
| 121 | }osal_queue_def_t; |
| 122 | |
| 123 | typedef osal_queue_def_t* osal_queue_t; |
| 124 | |
| 125 | // role device/host is used by OS NONE for mutex (disable usb isr) only |
| 126 | #define OSAL_QUEUE_DEF(_role, _name, _depth, _type) \ |
| 127 | uint8_t _name##_buf[_depth*sizeof(_type)]; \ |
| 128 | osal_queue_def_t _name = { \ |
| 129 | .role = _role, \ |
| 130 | .ff = TU_FIFO_INIT(_name##_buf, _depth, _type, false) \ |
| 131 | } |
| 132 | |
| 133 | // lock queue by disable USB interrupt |
| 134 | static inline void _osal_q_lock(osal_queue_t qhdl) |
| 135 | { |
| 136 | (void) qhdl; |
| 137 | |
| 138 | #if TUSB_OPT_DEVICE_ENABLED |
| 139 | if (qhdl->role == OPT_MODE_DEVICE) dcd_int_disable(TUD_OPT_RHPORT); |
| 140 | #endif |
| 141 | |
| 142 | #if TUSB_OPT_HOST_ENABLED |
| 143 | if (qhdl->role == OPT_MODE_HOST) hcd_int_disable(TUH_OPT_RHPORT); |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | // unlock queue |
| 148 | static inline void _osal_q_unlock(osal_queue_t qhdl) |
| 149 | { |
| 150 | (void) qhdl; |
| 151 | |
| 152 | #if TUSB_OPT_DEVICE_ENABLED |
| 153 | if (qhdl->role == OPT_MODE_DEVICE) dcd_int_enable(TUD_OPT_RHPORT); |
| 154 | #endif |
| 155 | |
| 156 | #if TUSB_OPT_HOST_ENABLED |
| 157 | if (qhdl->role == OPT_MODE_HOST) hcd_int_enable(TUH_OPT_RHPORT); |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) |
| 162 | { |
| 163 | tu_fifo_clear(&qdef->ff); |
| 164 | return (osal_queue_t) qdef; |
| 165 | } |
| 166 | |
| 167 | static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) |
| 168 | { |
| 169 | _osal_q_lock(qhdl); |
| 170 | bool success = tu_fifo_read(&qhdl->ff, data); |
| 171 | _osal_q_unlock(qhdl); |
| 172 | |
| 173 | return success; |
| 174 | } |
| 175 | |
| 176 | static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) |
| 177 | { |
| 178 | if (!in_isr) { |
| 179 | _osal_q_lock(qhdl); |
| 180 | } |
| 181 | |
| 182 | bool success = tu_fifo_write(&qhdl->ff, data); |
| 183 | |
| 184 | if (!in_isr) { |
| 185 | _osal_q_unlock(qhdl); |
| 186 | } |
| 187 | |
| 188 | TU_ASSERT(success); |
| 189 | |
| 190 | return success; |
| 191 | } |
| 192 | |
| 193 | static inline bool osal_queue_empty(osal_queue_t qhdl) |
| 194 | { |
| 195 | // Skip queue lock/unlock since this function is primarily called |
| 196 | // with interrupt disabled before going into low power mode |
| 197 | return tu_fifo_empty(&qhdl->ff); |
| 198 | } |
| 199 | |
| 200 | #ifdef __cplusplus |
| 201 | } |
| 202 | #endif |
| 203 | |
| 204 | #endif /* _TUSB_OSAL_NONE_H_ */ |