Skip to content

Commit ce67472

Browse files
committed
[clock] Adapt all uses of modm::Clock and Timers
1 parent 00b8f79 commit ce67472

File tree

107 files changed

+397
-413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+397
-413
lines changed

examples/avr/adc/oversample/main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using namespace modm::platform;
1717
using namespace modm::literals;
18+
using namespace std::chrono_literals;
1819

1920
// Create a new UART object
2021

@@ -32,7 +33,7 @@ typedef modm::AdcSampler< AdcInterrupt, 3, 32 > sensors;
3233
// the results are up to 16 bit wide
3334
sensors::DataType sensorData[3];
3435

35-
modm::ShortTimeout timeout(100);
36+
modm::ShortTimeout timeout(100ms);
3637

3738
int
3839
main()
@@ -67,7 +68,7 @@ main()
6768

6869
// start another readout
6970
sensors::startReadout();
70-
timeout.restart(200);
71+
timeout.restart(200ms);
7172
}
7273
}
7374
}

examples/avr/block_device_mirror/main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <stdlib.h>
1818

1919
using namespace modm::platform;
20-
typedef modm::platform::SystemClock clock;
2120

2221
// Create a new UART object and configure it to a baudrate of 115200
2322
Uart0 uart;

examples/avr/can/mcp2515_uart/main.cpp

+3-16
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
// ----------------------------------------------------------------------------
1313

1414
#include <modm/platform.hpp>
15-
#include <modm/architecture/interface/interrupt.hpp>
16-
1715
#include <modm/driver/can/mcp2515.hpp>
1816
#include <modm/processing/timer.hpp>
1917

2018
using namespace modm::platform;
2119
using namespace modm::literals;
20+
using namespace std::chrono_literals;
2221

2322
typedef GpioOutputB0 LedGreen;
2423
typedef GpioOutputB1 LedRed;
@@ -49,25 +48,13 @@ FLASH_STORAGE(uint8_t canFilter[]) =
4948
MCP2515_FILTER_EXTENDED(0), // Mask 1
5049
};
5150

