Skip to content

Commit eb89158

Browse files
authored
Add files via upload
1 parent 295edd5 commit eb89158

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

stm32/Adafruit_TinyUSB_stm32.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "tusb_option.h"
2+
3+
#if (defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_ARDUINO_CORE_STM32)) && CFG_TUD_ENABLED
4+
5+
#define USE_HAL_DRIVER
6+
#include "stm32f4xx_hal.h"
7+
#include "stm32f4xx_hal_rcc.h"
8+
#include "Arduino.h"
9+
#include "arduino/Adafruit_TinyUSB_API.h"
10+
#include "tusb.h"
11+
12+
//--------------------------------------------------------------------+
13+
// Forward USB interrupt events to TinyUSB IRQ Handler
14+
//--------------------------------------------------------------------+
15+
extern "C" {
16+
17+
void OTG_FS_IRQHandler(void)
18+
{
19+
tud_int_handler(0);
20+
}
21+
22+
void yield(void)
23+
{
24+
tud_task();
25+
if (tud_cdc_connected()) {
26+
tud_cdc_write_flush();
27+
}
28+
}
29+
30+
} // extern "C"
31+
32+
//--------------------------------------------------------------------+
33+
// Porting API
34+
//--------------------------------------------------------------------+
35+
void TinyUSB_Port_InitDevice(uint8_t rhport)
36+
{
37+
(void) rhport;
38+
39+
// Enable clocks FIRST
40+
__HAL_RCC_GPIOA_CLK_ENABLE();
41+
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
42+
43+
// Configure USB pins (PA11 = DM, PA12 = DP)
44+
GPIO_InitTypeDef GPIO_InitStruct = {};
45+
GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
46+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
47+
GPIO_InitStruct.Pull = GPIO_NOPULL;
48+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
49+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
50+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
51+
52+
// Enable USB IRQ
53+
NVIC_SetPriority(OTG_FS_IRQn, 0);
54+
NVIC_EnableIRQ(OTG_FS_IRQn);
55+
56+
// Disable VBUS sensing (we're bus-powered, don't need it)
57+
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
58+
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
59+
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
60+
61+
// Initialize TinyUSB device stack
62+
tud_init(rhport);
63+
64+
}
65+
66+
void TinyUSB_Port_EnterDFU(void) {
67+
// Optional - implement bootloader entry if needed
68+
}
69+
70+
uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16])
71+
{
72+
volatile uint32_t *uid = (volatile uint32_t *)0x1FFF7A10; // STM32F411 UID base
73+
uint32_t *serial_32 = (uint32_t *)serial_id;
74+
serial_32[0] = uid[0];
75+
serial_32[1] = uid[1];
76+
serial_32[2] = uid[2];
77+
return 12;
78+
}
79+
80+
#endif // ARDUINO_ARCH_STM32

stm32/tusb_config_stm32.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef TUSB_CONFIG_STM32_H_
2+
#define TUSB_CONFIG_STM32_H_
3+
4+
// USB Port
5+
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
6+
#define CFG_TUSB_RHPORT0_SPEED OPT_FULL_SPEED
7+
#define CFG_TUSB_RHPORT1_MODE OPT_MODE_NONE
8+
9+
// MCU / OS
10+
#define CFG_TUSB_MCU OPT_MCU_STM32F4
11+
#define CFG_TUSB_OS OPT_OS_NONE
12+
13+
// Debug
14+
#ifndef CFG_TUSB_DEBUG
15+
#define CFG_TUSB_DEBUG 0
16+
#endif
17+
18+
#define CFG_TUSB_MEM_SECTION
19+
#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
20+
21+
// Device stack
22+
#define CFG_TUD_ENABLED 1
23+
#define CFG_TUD_ENDPOINT0_SIZE 64
24+
25+
// Classes
26+
#define CFG_TUD_CDC 1
27+
#define CFG_TUD_MSC 0
28+
#define CFG_TUD_HID 1
29+
#define CFG_TUD_MIDI 1
30+
#define CFG_TUD_VENDOR 0
31+
32+
// Buffer sizes
33+
#define CFG_TUD_CDC_RX_BUFSIZE 64
34+
#define CFG_TUD_CDC_TX_BUFSIZE 64
35+
#define CFG_TUD_HID_EP_BUFSIZE 64
36+
#define CFG_TUD_MIDI_RX_BUFSIZE 128
37+
#define CFG_TUD_MIDI_TX_BUFSIZE 128
38+
#define CFG_TUD_VENDOR_RX_BUFSIZE 64
39+
#define CFG_TUD_VENDOR_TX_BUFSIZE 64
40+
41+
#endif

0 commit comments

Comments
 (0)