|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Niklas Hauser |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include <modm/platform.hpp> |
| 15 | +#include <modm/architecture.hpp> |
| 16 | +#include <modm/debug.hpp> |
| 17 | + |
| 18 | +using namespace modm::platform; |
| 19 | +%# |
| 20 | +%% if with_logger |
| 21 | +/// @ingroup modm_board_weact_h523ce |
| 22 | +#define MODM_BOARD_HAS_LOGGER |
| 23 | +%# |
| 24 | +%% endif |
| 25 | +namespace Board |
| 26 | +{ |
| 27 | +/// @ingroup modm_board_weact_h523ce |
| 28 | +/// @{ |
| 29 | +using namespace modm::literals; |
| 30 | + |
| 31 | +/// STM32H523Cx running at 250MHz from PLL clock generated from 8 MHz HSE |
| 32 | +struct SystemClock |
| 33 | +{ |
| 34 | + static constexpr uint32_t Hse = 8_MHz; |
| 35 | + static constexpr uint32_t Lse = 32.768_kHz; |
| 36 | + |
| 37 | + static constexpr Rcc::PllConfig pll1 |
| 38 | + { |
| 39 | + .range = Rcc::PllInputRange::MHz2_4, |
| 40 | + .M = 4, // 8 MHz / 4 = 2 MHz |
| 41 | + .N = 250, // 2 MHz * 120 = 500 MHz |
| 42 | + .P = 2, // 500 MHz / 2 = 250 MHz = F_cpu |
| 43 | + }; |
| 44 | + static constexpr uint32_t Pll1P = Hse / pll1.M * pll1.N / pll1.P; |
| 45 | + static_assert(Pll1P == Rcc::MaxFrequency); |
| 46 | + |
| 47 | + static constexpr Rcc::PllConfig pll3 |
| 48 | + { |
| 49 | + .range = Rcc::PllInputRange::MHz8_16, |
| 50 | + .M = 1, // 8 MHz / 1 = 8 MHz |
| 51 | + .N = 18, // 8 MHz * 18 = 144 MHz |
| 52 | + .Q = 3, // 144 MHz / 3 = 48 MHz = F_usb |
| 53 | + }; |
| 54 | + static constexpr uint32_t Pll3Q = Hse / pll3.M * pll3.N / pll3.Q; |
| 55 | + |
| 56 | + static constexpr uint32_t SysClk = Pll1P; |
| 57 | + static constexpr uint32_t Frequency = SysClk; |
| 58 | + |
| 59 | + // AHB Bus - Max 250MHz |
| 60 | + static constexpr uint32_t Hclk = SysClk / 1; |
| 61 | + static constexpr uint32_t Ahb = Hclk; |
| 62 | + |
| 63 | + // APB Buses - Max 250MHz |
| 64 | + static constexpr uint32_t Apb1 = Hclk / 1; |
| 65 | + static constexpr uint32_t Apb2 = Hclk / 1; |
| 66 | + static constexpr uint32_t Apb3 = Hclk / 1; |
| 67 | + |
| 68 | + // Peripherals on AHB2 |
| 69 | + static constexpr uint32_t Adc1 = Hclk; |
| 70 | + static constexpr uint32_t Dac1 = Hclk; |
| 71 | + |
| 72 | + // Peripherals on APB2 |
| 73 | + static constexpr uint32_t Spi1 = Apb2; |
| 74 | + static constexpr uint32_t Usart1 = Apb2; |
| 75 | + |
| 76 | + // Peripherals on APB1 |
| 77 | + static constexpr uint32_t Spi2 = Apb1; |
| 78 | + static constexpr uint32_t Spi3 = Apb1; |
| 79 | + static constexpr uint32_t Usart2 = Apb1; |
| 80 | + static constexpr uint32_t Usart3 = Apb1; |
| 81 | + static constexpr uint32_t Fdcan1 = Apb1; |
| 82 | + |
| 83 | + // Peripherals on APB3 |
| 84 | + static constexpr uint32_t LpUart1 = Apb3; |
| 85 | + |
| 86 | + // Timer Clocks |
| 87 | + static constexpr uint32_t Apb1Timer = Apb1 * 1; |
| 88 | + static constexpr uint32_t Apb2Timer = Apb2 * 1; |
| 89 | + |
| 90 | + static constexpr uint32_t Timer1 = Apb2Timer; |
| 91 | + static constexpr uint32_t Timer2 = Apb1Timer; |
| 92 | + static constexpr uint32_t Timer3 = Apb1Timer; |
| 93 | + static constexpr uint32_t Timer6 = Apb1Timer; |
| 94 | + static constexpr uint32_t Timer7 = Apb1Timer; |
| 95 | + |
| 96 | + static constexpr uint32_t Rtc = Lse; |
| 97 | + static constexpr uint32_t Usb = Pll3Q; |
| 98 | + static constexpr uint32_t Iwdg = Rcc::LsiFrequency; |
| 99 | + |
| 100 | + static bool inline |
| 101 | + enable() |
| 102 | + { |
| 103 | + Rcc::enableLseCrystal(); |
| 104 | + Rcc::enableHseCrystal(); |
| 105 | + |
| 106 | + Rcc::setVoltageScaling(Rcc::VoltageScaling::Scale0); |
| 107 | + Rcc::setFlashLatency<Frequency>(); |
| 108 | + |
| 109 | + Rcc::enablePll1(Rcc::PllSource::Hse, pll1); |
| 110 | + Rcc::enablePll3(Rcc::PllSource::Hse, pll3); |
| 111 | + |
| 112 | + Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1); |
| 113 | + Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div1); |
| 114 | + Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1); |
| 115 | + Rcc::setApb3Prescaler(Rcc::ApbPrescaler::Div1); |
| 116 | + |
| 117 | + Rcc::updateCoreFrequency<Frequency>(); |
| 118 | + |
| 119 | + Rcc::enableSystemClock(Rcc::SystemClockSource::Pll1P); |
| 120 | + Rcc::setUsbClockSource(Rcc::UsbClockSource::Pll3Q); |
| 121 | + Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse); |
| 122 | + |
| 123 | + return true; |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +using Button = GpioInverted<GpioInputA0>; |
| 128 | + |
| 129 | +using Led = GpioInverted<GpioOutputC13>; |
| 130 | +using Leds = SoftwareGpioPort< Led >; |
| 131 | +/// @} |
| 132 | + |
| 133 | +namespace usb |
| 134 | +{ |
| 135 | +/// @ingroup modm_board_weact_h523ce |
| 136 | +/// @{ |
| 137 | +using Dm = GpioA11; |
| 138 | +using Dp = GpioA12; |
| 139 | + |
| 140 | +using Device = UsbFs; |
| 141 | +/// @} |
| 142 | +} |
| 143 | + |
| 144 | +/// @ingroup modm_board_weact_h523ce |
| 145 | +/// @{ |
| 146 | +%% if with_logger |
| 147 | +using LoggerDevice = modm::IODeviceWrapper< Rtt<0>, modm::IOBuffer::DiscardIfFull >; |
| 148 | +%# |
| 149 | +%% endif |
| 150 | +inline void |
| 151 | +initialize() |
| 152 | +{ |
| 153 | + SystemClock::enable(); |
| 154 | + SysTickTimer::initialize<SystemClock>(); |
| 155 | + |
| 156 | + Led::setOutput(modm::Gpio::Low); |
| 157 | + Button::setInput(Gpio::InputType::PullUp); |
| 158 | +} |
| 159 | + |
| 160 | +inline void |
| 161 | +initializeUsb(uint8_t priority=3) |
| 162 | +{ |
| 163 | + usb::Device::initialize<SystemClock>(priority); |
| 164 | + usb::Device::connect<usb::Dm::Dm, usb::Dp::Dp>(); |
| 165 | +} |
| 166 | +/// @} |
| 167 | + |
| 168 | +} |
| 169 | + |
0 commit comments