1+ /*
2+ * The MIT License (MIT)
3+ *
4+ * Copyright (c) 2019, hathach for Adafruit
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in
14+ * all copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+ * THE SOFTWARE.
23+ */
24+
25+ #include " tusb_option.h"
26+
27+ #if (defined(ARDUINO_ARCH_STM32) || \
28+ defined (ARDUINO_ARCH_ARDUINO_CORE_STM32)) && \
29+ CFG_TUD_ENABLED
30+
31+ #define USE_HAL_DRIVER
32+ #include " Arduino.h"
33+ #include " arduino/Adafruit_TinyUSB_API.h"
34+ #include " stm32f4xx_hal.h"
35+ #include " stm32f4xx_hal_rcc.h"
36+ #include " tusb.h"
37+
38+ // --------------------------------------------------------------------+
39+ // Forward USB interrupt events to TinyUSB IRQ Handler
40+ // --------------------------------------------------------------------+
41+ extern " C" {
42+
43+ void OTG_FS_IRQHandler (void ) { tud_int_handler (0 ); }
44+
45+ void yield (void ) {
46+ tud_task ();
47+ if (tud_cdc_connected ()) {
48+ tud_cdc_write_flush ();
49+ }
50+ }
51+
52+ } // extern "C"
53+
54+ // --------------------------------------------------------------------+
55+ // Porting API
56+ // --------------------------------------------------------------------+
57+ void TinyUSB_Port_InitDevice (uint8_t rhport) {
58+ (void )rhport;
59+
60+ // Enable clocks FIRST
61+ __HAL_RCC_GPIOA_CLK_ENABLE ();
62+ __HAL_RCC_USB_OTG_FS_CLK_ENABLE ();
63+
64+ // Configure USB pins (PA11 = DM, PA12 = DP)
65+ GPIO_InitTypeDef GPIO_InitStruct = {};
66+ GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
67+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
68+ GPIO_InitStruct.Pull = GPIO_NOPULL;
69+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
70+ GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
71+ HAL_GPIO_Init (GPIOA, &GPIO_InitStruct);
72+
73+ // Enable USB IRQ
74+ NVIC_SetPriority (OTG_FS_IRQn, 0 );
75+ NVIC_EnableIRQ (OTG_FS_IRQn);
76+
77+ // Disable VBUS sensing (we're bus-powered, don't need it)
78+ USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
79+ USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
80+ USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
81+
82+ // Initialize TinyUSB device stack
83+ tud_init (rhport);
84+ }
85+
86+ void TinyUSB_Port_EnterDFU (void ) {
87+ // Optional - implement bootloader entry if needed
88+ }
89+
90+ uint8_t TinyUSB_Port_GetSerialNumber (uint8_t serial_id[16 ]) {
91+ volatile uint32_t *uid =
92+ (volatile uint32_t *)0x1FFF7A10 ; // STM32F411 UID base
93+ uint32_t *serial_32 = (uint32_t *)serial_id;
94+ serial_32[0 ] = uid[0 ];
95+ serial_32[1 ] = uid[1 ];
96+ serial_32[2 ] = uid[2 ];
97+ return 12 ;
98+ }
99+
100+ #endif // ARDUINO_ARCH_STM32
0 commit comments