|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2025 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 | +#ifndef TUSB_OSAL_ZEPHYR_H |
| 27 | +#define TUSB_OSAL_ZEPHYR_H |
| 28 | + |
| 29 | +#include <zephyr/kernel.h> |
| 30 | + |
| 31 | +//--------------------------------------------------------------------+ |
| 32 | +// TASK API |
| 33 | +//--------------------------------------------------------------------+ |
| 34 | +TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { |
| 35 | + k_msleep(msec); |
| 36 | +} |
| 37 | + |
| 38 | +//--------------------------------------------------------------------+ |
| 39 | +// Binary Semaphore API |
| 40 | +//--------------------------------------------------------------------+ |
| 41 | +typedef struct k_sem osal_semaphore_def_t, * osal_semaphore_t; |
| 42 | + |
| 43 | +TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) { |
| 44 | + k_sem_init(semdef, 0, 255); |
| 45 | + return semdef; |
| 46 | +} |
| 47 | + |
| 48 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) { |
| 49 | + (void) semd_hdl; |
| 50 | + return true; // nothing to do |
| 51 | +} |
| 52 | + |
| 53 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { |
| 54 | + (void) in_isr; |
| 55 | + k_sem_give(sem_hdl); |
| 56 | + return true; |
| 57 | +} |
| 58 | + |
| 59 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { |
| 60 | + return 0 == k_sem_take(sem_hdl, K_MSEC(msec)); |
| 61 | +} |
| 62 | + |
| 63 | +TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) { |
| 64 | + k_sem_reset(sem_hdl); |
| 65 | +} |
| 66 | + |
| 67 | +//--------------------------------------------------------------------+ |
| 68 | +// MUTEX API |
| 69 | +//--------------------------------------------------------------------+ |
| 70 | +typedef struct k_mutex osal_mutex_def_t, *osal_mutex_t; |
| 71 | + |
| 72 | +TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) { |
| 73 | + if ( 0 == k_mutex_init(mdef) ) { |
| 74 | + return mdef; |
| 75 | + } else { |
| 76 | + return NULL; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) { |
| 81 | + (void) mutex_hdl; |
| 82 | + return true; // nothing to do |
| 83 | +} |
| 84 | + |
| 85 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { |
| 86 | + return 0 == k_mutex_lock(mutex_hdl, K_MSEC(msec)); |
| 87 | +} |
| 88 | + |
| 89 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { |
| 90 | + return 0 == k_mutex_unlock(mutex_hdl); |
| 91 | +} |
| 92 | + |
| 93 | +//--------------------------------------------------------------------+ |
| 94 | +// QUEUE API |
| 95 | +//--------------------------------------------------------------------+ |
| 96 | +typedef struct k_msgq osal_queue_def_t, * osal_queue_t; |
| 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) K_MSGQ_DEFINE(_name, sizeof(_type), _depth, 4) |
| 100 | + |
| 101 | +TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { |
| 102 | + // K_MSGQ_DEFINE already initializes the queue |
| 103 | + return (osal_queue_t) qdef; |
| 104 | +} |
| 105 | + |
| 106 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) { |
| 107 | + (void) qhdl; |
| 108 | + return true; |
| 109 | +} |
| 110 | + |
| 111 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { |
| 112 | + return 0 == k_msgq_get(qhdl, data, K_MSEC(msec)); |
| 113 | +} |
| 114 | + |
| 115 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const* data, bool in_isr) { |
| 116 | + return 0 == k_msgq_put(qhdl, data, in_isr ? K_NO_WAIT : K_FOREVER); |
| 117 | +} |
| 118 | + |
| 119 | +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { |
| 120 | + return 0 == k_msgq_num_used_get(qhdl); |
| 121 | +} |
| 122 | + |
| 123 | +#endif |
0 commit comments