Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/modm/board/black_pill_f401/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ def build(env):
env.outbasepath = "modm/src/modm/board"
env.substitutions = {
"with_logger": False,
"with_assert": env.has_module(":architecture:assert")
"with_assert": env.has_module(":architecture:assert"),
"target": env[":target"].identifier,
}
env.template("../board.cpp.in", "board.cpp")
env.copy('../black_pill_f411/board.hpp', 'board.hpp')
env.template("../black_pill_f411/board.hpp.in", "board.hpp")

env.outbasepath = "modm/openocd/modm/board/"
env.template(repopath("tools/openocd/modm/stm32_swd.cfg.in"), "board.cfg",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
*/
// ----------------------------------------------------------------------------

#ifndef MODM_STM32_F4X1CEU_MINI_F4X1_HPP
#define MODM_STM32_F4X1CEU_MINI_F4X1_HPP
#pragma once

#include <modm/platform.hpp>
#include <modm/architecture/interface/clock.hpp>
Expand All @@ -19,36 +18,69 @@ using namespace modm::platform;

namespace Board
{
/// @ingroup modm_board_black_pill_f401 modm_board_black_pill_f411
/// @ingroup modm_board_black_pill_f4{{target.name}}
/// @{
using namespace modm::literals;

/// STM32F4x1 running at 84MHz generated from the external 25MHz crystal
/// STM32F4{{target.name}} running at {% if target.name == "11" %}96{% else %}84{% endif %}MHz generated from the external 25MHz crystal
struct SystemClock
{
static constexpr uint32_t Frequency = 84_MHz;
static constexpr uint32_t Hse = 25_MHz;
static constexpr uint32_t Lse = 32.768_kHz;
static constexpr Rcc::PllConfig pll
{
.M = 25, // 25 MHz / 25 = 1 MHz
%% if target.name == "11"
.N = 384, // 1 MHz * 384 = 384 MHz
.P = 4, // 384 MHz / 4 = 96 MHz = F_cpu
.Q = 8, // 384 MHz / 8 = 48 MHz = F_usb
%% else
.N = 336, // 1 MHz * 336 = 336 MHz
.P = 4, // 336 MHz / 4 = 84 MHz = F_cpu
.Q = 7, // 336 MHz / 7 = 48 MHz = F_usb
%% endif
};
static constexpr uint32_t PllP = Hse / pll.M * pll.N / pll.P;
static constexpr uint32_t PllQ = Hse / pll.M * pll.N / pll.Q;
static_assert(PllP {% if target.name == "11" %}<={% else %}=={% endif %} Rcc::MaxFrequency);

static constexpr uint32_t Frequency = PllP;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb1 = Frequency / 2;
static constexpr uint32_t Apb2 = Frequency;

static constexpr uint32_t Adc = Apb2;

static constexpr uint32_t Spi1 = Apb2;
static constexpr uint32_t Ahb1 = Ahb;

static constexpr uint32_t Crc = Ahb1;
static constexpr uint32_t Dma1 = Ahb1;
static constexpr uint32_t Dma2 = Ahb1;
static constexpr uint32_t Flash = Ahb1;

static constexpr uint32_t I2c1 = Apb1;
static constexpr uint32_t I2c2 = Apb1;
static constexpr uint32_t I2c3 = Apb1;
static constexpr uint32_t I2s2Ext = Apb1;
static constexpr uint32_t I2s3Ext = Apb1;
static constexpr uint32_t IwdgBus = Apb1;
static constexpr uint32_t Pwr = Apb1;
static constexpr uint32_t RtcBus = Apb1;
static constexpr uint32_t Spi2 = Apb1;
static constexpr uint32_t Spi3 = Apb1;
static constexpr uint32_t Usart2 = Apb1;
static constexpr uint32_t Wwdg = Apb1;

static constexpr uint32_t Adc = Apb2;
static constexpr uint32_t Adc1Common = Apb2;
static constexpr uint32_t Exti = Apb2;
static constexpr uint32_t Sdio = Apb2;
static constexpr uint32_t Spi1 = Apb2;
static constexpr uint32_t Spi4 = Apb2;
static constexpr uint32_t Spi5 = Apb2;

static constexpr uint32_t Syscfg = Apb2;
static constexpr uint32_t Usart1 = Apb2;
static constexpr uint32_t Usart2 = Apb1;
static constexpr uint32_t Usart6 = Apb2;

static constexpr uint32_t I2c1 = Apb1;
static constexpr uint32_t I2c2 = Apb1;
static constexpr uint32_t I2c3 = Apb1;

static constexpr uint32_t Apb1Timer = Apb1 * 2;
static constexpr uint32_t Apb2Timer = Apb2 * 1;
static constexpr uint32_t Apb2Timer = Apb2 * 2;
static constexpr uint32_t Timer1 = Apb2Timer;
static constexpr uint32_t Timer2 = Apb1Timer;
static constexpr uint32_t Timer3 = Apb1Timer;
Expand All @@ -58,37 +90,26 @@ struct SystemClock
static constexpr uint32_t Timer10 = Apb2Timer;
static constexpr uint32_t Timer11 = Apb2Timer;

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Usb = PllQ;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;
static constexpr uint32_t Rtc = Lse;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();
const Rcc::PllFactors pllFactors{
.pllM = 25, // 25MHz / M=25 -> 1MHz
.pllN = 336, // 1MHz * N=336 -> 336MHz
.pllP = 4, // 336MHz / P=4 -> 84MHz = F_cpu
.pllQ = 7, // 336MHz / Q=7 -> 48MHz = F_usb
};
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);

// set flash latency
Rcc::setFlashLatency<Frequency>();
Rcc::enableLseCrystal();
Rcc::enableHseCrystal();

// switch system clock to PLL output
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setFlashLatency<Frequency>();
Rcc::updateCoreFrequency<Frequency>();

Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);

// update frequencies for busy-wait delay functions
Rcc::updateCoreFrequency<Frequency>();
Rcc::enablePll(Rcc::PllSource::Hse, pll);
Rcc::enableSystemClock(Rcc::SystemClockSource::PllP);
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);

