|
| 1 | +/* |
| 2 | + * Copyright (c) 2013, Kevin Läufer |
| 3 | + * Copyright (c) 2015-2018, Niklas Hauser |
| 4 | + * Copyright (c) 2017, Sascha Schade |
| 5 | + * Copyright (c) 2018, Antal Szabó |
| 6 | + * Copyright (c) 2024, Carl Treudler |
| 7 | + * |
| 8 | + * This file is part of the modm project. |
| 9 | + * |
| 10 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 11 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 12 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 13 | + */ |
| 14 | +// ---------------------------------------------------------------------------- |
| 15 | + |
| 16 | +#ifndef MODM_STM32_F401_DISCOVERY_HPP |
| 17 | +#define MODM_STM32_F401_DISCOVERY_HPP |
| 18 | + |
| 19 | +#include <modm/platform.hpp> |
| 20 | +#include <modm/architecture/interface/clock.hpp> |
| 21 | +#include <modm/driver/inertial/lsm303a.hpp> |
| 22 | +#include <modm/driver/inertial/l3gd20.hpp> |
| 23 | + |
| 24 | +using namespace modm::platform; |
| 25 | + |
| 26 | +namespace Board |
| 27 | +{ |
| 28 | +/// @ingroup modm_board_disco_f401vc |
| 29 | +/// @{ |
| 30 | +using namespace modm::literals; |
| 31 | + |
| 32 | +/// STM32F401 running at 168MHz generated from the external 8MHz crystal |
| 33 | +struct SystemClock |
| 34 | +{ |
| 35 | + static constexpr uint32_t Frequency = 84_MHz; |
| 36 | + static constexpr uint32_t Ahb = Frequency; |
| 37 | + static constexpr uint32_t Apb1 = Frequency / 4; |
| 38 | + static constexpr uint32_t Apb2 = Frequency / 2; |
| 39 | + |
| 40 | + static constexpr uint32_t Adc = Apb2; |
| 41 | + |
| 42 | + static constexpr uint32_t Spi1 = Apb2; |
| 43 | + static constexpr uint32_t Spi2 = Apb1; |
| 44 | + static constexpr uint32_t Spi3 = Apb1; |
| 45 | + static constexpr uint32_t Spi4 = Apb2; |
| 46 | + |
| 47 | + static constexpr uint32_t Usart1 = Apb2; |
| 48 | + static constexpr uint32_t Usart2 = Apb1; |
| 49 | + static constexpr uint32_t Usart6 = Apb1; |
| 50 | + |
| 51 | + static constexpr uint32_t I2c1 = Apb1; |
| 52 | + static constexpr uint32_t I2c2 = Apb1; |
| 53 | + static constexpr uint32_t I2c3 = Apb1; |
| 54 | + |
| 55 | + static constexpr uint32_t Apb1Timer = Apb1 * 2; |
| 56 | + static constexpr uint32_t Apb2Timer = Apb2 * 2; |
| 57 | + static constexpr uint32_t Timer1 = Apb2Timer; |
| 58 | + static constexpr uint32_t Timer2 = Apb1Timer; |
| 59 | + static constexpr uint32_t Timer3 = Apb1Timer; |
| 60 | + static constexpr uint32_t Timer4 = Apb1Timer; |
| 61 | + static constexpr uint32_t Timer5 = Apb1Timer; |
| 62 | + static constexpr uint32_t Timer6 = Apb1Timer; |
| 63 | + static constexpr uint32_t Timer7 = Apb1Timer; |
| 64 | + static constexpr uint32_t Timer8 = Apb2Timer; |
| 65 | + static constexpr uint32_t Timer9 = Apb2Timer; |
| 66 | + static constexpr uint32_t Timer10 = Apb2Timer; |
| 67 | + static constexpr uint32_t Timer11 = Apb2Timer; |
| 68 | + |
| 69 | + static constexpr uint32_t Usb = 48_MHz; |
| 70 | + |
| 71 | + static bool inline |
| 72 | + enable() |
| 73 | + { |
| 74 | + Rcc::enableExternalCrystal(); // 8MHz |
| 75 | + const Rcc::PllFactors pllFactors{ |
| 76 | + .pllM = 4, // 8MHz / M=4 -> 2MHz |
| 77 | + .pllN = 168, // 2MHz * N=168 -> 336MHz |
| 78 | + .pllP = 4, // 336MHz / P=4 -> 84MHz = F_cpu |
| 79 | + .pllQ = 7 // 336MHz / Q=7 -> 48MHz = F_usb |
| 80 | + }; |
| 81 | + Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors); |
| 82 | + // set flash latency for 84MHz |
| 83 | + Rcc::setFlashLatency<Frequency>(); |
| 84 | + // switch system clock to PLL output |
| 85 | + Rcc::enableSystemClock(Rcc::SystemClockSource::Pll); |
| 86 | + Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1); |
| 87 | + // APB1 has max. 42MHz |
| 88 | + // APB2 has max. 84MHz |
| 89 | + Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div4); |
| 90 | + Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div2); |
| 91 | + // update frequencies for busy-wait delay functions |
| 92 | + Rcc::updateCoreFrequency<Frequency>(); |
| 93 | + |
| 94 | + return true; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +using Button = GpioInputA0; |
| 99 | +using ClockOut = GpioOutputA8; |
| 100 | +using SystemClockOut = GpioOutputC9; |
| 101 | + |
| 102 | +using LedOrange = GpioOutputD13; // User LED 3 |
| 103 | +using LedGreen = GpioOutputD12; // User LED 4 |
| 104 | +using LedRed = GpioOutputD14; // User LED 5 |
| 105 | +using LedBlue = GpioOutputD15; // User LED 6 |
| 106 | + |
| 107 | +using Leds = SoftwareGpioPort< LedGreen, LedBlue, LedRed, LedOrange >; |
| 108 | +/// @} |
| 109 | + |
| 110 | +namespace lsm3 |
| 111 | +{ |
| 112 | +/// @ingroup modm_board_disco_f401vc |
| 113 | +/// @{ |
| 114 | +using Drdy = GpioInputE2; // DRDY [LSM303DLHC_DRDY]: GPXTI2 |
| 115 | +using Int1 = GpioInputE4; // MEMS_INT3 [LSM303DLHC_INT1]: GPXTI4 |
| 116 | +using Int2 = GpioInputE5; // MEMS_INT4 [LSM303DLHC_INT2]: GPXTI5 |
| 117 | + |
| 118 | +using Scl = GpioB6; // I2C1_SCL [LSM303DLHC_SCL]: I2C1_SCL |
| 119 | +using Sda = GpioB9; // I2C1_SDA [LSM303DLHC_SDA]: I2C1_SDA |
| 120 | + |
| 121 | +using I2cMaster = I2cMaster1; |
| 122 | +using Accelerometer = modm::Lsm303a< I2cMaster >; |
| 123 | +/// @} |
| 124 | +} |
| 125 | + |
| 126 | +namespace l3g |
| 127 | +{ |
| 128 | +/// @ingroup modm_board_disco_f401vc |
| 129 | +/// @{ |
| 130 | +using Int1 = GpioInputE0; // MEMS_INT1 [L3GD20_INT1]: GPXTI0 |
| 131 | +using Int2 = GpioInputE1; // MEMS_INT2 [L3GD20_DRDY/INT2]: GPXTI1 |
| 132 | + |
| 133 | +using Cs = GpioOutputE3; // CS_I2C/SPI [L3GD20_CS_I2C/SPI] |
| 134 | +using Sck = GpioOutputA5; // SPI1_SCK [L3GD20_SCL/SPC] |
| 135 | +using Mosi = GpioOutputA7; // SPI1_MOSI [L3GD20_SDA/SDI/SDO] |
| 136 | +using Miso = GpioInputA6; // SPI1_MISO [L3GD20_SA0/SDO] |
| 137 | + |
| 138 | +using SpiMaster = SpiMaster1; |
| 139 | +using Transport = modm::Lis3TransportSpi< SpiMaster, Cs >; |
| 140 | +using Gyroscope = modm::L3gd20< Transport >; |
| 141 | +/// @} |
| 142 | +} |
| 143 | + |
| 144 | +namespace cs43 |
| 145 | +{ |
| 146 | +/// @ingroup modm_board_disco_f401vc |
| 147 | +/// @{ |
| 148 | +using Lrck = GpioOutputA4; // I2S3_WS |
| 149 | +using Mclk = GpioOutputC7; // I2S3_MCK |
| 150 | +using Sclk = GpioOutputC10; // I2S3_SCK |
| 151 | +using Sdin = GpioOutputC12; // I2S3_SD |
| 152 | + |
| 153 | +using Reset = GpioOutputD4; // Audio_RST |
| 154 | +using Scl = GpioB6; // Audio_SCL |
| 155 | +using Sda = GpioB9; // Audio_SDA |
| 156 | + |
| 157 | +using I2cMaster = I2cMaster1; |
| 158 | +//using I2sMaster = I2sMaster3; |
| 159 | +/// @} |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +namespace mp45 |
| 164 | +{ |
| 165 | +/// @ingroup modm_board_disco_f401vc |
| 166 | +/// @{ |
| 167 | +using Clk = GpioOutputB10; // CLK_IN: I2S2_CK |
| 168 | +using Dout = GpioInputC3; // PDM_OUT: I2S2_SD |
| 169 | +//using I2sMaster = I2sMaster2; |
| 170 | +/// @} |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +namespace usb |
| 175 | +{ |
| 176 | +/// @ingroup modm_board_disco_f401vc |
| 177 | +/// @{ |
| 178 | +using Vbus = GpioInputA9; // VBUS_FS: USB_OTG_HS_VBUS |
| 179 | +using Id = GpioA10; // OTG_FS_ID: USB_OTG_FS_ID |
| 180 | +using Dm = GpioA11; // OTG_FS_DM: USB_OTG_FS_DM |
| 181 | +using Dp = GpioA12; // OTG_FS_DP: USB_OTG_FS_DP |
| 182 | + |
| 183 | +using Overcurrent = GpioInputD5; // OTG_FS_OverCurrent |
| 184 | +using Power = GpioOutputC0; // OTG_FS_PowerSwitchOn |
| 185 | + |
| 186 | +using Device = UsbFs; |
| 187 | +/// @} |
| 188 | +} |
| 189 | + |
| 190 | + |
| 191 | +/// @ingroup modm_board_disco_f401vc |
| 192 | +/// @{ |
| 193 | +inline void |
| 194 | +initialize() |
| 195 | +{ |
| 196 | + SystemClock::enable(); |
| 197 | + SysTickTimer::initialize<SystemClock>(); |
| 198 | + |
| 199 | + Leds::setOutput(modm::Gpio::Low); |
| 200 | + |
| 201 | + Button::setInput(); |
| 202 | +} |
| 203 | + |
| 204 | + |
| 205 | +inline void |
| 206 | +initializeLsm3() |
| 207 | +{ |
| 208 | + lsm3::Int1::setInput(); |
| 209 | + lsm3::Int2::setInput(); |
| 210 | + lsm3::Drdy::setInput(); |
| 211 | + |
| 212 | + lsm3::I2cMaster::connect<lsm3::Scl::Scl, lsm3::Sda::Sda>(); |
| 213 | + lsm3::I2cMaster::initialize<SystemClock, 400_kHz>(); |
| 214 | +} |
| 215 | + |
| 216 | +/// not supported yet, due to missing I2S driver |
| 217 | +inline void |
| 218 | +initializeCs43() |
| 219 | +{ |
| 220 | +// cs43::Lrck::connect(cs43::I2sMaster::Ws); |
| 221 | +// cs43::Mclk::connect(cs43::I2sMaster::Mck); |
| 222 | +// cs43::Sclk::connect(cs43::I2sMaster::Ck); |
| 223 | +// cs43::Sdin::connect(cs43::I2sMaster::Sd); |
| 224 | + |
| 225 | + cs43::Reset::setOutput(modm::Gpio::High); |
| 226 | + |
| 227 | + cs43::I2cMaster::connect<cs43::Scl::Scl, cs43::Sda::Sda>(); |
| 228 | + cs43::I2cMaster::initialize<SystemClock, 100_kHz>(); |
| 229 | +} |
| 230 | + |
| 231 | +inline void |
| 232 | +initializeL3g() |
| 233 | +{ |
| 234 | + l3g::Int1::setInput(); |
| 235 | + l3g::Int2::setInput(); |
| 236 | + l3g::Cs::setOutput(modm::Gpio::High); |
| 237 | + |
| 238 | + l3g::SpiMaster::connect<l3g::Sck::Sck, l3g::Mosi::Mosi, l3g::Miso::Miso>(); |
| 239 | + l3g::SpiMaster::initialize<SystemClock, 10_MHz>(); |
| 240 | + l3g::SpiMaster::setDataMode(l3g::SpiMaster::DataMode::Mode3); |
| 241 | +} |
| 242 | + |
| 243 | +/// not supported yet, due to missing I2S driver |
| 244 | +inline void |
| 245 | +initializeMp45() |
| 246 | +{ |
| 247 | +// mp45::Clk::connect(mp45::I2sMaster::Ck); |
| 248 | +// mp45::Dout::connect(mp45::I2sMaster::Sd); |
| 249 | +} |
| 250 | + |
| 251 | +inline void |
| 252 | +initializeUsbFs(uint8_t priority=3) |
| 253 | +{ |
| 254 | + usb::Device::initialize<SystemClock>(priority); |
| 255 | + usb::Device::connect<usb::Dm::Dm, usb::Dp::Dp, usb::Id::Id>(); |
| 256 | + |
| 257 | + usb::Overcurrent::setInput(); |
| 258 | + usb::Vbus::setInput(); |
| 259 | + // Enable VBUS sense (B device) via pin PA9 |
| 260 | + USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS; |
| 261 | + USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN; |
| 262 | +} |
| 263 | + |
| 264 | +/// @} |
| 265 | + |
| 266 | +} |
| 267 | + |
| 268 | +#endif // MODM_STM32_F401_DISCOVERY_HPP |
0 commit comments