forked from 3devo/ChildbusBootloader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
timing_precise.h
180 lines (165 loc) · 5.73 KB
/
timing_precise.h
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
/**
* This file was copied from Marlin/Marlin/HAL/shared/Delay.h
* and adapted by removing Marlin's macros and unsupported architectures.
* Naming convention was changed to be more in line with Buddy
* coding standard.
* Macro DELAY_MS was changed to force inline delay_us_precise function.
* Support for non-constexpr parameter of DELAY_NS_PRECISE was
* removed as I considered it broken.
*
* @file
*/
#pragma once
#include <stdint.h>
#include <limits>
#include "Config.h"
#define FORCE_INLINE __attribute__((always_inline)) inline
#if defined(__arm__) || defined(__thumb__)
// https://blueprints.launchpad.net/gcc-arm-embedded/+spec/delay-cycles
/**
* @brief Delay number of multiplies of 4 CPU cycles
*
* Implementation detail, use timing_delay_cycles() instead.
*
* @param cy number of multiplies of 4 CPU cycles
*/
FORCE_INLINE static void timing_delay_4cycles(uint32_t cy) { // +1 cycle
#if ARCH_PIPELINE_RELOAD_CYCLES < 2
#define EXTRA_NOP_CYCLES " nop\n\t"
#else
#define EXTRA_NOP_CYCLES ""
#endif
__asm__ __volatile__(
" .syntax unified\n\t" // is to prevent CM0,CM1 non-unified syntax
"1:\n\t"
" subs %[cnt],#1\n\t" //
EXTRA_NOP_CYCLES
" bne 1b\n\t"
: [ cnt ] "+r"(cy) // output: +r means input+output
: // input:
: "cc" // clobbers:
);
#undef EXTRA_NOP_CYCLES
}
/**
* @brief Delay number of CPU cycles
*
* It is precise to single CPU cycle when x is compile time constant
* to 4 CPU cycles otherwise.
*
* @param x number of CPU cycles
*/
FORCE_INLINE static void timing_delay_cycles(uint32_t x) {
#define nop() __asm__ __volatile__("nop;\n\t" :: \
:)
if (__builtin_constant_p(x)) {
#define MAXNOPS 4
if (x <= (MAXNOPS)) {
switch (x) {
case 4:
nop();
/* fall-through */
case 3:
nop();
/* fall-through */
case 2:
nop();
/* fall-through */
case 1:
nop();
/* fall-through */
}
} else { // because of +1 cycle inside delay_4cycles
const uint32_t rem = (x - 1) % (MAXNOPS);
switch (rem) {
case 3:
nop();
/* fall-through */
case 2:
nop();
/* fall-through */
case 1:
nop();
/* fall-through */
}
if ((x = (x - 1) / (MAXNOPS)))
timing_delay_4cycles(x); // if need more then 4 nop loop is more optimal
}
#undef MAXNOPS
} else if ((x >>= 2))
timing_delay_4cycles(x);
#undef nop
}
#if __CORTEX_M == 7
#error "Support removed, you can get it from original Marlin source."
#endif
#elif defined(__AVR__) || defined(__PLAT_LINUX__)
#error "Support removed, you can get it from original Marlin source."
#else
#error "Unsupported MCU architecture"
#endif
/**
* @param ns time in nanoseconds
* @return number of CPU cycles
*/
FORCE_INLINE constexpr uint64_t timing_nanoseconds_to_cycles(uint64_t ns) {
return ((ns * (SYSTEM_CORE_CLOCK / 1000000UL)) / 1000UL);
}
/**
* @param us time in microseconds
* @return number of CPU cycles
*/
FORCE_INLINE constexpr uint32_t timing_microseconds_to_cycles(uint32_t us) {
return (us * (SYSTEM_CORE_CLOCK / 1000000UL));
}
/**
* @brief Delay nanoseconds
*
* Timing precision is single CPU cycle. E.g. 6 ns at 168Mhz
* It is always guaranteed to return if CPU is running.
* Correct timing is guaranteed after SystemClock_Config() call if
* caller can not be interrupted.
*
* @param ns time in nanoseconds (compile time constant)
*/
#define DELAY_NS_PRECISE(ns) \
do { \
static_assert((ns) < (std::numeric_limits<uint64_t>::max() / (SYSTEM_CORE_CLOCK / 1000000UL)), \
"ns out of range"); \
static_assert(timing_nanoseconds_to_cycles(ns) <= std::numeric_limits<uint32_t>::max(), \
"ns out of range"); \
constexpr uint32_t cycles = timing_nanoseconds_to_cycles(ns); \
timing_delay_cycles(cycles); \
} while (0)
/**
* @brief Delay microseconds
*
* Timing precision is single CPU cycle. E.g. 6 ns at 168Mhz
* It is always guaranteed to return if CPU is running.
* Correct timing is guaranteed after SystemClock_Config() call if
* caller can not be interrupted.
*
* @param us time in microseconds (compile time constant)
*/
#define DELAY_US_PRECISE(us) DELAY_NS_PRECISE(us * 1000ULL)
/**
* @brief Delay microseconds
*
* Timing precision is 1 microsecond if us is compile time constant and
* CPU clock is at least 1Mhz. Timing precision is 1 microsecond plus
* constant delay incurred by uint32_t multiplication otherwise if CPU
* clock is at least 4Mhz.
*
* It is always guaranteed to return if CPU is running.
* Correct timing is guaranteed after SystemClock_Config() call if
* caller can not be interrupted.
*
* Use DELAY_MS_PRECISE if dealing with compile time constant delay
* to get range check for free.
*
* @param us time in microseconds
* Maximum range depends on CPU clock. For 168 Mhz it is 25 565 us.
*/
FORCE_INLINE void delay_us_precise(uint32_t us) {
timing_delay_cycles(timing_microseconds_to_cycles(us));
}