-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathCosaBenchmarkUART.ino
233 lines (209 loc) · 7.85 KB
/
CosaBenchmarkUART.ino
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* @file CosaBenchmarkUART.ino
* @version 1.0
*
* @section License
* Copyright (C) 2013-2015, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* @section Description
* Benchmarking IOStream and UART functions; measure time to print
* characters, strings and numbers through the IOStream interface and
* IOBuffer to the UART.
*
* This file is part of the Arduino Che Cosa project.
*/
#include "Cosa/RTT.hh"
#include "Cosa/Watchdog.hh"
#include "Cosa/Trace.hh"
#include "Cosa/UART.hh"
#include <math.h>
// This benchmark can be run with a background interrupt load. Set the
// below symbol to enable a periodic job with the given period in
// micro-seconds.
// #define BACKGROUND_PULSE 150
#define PULSE_WIDTH 50
#if defined(BACKGROUND_PULSE)
#include "Cosa/OutputPin.hh"
#include "Cosa/Periodic.hh"
// Schedule from interrupt with time or fixed period
#define USE_ISR_TIME_PERIOD
// #define USE_ISR_RESCHEDULE
class Pulse : public Periodic {
public:
Pulse(Job::Scheduler* scheduler, uint32_t delay, Board::DigitalPin pin) :
Periodic(scheduler, delay),
m_pin(pin)
{
expire_at(100000UL);
}
#if defined(USE_ISR_TIME_PERIOD)
virtual void on_expired()
{
run();
expire_at(time() + period());
start();
}
#elif defined(USE_ISR_RESCHEDULE)
virtual void on_expired()
{
run();
reschedule();
}
#else
#error "on_expired: not defined"
#endif
virtual void run()
{
m_pin.toggle();
#if defined(PULSE_WIDTH)
DELAY(PULSE_WIDTH);
#endif
m_pin.toggle();
}
private:
OutputPin m_pin;
};
// The RTT job scheduler and background pulse generator
RTT::Scheduler scheduler;
Pulse background(&scheduler, BACKGROUND_PULSE, Board::D7);
#endif
void setup()
{
// Start serial output with given baud-rate
uart.begin(1000000);
// uart.begin(2000000);
// uart.begin(1000000);
// uart.begin(500000);
// uart.begin(250000);
// uart.begin(230400);
// uart.begin(115200);
// uart.begin(57600);
// uart.begin(38400);
// uart.begin(28800);
// uart.begin(19200);
// uart.begin(14400);
// uart.begin(9600);
trace.begin(&uart, PSTR("CosaBenchmarkUART: started"));
#if defined(BACKGROUND_PULSE)
ASSERT(background.start());
trace << PSTR("Background:") << BACKGROUND_PULSE << PSTR(" us") << endl;
#endif
// Start timers
Watchdog::begin();
RTT::begin();
}
void loop()
{
// Measure time to print character, string and number
MEASURE("one character (new-line):", 1) trace << '\n' << endl;
MEASURE("one character:", 1) trace << '1' << endl;
MEASURE("one character string:", 1) trace << PSTR("1") << endl;
MEASURE("utf8 character string:", 1) trace << PSTR("€") << endl;
MEASURE("integer:", 1) trace << 1 << endl;
MEASURE("long integer:", 1) trace << 1L << endl;
MEASURE("two characters:", 1) trace << '1' << '0' << endl;
MEASURE("two character string:", 1) trace << PSTR("10") << endl;
MEASURE("integer:", 1) trace << 10 << endl;
MEASURE("long integer:", 1) trace << 10L << endl;
MEASURE("floating point:", 1) trace << 10.0 << endl;
MEASURE("three characters:", 1) trace << '1' << '0' << '0' << endl;
MEASURE("three character string:", 1) trace << PSTR("100") << endl;
MEASURE("integer:", 1) trace << 100 << endl;
MEASURE("long integer:", 1) trace << 100L << endl;
MEASURE("floating point:", 1) trace << 100.0 << endl;
// Measure time to print min/max integer base(10)
MEASURE("zero int16_t:", 1) trace << 0 << endl;
MEASURE("min int16_t:", 1) trace << INT16_MIN << endl;
MEASURE("max int16_t:", 1) trace << INT16_MAX << endl;
MEASURE("max uint16_t:", 1) trace << UINT16_MAX << endl;
MEASURE("zero int32_t:", 1) trace << 0L << endl;
MEASURE("min int32_t:", 1) trace << INT32_MIN << endl;
MEASURE("max int32_t:", 1) trace << INT32_MAX << endl;
MEASURE("max uint32_t:", 1) trace << UINT32_MAX << endl;
// Measure time to print min/max integer base(2)
MEASURE("zero int16_t:", 1) trace << bin << 0 << endl;
MEASURE("min int16_t:", 1) trace << bin << INT16_MIN << endl;
MEASURE("max int16_t:", 1) trace << bin << INT16_MAX << endl;
MEASURE("max uint16_t:", 1) trace << bin << UINT16_MAX << endl;
MEASURE("zero int32_t:", 1) trace << bin << 0L << endl;
MEASURE("min int32_t:", 1) trace << bin << INT32_MIN << endl;
MEASURE("max int32_t:", 1) trace << bin << INT32_MAX << endl;
MEASURE("max uint32_t:", 1) trace << bin << UINT32_MAX << endl;
// Measure time to print min/max integer base(8)
MEASURE("zero int16_t:", 1) trace << oct << 0 << endl;
MEASURE("min int16_t:", 1) trace << oct << INT16_MIN << endl;
MEASURE("max int16_t:", 1) trace << oct << INT16_MAX << endl;
MEASURE("max uint16_t:", 1) trace << oct << UINT16_MAX << endl;
MEASURE("zero int32_t:", 1) trace << oct << 0L << endl;
MEASURE("min int32_t:", 1) trace << oct << INT32_MIN << endl;
MEASURE("max int32_t:", 1) trace << oct << INT32_MAX << endl;
MEASURE("max uint32_t:", 1) trace << oct << UINT32_MAX << endl;
// Measure time to print min/max integer base(16)
MEASURE("zero int16_t:", 1) trace << hex << 0 << endl;
MEASURE("min int16_t:", 1) trace << hex << INT16_MIN << endl;
MEASURE("max int16_t:", 1) trace << hex << INT16_MAX << endl;
MEASURE("max uint16_t:", 1) trace << hex << UINT16_MAX << endl;
MEASURE("zero int32_t:", 1) trace << hex << 0L << endl;
MEASURE("min int32_t:", 1) trace << hex << INT32_MIN << endl;
MEASURE("max int32_t:", 1) trace << hex << INT32_MAX << endl;
MEASURE("max uint32_t:", 1) trace << hex << UINT32_MAX << endl;
// Measure time to print some standar floating point numbers
MEASURE("floating point (zero):", 1) trace << 0.0 << endl;
MEASURE("floating point (nan):", 1) trace << NAN << endl;
MEASURE("floating point (infinity):", 1) trace << INFINITY << endl;
MEASURE("floating point (pi):", 1) trace << M_PI << endl;
MEASURE("floating point (e):", 1) trace << M_E << endl;
MEASURE("floating point (log2e):", 1) trace << M_LOG2E << endl;
MEASURE("floating point (log10e):", 1) trace << M_LOG10E << endl;
MEASURE("floating point (logn2):", 1) trace << M_LN2 << endl;
MEASURE("floating point (logn10):", 1) trace << M_LN10 << endl;
// Measure time to print unsigned integer; 16 and 32 bit.
MEASURE("uint16_t(digits=6):", 1) {
trace.print(100U, 6, IOStream::dec);
trace.println();
}
MEASURE("uint32_t(digits=6):", 1) {
trace.print(100UL, 6, IOStream::dec);
trace.println();
}
// Measure time to print some special characters
MEASURE("newline character:", 1) trace << '\n';
MEASURE("tab:", 1) trace << '\t' << endl;
MEASURE("newline string(1):", 1) trace << (char*) "\n";
MEASURE("newline string(2):", 1) trace << (char*) "\n\n";
// Measure time to print all characters
str_P s = PSTR(" !\"#$%&'()*+,-./0123456789:;<=>?@"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
"abcdefghijklmnopqrstuvwxyz{|}~\r\n");
const uint8_t LINES = 10;
// Use 10 ms delay before and after as markers for Logic Analyzer
delay(10);
MEASURE("performance:", 1) {
for (uint8_t i = 0; i < LINES; i++) trace << s;
trace.flush();
}
delay(10);
uint32_t BITS = 11;
uint16_t chars = LINES * strlen_P(s);
uint32_t Kbps = (BITS * chars * 1000UL) / trace.measure;
trace << PSTR("effective baudrate (") << chars << PSTR(" characters):")
<< Kbps << PSTR(" Kbps")
<< endl;
#if defined(BACKGROUND_PULSE)
uint16_t pulses = trace.measure / BACKGROUND_PULSE;
trace << PSTR("background pulses (") << pulses << PSTR("):")
<< (uint32_t) pulses * PULSE_WIDTH << PSTR(" us")
<< endl;
#endif
sleep(1);
}