Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
566 changes: 566 additions & 0 deletions CI/update/stm32tinyusb.py

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions CI/update/templates/mw-tinyusb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifdef USE_TINYUSB

#include "{{ include_path }}"

#endif /* USE_TINYUSB */
15 changes: 14 additions & 1 deletion boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15774,13 +15774,26 @@ Phoque.menu.xserial.disabled=Disabled (no Serial support)
Phoque.menu.xserial.disabled.build.xSerial=

# USB connectivity

# Global menu titles
menu.usb=USB connectivity
menu.usbstack=USB stack
menu.xusb=USB speed

Nucleo_144.menu.usb.none=None
Nucleo_144.menu.usb.CDCgen=CDC (generic 'Serial' supersede U(S)ART)
Nucleo_144.menu.usb.none.build.enable_usb=
Nucleo_144.menu.usb.CDCgen=CDC (generic 'Serial' supersedes U(S)ART)
Nucleo_144.menu.usb.CDCgen.build.enable_usb={build.usb_flags} -DUSBD_USE_CDC
Nucleo_144.menu.usb.CDC=CDC (no generic 'Serial')
Nucleo_144.menu.usb.CDC.build.enable_usb={build.usb_flags} -DUSBD_USE_CDC -DDISABLE_GENERIC_SERIALUSB
Nucleo_144.menu.usb.HID=HID (keyboard and mouse)
Nucleo_144.menu.usb.HID.build.enable_usb={build.usb_flags} -DUSBD_USE_HID_COMPOSITE
Nucleo_144.menu.usb.cdchid=CDC + HID (TinyUSB only)
Nucleo_144.menu.usb.cdchid.build.enable_usb={build.usb_flags} -DARDUINO_USB_MODE_CDC_HID
Nucleo_144.menu.usbstack.legacy=USBDevice (legacy)
Nucleo_144.menu.usbstack.legacy.build.usb_stack= -DUSBCON
Nucleo_144.menu.usbstack.tinyusb=TinyUSB
Nucleo_144.menu.usbstack.tinyusb.build.usb_stack= -DUSE_TINYUSB
Nucleo_144.menu.xusb.FS=Low/Full Speed
Nucleo_144.menu.xusb.HS=High Speed
Nucleo_144.menu.xusb.HS.build.usb_speed=-DUSE_USB_HS
Expand Down
1 change: 1 addition & 0 deletions cores/arduino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ add_library(core_bin STATIC EXCLUDE_FROM_ALL
wiring_time.c
WMath.cpp
WSerial.cpp
TinyUSB_Port_STM32.cpp
)
target_link_libraries(core_bin PUBLIC core_usage)

Expand Down
136 changes: 136 additions & 0 deletions cores/arduino/TinyUSB_Port_STM32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#if defined(USE_TINYUSB)

#include "Arduino.h"
#include "TinyUSB_API.h"

#ifndef TINYUSB_IRQ_PRIO
#ifdef USBD_IRQ_PRIO
#define TINYUSB_IRQ_PRIO USBD_IRQ_PRIO
#else
#define TINYUSB_IRQ_PRIO 1
#endif
#endif

#ifndef TINYUSB_IRQ_SUBPRIO
#ifdef USBD_IRQ_SUBPRIO
#define TINYUSB_IRQ_SUBPRIO USBD_IRQ_SUBPRIO
#else
#define TINYUSB_IRQ_SUBPRIO 1
#endif
#endif

// Sélec rapide du contrôleur USB selon les flags du core/menu
#if defined(USE_USB_HS)
#define TINYUSB_USE_HS 1
#define TINYUSB_RHPORT 1
#else
#define TINYUSB_USE_HS 0
#define TINYUSB_RHPORT 0
#endif

extern "C" {

// vrai symbole TinyUSB

Check failure on line 33 in cores/arduino/TinyUSB_Port_STM32.cpp

View workflow job for this annotation

GitHub Actions / Check for spelling errors

symbole ==> symbol
void tusb_int_handler(uint8_t rhport, bool in_isr);

static void tinyusb_port_init_fs(void) {
#if defined(USB_OTG_FS)
const PinMap *map = PinMap_USB_OTG_FS;
while (map != nullptr && map->pin != NC) {
pin_function(map->pin, map->function);
map++;
}

__HAL_RCC_USB_OTG_FS_CLK_ENABLE();

HAL_NVIC_SetPriority(OTG_FS_IRQn, TINYUSB_IRQ_PRIO, TINYUSB_IRQ_SUBPRIO);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);

USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
#endif
}

