-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongan_nano_chrono.hpp
466 lines (380 loc) · 17.2 KB
/
longan_nano_chrono.hpp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**********************************************************************************
MIT License
Copyright (c) 2020 Orso Eric
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**********************************************************************************/
/**********************************************************************************
** ENVIROMENT VARIABILE
**********************************************************************************/
#ifndef LONGAN_NANO_CHRONO_H_
#define LONGAN_NANO_CHRONO_H_
/**********************************************************************************
** GLOBAL INCLUDES
**********************************************************************************/
#include <gd32vf103.h>
/**********************************************************************************
** DEFINES
**********************************************************************************/
/**********************************************************************************
** MACROS
**********************************************************************************/
/**********************************************************************************
** NAMESPACE
**********************************************************************************/
//! @namespace Longan_nano namespace encapsulating all related drivers and HAL
namespace Longan_nano
{
/**********************************************************************************
** TYPEDEFS
**********************************************************************************/
/**********************************************************************************
** PROTOTYPE: STRUCTURES
**********************************************************************************/
/**********************************************************************************
** PROTOTYPE: GLOBAL VARIABILES
**********************************************************************************/
/**********************************************************************************
** PROTOTYPE: CLASS
**********************************************************************************/
/************************************************************************************/
//! @class Chrono
/************************************************************************************/
//! @author Orso Eric
//! @version 2020-07-20
//! @brief Chrono. Deals with busy delays and timestamps. Use SysTick timer.
//! @bug None
//! @copyright MIT 2020 Orso Eric
//! @details
//! SysTick
//! History Version \n
//! 2020-07-20\n
//! Rework Utils library into Chrono library to add start/stop timer
/************************************************************************************/
class Chrono
{
//Visible to all
public:
//--------------------------------------------------------------------------
// ENUM
//--------------------------------------------------------------------------
//Configurations of the SysTick
typedef enum _Config
{
systick_invalid = (uint64_t)0xffffffff, //Invalid timer value
pre = 4, //Prescaler for the systick timer
pedantic_checks = false, //Pedantic checks inside the functions
} Config;
//--------------------------------------------------------------------------
// CONSTRUCTORS
//--------------------------------------------------------------------------
/***************************************************************************/
//! @brief Empty Constructor
//! Chrono | void
/***************************************************************************/
//! @return void
//! @details \n
//! Initialize timer to invalid
/***************************************************************************/
Chrono( void )
{
//Initialize timer to invalid
this -> g_systick_start = Config::systick_invalid;
this -> g_systick_stop = Config::systick_invalid;
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
} //End Constructor: Chrono | void
//--------------------------------------------------------------------------
// DESTRUCTORS
//--------------------------------------------------------------------------
/***************************************************************************/
//! @brief Empty Destructor
/***************************************************************************/
//! ~Chrono | void
//! @return void
/***************************************************************************/
~Chrono( void )
{
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
}
//--------------------------------------------------------------------------
// OPERATORS
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// SETTERS
//--------------------------------------------------------------------------
/***************************************************************************/
//! @brief public setter
//! start | void
/***************************************************************************/
//! @return void
//! @details \n
//! Start the timer
/***************************************************************************/
void start( void )
{
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//Snap timer start
this -> g_systick_start = get_timer_value();
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
} //End setter: start | void
/***************************************************************************/
//! @brief public setter
//! stop | void
/***************************************************************************/
//! @return void
//! @details \n
//! Stop the timer. Snap the stop time
/***************************************************************************/
void stop( void )
{
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//Snap timer start
this -> g_systick_stop = get_timer_value();
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
} //End setter: stop | void
//--------------------------------------------------------------------------
// GETTERS
//--------------------------------------------------------------------------
/***************************************************************************/
//! @brief public setter
//! elapsed_ms | void
/***************************************************************************/
//! @return int | elapsed milliseconds. negative mean the timer was uninitialized
//! @details \n
//! Time elapsed between start and stop
/***************************************************************************/
int elapsed_ms( void )
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
uint64_t systick_tmp;
int ret;
//Ticks required to count 1uS
int numticks_ms = SystemCoreClock /1000 /pre;
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//If: a timetamp is invalid
if ((this -> g_systick_start == Config::systick_invalid) || (this -> g_systick_stop == Config::systick_invalid))
{
return -1; //Invalid
}
//Compute DeltaT
systick_tmp = this -> g_systick_stop -this -> g_systick_start;
//@todo: add rounding
//Compute elapsed time in ms
systick_tmp /= numticks_ms;
ret = systick_tmp;
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return ret;
} //End setter: elapsed_ms | void
/***************************************************************************/
//! @brief public setter
//! elapsed_us | void
/***************************************************************************/
//! @return int | elapsed microseconds. negative mean the timer was uninitialized
//! @details \n
//! Time elapsed between start and stop
/***************************************************************************/
int elapsed_us( void )
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
uint64_t systick_tmp;
int ret;
//Ticks required to count 1uS
int numticks_us = SystemCoreClock /1000000 /pre;
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//If: a timetamp is invalid
if ((this -> g_systick_start == Config::systick_invalid) || (this -> g_systick_stop == Config::systick_invalid))
{
return -1; //Invalid
}
//Compute DeltaT
systick_tmp = this -> g_systick_stop -this -> g_systick_start;
//@todo: add rounding
//Compute elapsed time in ms
systick_tmp /= numticks_us;
ret = systick_tmp;
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return ret;
} //End setter: elapsed_us | void
/***************************************************************************/
//! @brief public static method
//! get_systick_freq | void
/***************************************************************************/
//! @return unsigned int | frequency of the SysTick timer
//! @details \n
//! The SysTick timer is tied to the CPU clock prescaled by four
/***************************************************************************/
static unsigned int get_systick_freq( void )
{
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return SystemCoreClock /Config::pre;
} //End static method: get_systick_freq | void
//--------------------------------------------------------------------------
// TESTERS
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// PUBLIC METHODS
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// PUBLIC STATIC METHODS
//--------------------------------------------------------------------------
/***************************************************************************/
//! @brief public static method
//! delay_ms | unsigned int
/***************************************************************************/
//! @param delay_ms | unsigned int | how long to wait for in milliseconds
//! @return void |
//! @details \n
//! Use the SysTick timer counter to busy wait for the correct number of microseconds
//! The CPU SysTick timer is clocked by the ABH clock/4 = 27MHz
//! SystemCoreClock defines the frequency of the CPU in Hz
/***************************************************************************/
static void delay_ms( unsigned int delay_ms )
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//Temp timestamp
uint64_t systick_tmp;
//Compute final timestamp
uint64_t systick_stop;
//Ticks required to count 1mS
int numticks_ms = SystemCoreClock /1000 /pre;
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
// Wait for the correct number of ticks
//Snap start
systick_stop = get_timer_value();
//Compute stop time.
systick_stop += (uint64_t)numticks_ms *delay_ms;
//Wait an additional tick for current tick
systick_stop++;
//Busy wait for time to pass
do
{
//Snap timestamp
systick_tmp = get_timer_value();
}
while( systick_tmp < systick_stop );
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
} //End static method: delay_ms | unsigned int
/***************************************************************************/
//! @brief public static method
//! delay_us | unsigned int
/***************************************************************************/
//! @param delay_us | unsigned int | how long to wait for in microseconds
//! @return void |
//! @details \n
//! Use the SysTick timer counter to busy wait for the correct number of microseconds
//! The CPU SysTick timer is clocked by the ABH clock/4 = 27MHz
//! SystemCoreClock defines the frequency of the CPU in Hz
/***************************************************************************/
static void delay_us( unsigned int delay_us )
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//Temp timestamp
uint64_t systick_tmp;
//Compute final timestamp
uint64_t systick_stop;
//Ticks required to count 1uS
int numticks_us = SystemCoreClock /1000000 /pre;
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
// Wait for the correct number of ticks
//Snap start
systick_stop = get_timer_value();
//Compute stop time.
systick_stop += (uint64_t)numticks_us *delay_us;
//Wait an additional tick for current tick
systick_stop++;
//Busy wait for time to pass
do
{
//Snap timestamp
systick_tmp = get_timer_value();
}
while( systick_tmp < systick_stop );
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
} //End static method: delay_us | unsigned int
//--------------------------------------------------------------------------
// PUBLIC VARS
//--------------------------------------------------------------------------
//Visible to derived classes
protected:
//--------------------------------------------------------------------------
// PROTECTED METHODS
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// PROTECTED VARS
//--------------------------------------------------------------------------
//Visible only inside the class
private:
//--------------------------------------------------------------------------
// PRIVATE METHODS
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// PRIVATE VARS
//--------------------------------------------------------------------------
//Systick Timestamps
uint64_t g_systick_start;
uint64_t g_systick_stop;
}; //End Class: Chrono
/**********************************************************************************
** NAMESPACE
**********************************************************************************/
} //End Namespace: Longan_nano
#else
#warning "Multiple inclusion of hader file LONGAN_NANO_CHRONO_H_"
#endif