|
@@ -0,0 +1,176 @@
|
|
1
|
+/**
|
|
2
|
+ * Marlin 3D Printer Firmware
|
|
3
|
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
4
|
+ *
|
|
5
|
+ * Based on Sprinter and grbl.
|
|
6
|
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
|
7
|
+ *
|
|
8
|
+ * This program is free software: you can redistribute it and/or modify
|
|
9
|
+ * it under the terms of the GNU General Public License as published by
|
|
10
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+ * (at your option) any later version.
|
|
12
|
+ *
|
|
13
|
+ * This program is distributed in the hope that it will be useful,
|
|
14
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+ * GNU General Public License for more details.
|
|
17
|
+ *
|
|
18
|
+ * You should have received a copy of the GNU General Public License
|
|
19
|
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
20
|
+ *
|
|
21
|
+ */
|
|
22
|
+#include "Delay.h"
|
|
23
|
+
|
|
24
|
+#include "../../inc/MarlinConfig.h"
|
|
25
|
+
|
|
26
|
+#if defined(__arm__) || defined(__thumb__)
|
|
27
|
+
|
|
28
|
+ static uint32_t ASM_CYCLES_PER_ITERATION = 4; // Initial bet which will be adjusted in calibrate_delay_loop
|
|
29
|
+
|
|
30
|
+ // Simple assembler loop counting down
|
|
31
|
+ void delay_asm(uint32_t cy) {
|
|
32
|
+ cy = _MAX(cy / ASM_CYCLES_PER_ITERATION, 1U); // Zero is forbidden here
|
|
33
|
+ __asm__ __volatile__(
|
|
34
|
+ A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax
|
|
35
|
+ L("1")
|
|
36
|
+ A("subs %[cnt],#1")
|
|
37
|
+ A("bne 1b")
|
|
38
|
+ : [cnt]"+r"(cy) // output: +r means input+output
|
|
39
|
+ : // input:
|
|
40
|
+ : "cc" // clobbers:
|
|
41
|
+ );
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ // We can't use CMSIS since it's not available on all platform, so fallback to hardcoded register values
|
|
45
|
+ #define HW_REG(X) *(volatile uint32_t *)(X)
|
|
46
|
+ #define _DWT_CTRL 0xE0001000
|
|
47
|
+ #define _DWT_CYCCNT 0xE0001004 // CYCCNT is 32bits, takes 37s or so to wrap.
|
|
48
|
+ #define _DEM_CR 0xE000EDFC
|
|
49
|
+ #define _LAR 0xE0001FB0
|
|
50
|
+
|
|
51
|
+ // Use hardware cycle counter instead, it's much safer
|
|
52
|
+ void delay_dwt(uint32_t count) {
|
|
53
|
+ // Reuse the ASM_CYCLES_PER_ITERATION variable to avoid wasting another useless variable
|
|
54
|
+ register uint32_t start = HW_REG(_DWT_CYCCNT) - ASM_CYCLES_PER_ITERATION, elapsed;
|
|
55
|
+ do {
|
|
56
|
+ elapsed = HW_REG(_DWT_CYCCNT) - start;
|
|
57
|
+ } while (elapsed < count);
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ // Pointer to asm function, calling the functions has a ~20 cycles overhead
|
|
61
|
+ DelayImpl DelayCycleFnc = delay_asm;
|
|
62
|
+
|
|
63
|
+ void calibrate_delay_loop() {
|
|
64
|
+ // Check if we have a working DWT implementation in the CPU (see https://developer.arm.com/documentation/ddi0439/b/Data-Watchpoint-and-Trace-Unit/DWT-Programmers-Model)
|
|
65
|
+ if (!HW_REG(_DWT_CTRL)) {
|
|
66
|
+ // No DWT present, so fallback to plain old ASM nop counting
|
|
67
|
+ // Unfortunately, we don't exactly know how many iteration it'll take to decrement a counter in a loop
|
|
68
|
+ // It depends on the CPU architecture, the code current position (flash vs SRAM)
|
|
69
|
+ // So, instead of wild guessing and making mistake, instead
|
|
70
|
+ // compute it once for all
|
|
71
|
+ ASM_CYCLES_PER_ITERATION = 1;
|
|
72
|
+ // We need to fetch some reference clock before waiting
|
|
73
|
+ cli();
|
|
74
|
+ uint32_t start = micros();
|
|
75
|
+ delay_asm(1000); // On a typical CPU running in MHz, waiting 1000 "unknown cycles" means it'll take between 1ms to 6ms, that's perfectly acceptable
|
|
76
|
+ uint32_t end = micros();
|
|
77
|
+ sei();
|
|
78
|
+ uint32_t expectedCycles = (end - start) * ((F_CPU) / 1000000UL); // Convert microseconds to cycles
|
|
79
|
+ // Finally compute the right scale
|
|
80
|
+ ASM_CYCLES_PER_ITERATION = (uint32_t)(expectedCycles / 1000);
|
|
81
|
+
|
|
82
|
+ // No DWT present, likely a Cortex M0 so NOP counting is our best bet here
|
|
83
|
+ DelayCycleFnc = delay_asm;
|
|
84
|
+ }
|
|
85
|
+ else {
|
|
86
|
+ // Enable DWT counter
|
|
87
|
+ // From https://stackoverflow.com/a/41188674/1469714
|
|
88
|
+ HW_REG(_DEM_CR) = HW_REG(_DEM_CR) | 0x01000000; // Enable trace
|
|
89
|
+ #if __CORTEX_M == 7
|
|
90
|
+ HW_REG(_LAR) = 0xC5ACCE55; // Unlock access to DWT registers, see https://developer.arm.com/documentation/ihi0029/e/ section B2.3.10
|
|
91
|
+ #endif
|
|
92
|
+ HW_REG(_DWT_CYCCNT) = 0; // Clear DWT cycle counter
|
|
93
|
+ HW_REG(_DWT_CTRL) = HW_REG(_DWT_CTRL) | 1; // Enable DWT cycle counter
|
|
94
|
+
|
|
95
|
+ // Then calibrate the constant offset from the counter
|
|
96
|
+ ASM_CYCLES_PER_ITERATION = 0;
|
|
97
|
+ uint32_t s = HW_REG(_DWT_CYCCNT);
|
|
98
|
+ uint32_t e = HW_REG(_DWT_CYCCNT); // (e - s) contains the number of cycle required to read the cycle counter
|
|
99
|
+ delay_dwt(0);
|
|
100
|
+ uint32_t f = HW_REG(_DWT_CYCCNT); // (f - e) contains the delay to call the delay function + the time to read the cycle counter
|
|
101
|
+ ASM_CYCLES_PER_ITERATION = (f - e) - (e - s);
|
|
102
|
+
|
|
103
|
+ // Use safer DWT function
|
|
104
|
+ DelayCycleFnc = delay_dwt;
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ #if ENABLED(MARLIN_DEV_MODE)
|
|
109
|
+ void dump_delay_accuracy_check()
|
|
110
|
+ {
|
|
111
|
+ auto report_call_time = [](PGM_P const name, const uint32_t cycles, const uint32_t total, const bool do_flush=true) {
|
|
112
|
+ SERIAL_ECHOPGM("Calling ");
|
|
113
|
+ serialprintPGM(name);
|
|
114
|
+ SERIAL_ECHOLNPAIR(" for ", cycles, "cycles took: ", total, "cycles");
|
|
115
|
+ if (do_flush) SERIAL_FLUSH();
|
|
116
|
+ };
|
|
117
|
+
|
|
118
|
+ uint32_t s, e;
|
|
119
|
+
|
|
120
|
+ SERIAL_ECHOLNPAIR("Computed delay calibration value: ", ASM_CYCLES_PER_ITERATION);
|
|
121
|
+ SERIAL_FLUSH();
|
|
122
|
+ // Display the results of the calibration above
|
|
123
|
+ constexpr uint32_t testValues[] = { 1, 5, 10, 20, 50, 100, 150, 200, 350, 500, 750, 1000 };
|
|
124
|
+ for (auto i : testValues) {
|
|
125
|
+ s = micros(); DELAY_US(i); e = micros();
|
|
126
|
+ report_call_time(PSTR("delay"), i, e - s);
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ if (HW_REG(_DWT_CTRL)) {
|
|
130
|
+ for (auto i : testValues) {
|
|
131
|
+ s = HW_REG(_DWT_CYCCNT); DELAY_CYCLES(i); e = HW_REG(_DWT_CYCCNT);
|
|
132
|
+ report_call_time(PSTR("delay"), i, e - s);
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ // Measure the delay to call a real function compared to a function pointer
|
|
136
|
+ s = HW_REG(_DWT_CYCCNT); delay_dwt(1); e = HW_REG(_DWT_CYCCNT);
|
|
137
|
+ report_call_time(PSTR("delay_dwt"), 1, e - s);
|
|
138
|
+
|
|
139
|
+ static PGMSTR(dcd, "DELAY_CYCLES directly ");
|
|
140
|
+
|
|
141
|
+ s = HW_REG(_DWT_CYCCNT); DELAY_CYCLES( 1); e = HW_REG(_DWT_CYCCNT);
|
|
142
|
+ report_call_time(dcd, 1, e - s, false);
|
|
143
|
+
|
|
144
|
+ s = HW_REG(_DWT_CYCCNT); DELAY_CYCLES( 5); e = HW_REG(_DWT_CYCCNT);
|
|
145
|
+ report_call_time(dcd, 5, e - s, false);
|
|
146
|
+
|
|
147
|
+ s = HW_REG(_DWT_CYCCNT); DELAY_CYCLES(10); e = HW_REG(_DWT_CYCCNT);
|
|
148
|
+ report_call_time(dcd, 10, e - s, false);
|
|
149
|
+
|
|
150
|
+ s = HW_REG(_DWT_CYCCNT); DELAY_CYCLES(20); e = HW_REG(_DWT_CYCCNT);
|
|
151
|
+ report_call_time(dcd, 20, e - s, false);
|
|
152
|
+
|
|
153
|
+ s = HW_REG(_DWT_CYCCNT); DELAY_CYCLES(50); e = HW_REG(_DWT_CYCCNT);
|
|
154
|
+ report_call_time(dcd, 50, e - s, false);
|
|
155
|
+
|
|
156
|
+ s = HW_REG(_DWT_CYCCNT); DELAY_CYCLES(100); e = HW_REG(_DWT_CYCCNT);
|
|
157
|
+ report_call_time(dcd, 100, e - s, false);
|
|
158
|
+
|
|
159
|
+ s = HW_REG(_DWT_CYCCNT); DELAY_CYCLES(200); e = HW_REG(_DWT_CYCCNT);
|
|
160
|
+ report_call_time(dcd, 200, e - s, false);
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+ #endif // MARLIN_DEV_MODE
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+#else
|
|
167
|
+
|
|
168
|
+ void calibrate_delay_loop() {}
|
|
169
|
+ #if ENABLED(MARLIN_DEV_MODE)
|
|
170
|
+ void dump_delay_accuracy_check() {
|
|
171
|
+ static PGMSTR(none, "N/A on this platform");
|
|
172
|
+ serialprintPGM(none);
|
|
173
|
+ }
|
|
174
|
+ #endif
|
|
175
|
+
|
|
176
|
+#endif
|