static void tinyusb_port_init_hs(void) {
#if defined(USB_OTG_HS)
const PinMap *map = PinMap_USB_OTG_HS;
while (map != nullptr && map->pin != NC) {
pin_function(map->pin, map->function);
map++;
}

__HAL_RCC_USB_OTG_HS_CLK_ENABLE();

#if !defined(USE_USB_HS_IN_FS)
#ifdef __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
#endif
#endif

HAL_NVIC_SetPriority(OTG_HS_IRQn, TINYUSB_IRQ_PRIO, TINYUSB_IRQ_SUBPRIO);
HAL_NVIC_EnableIRQ(OTG_HS_IRQn);

USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
#endif
}

void TinyUSB_Port_InitDevice(uint8_t rhport) {
(void) rhport;


if (TINYUSB_USE_HS) {
tinyusb_port_init_hs();
} else {
tinyusb_port_init_fs();
}
}

void TinyUSB_Port_EnterDFU(void) {
}

uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) {
uint32_t uid0 = HAL_GetUIDw0();
uint32_t uid1 = HAL_GetUIDw1();
uint32_t uid2 = HAL_GetUIDw2();

serial_id[0] = (uid0 >> 0) & 0xFF;
serial_id[1] = (uid0 >> 8) & 0xFF;
serial_id[2] = (uid0 >> 16) & 0xFF;
serial_id[3] = (uid0 >> 24) & 0xFF;

serial_id[4] = (uid1 >> 0) & 0xFF;
serial_id[5] = (uid1 >> 8) & 0xFF;
serial_id[6] = (uid1 >> 16) & 0xFF;
serial_id[7] = (uid1 >> 24) & 0xFF;

serial_id[8] = (uid2 >> 0) & 0xFF;
serial_id[9] = (uid2 >> 8) & 0xFF;
serial_id[10] = (uid2 >> 16) & 0xFF;
serial_id[11] = (uid2 >> 24) & 0xFF;

return 12;
}

#if defined(USB_OTG_FS)
void OTG_FS_IRQHandler(void) {
tusb_int_handler(0, true);
}
#endif

#if defined(USB_OTG_HS)
void OTG_HS_IRQHandler(void) {
tusb_int_handler(1, true);
}
#endif

void yield(void) {
TinyUSB_Device_Task();
TinyUSB_Device_FlushCDC();
}

} // extern "C"

#endif
24 changes: 24 additions & 0 deletions cores/arduino/WSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#if defined (USBCON) && defined(USBD_USE_CDC)
#include "USBSerial.h"
#endif /* USBCON && USBD_USE_CDC */
#if defined(USE_TINYUSB)
#include "TinyUSB_API.h"
#include "TinyUSBSerial.h"
#endif
#if defined(VIRTIOCON)
#include "VirtIOSerial.h"
#endif /* VIRTIOCON */
Expand All @@ -26,6 +30,26 @@
extern void serialEventUSB(void) __attribute__((weak));
#endif /* USBCON && USBD_USE_CDC */

// #if defined(USE_TINYUSB)
// #define ENABLE_SERIALTINYUSB
// #if !defined(Serial)
// #define Serial SerialTinyUSB
// #define serialEvent serialEventTinyUSB
// #endif

// #if defined(ENABLE_SERIALTINYUSB)
// #define HAVE_SERIALTINYUSB
// #endif

// extern void serialEventTinyUSB(void) __attribute__((weak));
// #endif

#if defined(USE_TINYUSB)
#include "TinyUSBSerial.h"
#define HAVE_SERIALTINYUSB
extern void serialEventTinyUSB(void) __attribute__((weak));
#endif

#if defined(VIRTIOCON)
#ifndef DISABLE_GENERIC_SERIALVIRTIO
#define ENABLE_SERIALVIRTIO
Expand Down
15 changes: 15 additions & 0 deletions cores/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#define ARDUINO_MAIN
#include "Arduino.h"

#if defined(USE_TINYUSB)
#include "TinyUSB_API.h"
#endif

