|
@@ -150,8 +150,37 @@ void calibrate_delay_loop();
|
150
|
150
|
|
151
|
151
|
#endif
|
152
|
152
|
|
153
|
|
-// Delay in nanoseconds
|
154
|
|
-#define DELAY_NS(x) DELAY_CYCLES((x) * ((F_CPU) / 1000000UL) / 1000UL)
|
155
|
|
-
|
|
153
|
+/**************************************************************
|
|
154
|
+ * Delay in nanoseconds. Requires the F_CPU macro.
|
|
155
|
+ * These macros follow avr-libc delay conventions.
|
|
156
|
+ *
|
|
157
|
+ * For AVR there are three possible operation modes, due to its
|
|
158
|
+ * slower clock speeds and thus coarser delay resolution. For
|
|
159
|
+ * example, when F_CPU = 16000000 the resolution is 62.5ns.
|
|
160
|
+ *
|
|
161
|
+ * Round up (default)
|
|
162
|
+ * Round up the delay according to the CPU clock resolution.
|
|
163
|
+ * e.g., 100 will give a delay of 2 cycles (125ns).
|
|
164
|
+ *
|
|
165
|
+ * Round down (DELAY_NS_ROUND_DOWN)
|
|
166
|
+ * Round down the delay according to the CPU clock resolution.
|
|
167
|
+ * e.g., 100 will be rounded down to 1 cycle (62.5ns).
|
|
168
|
+ *
|
|
169
|
+ * Nearest (DELAY_NS_ROUND_CLOSEST)
|
|
170
|
+ * Round the delay to the nearest number of clock cycles.
|
|
171
|
+ * e.g., 165 will be rounded up to 3 cycles (187.5ns) because
|
|
172
|
+ * it's closer to the requested delay than 2 cycle (125ns).
|
|
173
|
+ */
|
156
|
174
|
|
|
175
|
+#ifndef __AVR__
|
|
176
|
+ #undef DELAY_NS_ROUND_DOWN
|
|
177
|
+ #undef DELAY_NS_ROUND_CLOSEST
|
|
178
|
+#endif
|
157
|
179
|
|
|
180
|
+#if ENABLED(DELAY_NS_ROUND_DOWN)
|
|
181
|
+ #define DELAY_NS(x) DELAY_CYCLES((x) * ((F_CPU) / 1000000UL) / 1000UL) // floor
|
|
182
|
+#elif ENABLED(DELAY_NS_ROUND_CLOSEST)
|
|
183
|
+ #define DELAY_NS(x) DELAY_CYCLES(((x) * ((F_CPU) / 1000000UL) + 500) / 1000UL) // round
|
|
184
|
+#else
|
|
185
|
+ #define DELAY_NS(x) DELAY_CYCLES(((x) * ((F_CPU) / 1000000UL) + 999) / 1000UL) // "ceil"
|
|
186
|
+#endif
|