Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SAMx7x timer support #956

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ Please [discover modm's peripheral drivers for your specific device][discover].
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center"></td>
<td align="center"></td>
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">○</td>
Expand Down
72 changes: 72 additions & 0 deletions examples/samv71_xplained_ultra/timer/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <modm/board.hpp>
#include <modm/platform/timer/timer_channel_0.hpp>
#include <modm/platform/timer/timer_channel_10.hpp>

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<Tioa0::Tioa>();

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;
}
10 changes: 10 additions & 0 deletions examples/samv71_xplained_ultra/timer/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:samv71-xplained-ultra</extends>
<options>
<option name="modm:build:build.path">../../../build/samv71_xplained_ultra/timer</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:timer:*</module>
</modules>
</library>
9 changes: 6 additions & 3 deletions src/modm/platform/timer/samg/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021, Jeff McBride
# Copyright (c) 2023, Christopher Durand
#
# This file is part of the modm project.
#
Expand Down Expand Up @@ -41,14 +42,15 @@ def init(module):

def prepare(module, options):
device = options[":target"]
if not device.has_driver("tc:samg*"):
family = device.identifier.family
if not device.has_driver("tc:sam*") or family not in ["g5x", "e7x/s7x/v7x"]:
return False

module.depends(
":cmsis:device",
":platform:gpio")

timers = device.get_all_drivers("tc:samg*")
timers = device.get_all_drivers("tc:sam*")
for driver in timers:
for instance in driver["instance"]:
instance = int(instance)
Expand All @@ -64,4 +66,5 @@ def prepare(module, options):

def build(env):
env.outbasepath = "modm/src/modm/platform/timer"
env.copy("timer_channel_base.hpp")
env.substitutions = props
env.template("timer_channel_base.hpp.in", "timer_channel_base.hpp")
Loading