// Force init to be called *first*, i.e. before static object allocation.
// Otherwise, statically allocated objects that need HAL may fail.
__attribute__((constructor(101))) void premain()
Expand Down Expand Up @@ -49,13 +53,24 @@ int main(void)
{
initVariant();

#if defined(USE_TINYUSB)
#if defined(USE_USB_HS)
TinyUSB_Device_Init(1);
#else
TinyUSB_Device_Init(0);
#endif
#endif

setup();

for (;;) {
#if defined(CORE_CALLBACK)
CoreCallback();
#endif
loop();
#if defined(USE_TINYUSB)
yield();
#endif
serialEventRun();
}

Expand Down
80 changes: 80 additions & 0 deletions libraries/tinyusb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
cmake_minimum_required(VERSION 3.21)

add_library(tinyusb INTERFACE)
add_library(tinyusb_usage INTERFACE)

set(TINYUSB_ROOT ${CMAKE_CURRENT_LIST_DIR}/src)
set(TINYUSB_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src/wrapper)

# Export TinyUSB public headers globally to all Arduino-related targets
# since WSerial.h includes TinyUSBSerial.h when USE_TINYUSB is enabled.
target_include_directories(base_config INTERFACE
${TINYUSB_ROOT}
${TINYUSB_SRC_DIR}
)

target_include_directories(tinyusb_usage INTERFACE
${TINYUSB_ROOT}
${TINYUSB_SRC_DIR}
)

target_link_libraries(tinyusb_usage INTERFACE
base_config
)

# add_library(tinyusb_bin OBJECT EXCLUDE_FROM_ALL
# # TinyUSB core
# ${TINYUSB_ROOT}/tusb.c
# ${TINYUSB_ROOT}/common/tusb_fifo.c
# ${TINYUSB_ROOT}/device/usbd.c

# # CDC device only
# ${TINYUSB_ROOT}/class/cdc/cdc_device.c

# # STM32 DWC2 device port
# ${TINYUSB_ROOT}/portable/synopsys/dwc2/dwc2_common.c

Check failure on line 35 in libraries/tinyusb/CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Check for spelling errors

synopsys ==> synopsis
# ${TINYUSB_ROOT}/portable/synopsys/dwc2/dcd_dwc2.c

Check failure on line 36 in libraries/tinyusb/CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Check for spelling errors

synopsys ==> synopsis

# # Arduino layer
# ${TINYUSB_SRC_DIR}/TinyUSB_API.cpp
# ${TINYUSB_SRC_DIR}/TinyUSBSerial.cpp
# ${TINYUSB_SRC_DIR}/usb_descriptors.c
# )

add_library(tinyusb_bin OBJECT EXCLUDE_FROM_ALL
# TinyUSB core
${TINYUSB_ROOT}/mw-tusb.c
${TINYUSB_ROOT}/mw-tusb_fifo.c
${TINYUSB_ROOT}/mw-usbd.c

# CDC device only
${TINYUSB_ROOT}/mw-cdc_device.c

# STM32 DWC2 device port
${TINYUSB_ROOT}/mw-dwc2_common.c
${TINYUSB_ROOT}/mw-dcd_dwc2.c

# Arduino layer
${TINYUSB_SRC_DIR}/TinyUSB_API.cpp
${TINYUSB_SRC_DIR}/TinyUSBSerial.cpp
${TINYUSB_SRC_DIR}/usb_descriptors.c
)

target_link_libraries(tinyusb_bin PUBLIC tinyusb_usage)

set_target_properties(tinyusb_bin PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED YES
C_EXTENSIONS YES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
)

target_compile_definitions(tinyusb_bin PUBLIC
USE_TINYUSB=1
)

target_link_libraries(tinyusb INTERFACE
tinyusb_usage
$<TARGET_OBJECTS:tinyusb_bin>
)
1 change: 1 addition & 0 deletions libraries/tinyusb/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
begin KEYWORD2
11 changes: 11 additions & 0 deletions libraries/tinyusb/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name=TinyUSB Library
version=
author=
maintainer=
sentence=TinyUSB library for Arduino
paragraph=
category=Communication
url=
architectures=*
includes=TinyUSB.h,TinyUSBSerial.h
depends=
6 changes: 6 additions & 0 deletions libraries/tinyusb/src/TinyUSB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once



// Objet Arduino style

Check failure on line 5 in libraries/tinyusb/src/TinyUSB.h

View workflow job for this annotation

GitHub Actions / Check for spelling errors

Objet ==> Object

5 changes: 5 additions & 0 deletions libraries/tinyusb/src/tinyusb/class/cdc/mw-cdc_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifdef USE_TINYUSB

#include "class/cdc/cdc_device.c"

#endif /* USE_TINYUSB */
5 changes: 5 additions & 0 deletions libraries/tinyusb/src/tinyusb/class/dfu/mw-dfu_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifdef USE_TINYUSB

#include "class/dfu/dfu_device.c"

#endif /* USE_TINYUSB */
5 changes: 5 additions & 0 deletions libraries/tinyusb/src/tinyusb/class/dfu/mw-dfu_rt_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifdef USE_TINYUSB

#include "class/dfu/dfu_rt_device.c"

#endif /* USE_TINYUSB */
5 changes: 5 additions & 0 deletions libraries/tinyusb/src/tinyusb/class/hid/mw-hid_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifdef USE_TINYUSB

#include "class/hid/hid_device.c"

#endif /* USE_TINYUSB */
5 changes: 5 additions & 0 deletions libraries/tinyusb/src/tinyusb/class/midi/mw-midi2_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifdef USE_TINYUSB

#include "class/midi/midi2_device.c"

#endif /* USE_TINYUSB */
5 changes: 5 additions & 0 deletions libraries/tinyusb/src/tinyusb/class/midi/mw-midi_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifdef USE_TINYUSB

#include "class/midi/midi_device.c"

#endif /* USE_TINYUSB */
Loading
Loading