diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index ee5b2ae9..92721a80 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -69,7 +69,8 @@ void resumeLoop(void); #include "utility/AdaCallback.h" #ifdef USE_TINYUSB - #include "Adafruit_TinyUSB_Core.h" + #include "Adafruit_USBD_Interface.h" + #include "Adafruit_USBD_CDC.h" #endif // Include board variant diff --git a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore deleted file mode 160000 index e7b89209..00000000 --- a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e7b892095f2bb5d8bef6a748238369bdd268ed5e diff --git a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp b/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp deleted file mode 100644 index 838beb22..00000000 --- a/cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019, hathach for Adafruit - * - * 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. - */ - -#ifdef USE_TINYUSB - -#include "nrfx.h" -#include "nrfx_power.h" - -#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 -//--------------------------------------------------------------------+ -extern "C" void USBD_IRQHandler(void) -{ -#if CFG_SYSVIEW - SEGGER_SYSVIEW_RecordEnterISR(); -#endif - - tud_int_handler(0); - -#if CFG_SYSVIEW - SEGGER_SYSVIEW_RecordExitISR(); -#endif -} - -//--------------------------------------------------------------------+ -// Core Init & Touch1200 -//--------------------------------------------------------------------+ - -// Init usb hardware when starting up. Softdevice is not enabled yet -static void usb_hardware_init(void) -{ - // Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice - // 2 is highest for application - NVIC_SetPriority(USBD_IRQn, 2); - - // USB power may already be ready at this time -> no event generated - // We need to invoke the handler based on the status initially - uint32_t usb_reg = NRF_POWER->USBREGSTATUS; - - // Power module init - const nrfx_power_config_t pwr_cfg = { 0 }; - nrfx_power_init(&pwr_cfg); - - // Register tusb function as USB power handler - const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; - nrfx_power_usbevt_init(&config); - - nrfx_power_usbevt_enable(); - - if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); -} - -// USB Device Driver task -// This top level thread process all usb events and invoke callbacks -static void usb_device_task(void* param) -{ - (void) param; - - Serial.setStringDescriptor("TinyUSB Serial"); - USBDevice.addInterface(Serial); - USBDevice.setID(USB_VID, USB_PID); - USBDevice.begin(); - - usb_hardware_init(); - - // 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); -} - -void Adafruit_TinyUSB_Core_touch1200(void) -{ - delay(5); // a few millisecond for USB control status completion - enterSerialDfu(); -} - -//--------------------------------------------------------------------+ -// Adafruit_USBD_Device platform dependent -//--------------------------------------------------------------------+ - -uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t* serial_str) -{ - // Serial is 64-bit DeviceID -> 16 chars len - char tmp_serial[17]; - sprintf(tmp_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]); - - for(uint8_t i=0; i<16; i++) serial_str[i] = tmp_serial[i]; - return 16; -} - -#endif // USE_TINYUSB diff --git a/cores/nRF5/TinyUSB/Adafruit_USBD_CDC.h b/cores/nRF5/TinyUSB/Adafruit_USBD_CDC.h new file mode 100644 index 00000000..23e40450 --- /dev/null +++ b/cores/nRF5/TinyUSB/Adafruit_USBD_CDC.h @@ -0,0 +1,73 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach for Adafruit Industries + * + * 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. + */ + +#ifndef ADAFRUIT_USBD_CDC_H_ +#define ADAFRUIT_USBD_CDC_H_ + +#ifdef __cplusplus + +#include "Adafruit_USBD_Interface.h" +#include "Stream.h" + +class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface +{ +public: + Adafruit_USBD_CDC(void); + + // fron Adafruit_USBD_Interface + virtual uint16_t getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize); + + void setPins(uint8_t pin_rx, uint8_t pin_tx) { (void) pin_rx; (void) pin_tx; } + void begin(uint32_t baud_count); + void begin(uint32_t baud, uint8_t config); + void end(void); + + // return line coding set by host + uint32_t baud(void); + uint8_t stopbits(void); + uint8_t paritytype(void); + uint8_t numbits(void); + int dtr(void); + + // Stream API + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual void flush(void); + virtual size_t write(uint8_t); + + virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *buffer, size_t size) { + return write((const uint8_t *)buffer, size); + } + + virtual int availableForWrite(void); + using Print::write; // pull in write(str) from Print + operator bool(); +}; + +extern Adafruit_USBD_CDC Serial; + +#endif // __cplusplus +#endif diff --git a/cores/nRF5/TinyUSB/Adafruit_USBD_Interface.h b/cores/nRF5/TinyUSB/Adafruit_USBD_Interface.h new file mode 100644 index 00000000..4e8f62cb --- /dev/null +++ b/cores/nRF5/TinyUSB/Adafruit_USBD_Interface.h @@ -0,0 +1,45 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2021 Ha Thach (tinyusb.org) for Adafruit Industries + * + * 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. + */ + +#ifndef ADAFRUIT_USBD_INTERFACE_H_ +#define ADAFRUIT_USBD_INTERFACE_H_ + +#ifdef __cplusplus + +class Adafruit_USBD_Interface +{ + protected: + const char* _desc_str; + + public: + Adafruit_USBD_Interface(void) { _desc_str = NULL; } + + virtual uint16_t getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize) = 0; + void setStringDescriptor(const char* str) { _desc_str = str; } + const char* getStringDescriptor(void) { return _desc_str; } +}; + +#endif + +#endif diff --git a/cores/nRF5/common_inc.h b/cores/nRF5/common_inc.h index f179ad12..449a953c 100644 --- a/cores/nRF5/common_inc.h +++ b/cores/nRF5/common_inc.h @@ -36,11 +36,13 @@ #ifndef COMMON_INC_H_ #define COMMON_INC_H_ +#define ATTR_PACKED __attribute__ ((packed)) +#define ATTR_WEAK __attribute__((weak)) + #include #include #include -#include "compiler_macro.h" #include "common_func.h" #include "verify.h" diff --git a/cores/nRF5/compiler_macro.h b/cores/nRF5/compiler_macro.h deleted file mode 100644 index 698717ae..00000000 --- a/cores/nRF5/compiler_macro.h +++ /dev/null @@ -1,48 +0,0 @@ -/******************************************************************************/ -/*! - @file compiler_macro.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, K. Townsend (microBuilder.eu) - Copyright (c) 2018, Adafruit Industries (adafruit.com) - - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -/******************************************************************************/ - -#ifndef _COMPILER_MACRO_H_ -#define _COMPILER_MACRO_H_ - -#include "common/tusb_compiler.h" - -#define ATTR_PACKED TU_ATTR_PACKED -#define ATTR_WEAK TU_ATTR_WEAK - -#endif /* _COMPILER_MACRO_H_ */ - diff --git a/cores/nRF5/delay.c b/cores/nRF5/delay.c index 73c68e1d..87d4ed24 100644 --- a/cores/nRF5/delay.c +++ b/cores/nRF5/delay.c @@ -25,6 +25,8 @@ extern "C" { #endif +uint32_t tud_cdc_n_write_flush(uint8_t itf) ATTR_WEAK; + uint32_t millis( void ) { return tick2ms(xTaskGetTickCount()); @@ -37,7 +39,7 @@ void delay( uint32_t ms ) #ifdef USE_TINYUSB // Take chance to flush usb cdc uint32_t flush_tick = xTaskGetTickCount(); - tud_cdc_write_flush(); + tud_cdc_n_write_flush(0); flush_tick = xTaskGetTickCount()-flush_tick; if (flush_tick >= ticks) return; diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index a9e78844..971335ac 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -15,10 +15,17 @@ #define ARDUINO_MAIN #include "Arduino.h" + #if (CFG_LOGGER == 2) #include #endif +// From the UI, setting debug level to 3 will enable SysView +#if CFG_SYSVIEW +#include "SEGGER_RTT.h" +#include "SEGGER_SYSVIEW.h" +#endif + // DEBUG Level 1 #if CFG_DEBUG // weak function to avoid compilation error with @@ -27,27 +34,33 @@ void Bluefruit_printInfo() __attribute__((weak)); void Bluefruit_printInfo() {} #endif -// From the UI, setting debug level to 3 will enable SysView -#if CFG_SYSVIEW -#include "SEGGER_RTT.h" -#include "SEGGER_SYSVIEW.h" -#endif - -static TaskHandle_t _loopHandle; - - // Weak empty variant initialization function. // May be redefined by variant files. void initVariant() __attribute__((weak)); void initVariant() { } +#ifdef USE_TINYUSB +// Called by main.cpp to initialize usb device typically with CDC device for Serial +void TinyUSB_Device_Init(uint8_t rhport) __attribute__((weak)); +#endif + + #define LOOP_STACK_SZ (256*4) #define CALLBACK_STACK_SZ (256*3) +static TaskHandle_t _loopHandle; + static void loop_task(void* arg) { (void) arg; +#ifdef USE_TINYUSB + TinyUSB_Device_Init(0); + + // yield for tinyusb task to run + taskYIELD(); +#endif + #if CFG_DEBUG // If Serial is not begin(), call it to avoid hard fault if(!Serial) Serial.begin(115200); @@ -80,12 +93,8 @@ 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); + xTaskCreate(loop_task, "loop", LOOP_STACK_SZ, NULL, TASK_PRIO_LOW, &_loopHandle); // Initialize callback task ada_callback_init(CALLBACK_STACK_SZ); diff --git a/cores/nRF5/rtos.cpp b/cores/nRF5/rtos.cpp index 2dc052ca..c1a44e0b 100644 --- a/cores/nRF5/rtos.cpp +++ b/cores/nRF5/rtos.cpp @@ -75,7 +75,7 @@ extern "C" void yield(void) { #ifdef USE_TINYUSB - tud_cdc_write_flush(); + Serial.flush(); #endif taskYIELD(); diff --git a/cores/nRF5/verify.h b/cores/nRF5/verify.h index 8c25c42d..b4d028a8 100644 --- a/cores/nRF5/verify.h +++ b/cores/nRF5/verify.h @@ -40,18 +40,16 @@ #ifndef _VERIFY_H_ #define _VERIFY_H_ -#include "compiler_macro.h" +//--------------------------------------------------------------------+ +// Compile-time Assert +//--------------------------------------------------------------------+ +#define VERIFY_STATIC(const_expr) _Static_assert(const_expr, "Assert failed") #ifdef __cplusplus extern "C" { #endif -//--------------------------------------------------------------------+ -// Compile-time Assert -//--------------------------------------------------------------------+ -#define VERIFY_STATIC(const_expr) TU_VERIFY_STATIC(const_expr, "Assert failed") - //--------------------------------------------------------------------+ // VERIFY Helper //--------------------------------------------------------------------+ @@ -132,8 +130,6 @@ extern "C" */ #define VERIFY(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__) -// TODO VERIFY with final statement - #ifdef __cplusplus } #endif