return true;
}
Expand All @@ -97,7 +118,7 @@ struct SystemClock

namespace usb
{
/// @ingroup modm_board_black_pill_f401 modm_board_black_pill_f411
/// @ingroup modm_board_black_pill_f4{{target.name}}
/// @{
using Id = GpioA10;
using Dm = GpioA11;
Expand All @@ -107,7 +128,7 @@ using Device = UsbFs;
/// @}
}

/// @ingroup modm_board_black_pill_f401 modm_board_black_pill_f411
/// @ingroup modm_board_black_pill_f4{{target.name}}
/// @{
using Led = GpioInverted< GpioOutputC13 >;
using Leds = SoftwareGpioPort< Led >;
Expand Down Expand Up @@ -145,5 +166,3 @@ initializeUsbFs(uint8_t priority=3)
/// @}

} // Board namespace

#endif // MODM_STM32_F4X1CEU_MINI_F4X1_HPP
5 changes: 3 additions & 2 deletions src/modm/board/black_pill_f411/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ def build(env):
env.outbasepath = "modm/src/modm/board"
env.substitutions = {
"with_logger": False,
"with_assert": env.has_module(":architecture:assert")
"with_assert": env.has_module(":architecture:assert"),
"target": env[":target"].identifier,
}
env.template("../board.cpp.in", "board.cpp")
env.copy('.')
env.template("board.hpp.in")

