瀏覽代碼

Basic delay for Max7219 on ARM

Scott Lahteine 7 年之前
父節點
當前提交
212e52e485
共有 1 個檔案被更改,包括 24 行新增18 行删除
  1. 24
    18
      Marlin/src/feature/Max7219_Debug_LEDs.cpp

+ 24
- 18
Marlin/src/feature/Max7219_Debug_LEDs.cpp 查看文件

@@ -64,40 +64,46 @@
64 64
 static uint8_t LEDs[8] = { 0 };
65 65
 
66 66
 #ifdef CPU_32_BIT
67
-void MS_DELAY() { DELAY_1US; }  // 32-bit processors need a delay to stabilize the signal
67
+  // Approximate a 1µs delay on 32-bit ARM
68
+  void SIG_DELAY() {
69
+    int16_t delay_cycles = CYCLES_PER_MICROSECOND - 10;
70
+    while (delay_cycles >= 10) { DELAY_NOPS(6); delay_cycles -= 10; }
71
+    if (delay_cycles > 0) DELAY_NOPS(delay_cycles);
72
+  }
68 73
 #else
69
-  #define MS_DELAY() DELAY_3_NOP
74
+  // Delay for 0.1875µs (16MHz AVR) or 0.15µs (20MHz AVR)
75
+  #define SIG_DELAY() DELAY_3_NOP
70 76
 #endif
71 77
 
72 78
 void Max7219_PutByte(uint8_t data) {
73
-  CRITICAL_SECTION_START
79
+  CRITICAL_SECTION_START;
74 80
   for (uint8_t i = 8; i--;) {
75
-    MS_DELAY();
81
+    SIG_DELAY();
76 82
     WRITE(MAX7219_CLK_PIN, LOW);       // tick
77
-    MS_DELAY();
83
+    SIG_DELAY();
78 84
     WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW);  // send 1 or 0 based on data bit
79
-    MS_DELAY();
85
+    SIG_DELAY();
80 86
     WRITE(MAX7219_CLK_PIN, HIGH);      // tock
81
-    MS_DELAY();
87
+    SIG_DELAY();
82 88
     data <<= 1;
83 89
   }
84
-  CRITICAL_SECTION_END
90
+  CRITICAL_SECTION_END;
85 91
 }
86 92
 
87 93
 void Max7219(const uint8_t reg, const uint8_t data) {
88
-  MS_DELAY();
89
-  CRITICAL_SECTION_START
94
+  SIG_DELAY();
95
+  CRITICAL_SECTION_START;
90 96
   WRITE(MAX7219_LOAD_PIN, LOW);  // begin
91
-  MS_DELAY();
97
+  SIG_DELAY();
92 98
   Max7219_PutByte(reg);          // specify register
93
-  MS_DELAY();
99
+  SIG_DELAY();
94 100
   Max7219_PutByte(data);         // put data
95
-  MS_DELAY();
101
+  SIG_DELAY();
96 102
   WRITE(MAX7219_LOAD_PIN, LOW);  // and tell the chip to load the data
97
-  MS_DELAY();
103
+  SIG_DELAY();
98 104
   WRITE(MAX7219_LOAD_PIN, HIGH);
99
-  CRITICAL_SECTION_END
100
-  MS_DELAY();
105
+  CRITICAL_SECTION_END;
106
+  SIG_DELAY();
101 107
 }
102 108
 
103 109
 void Max7219_LED_Set(const uint8_t col, const uint8_t row, const bool on) {
@@ -278,14 +284,14 @@ void Max7219_init() {
278 284
  */
279 285
 void Max7219_idle_tasks() {
280 286
   #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
281
-    CRITICAL_SECTION_START
287
+    CRITICAL_SECTION_START;
282 288
     #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_QUEUE
283 289
       const uint8_t head = planner.block_buffer_head;
284 290
     #endif
285 291
     #if MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
286 292
       const uint8_t tail = planner.block_buffer_tail;
287 293
     #endif
288
-    CRITICAL_SECTION_END
294
+    CRITICAL_SECTION_END;
289 295
   #endif
290 296
 
291 297
   static uint16_t refresh_cnt = 0;  // The Max7219 circuit boards available for several dollars on eBay

Loading…
取消
儲存