Skip to content

Commit 2ccf4c2

Browse files
authored
Merge pull request #565 from code-fiasco/stm32f4-support
STM32 F4 Support
2 parents 3e5369c + 88ef942 commit 2ccf4c2

4 files changed

Lines changed: 182 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Note: For ESP32 port, version before v3.0 requires all descriptors must be speci
5050
Following is cores without built-in support
5151

5252
- **mbed_rp2040**
53+
- **[stm32duino/Arduino_Core_STM32](https://github.com/stm32duino/Arduino_Core_STM32)**
54+
- Still WIP, only support/tested with F4, only support OTG_FS
5355

5456
It is still possible to use TinyUSB but with some limits such as:
5557

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
#ifndef TUSB_CONFIG_STM32_H_
26+
#define TUSB_CONFIG_STM32_H_
27+
28+
//--------------------------------------------------------------------
29+
// COMMON CONFIGURATION
30+
//--------------------------------------------------------------------
31+
// MCU / OS
32+
#define CFG_TUSB_MCU OPT_MCU_STM32F4
33+
#define CFG_TUSB_OS OPT_OS_NONE
34+
35+
// Debug
36+
#ifndef CFG_TUSB_DEBUG
37+
#define CFG_TUSB_DEBUG 0
38+
#endif
39+
40+
#define CFG_TUSB_MEM_SECTION
41+
#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
42+
43+
//--------------------------------------------------------------------
44+
// DEVICE CONFIGURATION
45+
//--------------------------------------------------------------------
46+
#define CFG_TUD_ENABLED 1
47+
#define CFG_TUD_MAX_SPEED OPT_MODE_FULL_SPEED // TODO some are highspeed
48+
#define CFG_TUD_ENDPOINT0_SIZE 64
49+
50+
// Classes
51+
#define CFG_TUD_CDC 1
52+
#define CFG_TUD_MSC 1
53+
#define CFG_TUD_HID 1
54+
#define CFG_TUD_MIDI 1
55+
#define CFG_TUD_VENDOR 1
56+
57+
// Buffer sizes
58+
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
59+
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
60+
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
61+
#define CFG_TUD_MSC_EP_BUFSIZE 512
62+
#define CFG_TUD_HID_EP_BUFSIZE 64
63+
#define CFG_TUD_MIDI_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 128)
64+
#define CFG_TUD_MIDI_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 128)
65+
#define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
66+
#define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
67+
68+
// Serial Redirect
69+
#define Serial SerialTinyUSB
70+
71+
//--------------------------------------------------------------------
72+
// Host Configuration
73+
//--------------------------------------------------------------------
74+
#define CFG_TUH_ENABLED 0 // disable for now
75+
76+
#endif

src/tusb_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343

4444
#elif defined(ARDUINO_ARCH_CH32) || defined(CH32V20x) || defined(CH32V30x)
4545
#include "arduino/ports/ch32/tusb_config_ch32.h"
46+
47+
#elif defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_ARDUINO_CORE_STM32)
48+
#include "arduino/ports/stm32/tusb_config_stm32.h"
49+
4650
#else
4751
#error TinyUSB Arduino Library does not support your core yet
4852
#endif

0 commit comments

Comments
 (0)