Skip to content

Commit

Permalink
[board] Enable TinyUSB on more STM32 dev boards
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Jan 5, 2024
1 parent 1c7af50 commit 1f210c1
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 120 deletions.
2 changes: 2 additions & 0 deletions examples/generic/usb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ int main()
{
Board::initialize();
Board::initializeUsbFs();
// DISCO-F746NG also has a HS port:
// Board::initializeUsbHs();
tusb_init();

while (true)
Expand Down
38 changes: 26 additions & 12 deletions examples/generic/usb/project.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
<library>
<extends>modm:blue-pill-f103</extends>
<!-- <extends>modm:disco-f072rb</extends> -->
<!-- <extends>modm:black-pill-f401</extends> -->
<!-- <extends>modm:black-pill-f411</extends> -->
<!-- <extends>modm:feather-m0</extends> -->
<!-- <extends>modm:samd21-mini</extends> -->
<!-- <extends>modm:nucleo-f429zi</extends> -->
<!-- <extends>modm:disco-f072rb</extends> -->
<!-- <extends>modm:disco-f303vc</extends> -->
<!-- <extends>modm:disco-f407vg</extends> -->
<!-- <extends>modm:disco-f429zi</extends> -->
<!-- <extends>modm:disco-f469ni</extends> -->
<!-- <extends>modm:disco-f746ng</extends> -->
<!-- <extends>modm:disco-l476vg</extends> -->
<!-- <extends>modm:feather-m0</extends> -->
<!-- <extends>modm:nucleo-f429zi</extends> -->
<!-- <extends>modm:nucleo-h723zg</extends> -->
<!-- <extends>modm:nucleo-h743zi</extends> -->
<!-- <extends>modm:rp-pico</extends> -->
<!-- <extends>modm:samd21-mini</extends> -->
<options>
<option name="modm:build:build.path">../../../build/generic/usb</option>
<option name="modm:build:openocd.cfg">openocd.cfg</option>
<option name="modm:tinyusb:config">device.cdc,device.msc</option>
<!-- <option name="modm:tinyusb:config">device.cdc,device.midi</option> -->
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:tinyusb</module>
<module>modm:processing:timer</module>
<module>modm:io</module>
</modules>
</library>

<!-- Required for modm:disco-f429zi -->
<!-- <option name="modm:tinyusb:device:port">hs</option> -->

<!-- Required for modm:nucleo-h723zg, modm:disco-f429zi -->
<!-- <option name="modm:tinyusb:max-speed">full</option> -->
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:tinyusb</module>
<module>modm:processing:timer</module>
<module>modm:io</module>
</modules>
<collectors>
<!-- <collect name="modm:build:cppdefines">CFG_TUSB_DEBUG=3</collect> -->
</collectors>
</library>
15 changes: 13 additions & 2 deletions examples/stm32f429_discovery/blink/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,31 @@ main()
Board::initialize();

LedRed::set();
usb::VBus::setOutput(modm::Gpio::Low);
usb::Vbus::setOutput(modm::Gpio::Low);
usb::Overcurrent::setOutput(modm::Gpio::Low);

// Use the logging streams to print some messages.
// Change MODM_LOG_LEVEL above to enable or disable these messages
MODM_LOG_DEBUG << "debug" << modm::endl;
MODM_LOG_INFO << "info" << modm::endl;
MODM_LOG_WARNING << "warning" << modm::endl;
MODM_LOG_ERROR << "error" << modm::endl;

uint32_t counter(0);

while (true)
{
LedRed::toggle();
LedGreen::toggle();

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

usb::VBus::toggle();
usb::Vbus::toggle();
usb::Overcurrent::toggle();

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

MODM_LOG_INFO << "loop: " << counter++ << modm::endl;
}

return 0;
Expand Down
54 changes: 0 additions & 54 deletions examples/stm32f429_discovery/logger/main.cpp

This file was deleted.

12 changes: 0 additions & 12 deletions examples/stm32f429_discovery/logger/project.xml

This file was deleted.

54 changes: 46 additions & 8 deletions src/modm/board/disco_f429zi/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@

#include <modm/platform.hpp>
#include <modm/architecture/interface/clock.hpp>
#include <modm/debug/logger.hpp>

using namespace modm::platform;

/// @ingroup modm_board_disco_f429zi
#define MODM_BOARD_HAS_LOGGER

namespace Board
{
/// @ingroup modm_board_disco_f429zi
/// @{
using namespace modm::literals;

/// STM32F429 running at 180MHz from the external 8MHz crystal
/// STM32F429 running at 168MHz from the external 8MHz crystal
struct SystemClock
{
static constexpr uint32_t Frequency = 180_MHz;
static constexpr uint32_t Frequency = 168_MHz;
static constexpr uint32_t Apb1 = Frequency / 4;
static constexpr uint32_t Apb2 = Frequency / 2;

Expand Down Expand Up @@ -74,14 +78,17 @@ struct SystemClock
static constexpr uint32_t Timer13 = Apb1Timer;
static constexpr uint32_t Timer14 = Apb1Timer;

static constexpr uint32_t Usb = 48_MHz;

static bool inline
enable()
{
Rcc::enableExternalCrystal(); // 8 MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M=4 -> 2MHz
.pllN = 180, // 2MHz * N=180 -> 360MHz
.pllP = 2 // 360MHz / P=2 -> 180MHz = F_cpu
.pllM = 4, // 8MHz / M -> 2MHz
.pllN = 168, // 2MHz * N -> 336MHz
.pllP = 2, // 336MHz / P -> 168MHz = F_cpu
.pllQ = 7 // 336MHz / Q -> 48MHz = F_usb
};
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
// Required for 180 MHz clock
Expand Down Expand Up @@ -222,20 +229,37 @@ using Id = GpioOutputB12; // OTG_FS_ID: USB_OTG_HS_ID

using Overcurrent = GpioC5; // OTG_FS_OC [OTG_FS_OverCurrent]: GPXTI5
using Power = GpioOutputC4; // OTG_FS_PSO [OTG_FS_PowerSwitchOn]
using VBus = GpioB13; // VBUS_FS: USB_OTG_HS_VBUS
//using Device = UsbFs;
using Vbus = GpioB13; // VBUS_FS: USB_OTG_HS_VBUS

using Device = UsbHs;
/// @}
}

namespace stlink
{
/// @ingroup modm_board_nucleo_h743zi
/// @{
using Tx = GpioOutputA9;
using Rx = GpioInputA10;
using Uart = Usart1;
/// @}
}



/// @ingroup modm_board_disco_f429zi
/// @{
using LoggerDevice = modm::IODeviceWrapper< stlink::Uart, modm::IOBuffer::BlockIfFull >;

inline void
initialize()
{
SystemClock::enable();
SysTickTimer::initialize<SystemClock>();

stlink::Uart::connect<stlink::Tx::Tx, stlink::Rx::Rx>();
stlink::Uart::initialize<SystemClock, 115200_Bd>();

LedGreen::setOutput(modm::Gpio::Low);
LedRed::setOutput(modm::Gpio::Low);

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

l3g::SpiMaster::connect<l3g::Sck::Sck, l3g::Mosi::Mosi, l3g::Miso::Miso>();
l3g::SpiMaster::initialize<SystemClock, 11.25_MHz>();
l3g::SpiMaster::initialize<SystemClock, 10.5_MHz>();
l3g::SpiMaster::setDataMode(l3g::SpiMaster::DataMode::Mode3);
}

inline void
initializeUsbFs(uint8_t priority=3)
{
Rcc::enable<Peripheral::Usbotgfs>();
usb::Device::initialize<SystemClock>(priority);
usb::Device::connect<usb::Dm::Dm, usb::Dp::Dp, usb::Id::Id>();

usb::Overcurrent::setInput();
usb::Vbus::setInput();
// Enable VBUS sense (B device) via pin PA9
USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
}
/// @}

}
Expand Down
23 changes: 21 additions & 2 deletions src/modm/board/disco_f429zi/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,44 @@ def init(module):
# STM32F429IDISCOVERY
[Discovery kit for STM32F429](https://www.st.com/en/evaluation-tools/32f429idiscovery.html)
## Logging
To use the logging, you need to close SB11 and SB15 and upgrade the STLINK/V2-B
firmware! See Section 6.3 "Embedded ST-LINK/V2-B" of UM1670.
## TinyUSB
To use the USB port, you must configure TinyUSB to use the HS port in FS mode:
```xml
<option name="modm:tinyusb:max-speed">full</option>
<!-- place either the device or host classes on the HS port -->
<option name="modm:tinyusb:device:port">hs</option>
<option name="modm:tinyusb:host:port">hs</option>
```
"""

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

module.depends(
":debug",
":architecture:clock",
":driver:l3gd20",
":platform:clock",
":platform:core",
":platform:gpio",
":platform:spi:5")
":platform:spi:5",
":platform:uart:1",
":platform:usb:hs")
return True

def build(env):
env.outbasepath = "modm/src/modm/board"
env.substitutions = {
"with_logger": False,
"with_logger": True,
"with_assert": env.has_module(":architecture:assert")
}
env.template("../board.cpp.in", "board.cpp")
Expand Down
40 changes: 40 additions & 0 deletions src/modm/board/disco_f746ng/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,46 @@ def init(module):
# STM32F7DISCOVERY
[Discovery kit for STM32F746](https://www.st.com/en/evaluation-tools/32f746gdiscovery.html)
## TinyUSB
This board has two USB ports: one with Full Speed support and another with true
High Speed support. By default, TinyUSB runs the device classes on the FS port,
however, you can reassign it to HS via this option:
```xml
<options>
<option name="modm:tinyusb:config">device.cdc,device.msc</option>
<option name="modm:tinyusb:device:port">hs</option>
</options>
```
Remember to initialize the HS instead of the FS port via the BSP:
```cpp
Board::initialize();
Board::initializeUsbHs();
```
Note that can use TinyUSB with both the device and host classes at the same time
if you assign them to different ports:
```xml
```xml
<options>
<option name="modm:tinyusb:config">device.cdc,device.msc</option>
<option name="modm:tinyusb:device:port">fs</option>
<option name="modm:tinyusb:host:port">hs</option>
</options>
```
You must initialize both ports via the BSP:
```cpp
Board::initialize();
Board::initializeUsbFs();
Board::initializeUsbHs();
```
"""

def prepare(module, options):
Expand Down
16 changes: 8 additions & 8 deletions src/modm/board/nucleo_f429zi/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,17 @@ using LoggerDevice = modm::IODeviceWrapper< stlink::Uart, modm::IOBuffer::BlockI
inline void
initialize()
{
SystemClock::enable();
SysTickTimer::initialize<SystemClock>();
SystemClock::enable();
SysTickTimer::initialize<SystemClock>();

stlink::Uart::connect<stlink::Tx::Tx, stlink::Rx::Rx>();
stlink::Uart::initialize<SystemClock, 115200_Bd>();
stlink::Uart::connect<stlink::Tx::Tx, stlink::Rx::Rx>();
stlink::Uart::initialize<SystemClock, 115200_Bd>();

LedGreen::setOutput(modm::Gpio::Low);
LedBlue::setOutput(modm::Gpio::Low);
LedRed::setOutput(modm::Gpio::Low);
LedGreen::setOutput(modm::Gpio::Low);
LedBlue::setOutput(modm::Gpio::Low);
LedRed::setOutput(modm::Gpio::Low);

Button::setInput();
Button::setInput();
}

inline void
Expand Down
Loading

0 comments on commit 1f210c1

Please sign in to comment.