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
0 commit comments