1
+ /*
2
+ * Copyright (c) 2021, Jeff McBride
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
+ #include < modm/io/iostream.hpp>
13
+ #include < modm/platform.hpp>
14
+
15
+ using namespace modm ::platform;
16
+ using namespace modm ::literals;
17
+
18
+ MODM_ISR (TC3)
19
+ {
20
+ // Clear pending interrupts by reading them
21
+ (void )TimerChannel3::getInterruptFlags ();
22
+
23
+ static bool flag = false ;
24
+ if (flag)
25
+ {
26
+ TimerChannel0::setTiobEffects (TimerChannel0::TioEffect::Clear,
27
+ TimerChannel0::TioEffect::Set);
28
+ } else
29
+ {
30
+ TimerChannel0::setTiobEffects (TimerChannel0::TioEffect::Set,
31
+ TimerChannel0::TioEffect::Clear);
32
+ }
33
+ flag = !flag;
34
+ }
35
+
36
+ int
37
+ main ()
38
+ {
39
+ /*
40
+ * This example uses channel 0 to generate two output waveforms, and channel3
41
+ * to create a periodic IRQ which swaps the polarity on the TIOB output.
42
+ *
43
+ * Note:
44
+ * On the SAMG55, the waveform outputs on the second module (TC1, TimerChannel[3-5])
45
+ * are not connected to external pins.
46
+ */
47
+ Board::initialize ();
48
+
49
+ TimerChannel0::initialize ();
50
+ TimerChannel0::connect<GpioA0::Tioa, GpioA1::Tiob>();
51
+
52
+ TimerChannel0::setClockSource (TimerChannel0::ClockSource::MckDiv2);
53
+ TimerChannel0::setWaveformMode (true );
54
+ // Setup timer to count up, and trigger reset on Rc match
55
+ TimerChannel0::setWaveformSelection (TimerChannel0::WavSel::Up_Rc);
56
+
57
+ // Setup TioA to set on RA match, and clear on RC match
58
+ TimerChannel0::setTioaEffects (TimerChannel0::TioEffect::Set, TimerChannel0::TioEffect::Clear);
59
+ // Setup TioB to clear on RB match, and set on RC match
60
+ TimerChannel0::setTiobEffects (TimerChannel0::TioEffect::Clear, TimerChannel0::TioEffect::Set);
61
+
62
+ // Change external event source, so that TIOB can be used as an output
63
+ TimerChannel0::setExtEventSource (TimerChannel0::ExtEventSource::Xc0);
64
+
65
+ TimerChannel0::setRegA (10000 );
66
+ TimerChannel0::setRegB (5000 );
67
+ TimerChannel0::setRegC (15000 );
68
+
69
+ TimerChannel0::enable ();
70
+ TimerChannel0::start ();
71
+
72
+ // Setup TC3 irq to swap TIOB polarity periodically
73
+ // Period = 128 * 10000 / 120MHz = ~10.6ms
74
+ TimerChannel3::initialize ();
75
+ TimerChannel3::setClockSource (TimerChannel0::ClockSource::MckDiv128);
76
+ TimerChannel3::setRegC (10000 );
77
+ TimerChannel3::setWaveformMode (true );
78
+ TimerChannel3::setWaveformSelection (TimerChannel0::WavSel::Up_Rc);
79
+ TimerChannel3::enableInterruptVector (true );
80
+ TimerChannel3::enableInterrupt (TimerChannel3::Interrupt::RcCompare);
81
+ TimerChannel3::enable ();
82
+ TimerChannel3::start ();
83
+
84
+ while (true )
85
+ ;
86
+ }
0 commit comments