Skip to content

Commit 1f210c1

Browse files
committed
[board] Enable TinyUSB on more STM32 dev boards
1 parent 1c7af50 commit 1f210c1

File tree

12 files changed

+232
-120
lines changed

12 files changed

+232
-120
lines changed

examples/generic/usb/main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ int main()
3939
{
4040
Board::initialize();
4141
Board::initializeUsbFs();
42+
// DISCO-F746NG also has a HS port:
43+
// Board::initializeUsbHs();
4244
tusb_init();
4345

4446
while (true)

examples/generic/usb/project.xml

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
<library>
22
<extends>modm:blue-pill-f103</extends>
3-
<!-- <extends>modm:disco-f072rb</extends> -->
43
<!-- <extends>modm:black-pill-f401</extends> -->
54
<!-- <extends>modm:black-pill-f411</extends> -->
6-
<!-- <extends>modm:feather-m0</extends> -->
7-
<!-- <extends>modm:samd21-mini</extends> -->
8-
<!-- <extends>modm:nucleo-f429zi</extends> -->
5+
<!-- <extends>modm:disco-f072rb</extends> -->
96
<!-- <extends>modm:disco-f303vc</extends> -->
107
<!-- <extends>modm:disco-f407vg</extends> -->
8+
<!-- <extends>modm:disco-f429zi</extends> -->
9+
<!-- <extends>modm:disco-f469ni</extends> -->
1110
<!-- <extends>modm:disco-f746ng</extends> -->
11+
<!-- <extends>modm:disco-l476vg</extends> -->
12+
<!-- <extends>modm:feather-m0</extends> -->
13+
<!-- <extends>modm:nucleo-f429zi</extends> -->
14+
<!-- <extends>modm:nucleo-h723zg</extends> -->
15+
<!-- <extends>modm:nucleo-h743zi</extends> -->
1216
<!-- <extends>modm:rp-pico</extends> -->
17+
<!-- <extends>modm:samd21-mini</extends> -->
1318
<options>
1419
<option name="modm:build:build.path">../../../build/generic/usb</option>
1520
<option name="modm:build:openocd.cfg">openocd.cfg</option>
1621
<option name="modm:tinyusb:config">device.cdc,device.msc</option>
1722
<!-- <option name="modm:tinyusb:config">device.cdc,device.midi</option> -->
18-
</options>
19-
<modules>
20-
<module>modm:build:scons</module>
21-
<module>modm:tinyusb</module>
22-
<module>modm:processing:timer</module>
23-
<module>modm:io</module>
24-
</modules>
25-
</library>
23+
24+
<!-- Required for modm:disco-f429zi -->
25+
<!-- <option name="modm:tinyusb:device:port">hs</option> -->
26+
27+
<!-- Required for modm:nucleo-h723zg, modm:disco-f429zi -->
28+
<!-- <option name="modm:tinyusb:max-speed">full</option> -->
29+
</options>
30+
<modules>
31+
<module>modm:build:scons</module>
32+
<module>modm:tinyusb</module>
33+
<module>modm:processing:timer</module>
34+
<module>modm:io</module>
35+
</modules>
36+
<collectors>
37+
<!-- <collect name="modm:build:cppdefines">CFG_TUSB_DEBUG=3</collect> -->
38+
</collectors>
39+
</library>

examples/stm32f429_discovery/blink/main.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,31 @@ main()
2020
Board::initialize();
2121

2222
LedRed::set();
23-
usb::VBus::setOutput(modm::Gpio::Low);
23+
usb::Vbus::setOutput(modm::Gpio::Low);
2424
usb::Overcurrent::setOutput(modm::Gpio::Low);
2525

26+
// Use the logging streams to print some messages.
27+
// Change MODM_LOG_LEVEL above to enable or disable these messages
28+
MODM_LOG_DEBUG << "debug" << modm::endl;
29+
MODM_LOG_INFO << "info" << modm::endl;
30+
MODM_LOG_WARNING << "warning" << modm::endl;
31+
MODM_LOG_ERROR << "error" << modm::endl;
32+
33+
uint32_t counter(0);
34+
2635
while (true)
2736
{
2837
LedRed::toggle();
2938
LedGreen::toggle();
3039

3140
modm::delay(Button::read() ? 125ms : 500ms);
3241

33-
usb::VBus::toggle();
42+
usb::Vbus::toggle();
3443
usb::Overcurrent::toggle();
3544

3645
modm::delay(Button::read() ? 125ms : 500ms);
46+
47+
MODM_LOG_INFO << "loop: " << counter++ << modm::endl;
3748
}
3849

3950
return 0;

examples/stm32f429_discovery/logger/main.cpp

-54
This file was deleted.

examples/stm32f429_discovery/logger/project.xml

-12
This file was deleted.

src/modm/board/disco_f429zi/board.hpp

+46-8
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@
1616

1717
#include <modm/platform.hpp>
1818
#include <modm/architecture/interface/clock.hpp>
19+
#include <modm/debug/logger.hpp>
1920

2021
using namespace modm::platform;
2122

23+
/// @ingroup modm_board_disco_f429zi
24+
#define MODM_BOARD_HAS_LOGGER
25+
2226
namespace Board
2327
{
2428
/// @ingroup modm_board_disco_f429zi
2529
/// @{
2630
using namespace modm::literals;
2731

28-
/// STM32F429 running at 180MHz from the external 8MHz crystal
32+
/// STM32F429 running at 168MHz from the external 8MHz crystal
2933
struct SystemClock
3034
{
31-
static constexpr uint32_t Frequency = 180_MHz;
35+
static constexpr uint32_t Frequency = 168_MHz;
3236
static constexpr uint32_t Apb1 = Frequency / 4;
3337
static constexpr uint32_t Apb2 = Frequency / 2;
3438

@@ -74,14 +78,17 @@ struct SystemClock
7478
static constexpr uint32_t Timer13 = Apb1Timer;
7579
static constexpr uint32_t Timer14 = Apb1Timer;
7680

81+
static constexpr uint32_t Usb = 48_MHz;
82+
7783
static bool inline
7884
enable()
7985
{
8086
Rcc::enableExternalCrystal(); // 8 MHz
8187
const Rcc::PllFactors pllFactors{
82-
.pllM = 4, // 8MHz / M=4 -> 2MHz
83-
.pllN = 180, // 2MHz * N=180 -> 360MHz
84-
.pllP = 2 // 360MHz / P=2 -> 180MHz = F_cpu
88+
.pllM = 4, // 8MHz / M -> 2MHz
89+
.pllN = 168, // 2MHz * N -> 336MHz
90+
.pllP = 2, // 336MHz / P -> 168MHz = F_cpu
91+
.pllQ = 7 // 336MHz / Q -> 48MHz = F_usb
8592
};
8693
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
8794
// Required for 180 MHz clock
@@ -222,20 +229,37 @@ using Id = GpioOutputB12; // OTG_FS_ID: USB_OTG_HS_ID
222229

223230
using Overcurrent = GpioC5; // OTG_FS_OC [OTG_FS_OverCurrent]: GPXTI5
224231
using Power = GpioOutputC4; // OTG_FS_PSO [OTG_FS_PowerSwitchOn]
225-
using VBus = GpioB13; // VBUS_FS: USB_OTG_HS_VBUS
226-
//using Device = UsbFs;
232+
using Vbus = GpioB13; // VBUS_FS: USB_OTG_HS_VBUS
233+
234+
using Device = UsbHs;
235+
/// @}
236+
}
237+
238+
namespace stlink
239+
{
240+
/// @ingroup modm_board_nucleo_h743zi
241+
/// @{
242+
using Tx = GpioOutputA9;
243+
using Rx = GpioInputA10;
244+
using Uart = Usart1;
227245
/// @}
228246
}
229247

230248

249+
231250
/// @ingroup modm_board_disco_f429zi
232251
/// @{
252+
using LoggerDevice = modm::IODeviceWrapper< stlink::Uart, modm::IOBuffer::BlockIfFull >;
253+
233254
inline void
234255
initialize()
235256
{
236257
SystemClock::enable();
237258
SysTickTimer::initialize<SystemClock>();
238259

260+
stlink::Uart::connect<stlink::Tx::Tx, stlink::Rx::Rx>();
261+
stlink::Uart::initialize<SystemClock, 115200_Bd>();
262+
239263
LedGreen::setOutput(modm::Gpio::Low);
240264
LedRed::setOutput(modm::Gpio::Low);
241265

@@ -250,9 +274,23 @@ initializeL3g()
250274
l3g::Cs::setOutput(modm::Gpio::High);
251275

252276
l3g::SpiMaster::connect<l3g::Sck::Sck, l3g::Mosi::Mosi, l3g::Miso::Miso>();
253-
l3g::SpiMaster::initialize<SystemClock, 11.25_MHz>();
277+
l3g::SpiMaster::initialize<SystemClock, 10.5_MHz>();
254278
l3g::SpiMaster::setDataMode(l3g::SpiMaster::DataMode::Mode3);
255279
}
280+
281+
inline void
282+
initializeUsbFs(uint8_t priority=3)
283+
{
284+
Rcc::enable<Peripheral::Usbotgfs>();
285+
usb::Device::initialize<SystemClock>(priority);
286+
usb::Device::connect<usb::Dm::Dm, usb::Dp::Dp, usb::Id::Id>();
287+
288+
usb::Overcurrent::setInput();
289+
usb::Vbus::setInput();
290+
// Enable VBUS sense (B device) via pin PA9
291+
USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS;
292+
USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
293+
}
256294
/// @}
257295

258296
}

src/modm/board/disco_f429zi/module.lb

+21-2
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,44 @@ def init(module):
1717
# STM32F429IDISCOVERY
1818
1919
[Discovery kit for STM32F429](https://www.st.com/en/evaluation-tools/32f429idiscovery.html)
20+
21+
## Logging
22+
23+
To use the logging, you need to close SB11 and SB15 and upgrade the STLINK/V2-B
24+
firmware! See Section 6.3 "Embedded ST-LINK/V2-B" of UM1670.
25+
26+
## TinyUSB
27+
28+
To use the USB port, you must configure TinyUSB to use the HS port in FS mode:
29+
30+
```xml
31+
<option name="modm:tinyusb:max-speed">full</option>
32+
<!-- place either the device or host classes on the HS port -->
33+
<option name="modm:tinyusb:device:port">hs</option>
34+
<option name="modm:tinyusb:host:port">hs</option>
35+
```
2036
"""
2137

2238
def prepare(module, options):
2339
if not options[":target"].partname.startswith("stm32f429zit"):
2440
return False
2541

2642
module.depends(
43+
":debug",
2744
":architecture:clock",
2845
":driver:l3gd20",
2946
":platform:clock",
3047
":platform:core",
3148
":platform:gpio",
32-
":platform:spi:5")
49+
":platform:spi:5",
50+
":platform:uart:1",
51+
":platform:usb:hs")
3352
return True
3453

3554
def build(env):
3655
env.outbasepath = "modm/src/modm/board"
3756
env.substitutions = {
38-
"with_logger": False,
57+
"with_logger": True,
3958
"with_assert": env.has_module(":architecture:assert")
4059
}
4160
env.template("../board.cpp.in", "board.cpp")

src/modm/board/disco_f746ng/module.lb

+40
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,46 @@ def init(module):
1717
# STM32F7DISCOVERY
1818
1919
[Discovery kit for STM32F746](https://www.st.com/en/evaluation-tools/32f746gdiscovery.html)
20+
21+
## TinyUSB
22+
23+
This board has two USB ports: one with Full Speed support and another with true
24+
High Speed support. By default, TinyUSB runs the device classes on the FS port,
25+
however, you can reassign it to HS via this option:
26+
27+
```xml
28+
<options>
29+
<option name="modm:tinyusb:config">device.cdc,device.msc</option>
30+
<option name="modm:tinyusb:device:port">hs</option>
31+
</options>
32+
```
33+
34+
Remember to initialize the HS instead of the FS port via the BSP:
35+
36+
```cpp
37+
Board::initialize();
38+
Board::initializeUsbHs();
39+
```
40+
41+
Note that can use TinyUSB with both the device and host classes at the same time
42+
if you assign them to different ports:
43+
44+
```xml
45+
```xml
46+
<options>
47+
<option name="modm:tinyusb:config">device.cdc,device.msc</option>
48+
<option name="modm:tinyusb:device:port">fs</option>
49+
<option name="modm:tinyusb:host:port">hs</option>
50+
</options>
51+
```
52+
53+
You must initialize both ports via the BSP:
54+
55+
```cpp
56+
Board::initialize();
57+
Board::initializeUsbFs();
58+
Board::initializeUsbHs();
59+
```
2060
"""
2161

2262
def prepare(module, options):

src/modm/board/nucleo_f429zi/board.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ using LoggerDevice = modm::IODeviceWrapper< stlink::Uart, modm::IOBuffer::BlockI
145145
inline void
146146
initialize()
147147
{
148-
SystemClock::enable();
149-
SysTickTimer::initialize<SystemClock>();
148+
SystemClock::enable();
149+
SysTickTimer::initialize<SystemClock>();
150150

151-
stlink::Uart::connect<stlink::Tx::Tx, stlink::Rx::Rx>();
152-
stlink::Uart::initialize<SystemClock, 115200_Bd>();
151+
stlink::Uart::connect<stlink::Tx::Tx, stlink::Rx::Rx>();
152+
stlink::Uart::initialize<SystemClock, 115200_Bd>();
153153

154-
LedGreen::setOutput(modm::Gpio::Low);
155-
LedBlue::setOutput(modm::Gpio::Low);
156-
LedRed::setOutput(modm::Gpio::Low);
154+
LedGreen::setOutput(modm::Gpio::Low);
155+
LedBlue::setOutput(modm::Gpio::Low);
156+
LedRed::setOutput(modm::Gpio::Low);
157157

158-
Button::setInput();
158+
Button::setInput();
159159
}
160160

161161
inline void

0 commit comments

Comments
 (0)