From fc358eaf658d902dd3b813e874c80ebbec83a62e Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 6 May 2021 16:03:34 +0700 Subject: [PATCH 01/14] remove tinyusb core submodule, move tinyusb src to its own library to reduce dependency --- .gitmodules | 3 - cores/arduino/Arduino.h | 11 +- .../TinyUSB/Adafruit_TinyUSB_ArduinoCore | 1 - .../arduino/TinyUSB/Adafruit_TinyUSB_SAMD.cpp | 195 ------------------ cores/arduino/TinyUSB/Adafruit_USBD_CDC.h | 73 +++++++ .../arduino/TinyUSB/Adafruit_USBD_Interface.h | 45 ++++ cores/arduino/main.cpp | 18 +- variants/circuitplay/variant.h | 7 +- 8 files changed, 132 insertions(+), 221 deletions(-) delete mode 160000 cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore delete mode 100644 cores/arduino/TinyUSB/Adafruit_TinyUSB_SAMD.cpp create mode 100644 cores/arduino/TinyUSB/Adafruit_USBD_CDC.h create mode 100644 cores/arduino/TinyUSB/Adafruit_USBD_Interface.h diff --git a/.gitmodules b/.gitmodules index 293215518..e69de29bb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore"] - path = cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore - url = https://github.com/adafruit/Adafruit_TinyUSB_ArduinoCore.git diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 9bc114f69..3e8e04eee 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -150,12 +150,13 @@ void loop( void ) ; // USB #ifdef USE_TINYUSB -#include "Adafruit_TinyUSB_Core.h" + #include "Adafruit_USBD_Interface.h" + #include "Adafruit_USBD_CDC.h" #else -#include "USB/USBDesc.h" -#include "USB/USBCore.h" -#include "USB/USBAPI.h" -#include "USB/USB_host.h" + #include "USB/USBDesc.h" + #include "USB/USBCore.h" + #include "USB/USBAPI.h" + #include "USB/USB_host.h" #endif #endif // Arduino_h diff --git a/cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore b/cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore deleted file mode 160000 index e7b892095..000000000 --- a/cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e7b892095f2bb5d8bef6a748238369bdd268ed5e diff --git a/cores/arduino/TinyUSB/Adafruit_TinyUSB_SAMD.cpp b/cores/arduino/TinyUSB/Adafruit_TinyUSB_SAMD.cpp deleted file mode 100644 index 867eac926..000000000 --- a/cores/arduino/TinyUSB/Adafruit_TinyUSB_SAMD.cpp +++ /dev/null @@ -1,195 +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 "Arduino.h" -#include "Adafruit_TinyUSB_Core.h" -#include // Needed for auto-reset with 1200bps port touch - -//--------------------------------------------------------------------+ -// Forward USB interrupt events to TinyUSB IRQ Handler -//--------------------------------------------------------------------+ -extern "C" -{ -#if defined(__SAMD51__) - -void USB_0_Handler (void) { tud_int_handler(0); } -void USB_1_Handler (void) { tud_int_handler(0); } -void USB_2_Handler (void) { tud_int_handler(0); } -void USB_3_Handler (void) { tud_int_handler(0); } - -#else - -void USB_Handler(void) { tud_int_handler(0); } - -#endif -} // extern C - - - -//--------------------------------------------------------------------+ -// MACRO TYPEDEF CONSTANT ENUM DECLARATION -//--------------------------------------------------------------------+ -static void usb_hardware_init(void); - -#if CFG_TUSB_DEBUG -extern "C" int serial1_printf(const char *__restrict format, ...) -{ - char buf[PRINTF_BUF]; - va_list ap; - va_start(ap, format); - vsnprintf(buf, sizeof(buf), format, ap); - Serial1.write(buf); - va_end(ap); - -} -#endif - -//--------------------------------------------------------------------+ -// Core Init & Touch1200 -//--------------------------------------------------------------------+ -void Adafruit_TinyUSB_Core_init(void) -{ -#if CFG_TUSB_DEBUG - Serial1.begin(115200); - serial1_printf("TinyUSB debugging with Serial1\n"); -#endif - - Serial.setStringDescriptor("TinyUSB Serial"); - USBDevice.addInterface(Serial); - USBDevice.setID(USB_VID, USB_PID); - USBDevice.begin(); - - usb_hardware_init(); - - // Init tinyusb stack - tusb_init(); -} - -void Adafruit_TinyUSB_Core_touch1200(void) -{ - initiateReset(250); -} - -//--------------------------------------------------------------------+ -// Adafruit_USBD_Device platform dependent -//--------------------------------------------------------------------+ - -uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t* serial_str) -{ - enum { SERIAL_BYTE_LEN = 16 }; - -#ifdef __SAMD51__ - uint32_t* id_addresses[4] = {(uint32_t *) 0x008061FC, (uint32_t *) 0x00806010, - (uint32_t *) 0x00806014, (uint32_t *) 0x00806018}; -#else // samd21 - uint32_t* id_addresses[4] = {(uint32_t *) 0x0080A00C, (uint32_t *) 0x0080A040, - (uint32_t *) 0x0080A044, (uint32_t *) 0x0080A048}; - -#endif - - uint8_t raw_id[SERIAL_BYTE_LEN]; - - for (int i=0; i<4; i++) { - for (int k=0; k<4; k++) { - raw_id[4 * i + (3 - k)] = (*(id_addresses[i]) >> k * 8) & 0xff; - } - } - - static const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - - for (unsigned int i = 0; i < sizeof(raw_id); i++) { - for (int j = 0; j < 2; j++) { - uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf; - // Strings are UTF-16-LE encoded. - serial_str[i * 2 + (1 - j)] = nibble_to_hex[nibble]; - } - } - - return sizeof(raw_id)*2; -} - -//--------------------------------------------------------------------+ -// Helpers -//--------------------------------------------------------------------+ - -// Init usb hardware when starting up. Softdevice is not enabled yet -static void usb_hardware_init(void) -{ -#ifdef PIN_LED_TXL -// txLEDPulse = 0; - pinMode(PIN_LED_TXL, OUTPUT); - digitalWrite(PIN_LED_TXL, HIGH); -#endif - -#ifdef PIN_LED_RXL -// rxLEDPulse = 0; - pinMode(PIN_LED_RXL, OUTPUT); - digitalWrite(PIN_LED_RXL, HIGH); -#endif - - /* Enable USB clock */ -#if defined(__SAMD51__) - MCLK->APBBMASK.reg |= MCLK_APBBMASK_USB; - MCLK->AHBMASK.reg |= MCLK_AHBMASK_USB; - - // Set up the USB DP/DN pins - PORT->Group[0].PINCFG[PIN_PA24H_USB_DM].bit.PMUXEN = 1; - PORT->Group[0].PMUX[PIN_PA24H_USB_DM/2].reg &= ~(0xF << (4 * (PIN_PA24H_USB_DM & 0x01u))); - PORT->Group[0].PMUX[PIN_PA24H_USB_DM/2].reg |= MUX_PA24H_USB_DM << (4 * (PIN_PA24H_USB_DM & 0x01u)); - PORT->Group[0].PINCFG[PIN_PA25H_USB_DP].bit.PMUXEN = 1; - PORT->Group[0].PMUX[PIN_PA25H_USB_DP/2].reg &= ~(0xF << (4 * (PIN_PA25H_USB_DP & 0x01u))); - PORT->Group[0].PMUX[PIN_PA25H_USB_DP/2].reg |= MUX_PA25H_USB_DP << (4 * (PIN_PA25H_USB_DP & 0x01u)); - - - GCLK->PCHCTRL[USB_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos); - - NVIC_SetPriority(USB_0_IRQn, 0UL); - NVIC_SetPriority(USB_1_IRQn, 0UL); - NVIC_SetPriority(USB_2_IRQn, 0UL); - NVIC_SetPriority(USB_3_IRQn, 0UL); -#else - PM->APBBMASK.reg |= PM_APBBMASK_USB; - - // Set up the USB DP/DN pins - PORT->Group[0].PINCFG[PIN_PA24G_USB_DM].bit.PMUXEN = 1; - PORT->Group[0].PMUX[PIN_PA24G_USB_DM/2].reg &= ~(0xF << (4 * (PIN_PA24G_USB_DM & 0x01u))); - PORT->Group[0].PMUX[PIN_PA24G_USB_DM/2].reg |= MUX_PA24G_USB_DM << (4 * (PIN_PA24G_USB_DM & 0x01u)); - PORT->Group[0].PINCFG[PIN_PA25G_USB_DP].bit.PMUXEN = 1; - PORT->Group[0].PMUX[PIN_PA25G_USB_DP/2].reg &= ~(0xF << (4 * (PIN_PA25G_USB_DP & 0x01u))); - PORT->Group[0].PMUX[PIN_PA25G_USB_DP/2].reg |= MUX_PA25G_USB_DP << (4 * (PIN_PA25G_USB_DP & 0x01u)); - - // Put Generic Clock Generator 0 as source for Generic Clock Multiplexer 6 (USB reference) - GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(6) | // Generic Clock Multiplexer 6 - GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source - GCLK_CLKCTRL_CLKEN; - while (GCLK->STATUS.bit.SYNCBUSY) - ; - - NVIC_SetPriority((IRQn_Type) USB_IRQn, 0UL); -#endif -} - -#endif // USE_TINYUSB diff --git a/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h b/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h new file mode 100644 index 000000000..23e404508 --- /dev/null +++ b/cores/arduino/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/arduino/TinyUSB/Adafruit_USBD_Interface.h b/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h new file mode 100644 index 000000000..4e8f62cb0 --- /dev/null +++ b/cores/arduino/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/arduino/main.cpp b/cores/arduino/main.cpp index 10db6a9af..19bbd8222 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -24,6 +24,11 @@ void initVariant() __attribute__((weak)); void initVariant() { } +#ifdef USE_TINYUSB +// Called by main.cpp to initialize usb device typically with CDC device for Serial +void Adafruit_TinyUSB_Device_init(uint8_t rhport) __attribute__((weak)); +#endif + // Initialize C library extern "C" void __libc_init_array(void); @@ -41,7 +46,7 @@ int main( void ) delay(1); #if defined(USE_TINYUSB) - Adafruit_TinyUSB_Core_init(); + Adafruit_TinyUSB_Device_init(0); #elif defined(USBCON) USBDevice.init(); USBDevice.attach(); @@ -59,14 +64,3 @@ int main( void ) return 0; } - -#if defined(USE_TINYUSB) - -// run TinyUSB background task when yield() -extern "C" void yield(void) -{ - tud_task(); - tud_cdc_write_flush(); -} - -#endif diff --git a/variants/circuitplay/variant.h b/variants/circuitplay/variant.h index 70deefcf9..cb8a19915 100644 --- a/variants/circuitplay/variant.h +++ b/variants/circuitplay/variant.h @@ -242,10 +242,7 @@ extern Uart Serial1; // // SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX // pins are NOT connected to anything by default. -#define SERIAL_PORT_USBVIRTUAL SerialUSB -#define SERIAL_PORT_MONITOR SerialUSB +#define SERIAL_PORT_USBVIRTUAL Serial +#define SERIAL_PORT_MONITOR Serial #define SERIAL_PORT_HARDWARE Serial1 #define SERIAL_PORT_HARDWARE_OPEN Serial1 - -// Alias Serial to SerialUSB -#define Serial SerialUSB From d3091df758967b2ca464a337169dffba48613854 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 6 May 2021 21:47:26 +0700 Subject: [PATCH 02/14] change porting API --- cores/arduino/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index 19bbd8222..cb96f8b6f 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -26,7 +26,7 @@ void initVariant() { } #ifdef USE_TINYUSB // Called by main.cpp to initialize usb device typically with CDC device for Serial -void Adafruit_TinyUSB_Device_init(uint8_t rhport) __attribute__((weak)); +void TinyUSB_Device_init(uint8_t rhport) __attribute__((weak)); #endif // Initialize C library @@ -46,7 +46,7 @@ int main( void ) delay(1); #if defined(USE_TINYUSB) - Adafruit_TinyUSB_Device_init(0); + TinyUSB_Device_init(0); #elif defined(USBCON) USBDevice.init(); USBDevice.attach(); From 6fd7609c6eeb35111c14aca03321aec5dff1afef Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 7 May 2021 00:15:08 +0700 Subject: [PATCH 03/14] add feather m4 can to ci --- .github/workflows/githubci.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index dc6b31a01..283c88cae 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -7,8 +7,18 @@ jobs: strategy: fail-fast: false matrix: - arduino-platform: ['metro_m0', 'hallowing', 'circuitplayground_m0', - 'metro_m4', 'pybadge_m4', 'pygamer_m4', 'hallowing_m4', 'pyportal_m4', 'pyportal_m4_titano'] + arduino-platform: + # Alphabetical order + - 'metro_m0' + - 'hallowing' + - 'circuitplayground_m0' + - 'metro_m4' + - 'pybadge_m4' + - 'pygamer_m4' + - 'hallowing_m4' + - 'pyportal_m4' + - 'pyportal_m4_titano' + - 'feather_m4_can' runs-on: ubuntu-latest From 1f029ece68e26e206cd80015807e5cc1fd759b70 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 7 May 2021 00:30:15 +0700 Subject: [PATCH 04/14] fix typo --- cores/arduino/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index cb96f8b6f..647f42fe5 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -26,7 +26,7 @@ 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)); +void TinyUSB_Device_Init(uint8_t rhport) __attribute__((weak)); #endif // Initialize C library @@ -46,7 +46,7 @@ int main( void ) delay(1); #if defined(USE_TINYUSB) - TinyUSB_Device_init(0); + TinyUSB_Device_Init(0); #elif defined(USBCON) USBDevice.init(); USBDevice.attach(); From 88520ba975b5e211f213ebaf65b865f6939cf199 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 7 May 2021 14:49:02 +0700 Subject: [PATCH 05/14] update tinyusb header --- cores/arduino/TinyUSB/Adafruit_USBD_CDC.h | 8 ++++++-- cores/arduino/TinyUSB/Adafruit_USBD_Interface.h | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h b/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h index 23e404508..16735eb53 100644 --- a/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h +++ b/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h @@ -36,10 +36,10 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface Adafruit_USBD_CDC(void); // fron Adafruit_USBD_Interface - virtual uint16_t getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize); + virtual uint16_t getInterfaceDescriptor(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); void begin(uint32_t baud, uint8_t config); void end(void); @@ -65,6 +65,10 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface virtual int availableForWrite(void); using Print::write; // pull in write(str) from Print operator bool(); + +private: + bool _begun; + uint8_t _itf; }; extern Adafruit_USBD_CDC Serial; diff --git a/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h b/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h index 4e8f62cb0..45672f271 100644 --- a/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h +++ b/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h @@ -35,7 +35,10 @@ class Adafruit_USBD_Interface public: Adafruit_USBD_Interface(void) { _desc_str = NULL; } - virtual uint16_t getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize) = 0; + // Get Interface Descriptor + // Device fill descriptor and return its length + virtual uint16_t getInterfaceDescriptor(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; } }; From 15fd604313558ad04343276b7a390d89d1833942 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 7 May 2021 17:30:00 +0700 Subject: [PATCH 06/14] clean up --- cores/arduino/Arduino.h | 2 ++ cores/arduino/TinyUSB/Adafruit_USBD_CDC.h | 3 --- cores/arduino/TinyUSB/Adafruit_USBD_Interface.h | 4 ---- platform.txt | 2 +- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 3e8e04eee..ba28136cf 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -150,8 +150,10 @@ void loop( void ) ; // USB #ifdef USE_TINYUSB +#ifdef __cplusplus #include "Adafruit_USBD_Interface.h" #include "Adafruit_USBD_CDC.h" +#endif #else #include "USB/USBDesc.h" #include "USB/USBCore.h" diff --git a/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h b/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h index 16735eb53..3a2aa8a80 100644 --- a/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h +++ b/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h @@ -25,8 +25,6 @@ #ifndef ADAFRUIT_USBD_CDC_H_ #define ADAFRUIT_USBD_CDC_H_ -#ifdef __cplusplus - #include "Adafruit_USBD_Interface.h" #include "Stream.h" @@ -73,5 +71,4 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface extern Adafruit_USBD_CDC Serial; -#endif // __cplusplus #endif diff --git a/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h b/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h index 45672f271..ac9d34d74 100644 --- a/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h +++ b/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h @@ -25,8 +25,6 @@ #ifndef ADAFRUIT_USBD_INTERFACE_H_ #define ADAFRUIT_USBD_INTERFACE_H_ -#ifdef __cplusplus - class Adafruit_USBD_Interface { protected: @@ -44,5 +42,3 @@ class Adafruit_USBD_Interface }; #endif - -#endif diff --git a/platform.txt b/platform.txt index aac6b6252..a6c731c25 100644 --- a/platform.txt +++ b/platform.txt @@ -77,7 +77,7 @@ compiler.libraries.ldflags= # USB Flags # --------- -build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} -DUSBCON -DUSB_CONFIG_POWER={build.usb_power} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' {build.flags.usbstack} {build.flags.debug} "-I{build.core.path}/TinyUSB" "-I{build.core.path}/TinyUSB/Adafruit_TinyUSB_ArduinoCore" "-I{build.core.path}/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src" +build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} -DUSBCON -DUSB_CONFIG_POWER={build.usb_power} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' {build.flags.usbstack} {build.flags.debug} "-I{build.core.path}/TinyUSB" # Default advertised device power setting in mA build.usb_power=100 From 704450cc1c53b977e5a93c0f5305b20f123fed6b Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 8 May 2021 00:56:20 +0700 Subject: [PATCH 07/14] add libraries/Adafruit_TinyUSB_Arduino as submodule include Adafruit_USBD_CDC.h from submodule --- .gitignore | 3 +- .gitmodules | 3 + cores/arduino/Arduino.h | 1 + cores/arduino/TinyUSB/Adafruit_USBD_CDC.h | 74 ------------------- .../arduino/TinyUSB/Adafruit_USBD_Interface.h | 44 ----------- cores/arduino/{TinyUSB => }/tusb_config.h | 0 extras/build_all.py | 2 +- libraries/Adafruit_TinyUSB_Arduino | 1 + platform.txt | 2 +- 9 files changed, 9 insertions(+), 121 deletions(-) delete mode 100644 cores/arduino/TinyUSB/Adafruit_USBD_CDC.h delete mode 100644 cores/arduino/TinyUSB/Adafruit_USBD_Interface.h rename cores/arduino/{TinyUSB => }/tusb_config.h (100%) create mode 160000 libraries/Adafruit_TinyUSB_Arduino diff --git a/.gitignore b/.gitignore index 22b823b91..ef8f29944 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.atsuo bootloaders/*/build/ -*~ \ No newline at end of file +*~ +/libraries/**/build/ diff --git a/.gitmodules b/.gitmodules index e69de29bb..5a5336ccf 100644 --- a/.gitmodules +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libraries/Adafruit_TinyUSB_Arduino"] + path = libraries/Adafruit_TinyUSB_Arduino + url = https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index ba28136cf..99b1b95d4 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -151,6 +151,7 @@ void loop( void ) ; // USB #ifdef USE_TINYUSB #ifdef __cplusplus + // Needed for declaring Serial #include "Adafruit_USBD_Interface.h" #include "Adafruit_USBD_CDC.h" #endif diff --git a/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h b/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h deleted file mode 100644 index 3a2aa8a80..000000000 --- a/cores/arduino/TinyUSB/Adafruit_USBD_CDC.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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_ - -#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 getInterfaceDescriptor(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); - 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(); - -private: - bool _begun; - uint8_t _itf; -}; - -extern Adafruit_USBD_CDC Serial; - -#endif diff --git a/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h b/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h deleted file mode 100644 index ac9d34d74..000000000 --- a/cores/arduino/TinyUSB/Adafruit_USBD_Interface.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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_ - -class Adafruit_USBD_Interface -{ - protected: - const char* _desc_str; - - public: - Adafruit_USBD_Interface(void) { _desc_str = NULL; } - - // Get Interface Descriptor - // Device fill descriptor and return its length - virtual uint16_t getInterfaceDescriptor(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 diff --git a/cores/arduino/TinyUSB/tusb_config.h b/cores/arduino/tusb_config.h similarity index 100% rename from cores/arduino/TinyUSB/tusb_config.h rename to cores/arduino/tusb_config.h diff --git a/extras/build_all.py b/extras/build_all.py index 4e701ace9..915feecda 100644 --- a/extras/build_all.py +++ b/extras/build_all.py @@ -27,7 +27,7 @@ metavar='board', nargs='*', help='list of boards to be built -- Note that the fqbn is created by prepending "{}"'.format(FQBN_PREFIX), - default= [ 'metro_m0', 'metro_m4', 'circuitplayground_m0' ] + default= [ 'metro_m0', 'metro_m4', 'circuitplayground_m0', 'feather_m4_can' ] ) args = parser.parse_args() diff --git a/libraries/Adafruit_TinyUSB_Arduino b/libraries/Adafruit_TinyUSB_Arduino new file mode 160000 index 000000000..2b16928b9 --- /dev/null +++ b/libraries/Adafruit_TinyUSB_Arduino @@ -0,0 +1 @@ +Subproject commit 2b16928b90a764b1e0b5b135d2ebad1518a329b2 diff --git a/platform.txt b/platform.txt index a6c731c25..a9783bf5d 100644 --- a/platform.txt +++ b/platform.txt @@ -77,7 +77,7 @@ compiler.libraries.ldflags= # USB Flags # --------- -build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} -DUSBCON -DUSB_CONFIG_POWER={build.usb_power} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' {build.flags.usbstack} {build.flags.debug} "-I{build.core.path}/TinyUSB" +build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} -DUSBCON -DUSB_CONFIG_POWER={build.usb_power} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' {build.flags.usbstack} {build.flags.debug} "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino" # Default advertised device power setting in mA build.usb_power=100 From 30d07382623d019f72ce13ae856e7d680ec806b9 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 May 2021 12:22:18 +0700 Subject: [PATCH 08/14] sync with tinyusb lib latest change --- cores/arduino/Arduino.h | 1 - cores/arduino/delay.c | 12 ++++++++++++ cores/arduino/main.cpp | 5 ----- libraries/Adafruit_TinyUSB_Arduino | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 99b1b95d4..5a2799eda 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -152,7 +152,6 @@ void loop( void ) ; #ifdef USE_TINYUSB #ifdef __cplusplus // Needed for declaring Serial - #include "Adafruit_USBD_Interface.h" #include "Adafruit_USBD_CDC.h" #endif #else diff --git a/cores/arduino/delay.c b/cores/arduino/delay.c index 7833512a6..e4e85d65b 100644 --- a/cores/arduino/delay.c +++ b/cores/arduino/delay.c @@ -117,6 +117,18 @@ void SysTick_DefaultHandler(void) tickReset(); } +#if defined(USE_TINYUSB) + +#include "Adafruit_TinyUSB_API.h" + +// run TinyUSB background task when yield() +void yield(void) +{ + TinyUSB_Device_Task(); +} + +#endif + #ifdef __cplusplus } #endif diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index 647f42fe5..3ea8ad3b3 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -24,11 +24,6 @@ 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 - // Initialize C library extern "C" void __libc_init_array(void); diff --git a/libraries/Adafruit_TinyUSB_Arduino b/libraries/Adafruit_TinyUSB_Arduino index 2b16928b9..49ac37232 160000 --- a/libraries/Adafruit_TinyUSB_Arduino +++ b/libraries/Adafruit_TinyUSB_Arduino @@ -1 +1 @@ -Subproject commit 2b16928b90a764b1e0b5b135d2ebad1518a329b2 +Subproject commit 49ac37232d536b0a3ff2fb72ece4613d4720efad From 3c173d5d191e7a52bf810c7a46818283bc7d18a6 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 May 2021 12:50:05 +0700 Subject: [PATCH 09/14] update with TinyUSB_Device_FlushCDC() --- cores/arduino/Arduino.h | 2 -- cores/arduino/delay.c | 3 +-- libraries/Adafruit_TinyUSB_Arduino | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 5a2799eda..175dcf705 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -150,10 +150,8 @@ void loop( void ) ; // USB #ifdef USE_TINYUSB -#ifdef __cplusplus // Needed for declaring Serial #include "Adafruit_USBD_CDC.h" -#endif #else #include "USB/USBDesc.h" #include "USB/USBCore.h" diff --git a/cores/arduino/delay.c b/cores/arduino/delay.c index e4e85d65b..773ce39e9 100644 --- a/cores/arduino/delay.c +++ b/cores/arduino/delay.c @@ -119,12 +119,11 @@ void SysTick_DefaultHandler(void) #if defined(USE_TINYUSB) -#include "Adafruit_TinyUSB_API.h" - // run TinyUSB background task when yield() void yield(void) { TinyUSB_Device_Task(); + TinyUSB_Device_FlushCDC(); } #endif diff --git a/libraries/Adafruit_TinyUSB_Arduino b/libraries/Adafruit_TinyUSB_Arduino index 49ac37232..d80d5796e 160000 --- a/libraries/Adafruit_TinyUSB_Arduino +++ b/libraries/Adafruit_TinyUSB_Arduino @@ -1 +1 @@ -Subproject commit 49ac37232d536b0a3ff2fb72ece4613d4720efad +Subproject commit d80d5796edf791f0d679ccc4ed7c049ff6577392 From 7234390bc166d53d4c47e3ab3775503b10a08dea Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 May 2021 17:16:42 +0700 Subject: [PATCH 10/14] sync tinyusb lib --- libraries/Adafruit_TinyUSB_Arduino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Adafruit_TinyUSB_Arduino b/libraries/Adafruit_TinyUSB_Arduino index d80d5796e..8f58b130c 160000 --- a/libraries/Adafruit_TinyUSB_Arduino +++ b/libraries/Adafruit_TinyUSB_Arduino @@ -1 +1 @@ -Subproject commit d80d5796edf791f0d679ccc4ed7c049ff6577392 +Subproject commit 8f58b130c94230651df47fd4f1d73b6c38d4f131 From c262dc2ff98f79793c402472a74d56b068dad849 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 May 2021 19:06:44 +0700 Subject: [PATCH 11/14] clean up and add note for "-Wclass-memaccess" --- libraries/SPI/SPI.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp index ba8122815..5a321b566 100644 --- a/libraries/SPI/SPI.cpp +++ b/libraries/SPI/SPI.cpp @@ -332,21 +332,25 @@ void SPIClass::dmaAllocate(void) { totalDescriptors * sizeof(DmacDescriptor)))) { use_dma = true; // Everything allocated successfully extraWriteDescriptors = &extraReadDescriptors[numReadDescriptors]; + + // dmac.h didn't include extern "C" which cause + // DmacDescriptor and its members are defined as C++ struct therefore + // memcpy will throw warning on copying where simple assignment won't work + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wclass-memaccess" + // Initialize descriptors (copy from first ones) for(int i=0; i Date: Mon, 10 May 2021 19:11:44 +0700 Subject: [PATCH 12/14] fix typo in tinyusb lib --- libraries/Adafruit_TinyUSB_Arduino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Adafruit_TinyUSB_Arduino b/libraries/Adafruit_TinyUSB_Arduino index 8f58b130c..a4a93035c 160000 --- a/libraries/Adafruit_TinyUSB_Arduino +++ b/libraries/Adafruit_TinyUSB_Arduino @@ -1 +1 @@ -Subproject commit 8f58b130c94230651df47fd4f1d73b6c38d4f131 +Subproject commit a4a93035c68e14c13b9a89e6af21ea7c509efb1e From a7bab5b5916eca74d3944e64d15f705526c85c1b Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 18 May 2021 17:35:49 +0700 Subject: [PATCH 13/14] update tinyusb lib to 1.0.0 --- libraries/Adafruit_TinyUSB_Arduino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Adafruit_TinyUSB_Arduino b/libraries/Adafruit_TinyUSB_Arduino index a4a93035c..1b8b9c9b0 160000 --- a/libraries/Adafruit_TinyUSB_Arduino +++ b/libraries/Adafruit_TinyUSB_Arduino @@ -1 +1 @@ -Subproject commit a4a93035c68e14c13b9a89e6af21ea7c509efb1e +Subproject commit 1b8b9c9b071065235341d0e503f2d6d05de19524 From 461f8559402f36daf605e41a1ace69e115df13dc Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 18 May 2021 21:24:48 +0700 Subject: [PATCH 14/14] ci skip tinyusb lib examples for now --- extras/build_all.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extras/build_all.py b/extras/build_all.py index 915feecda..ad9742d78 100644 --- a/extras/build_all.py +++ b/extras/build_all.py @@ -59,6 +59,10 @@ def build_examples(variant: str): fqbn = "{}{}".format(FQBN_PREFIX, variant) for sketch in glob.iglob('libraries/**/*.ino', recursive=True): + # TODO skip TinyUSB library examples for now + if "libraries/Adafruit_TinyUSB_Arduino" in sketch: + continue + start_time = time.monotonic() # Skip if contains: ".board.test.skip" or ".all.test.skip"