Update build system.

This commit is contained in:
HiFiPhile
2024-04-27 21:44:25 +02:00
parent c917d47e71
commit 4f41e105b3
19 changed files with 481 additions and 210 deletions

View File

@@ -1,22 +1,34 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.17)
# TOP is absolute path to root directory of TinyUSB git repo
# needed for esp32sx build. TOOD could be removed later on
set(TOP "../../..")
get_filename_component(TOP "${TOP}" REALPATH)
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>) # gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR}) family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
project(${PROJECT}) project(${PROJECT} C CXX ASM)
# Checks this example is valid for the family and initializes the project # Checks this example is valid for the family and initializes the project
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR}) family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
# Check for -DFAMILY= # Espressif has its own cmake build system
if(FAMILY MATCHES "^esp32s[2-3]") if(FAMILY STREQUAL "espressif")
else() return()
message(FATAL_ERROR "Invalid FAMILY specified: ${FAMILY}")
endif() endif()
add_executable(${PROJECT})
# Example source
target_sources(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src/freertos_hook.c
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
)
# Example include
target_include_directories(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
)
# Configure compilation flags and libraries for the example with FreeRTOS.
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
family_configure_device_example(${PROJECT} freertos)

View File

@@ -1,16 +1,14 @@
DEPS_SUBMODULES += lib/FreeRTOS-Kernel include ../../build_system/make/make.mk
include ../../../tools/top.mk
include ../../make.mk
FREERTOS_SRC = lib/FreeRTOS-Kernel FREERTOS_SRC = lib/FreeRTOS-Kernel
FREERTOS_PORTABLE_PATH = $(FREERTOS_SRC)/portable/$(if $(findstring iar,$(TOOLCHAIN)),IAR,GCC)
INC += \ INC += \
src \ src \
src/FreeRTOSConfig \ src/FreeRTOSConfig \
$(TOP)/hw \ $(TOP)/hw \
$(TOP)/$(FREERTOS_SRC)/include \ $(TOP)/$(FREERTOS_SRC)/include \
$(TOP)/$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT) $(TOP)/$(FREERTOS_PORTABLE_SRC) \
# Example source # Example source
EXAMPLE_SOURCE = \ EXAMPLE_SOURCE = \
@@ -26,7 +24,10 @@ SRC_C += \
$(FREERTOS_SRC)/queue.c \ $(FREERTOS_SRC)/queue.c \
$(FREERTOS_SRC)/tasks.c \ $(FREERTOS_SRC)/tasks.c \
$(FREERTOS_SRC)/timers.c \ $(FREERTOS_SRC)/timers.c \
$(subst ../../../,,$(wildcard ../../../$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT)/*.c)) $(subst $(TOP)/,,$(wildcard $(TOP)/$(FREERTOS_PORTABLE_SRC)/*.c))
SRC_S += \
$(subst $(TOP)/,,$(wildcard $(TOP)/$(FREERTOS_PORTABLE_SRC)/*.s))
# Suppress FreeRTOS warnings # Suppress FreeRTOS warnings
CFLAGS += -Wno-error=cast-qual -Wno-error=redundant-decls CFLAGS += -Wno-error=cast-qual -Wno-error=redundant-decls
@@ -34,4 +35,4 @@ CFLAGS += -Wno-error=cast-qual -Wno-error=redundant-decls
# FreeRTOS (lto + Os) linker issue # FreeRTOS (lto + Os) linker issue
LDFLAGS += -Wl,--undefined=vTaskSwitchContext LDFLAGS += -Wl,--undefined=vTaskSwitchContext
include ../../rules.mk include ../../build_system/make/rules.mk

View File

@@ -6,9 +6,10 @@
$ cd /tinyusb/examples/device/audio_4_channel_mic_freertos $ cd /tinyusb/examples/device/audio_4_channel_mic_freertos
``` ```
3. Run make in project directory specifying the board 3. Run cmake in project directory specifying the board
``` ```
$ make BOARD=espressif_s3_devkitc all $ cmake -DBOARD=espressif_s3_devkitc -B build -G Ninja .
$ ninja.exe -C build
``` ```
4. Flash the binary onto the esp32-s3 by copy-paste of the full command output by the esp-idf build system replacing **(PORT)** with eg. /dev/ttyUSB0 4. Flash the binary onto the esp32-s3 by copy-paste of the full command output by the esp-idf build system replacing **(PORT)** with eg. /dev/ttyUSB0

View File

@@ -0,0 +1,3 @@
CONFIG_IDF_CMAKE=y
CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y
CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION=y

View File

@@ -1,3 +1,4 @@
mcu:SAMD11 mcu:SAMD11
mcu:SAME5X mcu:SAME5X
mcu:SAMG mcu:SAMG
family:broadcom_64bit

View File

@@ -1,27 +1,4 @@
# This file is for ESP-IDF only
idf_component_register(SRCS "main.c" "usb_descriptors.c" idf_component_register(SRCS "main.c" "usb_descriptors.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES freertos soc) REQUIRES boards tinyusb_src)
file(TO_NATIVE_PATH "${TOP}/hw/bsp/${FAMILY}/boards/${BOARD}/board.cmake" board_cmake)
if(EXISTS ${board_cmake})
include(${board_cmake})
endif()
target_include_directories(${COMPONENT_TARGET} PUBLIC
"${TOP}/hw"
"${TOP}/src"
)
target_compile_definitions(${COMPONENT_TARGET} PUBLIC
ESP_PLATFORM
)
target_sources(${COMPONENT_TARGET} PUBLIC
"${TOP}/src/tusb.c"
"${TOP}/src/common/tusb_fifo.c"
"${TOP}/src/device/usbd.c"
"${TOP}/src/device/usbd_control.c"
"${TOP}/src/class/audio/audio_device.c"
"${TOP}/src/portable/espressif/esp32sx/dcd_esp32sx.c"
)

View File

@@ -42,6 +42,9 @@
* See http://www.freertos.org/a00110.html. * See http://www.freertos.org/a00110.html.
*----------------------------------------------------------*/ *----------------------------------------------------------*/
// skip if included from IAR assembler
#ifndef __IASMARM__
// Include MCU header // Include MCU header
#include "bsp/board_mcu.h" #include "bsp/board_mcu.h"
@@ -53,9 +56,12 @@
#if CFG_TUSB_MCU == OPT_MCU_MM32F327X #if CFG_TUSB_MCU == OPT_MCU_MM32F327X
extern u32 SystemCoreClock; extern u32 SystemCoreClock;
#else #else
// FIXME cause redundant-decls warnings
extern uint32_t SystemCoreClock; extern uint32_t SystemCoreClock;
#endif #endif
#endif
/* Cortex M23/M33 port configuration. */ /* Cortex M23/M33 port configuration. */
#define configENABLE_MPU 0 #define configENABLE_MPU 0
#define configENABLE_FPU 1 #define configENABLE_FPU 1
@@ -68,14 +74,14 @@
#define configTICK_RATE_HZ ( 1000 ) #define configTICK_RATE_HZ ( 1000 )
#define configMAX_PRIORITIES ( 5 ) #define configMAX_PRIORITIES ( 5 )
#define configMINIMAL_STACK_SIZE ( 128 ) #define configMINIMAL_STACK_SIZE ( 128 )
#define configTOTAL_HEAP_SIZE ( 0*1024 ) // dynamic is not used #define configTOTAL_HEAP_SIZE ( configSUPPORT_DYNAMIC_ALLOCATION*4*1024 )
#define configMAX_TASK_NAME_LEN 16 #define configMAX_TASK_NAME_LEN 16
#define configUSE_16_BIT_TICKS 0 #define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1 #define configIDLE_SHOULD_YIELD 1
#define configUSE_MUTEXES 1 #define configUSE_MUTEXES 1
#define configUSE_RECURSIVE_MUTEXES 1 #define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 1 #define configUSE_COUNTING_SEMAPHORES 1
#define configQUEUE_REGISTRY_SIZE 2 #define configQUEUE_REGISTRY_SIZE 4
#define configUSE_QUEUE_SETS 0 #define configUSE_QUEUE_SETS 0
#define configUSE_TIME_SLICING 0 #define configUSE_TIME_SLICING 0
#define configUSE_NEWLIB_REENTRANT 0 #define configUSE_NEWLIB_REENTRANT 0
@@ -90,6 +96,7 @@
#define configUSE_TICK_HOOK 0 #define configUSE_TICK_HOOK 0
#define configUSE_MALLOC_FAILED_HOOK 0 // cause nested extern warning #define configUSE_MALLOC_FAILED_HOOK 0 // cause nested extern warning
#define configCHECK_FOR_STACK_OVERFLOW 2 #define configCHECK_FOR_STACK_OVERFLOW 2
#define configCHECK_HANDLER_INSTALLATION 0
/* Run time and task stats gathering related definitions. */ /* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS 0 #define configGENERATE_RUN_TIME_STATS 0
@@ -124,29 +131,12 @@
#define INCLUDE_xEventGroupSetBitFromISR 0 #define INCLUDE_xEventGroupSetBitFromISR 0
#define INCLUDE_xTimerPendFunctionCall 0 #define INCLUDE_xTimerPendFunctionCall 0
/* Define to trap errors during development. */
// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7
#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__)
#define configASSERT(_exp) \
do {\
if ( !(_exp) ) { \
volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
if ( (*ARM_CM_DHCSR) & 1UL ) { /* Only halt mcu if debugger is attached */ \
taskDISABLE_INTERRUPTS(); \
__asm("BKPT #0\n"); \
}\
}\
} while(0)
#else
#define configASSERT( x )
#endif
#ifdef __RX__ #ifdef __RX__
/* Renesas RX series */ /* Renesas RX series */
#define vSoftwareInterruptISR INT_Excep_ICU_SWINT #define vSoftwareInterruptISR INT_Excep_ICU_SWINT
#define vTickISR INT_Excep_CMT0_CMI0 #define vTickISR INT_Excep_CMT0_CMI0
#define configPERIPHERAL_CLOCK_HZ (configCPU_CLOCK_HZ/2) #define configPERIPHERAL_CLOCK_HZ (configCPU_CLOCK_HZ/2)
#define configKERNEL_INTERRUPT_PRIORITY 1 #define configKERNEL_INTERRUPT_PRIORITY 1
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 4 #define configMAX_SYSCALL_INTERRUPT_PRIORITY 4
#else #else
@@ -162,9 +152,18 @@
#if defined(__NVIC_PRIO_BITS) #if defined(__NVIC_PRIO_BITS)
// For Cortex-M specific: __NVIC_PRIO_BITS is defined in core_cmx.h // For Cortex-M specific: __NVIC_PRIO_BITS is defined in core_cmx.h
#define configPRIO_BITS __NVIC_PRIO_BITS #define configPRIO_BITS __NVIC_PRIO_BITS
#elif defined(__ECLIC_INTCTLBITS) #elif defined(__ECLIC_INTCTLBITS)
// RISC-V Bumblebee core from nuclei // RISC-V Bumblebee core from nuclei
#define configPRIO_BITS __ECLIC_INTCTLBITS #define configPRIO_BITS __ECLIC_INTCTLBITS
#elif defined(__IASMARM__)
// FIXME: IAR Assembler cannot include mcu header directly to get __NVIC_PRIO_BITS.
// Therefore we will hard coded it to minimum value of 2 to get pass ci build.
// IAR user must update this to correct value of the target MCU
#message "configPRIO_BITS is hard coded to 2 to pass IAR build only. User should update it per MCU"
#define configPRIO_BITS 2
#else #else
#error "FreeRTOS configPRIO_BITS to be defined" #error "FreeRTOS configPRIO_BITS to be defined"
#endif #endif

View File

@@ -0,0 +1,115 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 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.
*
*/
//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
#include "FreeRTOS.h"
#include "task.h"
#include "common/tusb_common.h"
void vApplicationMallocFailedHook(void)
{
taskDISABLE_INTERRUPTS();
TU_ASSERT(false, );
}
void vApplicationStackOverflowHook(xTaskHandle pxTask, char *pcTaskName)
{
(void) pxTask;
(void) pcTaskName;
taskDISABLE_INTERRUPTS();
TU_ASSERT(false, );
}
/* configSUPPORT_STATIC_ALLOCATION is set to 1, so the application must provide an
* implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
* used by the Idle task. */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
/* If the buffers to be provided to the Idle task are declared inside this
* function then they must be declared static - otherwise they will be allocated on
* the stack and so not exists after this function exits. */
static StaticTask_t xIdleTaskTCB;
static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
/* Pass out a pointer to the StaticTask_t structure in which the Idle task's
state will be stored. */
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
/* Pass out the array that will be used as the Idle task's stack. */
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
/* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
Note that, as the array is necessarily of type StackType_t,
configMINIMAL_STACK_SIZE is specified in words, not bytes. */
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
/* configSUPPORT_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
* application must provide an implementation of vApplicationGetTimerTaskMemory()
* to provide the memory that is used by the Timer service task. */
void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
{
/* If the buffers to be provided to the Timer task are declared inside this
* function then they must be declared static - otherwise they will be allocated on
* the stack and so not exists after this function exits. */
static StaticTask_t xTimerTaskTCB;
static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
/* Pass out a pointer to the StaticTask_t structure in which the Timer
task's state will be stored. */
*ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
/* Pass out the array that will be used as the Timer task's stack. */
*ppxTimerTaskStackBuffer = uxTimerTaskStack;
/* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
Note that, as the array is necessarily of type StackType_t,
configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */
*pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}
#if CFG_TUSB_MCU == OPT_MCU_RX63X | CFG_TUSB_MCU == OPT_MCU_RX65X
#include "iodefine.h"
void vApplicationSetupTimerInterrupt(void)
{
/* Enable CMT0 */
unsigned short oldPRCR = SYSTEM.PRCR.WORD;
SYSTEM.PRCR.WORD = (0xA5u<<8) | TU_BIT(1);
MSTP(CMT0) = 0;
SYSTEM.PRCR.WORD = (0xA5u<<8) | oldPRCR;
CMT0.CMCNT = 0;
CMT0.CMCOR = (unsigned short)(((configPERIPHERAL_CLOCK_HZ/configTICK_RATE_HZ)-1)/128);
CMT0.CMCR.WORD = TU_BIT(6) | 2;
IR(CMT0, CMI0) = 0;
IPR(CMT0, CMI0) = configKERNEL_INTERRUPT_PRIORITY;
IEN(CMT0, CMI0) = 1;
CMT.CMSTR0.BIT.STR0 = 1;
}
#endif

View File

@@ -35,19 +35,30 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "bsp/board.h" #include "bsp/board_api.h"
#include "tusb.h" #include "tusb.h"
// ESP-IDF need "freertos/" prefix in include path. #if TUP_MCU_ESPRESSIF
// CFG_TUSB_OS_INC_PATH should be defined accordingly. // ESP-IDF need "freertos/" prefix in include path.
#include "freertos/FreeRTOS.h" // CFG_TUSB_OS_INC_PATH should be defined accordingly.
#include "freertos/semphr.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h" #include "freertos/semphr.h"
#include "freertos/task.h" #include "freertos/queue.h"
#include "freertos/timers.h" #include "freertos/task.h"
#include "freertos/timers.h"
#define USBD_STACK_SIZE 4096 #define USBD_STACK_SIZE 4096
#else
#include "FreeRTOS.h"
#include "semphr.h"
#include "queue.h"
#include "task.h"
#include "timers.h"
// Increase stack size when debug log is enabled
#define USBD_STACK_SIZE (4*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES // MACRO CONSTANT TYPEDEF PROTYPES
@@ -112,8 +123,12 @@ int main(void)
sampleFreqRng.subrange[0].bMax = AUDIO_SAMPLE_RATE; sampleFreqRng.subrange[0].bMax = AUDIO_SAMPLE_RATE;
sampleFreqRng.subrange[0].bRes = 0; sampleFreqRng.subrange[0].bRes = 0;
#if configSUPPORT_STATIC_ALLOCATION
// Create a task for tinyusb device stack // Create a task for tinyusb device stack
(void) xTaskCreateStatic( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef); xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
#else
xTaskCreate(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
#endif
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3 // skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3) #if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
@@ -136,9 +151,14 @@ void usb_device_task(void* param)
{ {
(void) param; (void) param;
// init device stack on configured roothub port
// This should be called after scheduler/kernel is started. // This should be called after scheduler/kernel is started.
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API. // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
tusb_init(); tud_init(BOARD_TUD_RHPORT);
if (board_init_after_tusb) {
board_init_after_tusb();
}
// RTOS forever loop // RTOS forever loop
while (1) while (1)

View File

@@ -30,6 +30,20 @@
extern "C" { extern "C" {
#endif #endif
//--------------------------------------------------------------------+
// Board Specific Configuration
//--------------------------------------------------------------------+
// RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_TUD_RHPORT
#define BOARD_TUD_RHPORT 0
#endif
// RHPort max operational speed can defined by board.mk
#ifndef BOARD_TUD_MAX_SPEED
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
#endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// COMMON CONFIGURATION // COMMON CONFIGURATION
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@@ -39,32 +53,25 @@ extern "C" {
#error CFG_TUSB_MCU must be defined #error CFG_TUSB_MCU must be defined
#endif #endif
// RHPort number used for device can be defined by board.mk, default to port 0 // This examples use FreeRTOS
#ifndef BOARD_DEVICE_RHPORT_NUM
#define BOARD_DEVICE_RHPORT_NUM 0
#endif
#ifndef BOARD_DEVICE_RHPORT_SPEED
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
#endif
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#ifndef CFG_TUSB_OS #ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_FREERTOS #define CFG_TUSB_OS OPT_OS_FREERTOS
#endif #endif
// Espressif IDF requires "freertos/" prefix in include path // Espressif IDF requires "freertos/" prefix in include path
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3) #if TUP_MCU_ESPRESSIF
#define CFG_TUSB_OS_INC_PATH freertos/ #define CFG_TUSB_OS_INC_PATH freertos/
#endif #endif
#ifndef CFG_TUSB_DEBUG #ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 0 #define CFG_TUSB_DEBUG 0
#endif #endif
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build // Enable Device stack
// #define CFG_TUSB_DEBUG 0 #define CFG_TUD_ENABLED 1
// Default is max speed that hardware controller could support with on-chip PHY
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
* Tinyusb use follows macros to declare transferring memory so that they can be put * Tinyusb use follows macros to declare transferring memory so that they can be put
@@ -78,7 +85,7 @@ extern "C" {
#endif #endif
#ifndef CFG_TUSB_MEM_ALIGN #ifndef CFG_TUSB_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
#endif #endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@@ -90,11 +97,11 @@ extern "C" {
#endif #endif
//------------- CLASS -------------// //------------- CLASS -------------//
#define CFG_TUD_AUDIO 1
#define CFG_TUD_CDC 0 #define CFG_TUD_CDC 0
#define CFG_TUD_MSC 0 #define CFG_TUD_MSC 0
#define CFG_TUD_HID 0 #define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0 #define CFG_TUD_MIDI 0
#define CFG_TUD_AUDIO 1
#define CFG_TUD_VENDOR 0 #define CFG_TUD_VENDOR 0
//-------------------------------------------------------------------- //--------------------------------------------------------------------

View File

@@ -1,22 +1,34 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.17)
# TOP is absolute path to root directory of TinyUSB git repo
# needed for esp32sx build. TOOD could be removed later on
set(TOP "../../..")
get_filename_component(TOP "${TOP}" REALPATH)
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>) # gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR}) family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
project(${PROJECT}) project(${PROJECT} C CXX ASM)
# Checks this example is valid for the family and initializes the project # Checks this example is valid for the family and initializes the project
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR}) family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
# Check for -DFAMILY= # Espressif has its own cmake build system
if(FAMILY MATCHES "^esp32s[2-3]") if(FAMILY STREQUAL "espressif")
else() return()
message(FATAL_ERROR "Invalid FAMILY specified: ${FAMILY}")
endif() endif()
add_executable(${PROJECT})
# Example source
target_sources(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src/freertos_hook.c
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
)
# Example include
target_include_directories(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
)
# Configure compilation flags and libraries for the example with FreeRTOS.
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
family_configure_device_example(${PROJECT} freertos)

View File

@@ -1,22 +1,19 @@
DEPS_SUBMODULES += lib/FreeRTOS-Kernel include ../../build_system/make/make.mk
include ../../../tools/top.mk
include ../../make.mk
FREERTOS_SRC = lib/FreeRTOS-Kernel FREERTOS_SRC = lib/FreeRTOS-Kernel
FREERTOS_PORTABLE_PATH = $(FREERTOS_SRC)/portable/$(if $(findstring iar,$(TOOLCHAIN)),IAR,GCC)
INC += \ INC += \
src \ src \
src/FreeRTOSConfig \ src/FreeRTOSConfig \
$(TOP)/hw \ $(TOP)/hw \
$(TOP)/$(FREERTOS_SRC)/include \ $(TOP)/$(FREERTOS_SRC)/include \
$(TOP)/$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT) $(TOP)/$(FREERTOS_PORTABLE_SRC) \
# Example source # Example source
EXAMPLE_SOURCE = \ EXAMPLE_SOURCE = \
src/freertos_hook.c \ src/freertos_hook.c \
src/main.c \ src/main.c \
src/msc_disk.c \
src/usb_descriptors.c src/usb_descriptors.c
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
@@ -27,12 +24,15 @@ SRC_C += \
$(FREERTOS_SRC)/queue.c \ $(FREERTOS_SRC)/queue.c \
$(FREERTOS_SRC)/tasks.c \ $(FREERTOS_SRC)/tasks.c \
$(FREERTOS_SRC)/timers.c \ $(FREERTOS_SRC)/timers.c \
$(subst ../../../,,$(wildcard ../../../$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT)/*.c)) $(subst $(TOP)/,,$(wildcard $(TOP)/$(FREERTOS_PORTABLE_SRC)/*.c))
SRC_S += \
$(subst $(TOP)/,,$(wildcard $(TOP)/$(FREERTOS_PORTABLE_SRC)/*.s))
# Suppress FreeRTOS warnings # Suppress FreeRTOS warnings
CFLAGS += -Wno-error=cast-qual CFLAGS += -Wno-error=cast-qual -Wno-error=redundant-decls
# FreeRTOS (lto + Os) linker issue # FreeRTOS (lto + Os) linker issue
LDFLAGS += -Wl,--undefined=vTaskSwitchContext LDFLAGS += -Wl,--undefined=vTaskSwitchContext
include ../../rules.mk include ../../build_system/make/rules.mk

View File

@@ -6,9 +6,10 @@
$ cd /tinyusb/examples/device/audio_test_freertos $ cd /tinyusb/examples/device/audio_test_freertos
``` ```
3. Run make in project directory specifying the board 3. Run cmake in project directory specifying the board
``` ```
$ make BOARD=espressif_s3_devkitc all $ cmake -DBOARD=espressif_s3_devkitc -B build -G Ninja .
$ ninja.exe -C build
``` ```
4. Flash the binary onto the esp32-s3 by copy-paste of the full command output by the esp-idf build system replacing **(PORT)** with eg. /dev/ttyUSB0 4. Flash the binary onto the esp32-s3 by copy-paste of the full command output by the esp-idf build system replacing **(PORT)** with eg. /dev/ttyUSB0

View File

@@ -0,0 +1,3 @@
CONFIG_IDF_CMAKE=y
CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y
CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION=y

View File

@@ -1,27 +1,4 @@
# This file is for ESP-IDF only
idf_component_register(SRCS "main.c" "usb_descriptors.c" idf_component_register(SRCS "main.c" "usb_descriptors.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES freertos soc) REQUIRES boards tinyusb_src)
file(TO_NATIVE_PATH "${TOP}/hw/bsp/${FAMILY}/boards/${BOARD}/board.cmake" board_cmake)
if(EXISTS ${board_cmake})
include(${board_cmake})
endif()
target_include_directories(${COMPONENT_TARGET} PUBLIC
"${TOP}/hw"
"${TOP}/src"
)
target_compile_definitions(${COMPONENT_TARGET} PUBLIC
ESP_PLATFORM
)
target_sources(${COMPONENT_TARGET} PUBLIC
"${TOP}/src/tusb.c"
"${TOP}/src/common/tusb_fifo.c"
"${TOP}/src/device/usbd.c"
"${TOP}/src/device/usbd_control.c"
"${TOP}/src/class/audio/audio_device.c"
"${TOP}/src/portable/espressif/esp32sx/dcd_esp32sx.c"
)

View File

@@ -42,6 +42,9 @@
* See http://www.freertos.org/a00110.html. * See http://www.freertos.org/a00110.html.
*----------------------------------------------------------*/ *----------------------------------------------------------*/
// skip if included from IAR assembler
#ifndef __IASMARM__
// Include MCU header // Include MCU header
#include "bsp/board_mcu.h" #include "bsp/board_mcu.h"
@@ -53,9 +56,12 @@
#if CFG_TUSB_MCU == OPT_MCU_MM32F327X #if CFG_TUSB_MCU == OPT_MCU_MM32F327X
extern u32 SystemCoreClock; extern u32 SystemCoreClock;
#else #else
// FIXME cause redundant-decls warnings
extern uint32_t SystemCoreClock; extern uint32_t SystemCoreClock;
#endif #endif
#endif
/* Cortex M23/M33 port configuration. */ /* Cortex M23/M33 port configuration. */
#define configENABLE_MPU 0 #define configENABLE_MPU 0
#define configENABLE_FPU 1 #define configENABLE_FPU 1
@@ -68,14 +74,14 @@
#define configTICK_RATE_HZ ( 1000 ) #define configTICK_RATE_HZ ( 1000 )
#define configMAX_PRIORITIES ( 5 ) #define configMAX_PRIORITIES ( 5 )
#define configMINIMAL_STACK_SIZE ( 128 ) #define configMINIMAL_STACK_SIZE ( 128 )
#define configTOTAL_HEAP_SIZE ( 0*1024 ) // dynamic is not used #define configTOTAL_HEAP_SIZE ( configSUPPORT_DYNAMIC_ALLOCATION*4*1024 )
#define configMAX_TASK_NAME_LEN 16 #define configMAX_TASK_NAME_LEN 16
#define configUSE_16_BIT_TICKS 0 #define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1 #define configIDLE_SHOULD_YIELD 1
#define configUSE_MUTEXES 1 #define configUSE_MUTEXES 1
#define configUSE_RECURSIVE_MUTEXES 1 #define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 1 #define configUSE_COUNTING_SEMAPHORES 1
#define configQUEUE_REGISTRY_SIZE 2 #define configQUEUE_REGISTRY_SIZE 4
#define configUSE_QUEUE_SETS 0 #define configUSE_QUEUE_SETS 0
#define configUSE_TIME_SLICING 0 #define configUSE_TIME_SLICING 0
#define configUSE_NEWLIB_REENTRANT 0 #define configUSE_NEWLIB_REENTRANT 0
@@ -90,6 +96,7 @@
#define configUSE_TICK_HOOK 0 #define configUSE_TICK_HOOK 0
#define configUSE_MALLOC_FAILED_HOOK 0 // cause nested extern warning #define configUSE_MALLOC_FAILED_HOOK 0 // cause nested extern warning
#define configCHECK_FOR_STACK_OVERFLOW 2 #define configCHECK_FOR_STACK_OVERFLOW 2
#define configCHECK_HANDLER_INSTALLATION 0
/* Run time and task stats gathering related definitions. */ /* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS 0 #define configGENERATE_RUN_TIME_STATS 0
@@ -124,29 +131,12 @@
#define INCLUDE_xEventGroupSetBitFromISR 0 #define INCLUDE_xEventGroupSetBitFromISR 0
#define INCLUDE_xTimerPendFunctionCall 0 #define INCLUDE_xTimerPendFunctionCall 0
/* Define to trap errors during development. */
// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7
#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__)
#define configASSERT(_exp) \
do {\
if ( !(_exp) ) { \
volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
if ( (*ARM_CM_DHCSR) & 1UL ) { /* Only halt mcu if debugger is attached */ \
taskDISABLE_INTERRUPTS(); \
__asm("BKPT #0\n"); \
}\
}\
} while(0)
#else
#define configASSERT( x )
#endif
#ifdef __RX__ #ifdef __RX__
/* Renesas RX series */ /* Renesas RX series */
#define vSoftwareInterruptISR INT_Excep_ICU_SWINT #define vSoftwareInterruptISR INT_Excep_ICU_SWINT
#define vTickISR INT_Excep_CMT0_CMI0 #define vTickISR INT_Excep_CMT0_CMI0
#define configPERIPHERAL_CLOCK_HZ (configCPU_CLOCK_HZ/2) #define configPERIPHERAL_CLOCK_HZ (configCPU_CLOCK_HZ/2)
#define configKERNEL_INTERRUPT_PRIORITY 1 #define configKERNEL_INTERRUPT_PRIORITY 1
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 4 #define configMAX_SYSCALL_INTERRUPT_PRIORITY 4
#else #else
@@ -162,9 +152,18 @@
#if defined(__NVIC_PRIO_BITS) #if defined(__NVIC_PRIO_BITS)
// For Cortex-M specific: __NVIC_PRIO_BITS is defined in core_cmx.h // For Cortex-M specific: __NVIC_PRIO_BITS is defined in core_cmx.h
#define configPRIO_BITS __NVIC_PRIO_BITS #define configPRIO_BITS __NVIC_PRIO_BITS
#elif defined(__ECLIC_INTCTLBITS) #elif defined(__ECLIC_INTCTLBITS)
// RISC-V Bumblebee core from nuclei // RISC-V Bumblebee core from nuclei
#define configPRIO_BITS __ECLIC_INTCTLBITS #define configPRIO_BITS __ECLIC_INTCTLBITS
#elif defined(__IASMARM__)
// FIXME: IAR Assembler cannot include mcu header directly to get __NVIC_PRIO_BITS.
// Therefore we will hard coded it to minimum value of 2 to get pass ci build.
// IAR user must update this to correct value of the target MCU
#message "configPRIO_BITS is hard coded to 2 to pass IAR build only. User should update it per MCU"
#define configPRIO_BITS 2
#else #else
#error "FreeRTOS configPRIO_BITS to be defined" #error "FreeRTOS configPRIO_BITS to be defined"
#endif #endif

View File

@@ -0,0 +1,115 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 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.
*
*/
//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
#include "FreeRTOS.h"
#include "task.h"
#include "common/tusb_common.h"
void vApplicationMallocFailedHook(void)
{
taskDISABLE_INTERRUPTS();
TU_ASSERT(false, );
}
void vApplicationStackOverflowHook(xTaskHandle pxTask, char *pcTaskName)
{
(void) pxTask;
(void) pcTaskName;
taskDISABLE_INTERRUPTS();
TU_ASSERT(false, );
}
/* configSUPPORT_STATIC_ALLOCATION is set to 1, so the application must provide an
* implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
* used by the Idle task. */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
/* If the buffers to be provided to the Idle task are declared inside this
* function then they must be declared static - otherwise they will be allocated on
* the stack and so not exists after this function exits. */
static StaticTask_t xIdleTaskTCB;
static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
/* Pass out a pointer to the StaticTask_t structure in which the Idle task's
state will be stored. */
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
/* Pass out the array that will be used as the Idle task's stack. */
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
/* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
Note that, as the array is necessarily of type StackType_t,
configMINIMAL_STACK_SIZE is specified in words, not bytes. */
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
/* configSUPPORT_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
* application must provide an implementation of vApplicationGetTimerTaskMemory()
* to provide the memory that is used by the Timer service task. */
void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
{
/* If the buffers to be provided to the Timer task are declared inside this
* function then they must be declared static - otherwise they will be allocated on
* the stack and so not exists after this function exits. */
static StaticTask_t xTimerTaskTCB;
static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
/* Pass out a pointer to the StaticTask_t structure in which the Timer
task's state will be stored. */
*ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
/* Pass out the array that will be used as the Timer task's stack. */
*ppxTimerTaskStackBuffer = uxTimerTaskStack;
/* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
Note that, as the array is necessarily of type StackType_t,
configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */
*pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}
#if CFG_TUSB_MCU == OPT_MCU_RX63X | CFG_TUSB_MCU == OPT_MCU_RX65X
#include "iodefine.h"
void vApplicationSetupTimerInterrupt(void)
{
/* Enable CMT0 */
unsigned short oldPRCR = SYSTEM.PRCR.WORD;
SYSTEM.PRCR.WORD = (0xA5u<<8) | TU_BIT(1);
MSTP(CMT0) = 0;
SYSTEM.PRCR.WORD = (0xA5u<<8) | oldPRCR;
CMT0.CMCNT = 0;
CMT0.CMCOR = (unsigned short)(((configPERIPHERAL_CLOCK_HZ/configTICK_RATE_HZ)-1)/128);
CMT0.CMCR.WORD = TU_BIT(6) | 2;
IR(CMT0, CMI0) = 0;
IPR(CMT0, CMI0) = configKERNEL_INTERRUPT_PRIORITY;
IEN(CMT0, CMI0) = 1;
CMT.CMSTR0.BIT.STR0 = 1;
}
#endif

View File

@@ -35,19 +35,32 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "bsp/board.h" #include "bsp/board_api.h"
#include "tusb.h" #include "tusb.h"
// ESP-IDF need "freertos/" prefix in include path. #if TUP_MCU_ESPRESSIF
// CFG_TUSB_OS_INC_PATH should be defined accordingly. // ESP-IDF need "freertos/" prefix in include path.
#include "freertos/FreeRTOS.h" // CFG_TUSB_OS_INC_PATH should be defined accordingly.
#include "freertos/semphr.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h" #include "freertos/semphr.h"
#include "freertos/task.h" #include "freertos/queue.h"
#include "freertos/timers.h" #include "freertos/task.h"
#include "freertos/timers.h"
#define USBD_STACK_SIZE 4096 #define USBD_STACK_SIZE 4096
#else
#include "FreeRTOS.h"
#include "semphr.h"
#include "queue.h"
#include "task.h"
#include "timers.h"
// Increase stack size when debug log is enabled
#define USBD_STACK_SIZE (4*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
#endif
#define BLINKY_STACK_SIZE configMINIMAL_STACK_SIZE
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES // MACRO CONSTANT TYPEDEF PROTYPES
@@ -68,12 +81,16 @@ enum {
BLINK_SUSPENDED = 2500, BLINK_SUSPENDED = 2500,
}; };
// static timer // static task
StaticTimer_t blinky_tmdef; #if configSUPPORT_STATIC_ALLOCATION
TimerHandle_t blinky_tm; StackType_t blinky_stack[BLINKY_STACK_SIZE];
StaticTask_t blinky_taskdef;
StackType_t usb_device_stack[USBD_STACK_SIZE]; StackType_t usb_device_stack[USBD_STACK_SIZE];
StaticTask_t usb_device_taskdef; StaticTask_t usb_device_taskdef;
#endif
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
// Audio controls // Audio controls
// Current states // Current states
@@ -90,7 +107,7 @@ audio_control_range_4_n_t(1) sampleFreqRng; // Sample frequency range stat
uint16_t test_buffer_audio[CFG_TUD_AUDIO_EP_SZ_IN/2]; uint16_t test_buffer_audio[CFG_TUD_AUDIO_EP_SZ_IN/2];
uint16_t startVal = 0; uint16_t startVal = 0;
void led_blinky_cb(TimerHandle_t xTimer); void led_blinking_task(void* param);
void usb_device_task(void* param); void usb_device_task(void* param);
void audio_task(void); void audio_task(void);
@@ -99,10 +116,6 @@ int main(void)
{ {
board_init(); board_init();
// soft timer for blinky
blinky_tm = xTimerCreateStatic(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb, &blinky_tmdef);
xTimerStart(blinky_tm, 0);
// Init values // Init values
sampFreq = AUDIO_SAMPLE_RATE; sampFreq = AUDIO_SAMPLE_RATE;
clkValid = 1; clkValid = 1;
@@ -112,8 +125,16 @@ int main(void)
sampleFreqRng.subrange[0].bMax = AUDIO_SAMPLE_RATE; sampleFreqRng.subrange[0].bMax = AUDIO_SAMPLE_RATE;
sampleFreqRng.subrange[0].bRes = 0; sampleFreqRng.subrange[0].bRes = 0;
#if configSUPPORT_STATIC_ALLOCATION
// blinky task
xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef);
// Create a task for tinyusb device stack // Create a task for tinyusb device stack
(void) xTaskCreateStatic( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef); xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
#else
xTaskCreate(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
#endif
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3 // skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3) #if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
@@ -136,9 +157,14 @@ void usb_device_task(void* param)
{ {
(void) param; (void) param;
// init device stack on configured roothub port
// This should be called after scheduler/kernel is started. // This should be called after scheduler/kernel is started.
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API. // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
tusb_init(); tud_init(BOARD_TUD_RHPORT);
if (board_init_after_tusb) {
board_init_after_tusb();
}
// RTOS forever loop // RTOS forever loop
while (1) while (1)
@@ -153,30 +179,26 @@ void usb_device_task(void* param)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Invoked when device is mounted // Invoked when device is mounted
void tud_mount_cb(void) void tud_mount_cb(void) {
{ blink_interval_ms = BLINK_MOUNTED;
xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_MOUNTED), 0);
} }
// Invoked when device is unmounted // Invoked when device is unmounted
void tud_umount_cb(void) void tud_umount_cb(void) {
{ blink_interval_ms = BLINK_NOT_MOUNTED;
xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), 0);
} }
// Invoked when usb bus is suspended // Invoked when usb bus is suspended
// remote_wakeup_en : if host allow us to perform remote wakeup // remote_wakeup_en : if host allow us to perform remote wakeup
// Within 7ms, device must draw an average of current less than 2.5 mA from bus // Within 7ms, device must draw an average of current less than 2.5 mA from bus
void tud_suspend_cb(bool remote_wakeup_en) void tud_suspend_cb(bool remote_wakeup_en) {
{
(void) remote_wakeup_en; (void) remote_wakeup_en;
xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_SUSPENDED), 0); blink_interval_ms = BLINK_SUSPENDED;
} }
// Invoked when usb bus is resumed // Invoked when usb bus is resumed
void tud_resume_cb(void) void tud_resume_cb(void) {
{ blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_MOUNTED), 0);
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@@ -476,11 +498,17 @@ bool tud_audio_set_itf_close_EP_cb(uint8_t rhport, tusb_control_request_t const
///--------------------------------------------------------------------+ ///--------------------------------------------------------------------+
// BLINKING TASK // BLINKING TASK
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void led_blinky_cb(TimerHandle_t xTimer) void led_blinking_task(void* param) {
{ (void) param;
(void) xTimer; static uint32_t start_ms = 0;
static bool led_state = false; static bool led_state = false;
board_led_write(led_state); while (1) {
led_state = 1 - led_state; // toggle // Blink every interval ms
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
start_ms += blink_interval_ms;
board_led_write(led_state);
led_state = 1 - led_state; // toggle
}
} }

View File

@@ -10,7 +10,7 @@ if __name__ == '__main__':
# print(sd.query_devices()) # print(sd.query_devices())
fs = 48000 # Sample rate fs = 48000 # Sample rate
duration = 100e-3 # Duration of recording duration = 1000e-3 # Duration of recording
if platform.system() == 'Windows': if platform.system() == 'Windows':
# MME is needed since there are more than one MicNode device APIs (at least in Windows) # MME is needed since there are more than one MicNode device APIs (at least in Windows)