-
Notifications
You must be signed in to change notification settings - Fork 134
/
main.cpp
208 lines (183 loc) · 4.61 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) 2021, Niklas Hauser
*
* 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/driver/time/cycle_counter.hpp>
using namespace Board;
#ifdef CFG_TUSB_MCU
#include <modm/debug.hpp>
modm::IODeviceWrapper<UsbUart0, modm::IOBuffer::DiscardIfFull> usb_io_device;
modm::log::Logger modm::log::info(usb_io_device);
#endif
modm_fastdata modm::CycleCounter counter;
#ifdef TCNT1
constexpr uint32_t SystemCoreClock = F_CPU;
#endif
static void
run_delay_ns(uint32_t ns)
{
#ifdef CFG_TUSB_MCU
tud_task();
#endif
{
modm::atomic::Lock _;
counter.start();
modm::delay_ns(ns);
counter.stop();
}
#ifdef CFG_TUSB_MCU
tud_task();
#endif
const uint32_t cycles = counter.cycles();
const uint32_t expected = uint64_t(SystemCoreClock) * ns / 1'000'000'000ull;
const uint32_t real = counter.nanoseconds();
MODM_LOG_INFO.printf("%8lu | %7lu | %7lu | %8lu %c\n", ns, expected, cycles, real,
(cycles < expected*1.2f ? (cycles > expected*0.8f ? ' ' : '<') : '>')) << modm::flush;
#ifdef CFG_TUSB_MCU
tud_task();
#endif
}
static void
run_delay_us(uint32_t us)
{
#ifdef CFG_TUSB_MCU
tud_task();
#endif
{
modm::atomic::Lock _;
counter.start();
modm::delay_us(us);
counter.stop();
}
#ifdef CFG_TUSB_MCU
tud_task();
#endif
const uint32_t cycles = counter.cycles();
const uint32_t expected = uint64_t(SystemCoreClock) * us / 1'000'000ull;
const uint32_t real = counter.microseconds();
MODM_LOG_INFO.printf("%8lu | %8lu | %8lu | %8lu %c\n", us, expected, cycles, real,
(cycles < expected*1.2f ? (cycles > expected*0.8f ? ' ' : '<') : '>')) << modm::flush;
#ifdef CFG_TUSB_MCU
tud_task();
#endif
}
static void
run_test_ns(bool short_test=false)
{
MODM_LOG_INFO << "\nmodm::delay_ns for system clock = " << SystemCoreClock << modm::endl;
MODM_LOG_INFO << " expected | measured\n";
MODM_LOG_INFO << " ns | cycles | cycles | ns\n";
if (short_test)
{
run_delay_ns( 100);
run_delay_ns( 1'000);
run_delay_ns( 10'000);
run_delay_ns( 100'000);
run_delay_ns( 1'000'000);
run_delay_ns(10'000'000);
}
else
{
for (uint32_t ii=0; ii <= 10000; ii += 10) run_delay_ns(ii);
// run_delay_ns(1);
// run_delay_ns(5);
// run_delay_ns(10);
// for (uint32_t ii= 50; ii < 1000; ii += 50) run_delay_ns(ii);
// for (uint32_t ii=1000; ii <= 10000; ii += 100) run_delay_ns(ii);
run_delay_ns(100'000);
run_delay_ns(1'000'000);
run_delay_ns(10'000'000);
}
}
static void
run_test_us(bool short_test=false)
{
MODM_LOG_INFO << "\nmodm::delay_us for system clock = " << SystemCoreClock << modm::endl;
MODM_LOG_INFO << " expected | measured\n";
MODM_LOG_INFO << " us | cycles | cycles | us\n";
if (short_test)
{
run_delay_us( 1);
run_delay_us( 5);
run_delay_us( 10);
run_delay_us( 100);
run_delay_us( 1'000);
run_delay_us( 10'000);
run_delay_us(100'000);
}
else
{
for (uint32_t ii=1; ii <= 1000; ii += 1) run_delay_us(ii);
// for (uint32_t ii=10; ii <= 100; ii += 10) run_delay_us(ii);
// run_delay_us(1'000);
run_delay_us( 10'000);
run_delay_us(100'000);
}
}
#ifdef TCNT1
int main()
{
Board::initialize();
counter.initialize();
run_test_ns();
run_test_us();
while(true) {}
return 0;
}
#elif defined CFG_TUSB_MCU
// SAMD21
int main()
{
Board::initialize();
Board::initializeUsbFs();
counter.initialize(true);
tusb_init();
MODM_LOG_INFO << "Hello World\n";
static int32_t counter{0};
while (true)
{
tud_task();
if (counter-- < 0)
{
Leds::toggle();
counter = 1'000'000;
run_test_ns();
run_test_us();
}
}
return 0;
}
#else
// STM32 device
struct BootClock
{
// STM32L4 needs /4, STM32F0 needs /2
static constexpr float scalar = 1.0 / 1;
static constexpr uint32_t Usart1 = Rcc::BootFrequency * scalar;
static constexpr uint32_t Usart2 = Rcc::BootFrequency * scalar;
static constexpr uint32_t Usart3 = Rcc::BootFrequency * scalar;
static constexpr uint32_t Usart4 = Rcc::BootFrequency * scalar;
static constexpr uint32_t Usart5 = Rcc::BootFrequency * scalar;
};
int main()
{
Board::stlink::Uart::connect<Board::stlink::Tx::Tx>();
Board::stlink::Uart::initialize<BootClock, 115200_Bd, 5_pct>();
counter.initialize(true);
run_test_ns();
run_test_us();
Board::initialize();
counter.initialize(true);
run_test_ns();
run_test_us();
while(true) {}
return 0;
}
#endif