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);