-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sam] Add driver for SAMG timer channels
- Loading branch information
Showing
8 changed files
with
600 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2021, Jeff McBride | ||
* | ||
* 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/io/iostream.hpp> | ||
#include <modm/platform.hpp> | ||
|
||
using namespace modm::platform; | ||
using namespace modm::literals; | ||
|
||
MODM_ISR(TC3) | ||
{ | ||
// Clear pending interrupts by reading them | ||
(void)TimerChannel3::getInterruptFlags(); | ||
|
||
static bool flag = false; | ||
if (flag) | ||
{ | ||
TimerChannel0::setTiobEffects(TimerChannel0::TioEffect::Clear, | ||
TimerChannel0::TioEffect::Set); | ||
} else | ||
{ | ||
TimerChannel0::setTiobEffects(TimerChannel0::TioEffect::Set, | ||
TimerChannel0::TioEffect::Clear); | ||
} | ||
flag = !flag; | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
/* | ||
* This example uses channel 0 to generate two output waveforms, and channel3 | ||
* to create a periodic IRQ which swaps the polarity on the TIOB output. | ||
* | ||
* Note: | ||
* On the SAMG55, the waveform outputs on the second module (TC1, TimerChannel[3-5]) | ||
* are not connected to external pins. | ||
*/ | ||
Board::initialize(); | ||
|
||
TimerChannel0::initialize(); | ||
TimerChannel0::connect<GpioA0::Tioa, GpioA1::Tiob>(); | ||
|
||
TimerChannel0::setClockSource(TimerChannel0::ClockSource::MckDiv2); | ||
TimerChannel0::setWaveformMode(true); | ||
// Setup timer to count up, and trigger reset on Rc match | ||
TimerChannel0::setWaveformSelection(TimerChannel0::WavSel::Up_Rc); | ||
|
||
// Setup TioA to set on RA match, and clear on RC match | ||
TimerChannel0::setTioaEffects(TimerChannel0::TioEffect::Set, TimerChannel0::TioEffect::Clear); | ||
// Setup TioB to clear on RB match, and set on RC match | ||
TimerChannel0::setTiobEffects(TimerChannel0::TioEffect::Clear, TimerChannel0::TioEffect::Set); | ||
|
||
// Change external event source, so that TIOB can be used as an output | ||
TimerChannel0::setExtEventSource(TimerChannel0::ExtEventSource::Xc0); | ||
|
||
TimerChannel0::setRegA(10000); | ||
TimerChannel0::setRegB(5000); | ||
TimerChannel0::setRegC(15000); | ||
|
||
TimerChannel0::enable(); | ||
TimerChannel0::start(); | ||
|
||
// Setup TC3 irq to swap TIOB polarity periodically | ||
// Period = 128 * 10000 / 120MHz = ~10.6ms | ||
TimerChannel3::initialize(); | ||
TimerChannel3::setClockSource(TimerChannel0::ClockSource::MckDiv128); | ||
TimerChannel3::setRegC(10000); | ||
TimerChannel3::setWaveformMode(true); | ||
TimerChannel3::setWaveformSelection(TimerChannel0::WavSel::Up_Rc); | ||
TimerChannel3::enableInterruptVector(true); | ||
TimerChannel3::enableInterrupt(TimerChannel3::Interrupt::RcCompare); | ||
TimerChannel3::enable(); | ||
TimerChannel3::start(); | ||
|
||
while (true) | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
# Configure for ATSAMG55-XPRO dev board | ||
source [find interface/cmsis-dap.cfg] | ||
|
||
set CHIPNAME ATSAMG55J19 | ||
source [find target/at91samg5x.cfg] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<library> | ||
<extends>modm:samg55-xplained-pro</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/samg55_xplained_pro/timer</option> | ||
<option name="modm:build:openocd.cfg">openocd.cfg</option> | ||
</options> | ||
<modules> | ||
<module>modm:build:scons</module> | ||
<module>modm:platform:timer:0</module> | ||
<module>modm:platform:timer:1</module> | ||
<module>modm:platform:timer:2</module> | ||
<module>modm:platform:timer:3</module> | ||
</modules> | ||
</library> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021, Jeff McBride | ||
# | ||
# 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/. | ||
# ----------------------------------------------------------------------------- | ||
|
||
props = {} | ||
class Instance(Module): | ||
def __init__(self, driver, instance, channel): | ||
self.driver = driver | ||
self.instance = int(instance) | ||
self.channel = channel | ||
self.vectors = None | ||
|
||
def init(self, module): | ||
module.name = str(self.instance) | ||
module.description = "Instance {}".format(self.instance) | ||
|
||
def prepare(self, module, options): | ||
return True | ||
|
||
def build(self, env): | ||
global props | ||
props["id"] = self.instance | ||
props["module"] = int(self.instance / 3) | ||
props["channel"] = self.channel | ||
|
||
env.substitutions = props | ||
env.outbasepath = "modm/src/modm/platform/timer" | ||
env.template("timer_channel.hpp.in", "timer_channel_{}.hpp".format(self.instance)) | ||
|
||
def init(module): | ||
module.name = ":platform:timer" | ||
module.description = "Timers (TC)" | ||
|
||
def prepare(module, options): | ||
device = options[":target"] | ||
if not device.has_driver("tc:samg*"): | ||
return False | ||
|
||
module.depends( | ||
":cmsis:device", | ||
":platform:gpio") | ||
|
||
timers = device.get_all_drivers("tc:samg*") | ||
for driver in timers: | ||
for instance in driver["instance"]: | ||
instance = int(instance) | ||
# Each TC instance has 3 channels, and we create an independent driver for each | ||
for channel in range(3): | ||
module.add_submodule(Instance(driver, instance*3+channel, channel)) | ||
|
||
global props | ||
device = options[":target"] | ||
props["target"] = device.identifier | ||
|
||
return True | ||
|
||
def build(env): | ||
env.outbasepath = "modm/src/modm/platform/timer" | ||
env.copy("timer_channel_base.hpp") |
Oops, something went wrong.