52-
// timer interrupt routine
53-
MODM_ISR(TIMER2_COMPA)
54-
{
55-
modm::Clock::increment();
56-
}
57-
5851
int
5952
main()
6053
{
54+
SystemClock::enable();
6155
LedGreen::setOutput(modm::Gpio::High);
6256
LedRed::setOutput(modm::Gpio::Low);
6357

64-
// timer initialization
65-
// compare-match-interrupt every 1 ms at 14.7456 MHz
66-
TCCR2A = (1 << WGM21);
67-
TCCR2B = (1 << CS22);
68-
TIMSK2 = (1 << OCIE2A);
69-
OCR2A = 230;
70-
7158
Uart0::connect<GpioD1::Txd, GpioD0::Rxd>();
7259
Uart0::initialize<SystemClock, 115200_Bd>();
7360

@@ -99,7 +86,7 @@ main()
9986

10087
mcp2515.sendMessage(message);
10188

102-
modm::ShortPeriodicTimer timer(200);
89+
modm::ShortPeriodicTimer timer(200ms);
10390
while (true)
10491
{
10592
if (mcp2515.isMessageAvailable())

examples/avr/display/dogm128/benchmark/main.cpp

+3-14
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,11 @@ modm::DogM128< lcd::SPI, lcd::Cs, lcd::A0, lcd::Reset, true > display;
4747

4848
using namespace modm::glcd;
4949

50-
// timer interrupt routine
51-
MODM_ISR(TIMER2_COMPA)
52-
{
53-
modm::Clock::increment();
54-
}
55-
5650
void
5751
setup()
5852
{
53+
SystemClock::enable();
54+
5955
led::R::set();
6056
led::G::set();
6157
led::B::reset();
@@ -67,13 +63,6 @@ setup()
6763
lcd::SPI::connect<lcd::Sck::BitBang, lcd::Mosi::BitBang>();
6864
lcd::SPI::initialize<SystemClock, 2_MHz>();
6965

70-
// timer initialization
71-
// compare-match-interrupt every 1 ms at 14.7456 MHz
72-
TCCR2A = (1 << WGM21);
73-
TCCR2B = (1 << CS22);
74-
TIMSK2 = (1 << OCIE2A);
75-
OCR2A = 230;
76-
7766
// enable interrupts
7867
enableInterrupts();
7968

@@ -165,7 +154,7 @@ main()
165154

166155
display.setFont(modm::font::FixedWidth5x8);
167156

168-
modm::ShortPeriodicTimer timer(1000);
157+
modm::ShortPeriodicTimer timer(1s);
169158
while (true)
170159
{
171160
uint8_t iter = 0;

examples/avr/protothread/main.cpp

+14-25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <modm/processing/timer.hpp>
1818

1919
using namespace modm::platform;
20+
using namespace std::chrono_literals;
2021

2122
typedef GpioOutputB0 LedGreen;
2223
typedef GpioOutputB1 LedRed;
@@ -37,13 +38,13 @@ class BlinkingLightGreen : public modm::pt::Protothread
3738
{
3839
LedGreen::set();
3940

40-
this->timeout.restart(100);
41-
PT_WAIT_UNTIL(this->timeout.isExpired());
41+
timeout.restart(100ms);
42+
PT_WAIT_UNTIL(timeout.isExpired());
4243

4344
LedGreen::reset();
4445

45-
this->timeout.restart(600);
46-
PT_WAIT_UNTIL(this->timeout.isExpired());
46+
timeout.restart(600ms);
47+
PT_WAIT_UNTIL(timeout.isExpired());
4748
}
4849

4950
PT_END();
@@ -69,23 +70,23 @@ class BlinkingLightRed : public modm::pt::Protothread
6970
{
7071
LedRed::set();
7172

72-
this->timeout.restart(200);
73-
PT_WAIT_UNTIL(this->timeout.isExpired());
73+
timeout.restart(200ms);
74+
PT_WAIT_UNTIL(timeout.isExpired());
7475

7576
LedRed::reset();
7677

77-
this->timeout.restart(300);
78-
PT_WAIT_UNTIL(this->timeout.isExpired());
78+
timeout.restart(300ms);
79+
PT_WAIT_UNTIL(timeout.isExpired());
7980

8081
LedRed::set();
8182

82-
this->timeout.restart(200);
83-
PT_WAIT_UNTIL(this->timeout.isExpired());
83+
timeout.restart(200ms);
84+
PT_WAIT_UNTIL(timeout.isExpired());
8485

8586
LedRed::reset();
8687

87-
this->timeout.restart(1000);
88-
PT_WAIT_UNTIL(this->timeout.isExpired());
88+
timeout.restart(1s);
89+
PT_WAIT_UNTIL(timeout.isExpired());
8990
}
9091

9192
PT_END();
@@ -95,22 +96,10 @@ class BlinkingLightRed : public modm::pt::Protothread
9596
modm::ShortTimeout timeout;
9697
};
9798

98-
// timer interrupt routine
99-
MODM_ISR(TIMER2_COMPA)
100-
{
101-
modm::Clock::increment();
102-
}
103-
10499
int
105100
main()
106101
{
107-
// timeout initialization
108-
// compare-match-interrupt every 1 ms at 14.7456 MHz
109-
TCCR2A = (1 << WGM21);
110-
TCCR2B = (1 << CS22);
111-
TIMSK2 = (1 << OCIE2A);
112-
OCR2A = 230;
113-
102+
SystemClock::enable();
114103
enableInterrupts();
115104

116105
BlinkingLightGreen greenLight;

examples/avr/protothread/project.xml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</options>
77
<modules>
88
<module>modm:platform:core</module>
9+
<module>modm:platform:clock</module>
910
<module>modm:platform:gpio</module>
1011
<module>modm:processing:protothread</module>
1112
<module>modm:processing:timer</module>

examples/avr/timeout/main.cpp

+4-15
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,31 @@
1212
// ----------------------------------------------------------------------------
1313

1414
#include <modm/platform.hpp>
15-
#include <modm/architecture/interface/interrupt.hpp>
1615
#include <modm/processing/timer.hpp>
1716

1817
using namespace modm::platform;
18+
using namespace std::chrono_literals;
1919

2020
// create a output device for the led
2121
typedef GpioOutputB0 Led;
2222

23-
// timer interrupt routine
24-
MODM_ISR(TIMER2_COMPA)
25-
{
26-
modm::Clock::increment();
27-
}
2823

2924
int
3025
main()
3126
{
27+
SystemClock::enable();
3228
Led::setOutput();
3329
Led::reset();
3430

35-
// timer initialization
36-
// compare-match-interrupt every 1 ms at 14.7456 MHz
37-
TCCR2A = (1 << WGM21);
38-
TCCR2B = (1 << CS22);
39-
TIMSK2 = (1 << OCIE2A);
40-
OCR2A = 230;
41-
4231
// enable interrupts
4332
enableInterrupts();
4433

45-
modm::ShortTimeout timeout(200);
34+
modm::ShortTimeout timeout(200ms);
4635
while (true)
4736
{
4837
if (timeout.isExpired())
4938
{
50-
timeout.restart(200);
39+
timeout.restart(200ms);
5140
Led::toggle();
5241
}
5342
}

examples/avr/timeout/project.xml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<modules>
88
<module>modm:architecture:interrupt</module>
99
<module>modm:platform:core</module>
10+
<module>modm:platform:clock</module>
1011
<module>modm:platform:gpio</module>
1112
<module>modm:processing:timer</module>
1213
<module>modm:build:scons</module>

examples/avr/timer/main.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2020, Niklas Hauser
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+
12+
#include <modm/board.hpp>
13+
#include <modm/processing.hpp>
14+
15+
int main()
16+
{
17+
Board::initialize();
18+
19+
uint32_t counter(0);
20+
modm::PrecisePeriodicTimer ptmr(0.500990s);
21+
modm::PeriodicTimer tmr(0.500990s);
22+
23+
for (int ii=0; ii<20; ii++)
24+
{
25+
LedD13::toggle();
26+
modm::delay(std::chrono::milliseconds(10*ii));
27+
}
28+
29+
uint32_t ms_counter{0};
30+
uint32_t us_counter{0};
31+
32+
while (true)
33+
{
34+
{
35+
uint32_t ms = modm::Clock::now().time_since_epoch().count();
36+
if (ms < ms_counter) {
37+
MODM_LOG_ERROR << ms << " < " << ms_counter << modm::endl;
38+
}
39+
ms_counter = ms;
40+
}{
41+
uint32_t us = modm::PreciseClock::now().time_since_epoch().count();
42+
if (us < us_counter) {
43+
MODM_LOG_ERROR << us << " < " << us_counter << modm::endl;
44+
}
45+
us_counter = us;
46+
}
47+
48+
if (ptmr.execute())
49+
{
50+
LedD13::set();
51+
MODM_LOG_INFO << "loop: " << counter++ << modm::endl;
52+
}
53+
if (tmr.execute())
54+
{
55+
LedD13::reset();
56+
}
57+
}
58+
}

examples/avr/timer/project.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<library>
2+
<extends>modm:arduino-nano</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/avr/timer</option>
5+
</options>
6+
<modules>
7+
<module>modm:architecture:delay</module>
8+
<module>modm:processing:timer</module>
9+
<module>modm:build:scons</module>
10+
<module>modm:build:cmake</module>
11+
</modules>
12+
</library>

examples/avr/uart/basic_mega8/main.cpp

-39
This file was deleted.

examples/avr/uart/basic_mega8/project.xml

-14
This file was deleted.

0 commit comments

Comments
 (0)