summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2025-01-26 00:24:25 +0700
committerGitHub <[email protected]>2025-01-26 00:24:25 +0700
commit0d4f945bdce5bbe47feab51b2a77a26f30aac0bd (patch)
treeae8b33a10bf1a13b70de459796da9f78494e6b19 /src
parent37e6f496197e0bc35ca0f71cfef80c19be273ca3 (diff)
parent882cb1406d19a3a16d530ec8bd5d8fb7571a4eef (diff)
Merge pull request #2962 from hathach/zephyr-support
Initial Zephyr support
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt71
-rw-r--r--src/osal/osal.h2
-rw-r--r--src/osal/osal_zephyr.h123
-rw-r--r--src/portable/nordic/nrf5x/dcd_nrf5x.c14
-rw-r--r--src/tusb_option.h1
5 files changed, 136 insertions, 75 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index cf6878389..7b3ab4d42 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,11 +1,10 @@
# TODO more docs and example on how to use this file
-# Usage: requires target tinyusb_config which expose tusb_config.h file
# TINYUSB_TARGET_PREFIX and TINYUSB_TARGET_SUFFIX can be used to change the name of the target
-cmake_minimum_required(VERSION 3.17)
+cmake_minimum_required(VERSION 3.20)
-# Add tinyusb to a target, if user don't want to compile tinyusb as a library
-function(add_tinyusb TARGET)
+# Add tinyusb to a existing target
+function(tinyusb_target_add TARGET)
target_sources(${TARGET} PRIVATE
# common
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/tusb.c
@@ -40,68 +39,4 @@ function(add_tinyusb TARGET)
# TODO for net driver, should be removed/changed
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../lib/networking
)
-
- if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
- target_compile_options(${TARGET} PRIVATE
- -Wall
- -Wextra
- -Werror
- -Wfatal-errors
- -Wdouble-promotion
- -Wstrict-prototypes
- -Wstrict-overflow
- -Werror-implicit-function-declaration
- -Wfloat-equal
- -Wundef
- -Wshadow
- -Wwrite-strings
- -Wsign-compare
- -Wmissing-format-attribute
- -Wunreachable-code
- -Wcast-align
- -Wcast-function-type
- -Wcast-qual
- -Wnull-dereference
- -Wuninitialized
- -Wunused
- -Wunused-function
- -Wreturn-type
- -Wredundant-decls
- -Wmissing-prototypes
- )
- elseif (CMAKE_C_COMPILER_ID STREQUAL "IAR")
-
- endif ()
endfunction()
-
-#------------------------------------
-# TinyUSB as library target
-#------------------------------------
-if (NOT DEFINED TINYUSB_TARGET)
- set(TINYUSB_TARGET "tinyusb")
-endif ()
-
-set(TINYUSB_CONFIG_TARGET "${TINYUSB_TARGET}_config")
-
-if (DEFINED TINYUSB_TARGET_PREFIX)
- set(TINYUSB_TARGET "${TINYUSB_TARGET_PREFIX}${TINYUSB_TARGET}")
- set(TINYUSB_CONFIG_TARGET "${TINYUSB_TARGET_PREFIX}${TINYUSB_CONFIG_TARGET}")
-endif ()
-
-if (DEFINED TINYUSB_TARGET_SUFFIX)
- set(TINYUSB_TARGET "${TINYUSB_TARGET}${TINYUSB_TARGET_SUFFIX}")
- set(TINYUSB_CONFIG_TARGET "${TINYUSB_CONFIG_TARGET}${TINYUSB_TARGET_SUFFIX}")
-endif ()
-
-add_library(${TINYUSB_TARGET} STATIC)
-add_tinyusb(${TINYUSB_TARGET})
-
-# Check if tinyusb_config target is defined
-if (NOT TARGET ${TINYUSB_CONFIG_TARGET})
- message(FATAL_ERROR "${TINYUSB_CONFIG_TARGET} target is not defined")
-endif()
-
-# Link with tinyusb_config target
-target_link_libraries(${TINYUSB_TARGET} PUBLIC
- ${TINYUSB_CONFIG_TARGET}
- )
diff --git a/src/osal/osal.h b/src/osal/osal.h
index 8f45ea5c1..38d45da44 100644
--- a/src/osal/osal.h
+++ b/src/osal/osal.h
@@ -63,6 +63,8 @@ typedef void (*osal_task_func_t)( void * );
#include "osal_rtthread.h"
#elif CFG_TUSB_OS == OPT_OS_RTX4
#include "osal_rtx4.h"
+#elif CFG_TUSB_OS == OPT_OS_ZEPHYR
+ #include "osal_zephyr.h"
#elif CFG_TUSB_OS == OPT_OS_CUSTOM
#include "tusb_os_custom.h" // implemented by application
#else
diff --git a/src/osal/osal_zephyr.h b/src/osal/osal_zephyr.h
new file mode 100644
index 000000000..8ecb13c6d
--- /dev/null
+++ b/src/osal/osal_zephyr.h
@@ -0,0 +1,123 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2025 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+#ifndef TUSB_OSAL_ZEPHYR_H
+#define TUSB_OSAL_ZEPHYR_H
+
+#include <zephyr/kernel.h>
+
+//--------------------------------------------------------------------+
+// TASK API
+//--------------------------------------------------------------------+
+TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
+ k_msleep(msec);
+}
+
+//--------------------------------------------------------------------+
+// Binary Semaphore API
+//--------------------------------------------------------------------+
+typedef struct k_sem osal_semaphore_def_t, * osal_semaphore_t;
+
+TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) {
+ k_sem_init(semdef, 0, 255);
+ return semdef;
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) {
+ (void) semd_hdl;
+ return true; // nothing to do
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
+ (void) in_isr;
+ k_sem_give(sem_hdl);
+ return true;
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
+ return 0 == k_sem_take(sem_hdl, K_MSEC(msec));
+}
+
+TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) {
+ k_sem_reset(sem_hdl);
+}
+
+//--------------------------------------------------------------------+
+// MUTEX API
+//--------------------------------------------------------------------+
+typedef struct k_mutex osal_mutex_def_t, *osal_mutex_t;
+
+TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) {
+ if ( 0 == k_mutex_init(mdef) ) {
+ return mdef;
+ } else {
+ return NULL;
+ }
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) {
+ (void) mutex_hdl;
+ return true; // nothing to do
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) {
+ return 0 == k_mutex_lock(mutex_hdl, K_MSEC(msec));
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
+ return 0 == k_mutex_unlock(mutex_hdl);
+}
+
+//--------------------------------------------------------------------+
+// QUEUE API
+//--------------------------------------------------------------------+
+typedef struct k_msgq osal_queue_def_t, * osal_queue_t;
+
+// role device/host is used by OS NONE for mutex (disable usb isr) only
+#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) K_MSGQ_DEFINE(_name, sizeof(_type), _depth, 4)
+
+TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) {
+ // K_MSGQ_DEFINE already initializes the queue
+ return (osal_queue_t) qdef;
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) {
+ (void) qhdl;
+ return true;
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
+ return 0 == k_msgq_get(qhdl, data, K_MSEC(msec));
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const* data, bool in_isr) {
+ return 0 == k_msgq_put(qhdl, data, in_isr ? K_NO_WAIT : K_FOREVER);
+}
+
+TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
+ return 0 == k_msgq_num_used_get(qhdl);
+}
+
+#endif
diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c
index 5bfedae17..9e5f5117f 100644
--- a/src/portable/nordic/nrf5x/dcd_nrf5x.c
+++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c
@@ -39,8 +39,8 @@
#endif
#include "nrf.h"
-#include "nrf_clock.h"
-#include "nrfx_usbd_errata.h"
+#include "nrfx_clock.h"
+#include "nrf_erratas.h"
#ifdef __GNUC__
#pragma GCC diagnostic pop
@@ -926,7 +926,7 @@ void tusb_hal_nrf_power_event(uint32_t event) {
#ifdef NRF52_SERIES // NRF53 does not need this errata
// ERRATA 171, 187, 166
- if (nrfx_usbd_errata_187()) {
+ if (nrf52_errata_187()) {
// CRITICAL_REGION_ENTER();
if (*((volatile uint32_t*) (0x4006EC00)) == 0x00000000) {
*((volatile uint32_t*) (0x4006EC00)) = 0x00009375;
@@ -938,7 +938,7 @@ void tusb_hal_nrf_power_event(uint32_t event) {
// CRITICAL_REGION_EXIT();
}
- if (nrfx_usbd_errata_171()) {
+ if (nrf52_errata_171()) {
// CRITICAL_REGION_ENTER();
if (*((volatile uint32_t*) (0x4006EC00)) == 0x00000000) {
*((volatile uint32_t*) (0x4006EC00)) = 0x00009375;
@@ -974,7 +974,7 @@ void tusb_hal_nrf_power_event(uint32_t event) {
__DSB(); // for sync
#ifdef NRF52_SERIES
- if (nrfx_usbd_errata_171()) {
+ if (nrf52_errata_171()) {
// CRITICAL_REGION_ENTER();
if (*((volatile uint32_t*) (0x4006EC00)) == 0x00000000) {
*((volatile uint32_t*) (0x4006EC00)) = 0x00009375;
@@ -987,7 +987,7 @@ void tusb_hal_nrf_power_event(uint32_t event) {
// CRITICAL_REGION_EXIT();
}
- if (nrfx_usbd_errata_187()) {
+ if (nrf52_errata_187()) {
// CRITICAL_REGION_ENTER();
if (*((volatile uint32_t*) (0x4006EC00)) == 0x00000000) {
*((volatile uint32_t*) (0x4006EC00)) = 0x00009375;
@@ -999,7 +999,7 @@ void tusb_hal_nrf_power_event(uint32_t event) {
// CRITICAL_REGION_EXIT();
}
- if (nrfx_usbd_errata_166()) {
+ if (nrf52_errata_166()) {
*((volatile uint32_t*) (NRF_USBD_BASE + 0x800)) = 0x7E3;
*((volatile uint32_t*) (NRF_USBD_BASE + 0x804)) = 0x40;
diff --git a/src/tusb_option.h b/src/tusb_option.h
index 86cb6d046..29fdcb0d6 100644
--- a/src/tusb_option.h
+++ b/src/tusb_option.h
@@ -215,6 +215,7 @@
#define OPT_OS_PICO 5 ///< Raspberry Pi Pico SDK
#define OPT_OS_RTTHREAD 6 ///< RT-Thread
#define OPT_OS_RTX4 7 ///< Keil RTX 4
+#define OPT_OS_ZEPHYR 8 ///< Zephyr
//--------------------------------------------------------------------+
// Mode and Speed