From 81a78d74ed1dd6860b9ab83da6cbf01b6686fdbe Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 22 Jul 2020 21:54:55 +0700 Subject: [PATCH 1/4] update TinyUSB core to commit 11d669b4d2a40eb2fc5e51b2a9707a6de9d42363 Note: due to osal_freertos changes https://github.com/hathach/tinyusb/pull/468 the Adafruit_TinyUSB_Core_init() should be near the task scheduler (or better inside loop task) to prevent calling portYIELD_FROM_ISR() when scheduler not started --- cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore | 2 +- cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp | 2 +- cores/nRF5/main.cpp | 8 ++++---- platform.txt | 2 +- tools/build_all.py | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore index 07490772..11d669b4 160000 --- a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore +++ b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore @@ -1 +1 @@ -Subproject commit 07490772a559b2b0cac8aa187566aff4bec7376a +Subproject commit 11d669b4d2a40eb2fc5e51b2a9707a6de9d42363 diff --git a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp index 919e89a8..1873e3fc 100644 --- a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp +++ b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp @@ -42,7 +42,7 @@ extern "C" void USBD_IRQHandler(void) tud_int_handler(0); #if CFG_SYSVIEW - SEGGER_SYSVIEW_RecordExitISR(); + SEGGER_SYSVIEW_RecordExitISR(); #endif } diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index 1998345e..c3ac0894 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -72,10 +72,6 @@ int main( void ) init(); initVariant(); -#ifdef USE_TINYUSB - Adafruit_TinyUSB_Core_init(); -#endif - #if CFG_SYSVIEW SEGGER_SYSVIEW_Conf(); #endif @@ -86,6 +82,10 @@ int main( void ) // Initialize callback task ada_callback_init(CALLBACK_STACK_SZ); +#ifdef USE_TINYUSB + Adafruit_TinyUSB_Core_init(); +#endif + // Start FreeRTOS scheduler. vTaskStartScheduler(); diff --git a/platform.txt b/platform.txt index 358ade4d..f8f47382 100644 --- a/platform.txt +++ b/platform.txt @@ -60,7 +60,7 @@ nordic.path={build.core.path}/nordic # build.logger_flags and build.sysview_flags and intentionally empty, # to allow modification via a user's own boards.local.txt or platform.local.txt files. -build.flags.nrf= -DSOFTDEVICE_PRESENT -DARDUINO_NRF52_ADAFRUIT -DNRF52_SERIES -DLFS_NAME_MAX=64 -Ofast {build.debug_flags} {build.logger_flags} {build.sysview_flags} "-I{build.core.path}/cmsis/Core/Include" "-I{nordic.path}" "-I{nordic.path}/nrfx" "-I{nordic.path}/nrfx/hal" "-I{nordic.path}/nrfx/mdk" "-I{nordic.path}/nrfx/soc" "-I{nordic.path}/nrfx/drivers/include" "-I{nordic.path}/nrfx/drivers/src" "-I{nordic.path}/softdevice/{build.sd_name}_nrf52_{build.sd_version}_API/include" "-I{rtos.path}/Source/include" "-I{rtos.path}/config" "-I{rtos.path}/portable/GCC/nrf52" "-I{rtos.path}/portable/CMSIS/nrf52" "-I{build.core.path}/sysview/SEGGER" "-I{build.core.path}/sysview/Config" "-I{build.core.path}/TinyUSB" "-I{build.core.path}/TinyUSB/Adafruit_TinyUSB_ArduinoCore" "-I{build.core.path}/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src" +build.flags.nrf= -DSOFTDEVICE_PRESENT -DARDUINO_NRF52_ADAFRUIT -DNRF52_SERIES -DLFS_NAME_MAX=64 -Ofast {build.debug_flags} {build.logger_flags} {build.sysview_flags} "-I{build.core.path}/cmsis/Core/Include" "-I{nordic.path}" "-I{nordic.path}/nrfx" "-I{nordic.path}/nrfx/hal" "-I{nordic.path}/nrfx/mdk" "-I{nordic.path}/nrfx/soc" "-I{nordic.path}/nrfx/drivers/include" "-I{nordic.path}/nrfx/drivers/src" "-I{nordic.path}/softdevice/{build.sd_name}_nrf52_{build.sd_version}_API/include" "-I{nordic.path}/softdevice/{build.sd_name}_nrf52_{build.sd_version}_API/include/nrf52" "-I{rtos.path}/Source/include" "-I{rtos.path}/config" "-I{rtos.path}/portable/GCC/nrf52" "-I{rtos.path}/portable/CMSIS/nrf52" "-I{build.core.path}/sysview/SEGGER" "-I{build.core.path}/sysview/Config" "-I{build.core.path}/TinyUSB" "-I{build.core.path}/TinyUSB/Adafruit_TinyUSB_ArduinoCore" "-I{build.core.path}/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src" # usb flags build.flags.usb= -DUSBCON -DUSE_TINYUSB -DUSB_VID={build.vid} -DUSB_PID={build.pid} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' diff --git a/tools/build_all.py b/tools/build_all.py index 62f46757..b9f99262 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -10,7 +10,7 @@ success_count = 0 fail_count = 0 skip_count = 0 -build_format = '| {:20} | {:35} | {:9} ' +build_format = '| {:20} | {:35} | {:9} | {:6} |' build_separator = '-' * 83 default_boards = [ 'cluenrf52840', 'cplaynrf52840', 'feather52832', 'feather52840', 'feather52840sense', 'itsybitsy52840' ] @@ -39,7 +39,7 @@ def build_examples(variant): print(build_separator) print('| {:^79} |'.format('Board ' + variant)) print(build_separator) - print((build_format + '| {:6} |').format('Library', 'Example', 'Result', 'Time')) + print(build_format.format('Library', 'Example', 'Result', 'Time')) print(build_separator) fqbn = "adafruit:nrf52:{}:softdevice={},debug=l0".format(variant, 's140v6' if variant != 'feather52832' else 's132v6') @@ -83,7 +83,7 @@ def build_examples(variant): build_duration = time.monotonic() - start_time - print((build_format + '| {:5.2f}s |').format(sketch.split(os.path.sep)[1], os.path.basename(sketch), success, build_duration)) + print(build_format.format(sketch.split(os.path.sep)[1], os.path.basename(sketch), success, '{:5.2f}s'.format(build_duration))) if success != "\033[33mskipped\033[0m ": if build_result.returncode != 0: From 9a2f1384ad96a49a6ec0e5bc5b2c258b3c937fa1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 22 Jul 2020 22:25:20 +0700 Subject: [PATCH 2/4] move Adafruit_TinyUSB_Core_init() inside loop() task --- cores/nRF5/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index c3ac0894..81eb60e3 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -44,6 +44,10 @@ static void loop_task(void* arg) { (void) arg; +#ifdef USE_TINYUSB + Adafruit_TinyUSB_Core_init(); +#endif + #if CFG_DEBUG // If Serial is not begin(), call it to avoid hard fault if(!Serial) Serial.begin(115200); @@ -82,10 +86,6 @@ int main( void ) // Initialize callback task ada_callback_init(CALLBACK_STACK_SZ); -#ifdef USE_TINYUSB - Adafruit_TinyUSB_Core_init(); -#endif - // Start FreeRTOS scheduler. vTaskStartScheduler(); From ca4f2bcbb4f93f3f1e294c51f9a884c1deaf9eb3 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 23 Jul 2020 07:07:22 +0700 Subject: [PATCH 3/4] sync with Adafruit_TinyUSB_ArduinoCore commit 238a5c0b95fcd402d24524c7bebbdd822168a51e --- cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore index 11d669b4..238a5c0b 160000 --- a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore +++ b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore @@ -1 +1 @@ -Subproject commit 11d669b4d2a40eb2fc5e51b2a9707a6de9d42363 +Subproject commit 238a5c0b95fcd402d24524c7bebbdd822168a51e From cb6d216c9ce9f498c430959c0c4a779cfb1f688e Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 23 Jul 2020 07:45:09 +0700 Subject: [PATCH 4/4] move usb hardware init and tud_init into usb device thread help to prevent USB IRQ when scheduler is not started yet --- cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp | 50 ++++++++++----------- cores/nRF5/main.cpp | 8 ++-- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp index 1873e3fc..e4f50237 100644 --- a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp +++ b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp @@ -30,6 +30,16 @@ #include "Arduino.h" #include "Adafruit_TinyUSB_Core.h" +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + +#define USBD_STACK_SZ (200) + +// tinyusb function that handles power event (detected, ready, removed) +// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. +extern "C" void tusb_hal_nrf_power_event(uint32_t event); + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -46,30 +56,6 @@ extern "C" void USBD_IRQHandler(void) #endif } -//--------------------------------------------------------------------+ -// MACRO TYPEDEF CONSTANT ENUM DECLARATION -//--------------------------------------------------------------------+ - -#define USBD_STACK_SZ (200) - -// tinyusb function that handles power event (detected, ready, removed) -// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. -extern "C" void tusb_hal_nrf_power_event(uint32_t event); - -// USB Device Driver task -// This top level thread process all usb events and invoke callbacks -static void usb_device_task(void* param) -{ - (void) param; - - // RTOS forever loop - while (1) - { - // tinyusb device task - tud_task(); - } -} - //--------------------------------------------------------------------+ // Core Init & Touch1200 //--------------------------------------------------------------------+ @@ -99,8 +85,12 @@ static void usb_hardware_init(void) if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY); } -void Adafruit_TinyUSB_Core_init(void) +// USB Device Driver task +// This top level thread process all usb events and invoke callbacks +static void usb_device_task(void* param) { + (void) param; + USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial); USBDevice.setID(USB_VID, USB_PID); USBDevice.begin(); @@ -110,6 +100,16 @@ void Adafruit_TinyUSB_Core_init(void) // Init tinyusb stack tusb_init(); + // RTOS forever loop + while (1) + { + // tinyusb device task + tud_task(); + } +} + +void Adafruit_TinyUSB_Core_init(void) +{ // Create a task for tinyusb device stack xTaskCreate( usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL); } diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index 81eb60e3..98e3a291 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -44,10 +44,6 @@ static void loop_task(void* arg) { (void) arg; -#ifdef USE_TINYUSB - Adafruit_TinyUSB_Core_init(); -#endif - #if CFG_DEBUG // If Serial is not begin(), call it to avoid hard fault if(!Serial) Serial.begin(115200); @@ -80,6 +76,10 @@ int main( void ) SEGGER_SYSVIEW_Conf(); #endif +#ifdef USE_TINYUSB + Adafruit_TinyUSB_Core_init(); +#endif + // Create a task for loop() xTaskCreate( loop_task, "loop", LOOP_STACK_SZ, NULL, TASK_PRIO_LOW, &_loopHandle);