env.outbasepath = "modm/openocd/modm/board/"
env.template(repopath("tools/openocd/modm/stm32_swd.cfg.in"), "board.cfg",
Expand Down
108 changes: 66 additions & 42 deletions src/modm/board/devebox_stm32f4xx/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,65 @@ using namespace modm::literals;
/// STM32F407 running at 168MHz generated from the external 8MHz crystal
struct SystemClock
{
static constexpr uint32_t Frequency = 168_MHz;
static constexpr uint32_t Hse = 8_MHz;
static constexpr uint32_t Lse = 32.768_kHz;
static constexpr Rcc::PllConfig pll
{
.M = 4, // 8 MHz / 4 = 2 MHz
.N = 168, // 2 MHz * 168 = 336 MHz
.P = 2, // 336 MHz / 2 = 168 MHz = F_cpu
.Q = 7, // 336 MHz / 7 = 48 MHz
};
static constexpr uint32_t PllP = Hse / pll.M * pll.N / pll.P;
static constexpr uint32_t PllQ = Hse / pll.M * pll.N / pll.Q;
static_assert(PllP == Rcc::MaxFrequency);

static constexpr uint32_t Frequency = PllP;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb1 = Frequency / 4;
static constexpr uint32_t Apb2 = Frequency / 2;
static constexpr uint32_t Ahb1 = Ahb;
static constexpr uint32_t Ahb2 = Ahb;

static constexpr uint32_t Crc = Ahb1;
static constexpr uint32_t Dma1 = Ahb1;
static constexpr uint32_t Dma2 = Ahb1;
static constexpr uint32_t Eth = Ahb1;
static constexpr uint32_t Flash = Ahb1;

static constexpr uint32_t Dcmi = Ahb2;
static constexpr uint32_t Rng = Ahb2;

static constexpr uint32_t Can1 = Apb1;
static constexpr uint32_t Can2 = Apb1;
static constexpr uint32_t Dac = Apb1;
static constexpr uint32_t I2c1 = Apb1;
static constexpr uint32_t I2c2 = Apb1;
static constexpr uint32_t I2c3 = Apb1;
static constexpr uint32_t I2s2Ext = Apb1;
static constexpr uint32_t I2s3Ext = Apb1;
static constexpr uint32_t IwdgBus = Apb1;
static constexpr uint32_t Pwr = Apb1;
static constexpr uint32_t RtcBus = Apb1;
static constexpr uint32_t Spi2 = Apb1;
static constexpr uint32_t Spi3 = Apb1;
static constexpr uint32_t Usart2 = Apb1;
static constexpr uint32_t Usart3 = Apb1;
static constexpr uint32_t Uart4 = Apb1;
static constexpr uint32_t Uart5 = Apb1;
static constexpr uint32_t Wwdg = Apb1;

static constexpr uint32_t Adc = Apb2;

static constexpr uint32_t Can1 = Apb1;
static constexpr uint32_t Can2 = Apb1;

static constexpr uint32_t Spi1 = Apb2;
static constexpr uint32_t Spi2 = Apb1;
static constexpr uint32_t Spi3 = Apb1;
static constexpr uint32_t Spi4 = Apb2;
static constexpr uint32_t Spi5 = Apb2;
static constexpr uint32_t Spi6 = Apb2;

static constexpr uint32_t Adc1 = Apb2;
static constexpr uint32_t Adc2 = Apb2;
static constexpr uint32_t Adc3 = Apb2;
static constexpr uint32_t Adc123Common = Apb2;
static constexpr uint32_t Exti = Apb2;
static constexpr uint32_t Sdio = Apb2;
static constexpr uint32_t Spi1 = Apb2;
static constexpr uint32_t Syscfg = Apb2;
static constexpr uint32_t Usart1 = Apb2;
static constexpr uint32_t Usart2 = Apb1;
static constexpr uint32_t Usart3 = Apb1;
static constexpr uint32_t Uart4 = Apb1;
static constexpr uint32_t Uart5 = Apb1;
static constexpr uint32_t Usart6 = Apb2;
static constexpr uint32_t Uart7 = Apb1;
static constexpr uint32_t Uart8 = Apb1;

static constexpr uint32_t I2c1 = Apb1;
static constexpr uint32_t I2c2 = Apb1;
static constexpr uint32_t I2c3 = Apb1;

static constexpr uint32_t Apb1Timer = Apb1 * 2;
static constexpr uint32_t Apb2Timer = Apb2 * 2;
Expand All @@ -77,34 +107,28 @@ struct SystemClock
static constexpr uint32_t Timer12 = Apb1Timer;
static constexpr uint32_t Timer13 = Apb1Timer;
static constexpr uint32_t Timer14 = Apb1Timer;

static constexpr uint32_t Usb = PllQ;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;
static constexpr uint32_t Rtc = Lse;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M=4 -> 2MHz
.pllN = 168, // 2MHz * N=168 -> 336MHz
.pllP = 2 // 336MHz / P=2 -> 168MHz = F_cpu
};
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
// set flash latency for 168MHz
Rcc::enableLseCrystal();
Rcc::enableHseCrystal();

Rcc::setFlashLatency<Frequency>();
// switch system clock to PLL output
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
// APB1 has max. 42MHz
// APB2 has max. 84MHz
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div4);
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div2);
// update frequencies for busy-wait delay functions
Rcc::updateCoreFrequency<Frequency>();

Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div4);
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div2);

Rcc::enablePll(Rcc::PllSource::Hse, pll);
Rcc::enableSystemClock(Rcc::SystemClockSource::PllP);
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);

return true;
}
};
Expand Down
Loading
Loading