|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Thomas Sommer |
| 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 | +#pragma once |
| 12 | + |
| 13 | +#include <modm/architecture/interface/clock.hpp> |
| 14 | +#include <modm/driver/inertial/lis3dsh.hpp> |
| 15 | +#include <modm/platform.hpp> |
| 16 | + |
| 17 | +using namespace modm::platform; |
| 18 | + |
| 19 | +namespace Board |
| 20 | +{ |
| 21 | +/// @ingroup modm_board_disco_f411ve |
| 22 | +/// @{ |
| 23 | +using namespace modm::literals; |
| 24 | + |
| 25 | +/// STM32F411 running at 84MHz generated from the external 8MHz crystal |
| 26 | +struct SystemClock |
| 27 | +{ |
| 28 | + static constexpr uint32_t Frequency = 84_MHz; |
| 29 | + static constexpr uint32_t Ahb = Frequency; |
| 30 | + static constexpr uint32_t Apb1 = Frequency / 2; |
| 31 | + static constexpr uint32_t Apb2 = Frequency; |
| 32 | + |
| 33 | + static constexpr uint32_t Adc = Apb2; |
| 34 | + |
| 35 | + static constexpr uint32_t Spi1 = Apb2; |
| 36 | + static constexpr uint32_t Spi2 = Apb1; |
| 37 | + static constexpr uint32_t Spi3 = Apb1; |
| 38 | + static constexpr uint32_t Spi4 = Apb2; |
| 39 | + static constexpr uint32_t Spi5 = Apb2; |
| 40 | + |
| 41 | + static constexpr uint32_t Usart1 = Apb2; |
| 42 | + static constexpr uint32_t Usart2 = Apb1; |
| 43 | + static constexpr uint32_t Usart3 = Apb1; |
| 44 | + static constexpr uint32_t Uart4 = Apb1; |
| 45 | + static constexpr uint32_t Uart5 = Apb1; |
| 46 | + static constexpr uint32_t Usart6 = Apb2; |
| 47 | + static constexpr uint32_t Uart7 = Apb1; |
| 48 | + static constexpr uint32_t Uart8 = Apb1; |
| 49 | + |
| 50 | + static constexpr uint32_t I2c1 = Apb1; |
| 51 | + static constexpr uint32_t I2c2 = Apb1; |
| 52 | + static constexpr uint32_t I2c3 = Apb1; |
| 53 | + |
| 54 | + static constexpr uint32_t Apb1Timer = Apb1 * 2; |
| 55 | + static constexpr uint32_t Apb2Timer = Apb2 * 2; |
| 56 | + static constexpr uint32_t Timer1 = Apb2Timer; |
| 57 | + static constexpr uint32_t Timer2 = Apb1Timer; |
| 58 | + static constexpr uint32_t Timer3 = Apb1Timer; |
| 59 | + static constexpr uint32_t Timer4 = Apb1Timer; |
| 60 | + static constexpr uint32_t Timer5 = Apb1Timer; |
| 61 | + static constexpr uint32_t Timer9 = Apb2Timer; |
| 62 | + static constexpr uint32_t Timer10 = Apb2Timer; |
| 63 | + static constexpr uint32_t Timer11 = Apb2Timer; |
| 64 | + |
| 65 | + static constexpr uint32_t Usb = 48_MHz; |
| 66 | + |
| 67 | + static bool inline enable() |
| 68 | + { |
| 69 | + Rcc::enableExternalCrystal(); // 8MHz |
| 70 | + const Rcc::PllFactors pllFactors{ |
| 71 | + .pllM = 8, // 8MHz / M=8 -> 1MHz |
| 72 | + .pllN = 336, // 1MHz * N=336 -> 84MHz |
| 73 | + .pllP = 4, // 336MHz / P=4 -> 100MHz = F_cpu |
| 74 | + .pllQ = 7, // 336MHz / P=7 -> 100MHz = F_cpu |
| 75 | + }; |
| 76 | + Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors); |
| 77 | + // set flash latency for 100MHz |
| 78 | + Rcc::setFlashLatency<Frequency>(); |
| 79 | + // switch system clock to PLL output |
| 80 | + Rcc::enableSystemClock(Rcc::SystemClockSource::Pll); |
| 81 | + Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1); |
| 82 | + // APB1 has max. 50MHz |
| 83 | + // APB2 has max. 100MHz |
| 84 | + Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2); |
| 85 | + Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1); |
| 86 | + // update frequencies for busy-wait delay functions |
| 87 | + Rcc::updateCoreFrequency<Frequency>(); |
| 88 | + |
| 89 | + return true; |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +using LedUsb = GpioOutputA9; |
| 94 | +using ClockOut = GpioOutputA8; |
| 95 | +using SystemClockOut = GpioOutputC9; |
| 96 | + |
| 97 | +using Button = GpioInputA0; |
| 98 | + |
| 99 | +// 4 user colored LEDs aligned in a circle |
| 100 | +using LedGreen = GpioOutputD12; |
| 101 | +using LedOrange = GpioOutputD13; |
| 102 | +using LedRed = GpioOutputD14; |
| 103 | +using LedBlue = GpioOutputD15; |
| 104 | +using Leds = SoftwareGpioPort<LedGreen, LedBlue, LedRed, LedOrange>; |
| 105 | +/// @} |
| 106 | + |
| 107 | +namespace lis3 |
| 108 | +{ |
| 109 | +/// @ingroup modm_board_disco_f411ve |
| 110 | +/// @{ |
| 111 | +using Int = GpioInputE1; // LIS302DL_INT2 |
| 112 | + |
| 113 | +using Cs = GpioOutputE3; // LIS302DL_CS_I2C/SPI |
| 114 | +using Sck = GpioOutputA5; // SPI1_SCK |
| 115 | +using Mosi = GpioOutputA7; // SPI1_MOSI |
| 116 | +using Miso = GpioInputA6; // SPI1_MISO |
| 117 | + |
| 118 | +using SpiMaster = SpiMaster1; |
| 119 | +using Transport = modm::Lis3TransportSpi<SpiMaster, Cs>; |
| 120 | +/// @} |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +namespace cs43 |
| 125 | +{ |
| 126 | +/// @ingroup modm_board_disco_f411ve |
| 127 | +/// @{ |
| 128 | +using Lrck = GpioOutputA4; // I2S3_WS |
| 129 | +using Mclk = GpioOutputC7; // I2S3_MCK |
| 130 | +using Sclk = GpioOutputC10; // I2S3_SCK |
| 131 | +using Sdin = GpioOutputC12; // I2S3_SD |
| 132 | + |
| 133 | +using Reset = GpioOutputD4; // Audio_RST |
| 134 | +using Scl = GpioB6; // Audio_SCL |
| 135 | +using Sda = GpioB9; // Audio_SDA |
| 136 | + |
| 137 | +using I2cMaster = I2cMaster1; |
| 138 | +// using I2sMaster = I2sMaster3; |
| 139 | +/// @} |
| 140 | +} |
| 141 | + |
| 142 | + |
| 143 | +namespace mp45 |
| 144 | +{ |
| 145 | +/// @ingroup modm_board_disco_f411ve |
| 146 | +/// @{ |
| 147 | +using Clk = GpioOutputB10; // CLK_IN: I2S2_CK |
| 148 | +using Dout = GpioInputC3; // PDM_OUT: I2S2_SD |
| 149 | +// using I2sMaster = I2sMaster2; |
| 150 | +/// @} |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +namespace usb |
| 155 | +{ |
| 156 | +/// @ingroup modm_board_disco_f411ve |
| 157 | +/// @{ |
| 158 | +using Vbus = GpioInputA9; // VBUS_FS: USB_OTG_HS_VBUS |
| 159 | +using Id = GpioA10; // OTG_FS_ID: USB_OTG_FS_ID |
| 160 | +using Dm = GpioA11; // OTG_FS_DM: USB_OTG_FS_DM |
| 161 | +using Dp = GpioA12; // OTG_FS_DP: USB_OTG_FS_DP |
| 162 | + |
| 163 | +using Overcurrent = GpioInputD5; // OTG_FS_OverCurrent |
| 164 | +using Power = GpioOutputC0; // OTG_FS_PowerSwitchOn |
| 165 | + |
| 166 | +using Device = UsbFs; |
| 167 | +/// @} |
| 168 | +} |
| 169 | + |
| 170 | +/// @ingroup modm_board_disco_f411ve |
| 171 | +/// @{ |
| 172 | +inline void |
| 173 | +initialize() |
| 174 | +{ |
| 175 | + SystemClock::enable(); |
| 176 | + SysTickTimer::initialize<SystemClock>(); |
| 177 | + |
| 178 | + Leds::setOutput(modm::Gpio::Low); |
| 179 | + LedUsb::setOutput(modm::Gpio::Low); |
| 180 | + |
| 181 | + Button::setInput(Gpio::InputType::Floating); |
| 182 | +} |
| 183 | + |
| 184 | +inline void |
| 185 | +initializeLis3() |
| 186 | +{ |
| 187 | + lis3::Int::setInput(); |
| 188 | + lis3::Cs::setOutput(modm::Gpio::High); |
| 189 | + |
| 190 | + lis3::SpiMaster::connect<lis3::Sck::Sck, lis3::Mosi::Mosi, lis3::Miso::Miso>(); |
| 191 | + lis3::SpiMaster::initialize<SystemClock, 5.25_MHz>(); |
| 192 | + lis3::SpiMaster::setDataMode(lis3::SpiMaster::DataMode::Mode3); |
| 193 | +} |
| 194 | + |
| 195 | +/// not supported yet, due to missing I2S driver |
| 196 | +inline void |
| 197 | +initializeCs43() |
| 198 | +{ |
| 199 | + // cs43::Lrck::connect(cs43::I2sMaster::Ws); |
| 200 | + // cs43::Mclk::connect(cs43::I2sMaster::Mck); |
| 201 | + // cs43::Sclk::connect(cs43::I2sMaster::Ck); |
| 202 | + // cs43::Sdin::connect(cs43::I2sMaster::Sd); |
| 203 | + |
| 204 | + cs43::Reset::setOutput(modm::Gpio::High); |
| 205 | + |
| 206 | + cs43::I2cMaster::connect<cs43::Scl::Scl, cs43::Sda::Sda>(); |
| 207 | + cs43::I2cMaster::initialize<SystemClock, 100_kHz>(); |
| 208 | +} |
| 209 | + |
| 210 | +/// not supported yet, due to missing I2S driver |
| 211 | +inline void |
| 212 | +initializeMp45() |
| 213 | +{ |
| 214 | + // mp45::Clk::connect(mp45::I2sMaster::Ck); |
| 215 | + // mp45::Dout::connect(mp45::I2sMaster::Sd); |
| 216 | +} |
| 217 | + |
| 218 | +inline void |
| 219 | +initializeUsbFs(uint8_t priority=3) |
| 220 | +{ |
| 221 | + usb::Device::initialize<SystemClock>(priority); |
| 222 | + usb::Device::connect<usb::Dm::Dm, usb::Dp::Dp, usb::Id::Id>(); |
| 223 | + |
| 224 | + usb::Overcurrent::setInput(); |
| 225 | + usb::Vbus::setInput(); |
| 226 | + // Enable VBUS sense (B device) via pin PA9 |
| 227 | + USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS; |
| 228 | + USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN; |
| 229 | +} |
| 230 | +/// @} |
| 231 | +} |
0 commit comments