|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Christopher Durand |
| 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 | +#include <modm/board.hpp> |
| 12 | + |
| 13 | +using namespace Board; |
| 14 | + |
| 15 | +// Blink LED with timer 2 interrupt. A custom handler is configured at runtime |
| 16 | +// in the vector table located in SRAM. |
| 17 | + |
| 18 | +// If the LED is blinking with a period of 1 second, the vector table has been |
| 19 | +// successfully relocated to ram. |
| 20 | +// Set the option "modm:platform:cortex-m:vector_table_location" in project.xml |
| 21 | +// to place the vector table in ram. |
| 22 | + |
| 23 | +static void tim2Handler() |
| 24 | +{ |
| 25 | + Timer2::acknowledgeInterruptFlags(Timer2::InterruptFlag::Update); |
| 26 | + LedD13::toggle(); |
| 27 | +} |
| 28 | + |
| 29 | +int |
| 30 | +main() |
| 31 | +{ |
| 32 | + Board::initialize(); |
| 33 | + LedD13::setOutput(); |
| 34 | + |
| 35 | + // Set custom handler, only works if vector table is in RAM |
| 36 | + NVIC_SetVector(TIM2_IRQn, reinterpret_cast<uintptr_t>(&tim2Handler)); |
| 37 | + |
| 38 | + Timer2::enable(); |
| 39 | + Timer2::setMode(Timer2::Mode::UpCounter); |
| 40 | + Timer2::setPeriod<Board::SystemClock>(500'000 /* us */); |
| 41 | + Timer2::applyAndReset(); |
| 42 | + Timer2::start(); |
| 43 | + Timer2::enableInterrupt(Timer2::Interrupt::Update); |
| 44 | + Timer2::enableInterruptVector(true, 5); |
| 45 | + |
| 46 | + uint32_t counter{0}; |
| 47 | + while (true) |
| 48 | + { |
| 49 | + modm::delay(100ms); |
| 50 | + MODM_LOG_INFO << "loop: " << counter++ << modm::endl; |
| 51 | + } |
| 52 | + |
| 53 | + return 0; |
| 54 | +} |
0 commit comments