diff --git a/examples/samv71_xplained_ultra/timer/main.cpp b/examples/samv71_xplained_ultra/timer/main.cpp new file mode 100644 index 0000000000..6ee46dd7bc --- /dev/null +++ b/examples/samv71_xplained_ultra/timer/main.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023, Christopher Durand + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +// ---------------------------------------------------------------------------- + +#include +#include +#include + +using namespace Board; + +// use timer channel 10 to toggle led +MODM_ISR(TC10) +{ + // clear interrupt flags by reading + (void) TimerChannel10::getInterruptFlags(); + Led0::toggle(); +} + +using Tioa0 = GpioA0; + + +int main() +{ + Board::initialize(); + + MODM_LOG_INFO << "Timer / Counter Test" << modm::endl; + + // generate 25% duty-cycle 100 kHz PWM waveform on TIOA0 output (GpioA0, output labeled D3 on board) + TimerChannel0::initialize(); + TimerChannel0::connect(); + + TimerChannel0::setClockSource(TimerChannel0::ClockSource::Mck); + TimerChannel0::setWaveformMode(true); + // Up-counter, reset on register C compare match + TimerChannel0::setWaveformSelection(TimerChannel0::WavSel::Up_Rc); + + // Clear output on register A match, set on register C match + TimerChannel0::setTioaEffects(TimerChannel0::TioEffect::Clear, TimerChannel0::TioEffect::Set); + // period MCLK = 150 MHz / 1500 = 100 kHz + // duty-cycle 375 / 1500 = 25% + TimerChannel0::setRegA(375); + TimerChannel0::setRegC(1500); + TimerChannel0::enable(); + TimerChannel0::start(); + + // setup timer channel 10 to run interrupt at ~1 Hz from ~32 kHz internal slow clock + TimerChannel10::initialize(); + TimerChannel10::setClockSource(TimerChannel10::ClockSource::Slck); + // Toggle every 16384 / 32768 kHz = 0.5s => 1 Hz period + TimerChannel10::setRegC(16384); + TimerChannel10::setWaveformMode(true); + TimerChannel10::setWaveformSelection(TimerChannel10::WavSel::Up_Rc); + TimerChannel10::enableInterruptVector(true); + TimerChannel10::enableInterrupt(TimerChannel10::Interrupt::RcCompare); + TimerChannel10::enable(); + TimerChannel10::start(); + + while (true) + { + Led1::toggle(); + modm::delay(500ms); + } + + return 0; +} diff --git a/examples/samv71_xplained_ultra/timer/project.xml b/examples/samv71_xplained_ultra/timer/project.xml new file mode 100644 index 0000000000..be65ad7951 --- /dev/null +++ b/examples/samv71_xplained_ultra/timer/project.xml @@ -0,0 +1,10 @@ + + modm:samv71-xplained-ultra + + + + + modm:build:scons + modm:platform:timer:* + +