diff --git a/README.md b/README.md index 5b346b2a..b6497116 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ Note: For ESP32 port, version before v3.0 requires all descriptors must be speci Following is cores without built-in support - **mbed_rp2040** +- **[stm32duino/Arduino_Core_STM32](https://github.com/stm32duino/Arduino_Core_STM32)** + - Still WIP, only support/tested with F4, only support OTG_FS It is still possible to use TinyUSB but with some limits such as: diff --git a/src/arduino/ports/stm32/Adafruit_TinyUSB_stm32.cpp b/src/arduino/ports/stm32/Adafruit_TinyUSB_stm32.cpp new file mode 100644 index 00000000..f40a2a93 --- /dev/null +++ b/src/arduino/ports/stm32/Adafruit_TinyUSB_stm32.cpp @@ -0,0 +1,100 @@ +/* + * 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. + */ + +#include "tusb_option.h" + +#if (defined(ARDUINO_ARCH_STM32) || \ + defined(ARDUINO_ARCH_ARDUINO_CORE_STM32)) && \ + CFG_TUD_ENABLED + +#define USE_HAL_DRIVER +#include "Arduino.h" +#include "arduino/Adafruit_TinyUSB_API.h" +#include "stm32f4xx_hal.h" +#include "stm32f4xx_hal_rcc.h" +#include "tusb.h" + +//--------------------------------------------------------------------+ +// Forward USB interrupt events to TinyUSB IRQ Handler +//--------------------------------------------------------------------+ +extern "C" { + +void OTG_FS_IRQHandler(void) { tud_int_handler(0); } + +void yield(void) { + tud_task(); + if (tud_cdc_connected()) { + tud_cdc_write_flush(); + } +} + +} // extern "C" + +//--------------------------------------------------------------------+ +// Porting API +//--------------------------------------------------------------------+ +void TinyUSB_Port_InitDevice(uint8_t rhport) { + (void)rhport; + + // Enable clocks FIRST + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + + // Configure USB pins (PA11 = DM, PA12 = DP) + GPIO_InitTypeDef GPIO_InitStruct = {}; + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + // Enable USB IRQ + NVIC_SetPriority(OTG_FS_IRQn, 0); + NVIC_EnableIRQ(OTG_FS_IRQn); + + // Disable VBUS sensing (we're bus-powered, don't need it) + USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS; + USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN; + USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN; + + // Initialize TinyUSB device stack + tud_init(rhport); +} + +void TinyUSB_Port_EnterDFU(void) { + // Optional - implement bootloader entry if needed +} + +uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) { + volatile uint32_t *uid = + (volatile uint32_t *)0x1FFF7A10; // STM32F411 UID base + uint32_t *serial_32 = (uint32_t *)serial_id; + serial_32[0] = uid[0]; + serial_32[1] = uid[1]; + serial_32[2] = uid[2]; + return 12; +} + +#endif // ARDUINO_ARCH_STM32 \ No newline at end of file diff --git a/src/arduino/ports/stm32/tusb_config_stm32.h b/src/arduino/ports/stm32/tusb_config_stm32.h new file mode 100644 index 00000000..9b719450 --- /dev/null +++ b/src/arduino/ports/stm32/tusb_config_stm32.h @@ -0,0 +1,76 @@ +/* + * 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. + */ + +#ifndef TUSB_CONFIG_STM32_H_ +#define TUSB_CONFIG_STM32_H_ + +//-------------------------------------------------------------------- +// COMMON CONFIGURATION +//-------------------------------------------------------------------- +// MCU / OS +#define CFG_TUSB_MCU OPT_MCU_STM32F4 +#define CFG_TUSB_OS OPT_OS_NONE + +// Debug +#ifndef CFG_TUSB_DEBUG +#define CFG_TUSB_DEBUG 0 +#endif + +#define CFG_TUSB_MEM_SECTION +#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4))) + +//-------------------------------------------------------------------- +// DEVICE CONFIGURATION +//-------------------------------------------------------------------- +#define CFG_TUD_ENABLED 1 +#define CFG_TUD_MAX_SPEED OPT_MODE_FULL_SPEED // TODO some are highspeed +#define CFG_TUD_ENDPOINT0_SIZE 64 + +// Classes +#define CFG_TUD_CDC 1 +#define CFG_TUD_MSC 1 +#define CFG_TUD_HID 1 +#define CFG_TUD_MIDI 1 +#define CFG_TUD_VENDOR 1 + +// Buffer sizes +#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) +#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) +#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) +#define CFG_TUD_MSC_EP_BUFSIZE 512 +#define CFG_TUD_HID_EP_BUFSIZE 64 +#define CFG_TUD_MIDI_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 128) +#define CFG_TUD_MIDI_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 128) +#define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) +#define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) + +// Serial Redirect +#define Serial SerialTinyUSB + +//-------------------------------------------------------------------- +// Host Configuration +//-------------------------------------------------------------------- +#define CFG_TUH_ENABLED 0 // disable for now + +#endif diff --git a/src/tusb_config.h b/src/tusb_config.h index df0752a7..42273e0d 100644 --- a/src/tusb_config.h +++ b/src/tusb_config.h @@ -43,6 +43,10 @@ #elif defined(ARDUINO_ARCH_CH32) || defined(CH32V20x) || defined(CH32V30x) #include "arduino/ports/ch32/tusb_config_ch32.h" + +#elif defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_ARDUINO_CORE_STM32) + #include "arduino/ports/stm32/tusb_config_stm32.h" + #else #error TinyUSB Arduino Library does not support your core yet #endif