瀏覽代碼

Make DELAY_NS round up on AVR (#21546)

Mike La Spina 4 年之前
父節點
當前提交
993609b5aa
沒有連結到貢獻者的電子郵件帳戶。
共有 2 個檔案被更改,包括 33 行新增4 行删除
  1. 32
    3
      Marlin/src/HAL/shared/Delay.h
  2. 1
    1
      Marlin/src/lcd/dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp

+ 32
- 3
Marlin/src/HAL/shared/Delay.h 查看文件

@@ -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

+ 1
- 1
Marlin/src/lcd/dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp 查看文件

@@ -60,7 +60,7 @@
60 60
 #elif F_CPU == 16000000
61 61
   #define CPU_ST7920_DELAY_1 DELAY_NS(125)
62 62
   #define CPU_ST7920_DELAY_2 DELAY_NS(0)
63
-  #define CPU_ST7920_DELAY_3 DELAY_NS(125)
63
+  #define CPU_ST7920_DELAY_3 DELAY_NS(188)
64 64
 #else
65 65
   #error "No valid condition for delays in 'ultralcd_st7920_u8glib_rrd_AVR.h'"
66 66
 #endif

Loading…
取消
儲存