|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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/interface/clock.hpp> |
| 16 | +#include <modm/debug/logger.hpp> |
| 17 | + |
| 18 | +using namespace modm::platform; |
| 19 | + |
| 20 | +/// @ingroup modm_board_nucleo_h743zi |
| 21 | +/// @{ |
| 22 | +#define MODM_BOARD_HAS_LOGGER |
| 23 | + |
| 24 | +namespace Board |
| 25 | +{ |
| 26 | +using namespace modm::literals; |
| 27 | + |
| 28 | +/// STM32H743 running at 400MHz from the external 8MHz HSE |
| 29 | +struct SystemClock |
| 30 | +{ |
| 31 | + // NOTE: revision Y at 400MHz, only revision V runs at 480Mhz!!! |
| 32 | + static constexpr uint32_t SysClk = 400_MHz; |
| 33 | + // Max 400MHz or 480MHz |
| 34 | + static constexpr uint32_t Hclk = SysClk / 1; // D1CPRE |
| 35 | + static constexpr uint32_t Frequency = Hclk; |
| 36 | + // Max 200MHz or 240MHz |
| 37 | + static constexpr uint32_t Ahb = Hclk / 2; // HPRE |
| 38 | + static constexpr uint32_t Ahb1 = Ahb; |
| 39 | + static constexpr uint32_t Ahb2 = Ahb; |
| 40 | + static constexpr uint32_t Ahb3 = Ahb; |
| 41 | + static constexpr uint32_t Ahb4 = Ahb; |
| 42 | + // Max 100MHz or 120MHz |
| 43 | + static constexpr uint32_t Apb1 = Ahb / 2; // D2PPRE1 |
| 44 | + static constexpr uint32_t Apb2 = Ahb / 2; // D2PPRE2 |
| 45 | + static constexpr uint32_t Apb3 = Ahb / 2; // D1PPRE |
| 46 | + static constexpr uint32_t Apb4 = Ahb / 2; // D3PPRE |
| 47 | + |
| 48 | + static constexpr uint32_t Adc1 = Ahb1; |
| 49 | + static constexpr uint32_t Adc2 = Ahb1; |
| 50 | + static constexpr uint32_t Adc3 = Ahb4; |
| 51 | + |
| 52 | + static constexpr uint32_t Dac1 = Apb1; |
| 53 | + |
| 54 | + static constexpr uint32_t Spi1 = Apb2; |
| 55 | + static constexpr uint32_t Spi2 = Apb1; |
| 56 | + static constexpr uint32_t Spi3 = Apb1; |
| 57 | + static constexpr uint32_t Spi4 = Apb2; |
| 58 | + static constexpr uint32_t Spi5 = Apb2; |
| 59 | + static constexpr uint32_t Spi6 = Apb4; |
| 60 | + |
| 61 | + static constexpr uint32_t Usart1 = Apb2; |
| 62 | + static constexpr uint32_t Usart2 = Apb1; |
| 63 | + static constexpr uint32_t Usart3 = Apb1; |
| 64 | + static constexpr uint32_t Uart4 = Apb1; |
| 65 | + static constexpr uint32_t Uart5 = Apb1; |
| 66 | + static constexpr uint32_t Usart6 = Apb2; |
| 67 | + static constexpr uint32_t Uart7 = Apb1; |
| 68 | + static constexpr uint32_t Uart8 = Apb1; |
| 69 | + |
| 70 | + static constexpr uint32_t LpUart1 = Apb4; |
| 71 | + |
| 72 | + static constexpr uint32_t Can1 = Apb1; |
| 73 | + static constexpr uint32_t Can2 = Apb1; |
| 74 | + |
| 75 | + static constexpr uint32_t I2c1 = Apb1; |
| 76 | + static constexpr uint32_t I2c2 = Apb1; |
| 77 | + static constexpr uint32_t I2c3 = Apb1; |
| 78 | + static constexpr uint32_t I2c4 = Apb4; |
| 79 | + |
| 80 | + static constexpr uint32_t Apb1Timer = Apb1 * 2; |
| 81 | + static constexpr uint32_t Apb2Timer = Apb2 * 2; |
| 82 | + static constexpr uint32_t Timer1 = Apb2Timer; |
| 83 | + static constexpr uint32_t Timer2 = Apb1Timer; |
| 84 | + static constexpr uint32_t Timer3 = Apb1Timer; |
| 85 | + static constexpr uint32_t Timer4 = Apb1Timer; |
| 86 | + static constexpr uint32_t Timer5 = Apb1Timer; |
| 87 | + static constexpr uint32_t Timer6 = Apb1Timer; |
| 88 | + static constexpr uint32_t Timer7 = Apb1Timer; |
| 89 | + static constexpr uint32_t Timer8 = Apb2Timer; |
| 90 | + static constexpr uint32_t Timer12 = Apb1Timer; |
| 91 | + static constexpr uint32_t Timer13 = Apb1Timer; |
| 92 | + static constexpr uint32_t Timer14 = Apb1Timer; |
| 93 | + static constexpr uint32_t Timer15 = Apb2Timer; |
| 94 | + static constexpr uint32_t Timer16 = Apb2Timer; |
| 95 | + static constexpr uint32_t Timer17 = Apb2Timer; |
| 96 | + |
| 97 | + static constexpr uint32_t Usb = 48_MHz; // From PLL3Q |
| 98 | + |
| 99 | + static bool inline |
| 100 | + enable() |
| 101 | + { |
| 102 | + Rcc::enableExternalClock(); // 8 MHz |
| 103 | + Rcc::setVoltageScaling(Rcc::VoltageScaling::Scale0); // required for 400MHz/480MHz |
| 104 | + const Rcc::PllFactors pllFactors1{ |
| 105 | + .range = Rcc::PllInputRange::MHz4_8, |
| 106 | + .pllM = 2, // 8MHz / M= 4MHz |
| 107 | + .pllN = 100, // 4MHz * N= 400MHz |
| 108 | + .pllP = 1, // 400MHz / P= 400MHz = F_cpu |
| 109 | + .pllQ = 10, // 400MHz / Q= 40MHz |
| 110 | + .pllR = 10, // 400MHz / R= 40MHz |
| 111 | + }; |
| 112 | + Rcc::enablePll1(Rcc::PllSource::ExternalClock, pllFactors1); |
| 113 | + // Use PLL3 for USB 48MHz |
| 114 | + const Rcc::PllFactors pllFactors3{ |
| 115 | + .range = Rcc::PllInputRange::MHz4_8, |
| 116 | + .pllM = 2, // 8MHz / M= 4MHz |
| 117 | + .pllN = 60, // 4MHz * N= 240MHz |
| 118 | + .pllP = 5, // 240MHz / P= 48MHz |
| 119 | + .pllQ = 5, // 240MHz / Q= 48MHz = F_usb |
| 120 | + .pllR = 5, // 240MHz / R= 48MHz |
| 121 | + }; |
| 122 | + Rcc::enablePll3(Rcc::PllSource::ExternalClock, pllFactors3); |
| 123 | + Rcc::setFlashLatency<Ahb>(); |
| 124 | + // max 240MHz on AHB |
| 125 | + Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div2); |
| 126 | + // max 120MHz on APB |
| 127 | + Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2); |
| 128 | + Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div2); |
| 129 | + Rcc::setApb3Prescaler(Rcc::Apb3Prescaler::Div2); |
| 130 | + Rcc::setApb4Prescaler(Rcc::Apb4Prescaler::Div2); |
| 131 | + // update clock frequencies |
| 132 | + Rcc::updateCoreFrequency<Hclk>(); |
| 133 | + Rcc::enableUsbClockSource(Rcc::UsbClockSource::Pll3Q); |
| 134 | + // Switch the main clock source to PLL |
| 135 | + Rcc::enableSystemClock(Rcc::SystemClockSource::Pll1P); |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | +}; |
| 140 | + |
| 141 | +// Arduino Footprint |
| 142 | +#include "nucleo144_arduino.hpp" |
| 143 | + |
| 144 | +using Button = GpioInputC13; |
| 145 | + |
| 146 | +using LedGreen = GpioOutputB0; |
| 147 | +using LedBlue = GpioOutputB7; |
| 148 | +using LedRed = GpioOutputB14; |
| 149 | +using Leds = SoftwareGpioPort< LedRed, LedBlue, LedGreen >; |
| 150 | + |
| 151 | +namespace usb |
| 152 | +{ |
| 153 | +using Vbus = GpioA9; |
| 154 | +using Id = GpioA10; |
| 155 | +using Dm = GpioA11; |
| 156 | +using Dp = GpioA12; |
| 157 | + |
| 158 | +using Overcurrent = GpioInputG7; // OTG_FS_OverCurrent |
| 159 | +using Power = GpioOutputG6; // OTG_FS_PowerSwitchOn |
| 160 | + |
| 161 | +using Device = UsbFs; |
| 162 | +} |
| 163 | + |
| 164 | +namespace stlink |
| 165 | +{ |
| 166 | +using Tx = GpioOutputD8; |
| 167 | +using Rx = GpioInputD9; |
| 168 | +using Uart = Usart3; |
| 169 | +} |
| 170 | + |
| 171 | +using LoggerDevice = modm::IODeviceWrapper< stlink::Uart, modm::IOBuffer::BlockIfFull >; |
| 172 | + |
| 173 | + |
| 174 | +inline void |
| 175 | +initialize() |
| 176 | +{ |
| 177 | + SystemClock::enable(); |
| 178 | + SysTickTimer::initialize<SystemClock>(); |
| 179 | + |
| 180 | + stlink::Uart::connect<stlink::Tx::Tx, stlink::Rx::Rx>(); |
| 181 | + stlink::Uart::initialize<SystemClock, 115200_Bd>(); |
| 182 | + |
| 183 | + LedGreen::setOutput(modm::Gpio::Low); |
| 184 | + LedBlue::setOutput(modm::Gpio::Low); |
| 185 | + LedRed::setOutput(modm::Gpio::Low); |
| 186 | + |
| 187 | + Button::setInput(); |
| 188 | + Button::setInputTrigger(Gpio::InputTrigger::RisingEdge); |
| 189 | + Button::enableExternalInterrupt(); |
| 190 | +// Button::enableExternalInterruptVector(12); |
| 191 | +} |
| 192 | + |
| 193 | +/// FIXME: USB does not work on this board. |
| 194 | +inline void |
| 195 | +initializeUsbFs() |
| 196 | +{ |
| 197 | + usb::Device::initialize<SystemClock>(); |
| 198 | + usb::Device::connect<usb::Dm::Dm, usb::Dp::Dp, usb::Id::Id>(); |
| 199 | + usb::Id::configure(Gpio::InputType::Floating); |
| 200 | + |
| 201 | + usb::Overcurrent::setInput(); |
| 202 | + usb::Vbus::setInput(); |
| 203 | + // Enable VBUS sense (B device) via pin PA9 |
| 204 | + USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN; |
| 205 | +} |
| 206 | + |
| 207 | +} |
| 208 | +/// @} |
| 209 | + |
0 commit comments