Browse Source

Basic delay for Max7219 on ARM

Scott Lahteine 7 years ago
parent
commit
212e52e485
1 changed files with 24 additions and 18 deletions
  1. 24
    18
      Marlin/src/feature/Max7219_Debug_LEDs.cpp

+ 24
- 18
Marlin/src/feature/Max7219_Debug_LEDs.cpp View File

64
 static uint8_t LEDs[8] = { 0 };
64
 static uint8_t LEDs[8] = { 0 };
65
 
65
 
66
 #ifdef CPU_32_BIT
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
 #else
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
 #endif
76
 #endif
71
 
77
 
72
 void Max7219_PutByte(uint8_t data) {
78
 void Max7219_PutByte(uint8_t data) {
73
-  CRITICAL_SECTION_START
79
+  CRITICAL_SECTION_START;
74
   for (uint8_t i = 8; i--;) {
80
   for (uint8_t i = 8; i--;) {
75
-    MS_DELAY();
81
+    SIG_DELAY();
76
     WRITE(MAX7219_CLK_PIN, LOW);       // tick
82
     WRITE(MAX7219_CLK_PIN, LOW);       // tick
77
-    MS_DELAY();
83
+    SIG_DELAY();
78
     WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW);  // send 1 or 0 based on data bit
84
     WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW);  // send 1 or 0 based on data bit
79
-    MS_DELAY();
85
+    SIG_DELAY();
80
     WRITE(MAX7219_CLK_PIN, HIGH);      // tock
86
     WRITE(MAX7219_CLK_PIN, HIGH);      // tock
81
-    MS_DELAY();
87
+    SIG_DELAY();
82
     data <<= 1;
88
     data <<= 1;
83
   }
89
   }
84
-  CRITICAL_SECTION_END
90
+  CRITICAL_SECTION_END;
85
 }
91
 }
86
 
92
 
87
 void Max7219(const uint8_t reg, const uint8_t data) {
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
   WRITE(MAX7219_LOAD_PIN, LOW);  // begin
96
   WRITE(MAX7219_LOAD_PIN, LOW);  // begin
91
-  MS_DELAY();
97
+  SIG_DELAY();
92
   Max7219_PutByte(reg);          // specify register
98
   Max7219_PutByte(reg);          // specify register
93
-  MS_DELAY();
99
+  SIG_DELAY();
94
   Max7219_PutByte(data);         // put data
100
   Max7219_PutByte(data);         // put data
95
-  MS_DELAY();
101
+  SIG_DELAY();
96
   WRITE(MAX7219_LOAD_PIN, LOW);  // and tell the chip to load the data
102
   WRITE(MAX7219_LOAD_PIN, LOW);  // and tell the chip to load the data
97
-  MS_DELAY();
103
+  SIG_DELAY();
98
   WRITE(MAX7219_LOAD_PIN, HIGH);
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
 void Max7219_LED_Set(const uint8_t col, const uint8_t row, const bool on) {
109
 void Max7219_LED_Set(const uint8_t col, const uint8_t row, const bool on) {
278
  */
284
  */
279
 void Max7219_idle_tasks() {
285
 void Max7219_idle_tasks() {
280
   #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
286
   #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
281
-    CRITICAL_SECTION_START
287
+    CRITICAL_SECTION_START;
282
     #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_QUEUE
288
     #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_QUEUE
283
       const uint8_t head = planner.block_buffer_head;
289
       const uint8_t head = planner.block_buffer_head;
284
     #endif
290
     #endif
285
     #if MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
291
     #if MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
286
       const uint8_t tail = planner.block_buffer_tail;
292
       const uint8_t tail = planner.block_buffer_tail;
287
     #endif
293
     #endif
288
-    CRITICAL_SECTION_END
294
+    CRITICAL_SECTION_END;
289
   #endif
295
   #endif
290
 
296
 
291
   static uint16_t refresh_cnt = 0;  // The Max7219 circuit boards available for several dollars on eBay
297
   static uint16_t refresh_cnt = 0;  // The Max7219 circuit boards available for several dollars on eBay

Loading…
Cancel
Save