|
| 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.hpp> |
| 17 | +/// @ingroup modm_board_nucleo_l053r8 |
| 18 | +#define MODM_BOARD_HAS_LOGGER |
| 19 | + |
| 20 | +using namespace modm::platform; |
| 21 | + |
| 22 | +/// @ingroup modm_board_nucleo_l053r8 |
| 23 | +namespace Board |
| 24 | +{ |
| 25 | +using namespace modm::literals; |
| 26 | + |
| 27 | +/// STM32L053R8 running at 32MHz generated from 16 MHz HSI16 clock |
| 28 | +struct SystemClock |
| 29 | +{ |
| 30 | + static constexpr uint32_t Frequency = 32_MHz; |
| 31 | + static constexpr uint32_t Ahb = Frequency; |
| 32 | + static constexpr uint32_t Apb1 = Frequency; |
| 33 | + static constexpr uint32_t Apb2 = Frequency; |
| 34 | + |
| 35 | + static constexpr uint32_t Adc = Apb2; |
| 36 | + |
| 37 | + static constexpr uint32_t Dac = Apb1; |
| 38 | + |
| 39 | + static constexpr uint32_t Comp1 = Apb2; |
| 40 | + static constexpr uint32_t Comp2 = Apb2; |
| 41 | + |
| 42 | + static constexpr uint32_t Spi1 = Apb2; |
| 43 | + static constexpr uint32_t Spi2 = Apb1; |
| 44 | + |
| 45 | + static constexpr uint32_t Usart1 = Apb2; |
| 46 | + static constexpr uint32_t Usart2 = Apb1; |
| 47 | + |
| 48 | + static constexpr uint32_t Lpuart1 = Apb1; |
| 49 | + |
| 50 | + static constexpr uint32_t I2c1 = Apb1; |
| 51 | + static constexpr uint32_t I2c2 = Apb1; |
| 52 | + |
| 53 | + static constexpr uint32_t Usb = Apb1; |
| 54 | + |
| 55 | + static constexpr uint32_t Apb1Timer = Apb1 * 1; |
| 56 | + static constexpr uint32_t Apb2Timer = Apb2 * 1; |
| 57 | + static constexpr uint32_t Timer2 = Apb1Timer; |
| 58 | + static constexpr uint32_t Timer6 = Apb1Timer; |
| 59 | + static constexpr uint32_t Timer21 = Apb2Timer; |
| 60 | + static constexpr uint32_t Timer22 = Apb2Timer; |
| 61 | + |
| 62 | + static bool inline |
| 63 | + enable() |
| 64 | + { |
| 65 | + Rcc::enableInternalClock(); // 16MHz |
| 66 | + // (internal clock / 1) * 4 / 2 = 32MHz |
| 67 | + const Rcc::PllFactors pllFactors{ |
| 68 | + .pllMul = Rcc::PllMultiplier::Mul4, |
| 69 | + .pllDiv = 2, |
| 70 | + .enableHsiPrediv4 = false |
| 71 | + }; |
| 72 | + Rcc::enablePll(Rcc::PllSource::Hsi16, pllFactors); |
| 73 | + // set flash latency for 32MHz |
| 74 | + Rcc::setFlashLatency<Frequency>(); |
| 75 | + // switch system clock to PLL output |
| 76 | + Rcc::enableSystemClock(Rcc::SystemClockSource::Pll); |
| 77 | + Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1); |
| 78 | + Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div1); |
| 79 | + Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1); |
| 80 | + // update frequencies for busy-wait delay functions |
| 81 | + Rcc::updateCoreFrequency<Frequency>(); |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +// Arduino Footprint |
| 88 | +#include "nucleo64_arduino.hpp" |
| 89 | + |
| 90 | +using Button = GpioInverted<GpioInputC13>; |
| 91 | +using LedD13 = D13; |
| 92 | + |
| 93 | +using Leds = SoftwareGpioPort< LedD13 >; |
| 94 | + |
| 95 | + |
| 96 | +namespace stlink |
| 97 | +{ |
| 98 | +using Rx = GpioInputA3; |
| 99 | +using Tx = GpioOutputA2; |
| 100 | +using Uart = Usart2; |
| 101 | +} |
| 102 | + |
| 103 | +using LoggerDevice = modm::IODeviceWrapper< stlink::Uart, modm::IOBuffer::BlockIfFull >; |
| 104 | + |
| 105 | + |
| 106 | +inline void |
| 107 | +initialize() |
| 108 | +{ |
| 109 | + SystemClock::enable(); |
| 110 | + SysTickTimer::initialize<SystemClock>(); |
| 111 | + |
| 112 | + stlink::Uart::connect<stlink::Tx::Tx, stlink::Rx::Rx>(); |
| 113 | + stlink::Uart::initialize<SystemClock, 115200_Bd>(); |
| 114 | + |
| 115 | + Button::setInput(); |
| 116 | +} |
| 117 | + |
| 118 | +} |
0 commit comments