Browse Source

Better handling of DELAY_NS and DELAY_US (#10716)

Co-Authored-By: ejtagle <ejtagle@hotmail.com>
Scott Lahteine 6 years ago
parent
commit
a1062eec5b
No account linked to committer's email address

+ 133
- 0
Marlin/src/HAL/Delay.h View File

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+
25
+  Busy wait delay Cycles routines:
26
+
27
+  DELAY_CYCLES(count): Delay execution in cycles
28
+  DELAY_NS(count): Delay execution in nanoseconds
29
+  DELAY_US(count): Delay execution in microseconds
30
+
31
+  */
32
+
33
+#ifndef MARLIN_DELAY_H
34
+#define MARLIN_DELAY_H
35
+
36
+#if defined(__arm__) || defined(__thumb__)
37
+
38
+  /* https://blueprints.launchpad.net/gcc-arm-embedded/+spec/delay-cycles */
39
+
40
+  #define nop() __asm__ __volatile__("nop;\n\t":::)
41
+
42
+  FORCE_INLINE static void __delay_4cycles(uint32_t cy) { // +1 cycle
43
+    #if ARCH_PIPELINE_RELOAD_CYCLES < 2
44
+      #define EXTRA_NOP_CYCLES A("nop")
45
+    #else
46
+      #define EXTRA_NOP_CYCLES ""
47
+    #endif
48
+
49
+    __asm__ __volatile__(
50
+      A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax
51
+      L("1")
52
+      A("subs %[cnt],#1")
53
+      EXTRA_NOP_CYCLES
54
+      A("bne 1b")
55
+      : [cnt]"+r"(cy)   // output: +r means input+output
56
+      :                 // input:
57
+      : "cc"            // clobbers:
58
+    );
59
+  }
60
+
61
+  /* ---------------- Delay in cycles */
62
+  FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
63
+
64
+    if (__builtin_constant_p(x)) {
65
+      #define MAXNOPS 4
66
+
67
+      if (x <= (MAXNOPS)) {
68
+        switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
69
+      }
70
+      else { // because of +1 cycle inside delay_4cycles
71
+        const uint32_t rem = (x - 1) % (MAXNOPS);
72
+        switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
73
+        if ((x = (x - 1) / (MAXNOPS)))
74
+          __delay_4cycles(x); // if need more then 4 nop loop is more optimal
75
+      }
76
+      #undef MAXNOPS
77
+    }
78
+    else
79
+      __delay_4cycles(x / 4);
80
+  }
81
+  #undef nop
82
+
83
+#elif defined(__AVR__)
84
+
85
+  #define nop() __asm__ __volatile__("nop;\n\t":::)
86
+
87
+  FORCE_INLINE static void __delay_4cycles(uint8_t cy) {
88
+    __asm__ __volatile__(
89
+      L("1")
90
+      A("dec %[cnt]")
91
+      A("nop")
92
+      A("brne 1b")
93
+      : [cnt] "+r"(cy)  // output: +r means input+output
94
+      :                 // input:
95
+      : "cc"            // clobbers:
96
+    );
97
+  }
98
+
99
+  /* ---------------- Delay in cycles */
100
+  FORCE_INLINE static void DELAY_CYCLES(uint16_t x) {
101
+
102
+    if (__builtin_constant_p(x)) {
103
+      #define MAXNOPS 4
104
+
105
+      if (x <= (MAXNOPS)) {
106
+        switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
107
+      }
108
+      else {
109
+        const uint32_t rem = (x) % (MAXNOPS);
110
+        switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
111
+        if ((x = (x) / (MAXNOPS)))
112
+          __delay_4cycles(x); // if need more then 4 nop loop is more optimal
113
+      }
114
+
115
+      #undef MAXNOPS
116
+    }
117
+    else
118
+      __delay_4cycles(x / 4);
119
+  }
120
+  #undef nop
121
+
122
+#else
123
+  #error "Unsupported MCU architecture"
124
+#endif
125
+
126
+/* ---------------- Delay in nanoseconds */
127
+#define DELAY_NS(x) DELAY_CYCLES( (x) * (F_CPU/1000000L) / 1000L )
128
+
129
+/* ---------------- Delay in microseconds */
130
+#define DELAY_US(x) DELAY_CYCLES( (x) * (F_CPU/1000000L) )
131
+
132
+#endif // MARLIN_DELAY_H
133
+

+ 40
- 83
Marlin/src/HAL/HAL_DUE/HAL_spi_Due.cpp View File

42
 // --------------------------------------------------------------------------
42
 // --------------------------------------------------------------------------
43
 
43
 
44
 #include "../../inc/MarlinConfig.h"
44
 #include "../../inc/MarlinConfig.h"
45
+#include "../Delay.h"
45
 
46
 
46
 // --------------------------------------------------------------------------
47
 // --------------------------------------------------------------------------
47
 // Public Variables
48
 // Public Variables
58
   // software SPI
59
   // software SPI
59
   // --------------------------------------------------------------------------
60
   // --------------------------------------------------------------------------
60
 
61
 
61
-  // set optimization so ARDUINO optimizes this file
62
+  // Make sure GCC optimizes this file.
63
+  // Note that this line triggers a bug in GCC which is fixed by casting.
64
+  // See the note below.
62
   #pragma GCC optimize (3)
65
   #pragma GCC optimize (3)
63
 
66
 
64
-  /* ---------------- Delay Cycles routine -------------- */
65
-
66
-  /* https://blueprints.launchpad.net/gcc-arm-embedded/+spec/delay-cycles */
67
-
68
-  #define nop() __asm__ __volatile__("nop;\n\t":::)
69
-
70
-  FORCE_INLINE static void __delay_4cycles(uint32_t cy) { // +1 cycle
71
-    #if ARCH_PIPELINE_RELOAD_CYCLES<2
72
-      #define EXTRA_NOP_CYCLES "nop"
73
-    #else
74
-      #define EXTRA_NOP_CYCLES ""
75
-    #endif
76
-
77
-    __asm__ __volatile__(
78
-      ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
79
-
80
-      L("loop%=")
81
-      A("subs %[cnt],#1")
82
-      A(EXTRA_NOP_CYCLES)
83
-      A("bne loop%=")
84
-      : [cnt]"+r"(cy) // output: +r means input+output
85
-      : // input:
86
-      : "cc" // clobbers:
87
-    );
88
-  }
89
-
90
-  FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
91
-
92
-    if (__builtin_constant_p(x)) {
93
-
94
-      #define MAXNOPS 4
95
-
96
-      if (x <= (MAXNOPS)) {
97
-        switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
98
-      }
99
-      else { // because of +1 cycle inside delay_4cycles
100
-        const uint32_t rem = (x - 1) % (MAXNOPS);
101
-        switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
102
-        if ((x = (x - 1) / (MAXNOPS)))
103
-          __delay_4cycles(x); // if need more then 4 nop loop is more optimal
104
-      }
105
-    }
106
-    else
107
-      __delay_4cycles(x / 4);
108
-  }
109
-
110
-  /* ---------------- Delay in nanoseconds and in microseconds */
111
-
112
-  #define DELAY_NS(x) DELAY_CYCLES( (x) * (F_CPU/1000000) / 1000)
113
-
114
-  typedef uint8_t (*pfnSpiTransfer) (uint8_t b);
67
+  typedef uint8_t (*pfnSpiTransfer)(uint8_t b);
115
   typedef void    (*pfnSpiRxBlock)(uint8_t* buf, uint32_t nbyte);
68
   typedef void    (*pfnSpiRxBlock)(uint8_t* buf, uint32_t nbyte);
116
   typedef void    (*pfnSpiTxBlock)(const uint8_t* buf, uint32_t nbyte);
69
   typedef void    (*pfnSpiTxBlock)(const uint8_t* buf, uint32_t nbyte);
117
 
70
 
118
-
119
   /* ---------------- Macros to be able to access definitions from asm */
71
   /* ---------------- Macros to be able to access definitions from asm */
120
-
121
   #define _PORT(IO) DIO ##  IO ## _WPORT
72
   #define _PORT(IO) DIO ##  IO ## _WPORT
122
   #define _PIN_MASK(IO) MASK(DIO ## IO ## _PIN)
73
   #define _PIN_MASK(IO) MASK(DIO ## IO ## _PIN)
123
   #define _PIN_SHIFT(IO) DIO ## IO ## _PIN
74
   #define _PIN_SHIFT(IO) DIO ## IO ## _PIN
202
     return 0;
153
     return 0;
203
   }
154
   }
204
 
155
 
205
- // Calculates the bit band alias address and returns a pointer address to word.
206
- // addr: The byte address of bitbanding bit.
207
- // bit:  The bit position of bitbanding bit.
208
-#define BITBAND_ADDRESS(addr, bit) \
156
+   // Calculates the bit band alias address and returns a pointer address to word.
157
+   // addr: The byte address of bitbanding bit.
158
+   // bit:  The bit position of bitbanding bit.
159
+  #define BITBAND_ADDRESS(addr, bit) \
209
     (((uint32_t)(addr) & 0xF0000000) + 0x02000000 + ((uint32_t)(addr)&0xFFFFF)*32 + (bit)*4)
160
     (((uint32_t)(addr) & 0xF0000000) + 0x02000000 + ((uint32_t)(addr)&0xFFFFF)*32 + (bit)*4)
210
 
161
 
211
   // run at ~8 .. ~10Mhz - Rx version (Tx line not altered)
162
   // run at ~8 .. ~10Mhz - Rx version (Tx line not altered)
319
   }
270
   }
320
 
271
 
321
   // Pointers to generic functions for byte transfers
272
   // Pointers to generic functions for byte transfers
322
-  static pfnSpiTransfer spiTransferTx = spiTransferX;
323
-  static pfnSpiTransfer spiTransferRx = spiTransferX;
273
+
274
+  /**
275
+   * Note: The cast is unnecessary, but without it, this file triggers a GCC 4.8.3-2014 bug.
276
+   * Later GCC versions do not have this problem, but at this time (May 2018) Arduino still
277
+   * uses that buggy and obsolete GCC version!!
278
+   */
279
+  static pfnSpiTransfer spiTransferRx = (pfnSpiTransfer)spiTransferX;
280
+  static pfnSpiTransfer spiTransferTx = (pfnSpiTransfer)spiTransferX;
324
 
281
 
325
   // Block transfers run at ~8 .. ~10Mhz - Tx version (Rx data discarded)
282
   // Block transfers run at ~8 .. ~10Mhz - Tx version (Rx data discarded)
326
   static void spiTxBlock0(const uint8_t* ptr, uint32_t todo) {
283
   static void spiTxBlock0(const uint8_t* ptr, uint32_t todo) {
384
       A("str %[sck_mask],[%[sck_port],#0x4]")              /* CODR */
341
       A("str %[sck_mask],[%[sck_port],#0x4]")              /* CODR */
385
 
342
 
386
       /* Bit 0 */
343
       /* Bit 0 */
387
-      A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]")  /* Access the proper SODR or CODR registers based on that bit */
344
+      A("str %[mosi_mask],[%[mosi_port], %[work],LSL #2]") /* Access the proper SODR or CODR registers based on that bit */
388
       A("str %[sck_mask],[%[sck_port]]")                   /* SODR */
345
       A("str %[sck_mask],[%[sck_port]]")                   /* SODR */
389
       A("subs %[todo],#1")                                 /* Decrement count of pending words to send, update status */
346
       A("subs %[todo],#1")                                 /* Decrement count of pending words to send, update status */
390
       A("str %[sck_mask],[%[sck_port],#0x4]")              /* CODR */
347
       A("str %[sck_mask],[%[sck_port],#0x4]")              /* CODR */
491
   }
448
   }
492
 
449
 
493
   // Pointers to generic functions for block tranfers
450
   // Pointers to generic functions for block tranfers
494
-  static pfnSpiTxBlock spiTxBlock = spiTxBlockX;
495
-  static pfnSpiRxBlock spiRxBlock = spiRxBlockX;
451
+  static pfnSpiTxBlock spiTxBlock = (pfnSpiTxBlock)spiTxBlockX;
452
+  static pfnSpiRxBlock spiRxBlock = (pfnSpiRxBlock)spiRxBlockX;
496
 
453
 
497
   #if MB(ALLIGATOR)  // control SDSS pin
454
   #if MB(ALLIGATOR)  // control SDSS pin
498
     void spiBegin() {
455
     void spiBegin() {
580
   void spiInit(uint8_t spiRate) {
537
   void spiInit(uint8_t spiRate) {
581
     switch (spiRate) {
538
     switch (spiRate) {
582
       case 0:
539
       case 0:
583
-        spiTransferTx = spiTransferTx0;
584
-        spiTransferRx = spiTransferRx0;
585
-        spiTxBlock = spiTxBlock0;
586
-        spiRxBlock = spiRxBlock0;
540
+        spiTransferTx = (pfnSpiTransfer)spiTransferTx0;
541
+        spiTransferRx = (pfnSpiTransfer)spiTransferRx0;
542
+        spiTxBlock = (pfnSpiTxBlock)spiTxBlock0;
543
+        spiRxBlock = (pfnSpiRxBlock)spiRxBlock0;
587
         break;
544
         break;
588
       case 1:
545
       case 1:
589
-        spiTransferTx = spiTransfer1;
590
-        spiTransferRx = spiTransfer1;
591
-        spiTxBlock = spiTxBlockX;
592
-        spiRxBlock = spiRxBlockX;
546
+        spiTransferTx = (pfnSpiTransfer)spiTransfer1;
547
+        spiTransferRx = (pfnSpiTransfer)spiTransfer1;
548
+        spiTxBlock = (pfnSpiTxBlock)spiTxBlockX;
549
+        spiRxBlock = (pfnSpiRxBlock)spiRxBlockX;
593
         break;
550
         break;
594
       default:
551
       default:
595
         spiDelayCyclesX4 = (F_CPU/1000000) >> (6 - spiRate);
552
         spiDelayCyclesX4 = (F_CPU/1000000) >> (6 - spiRate);
596
-        spiTransferTx = spiTransferX;
597
-        spiTransferRx = spiTransferX;
598
-        spiTxBlock = spiTxBlockX;
599
-        spiRxBlock = spiRxBlockX;
553
+        spiTransferTx = (pfnSpiTransfer)spiTransferX;
554
+        spiTransferRx = (pfnSpiTransfer)spiTransferX;
555
+        spiTxBlock = (pfnSpiTxBlock)spiTxBlockX;
556
+        spiRxBlock = (pfnSpiRxBlock)spiRxBlockX;
600
         break;
557
         break;
601
     }
558
     }
602
 
559
 
614
 
571
 
615
   #pragma GCC reset_options
572
   #pragma GCC reset_options
616
 
573
 
617
-#else
574
+#else // !SOFTWARE_SPI
618
 
575
 
619
   #if MB(ALLIGATOR)
576
   #if MB(ALLIGATOR)
620
 
577
 
714
       while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
671
       while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
715
       // clear status
672
       // clear status
716
       SPI0->SPI_RDR;
673
       SPI0->SPI_RDR;
717
-      //delayMicroseconds(1U);
674
+      //DELAY_US(1U);
718
     }
675
     }
719
 
676
 
720
     void spiSend(const uint8_t* buf, size_t n) {
677
     void spiSend(const uint8_t* buf, size_t n) {
724
         while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
681
         while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
725
         while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
682
         while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
726
         SPI0->SPI_RDR;
683
         SPI0->SPI_RDR;
727
-        //delayMicroseconds(1U);
684
+        //DELAY_US(1U);
728
       }
685
       }
729
       spiSend(buf[n - 1]);
686
       spiSend(buf[n - 1]);
730
     }
687
     }
767
       // wait for receive register
724
       // wait for receive register
768
       while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
725
       while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
769
       // get byte from receive register
726
       // get byte from receive register
770
-      //delayMicroseconds(1U);
727
+      //DELAY_US(1U);
771
       return SPI0->SPI_RDR;
728
       return SPI0->SPI_RDR;
772
     }
729
     }
773
 
730
 
797
         SPI0->SPI_TDR = 0x000000FF | SPI_PCS(SPI_CHAN);
754
         SPI0->SPI_TDR = 0x000000FF | SPI_PCS(SPI_CHAN);
798
         while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
755
         while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
799
         buf[i] = SPI0->SPI_RDR;
756
         buf[i] = SPI0->SPI_RDR;
800
-        //delayMicroseconds(1U);
757
+        //DELAY_US(1U);
801
       }
758
       }
802
       buf[nbyte] = spiRec();
759
       buf[nbyte] = spiRec();
803
     }
760
     }
813
         while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
770
         while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0);
814
         while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
771
         while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0);
815
         SPI0->SPI_RDR;
772
         SPI0->SPI_RDR;
816
-        //delayMicroseconds(1U);
773
+        //DELAY_US(1U);
817
       }
774
       }
818
       spiSend(buf[511]);
775
       spiSend(buf[511]);
819
     }
776
     }
902
         spiTransfer(buf[i]);
859
         spiTransfer(buf[i]);
903
     }
860
     }
904
 
861
 
905
-  #endif  //MB(ALLIGATOR)
906
-#endif // ENABLED(SOFTWARE_SPI)
862
+  #endif // !ALLIGATOR
863
+#endif // !SOFTWARE_SPI
907
 
864
 
908
 #endif // ARDUINO_ARCH_SAM
865
 #endif // ARDUINO_ARCH_SAM

+ 2
- 1
Marlin/src/HAL/HAL_LPC1768/HAL.cpp View File

21
 #ifdef TARGET_LPC1768
21
 #ifdef TARGET_LPC1768
22
 
22
 
23
 #include "../../inc/MarlinConfig.h"
23
 #include "../../inc/MarlinConfig.h"
24
+#include "../Delay.h"
24
 
25
 
25
 HalSerial usb_serial;
26
 HalSerial usb_serial;
26
 
27
 
27
 // U8glib required functions
28
 // U8glib required functions
28
 extern "C" void u8g_xMicroDelay(uint16_t val) {
29
 extern "C" void u8g_xMicroDelay(uint16_t val) {
29
-  delayMicroseconds(val);
30
+  DELAY_US(val);
30
 }
31
 }
31
 extern "C" void u8g_MicroDelay(void) {
32
 extern "C" void u8g_MicroDelay(void) {
32
   u8g_xMicroDelay(1);
33
   u8g_xMicroDelay(1);

+ 3
- 3
Marlin/src/HAL/HAL_LPC1768/HAL.h View File

68
 #include "HAL_timers.h"
68
 #include "HAL_timers.h"
69
 #include "HardwareSerial.h"
69
 #include "HardwareSerial.h"
70
 
70
 
71
-#define ST7920_DELAY_1 DELAY_20_NOP;DELAY_20_NOP;DELAY_20_NOP
72
-#define ST7920_DELAY_2 DELAY_20_NOP;DELAY_20_NOP;DELAY_20_NOP;DELAY_10_NOP;DELAY_5_NOP
73
-#define ST7920_DELAY_3 DELAY_20_NOP;DELAY_20_NOP;DELAY_20_NOP;DELAY_10_NOP;DELAY_5_NOP
71
+#define ST7920_DELAY_1 DELAY_NS(600)
72
+#define ST7920_DELAY_2 DELAY_NS(750)
73
+#define ST7920_DELAY_3 DELAY_NS(750)
74
 
74
 
75
 extern HalSerial usb_serial;
75
 extern HalSerial usb_serial;
76
 
76
 

+ 2
- 20
Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp View File

37
 //
37
 //
38
 //#include <WInterrupts.h>
38
 //#include <WInterrupts.h>
39
 #include "../../inc/MarlinConfig.h"
39
 #include "../../inc/MarlinConfig.h"
40
+#include "../Delay.h"
40
 #include <stdint.h>
41
 #include <stdint.h>
41
 #include <stdarg.h>
42
 #include <stdarg.h>
42
 #include <Arduino.h>
43
 #include <Arduino.h>
78
 // Private methods
79
 // Private methods
79
 //
80
 //
80
 
81
 
81
-#if 0
82
-/* static */
83
-inline void SoftwareSerial::tunedDelay(const uint32_t count) {
84
-
85
-  asm volatile(
86
-
87
-    "mov r3, %[loopsPerMicrosecond] \n\t" //load the initial loop counter
88
-    "1: \n\t"
89
-    "sub r3, r3, #1 \n\t"
90
-    "bne 1b \n\t"
91
-
92
-    ://empty output list
93
-    :[loopsPerMicrosecond] "r" (count)
94
-    :"r3", "cc" //clobber list
95
-  );
96
-
97
-}
98
-#else
99
 inline void SoftwareSerial::tunedDelay(const uint32_t count) {
82
 inline void SoftwareSerial::tunedDelay(const uint32_t count) {
100
-  delayMicroseconds(count);
83
+  DELAY_US(count);
101
 }
84
 }
102
-#endif
103
 
85
 
104
 // This function sets the current object as the "listening"
86
 // This function sets the current object as the "listening"
105
 // one and returns true if it replaces another
87
 // one and returns true if it replaces another

+ 3
- 19
Marlin/src/HAL/HAL_LPC1768/arduino.cpp View File

26
 #include <lpc17xx_pinsel.h>
26
 #include <lpc17xx_pinsel.h>
27
 
27
 
28
 #include "../../inc/MarlinConfig.h"
28
 #include "../../inc/MarlinConfig.h"
29
+#include "../Delay.h"
29
 
30
 
30
 // Interrupts
31
 // Interrupts
31
 void cli(void) { __disable_irq(); } // Disable
32
 void cli(void) { __disable_irq(); } // Disable
40
   return _millis;
41
   return _millis;
41
 }
42
 }
42
 
43
 
44
+// This is required for some Arduino libraries we are using
43
 void delayMicroseconds(uint32_t us) {
45
 void delayMicroseconds(uint32_t us) {
44
-  static const int nop_factor = (SystemCoreClock / 11000000);
45
-  static volatile int loops = 0;
46
-
47
-  //previous ops already burned most of 1us, burn the rest
48
-  loops = nop_factor / 4; //measured at 1us
49
-  while (loops > 0) --loops;
50
-
51
-  if (us < 2) return;
52
-  us--;
53
-
54
-  //redirect to delay for large values, then set new delay to remainder
55
-  if (us > 1000) {
56
-    delay(us / 1000);
57
-    us = us % 1000;
58
-  }
59
-
60
-  // burn cycles, time in interrupts will not be taken into account
61
-  loops = us * nop_factor;
62
-  while (loops > 0) --loops;
46
+  DELAY_US(us);
63
 }
47
 }
64
 
48
 
65
 extern "C" void delay(const int msec) {
49
 extern "C" void delay(const int msec) {

+ 4
- 5
Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_ssd_sw_i2c.cpp under construction View File

63
 
63
 
64
   #include <U8glib.h>
64
   #include <U8glib.h>
65
 
65
 
66
-void delayMicroseconds(uint32_t us);
67
 //void pinMode(int16_t pin, uint8_t mode);
66
 //void pinMode(int16_t pin, uint8_t mode);
68
 //void digitalWrite(int16_t pin, uint8_t pin_status);
67
 //void digitalWrite(int16_t pin, uint8_t pin_status);
69
 
68
 
122
 
121
 
123
   LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
122
   LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
124
   LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
123
   LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
125
-  delayMicroseconds(2);
124
+  DELAY_US(2);
126
   LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
125
   LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
127
-  delayMicroseconds(2);
126
+  DELAY_US(2);
128
   LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
127
   LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
129
-  delayMicroseconds(2);
128
+  DELAY_US(2);
130
   LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
129
   LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
131
-  delayMicroseconds(2);
130
+  DELAY_US(2);
132
   LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
131
   LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
133
 
132
 
134
   u8g_i2c_send_byte_sw(I2C_SLA);  // send slave address with write bit
133
   u8g_i2c_send_byte_sw(I2C_SLA);  // send slave address with write bit

+ 2
- 52
Marlin/src/core/macros.h View File

50
   #define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR
50
   #define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR
51
 #endif
51
 #endif
52
 
52
 
53
-// Processor-level delays for hardware interfaces
54
-#ifndef _NOP
55
-  #define _NOP() do { __asm__ volatile ("nop"); } while (0)
56
-#endif
57
-#define DELAY_NOPS(X) \
58
-  switch (X) { \
59
-    case 20: _NOP(); case 19: _NOP(); case 18: _NOP(); case 17: _NOP(); \
60
-    case 16: _NOP(); case 15: _NOP(); case 14: _NOP(); case 13: _NOP(); \
61
-    case 12: _NOP(); case 11: _NOP(); case 10: _NOP(); case  9: _NOP(); \
62
-    case  8: _NOP(); case  7: _NOP(); case  6: _NOP(); case  5: _NOP(); \
63
-    case  4: _NOP(); case  3: _NOP(); case  2: _NOP(); case  1: _NOP(); \
64
-  }
65
-#define DELAY_0_NOP   NOOP
66
-#define DELAY_1_NOP   DELAY_NOPS( 1)
67
-#define DELAY_2_NOP   DELAY_NOPS( 2)
68
-#define DELAY_3_NOP   DELAY_NOPS( 3)
69
-#define DELAY_4_NOP   DELAY_NOPS( 4)
70
-#define DELAY_5_NOP   DELAY_NOPS( 5)
71
-#define DELAY_10_NOP  DELAY_NOPS(10)
72
-#define DELAY_20_NOP  DELAY_NOPS(20)
73
-
74
-#if CYCLES_PER_MICROSECOND <= 200
75
-  #define DELAY_100NS DELAY_NOPS((CYCLES_PER_MICROSECOND + 9) / 10)
76
-#else
77
-  #define DELAY_100NS DELAY_20_NOP
78
-#endif
79
-
80
-// Microsecond delays for hardware interfaces
81
-#if CYCLES_PER_MICROSECOND <= 20
82
-  #define DELAY_1US DELAY_NOPS(CYCLES_PER_MICROSECOND)
83
-  #define DELAY_US(X) \
84
-    switch (X) { \
85
-      case 20: DELAY_1US; case 19: DELAY_1US; case 18: DELAY_1US; case 17: DELAY_1US; \
86
-      case 16: DELAY_1US; case 15: DELAY_1US; case 14: DELAY_1US; case 13: DELAY_1US; \
87
-      case 12: DELAY_1US; case 11: DELAY_1US; case 10: DELAY_1US; case  9: DELAY_1US; \
88
-      case  8: DELAY_1US; case  7: DELAY_1US; case  6: DELAY_1US; case  5: DELAY_1US; \
89
-      case  4: DELAY_1US; case  3: DELAY_1US; case  2: DELAY_1US; case  1: DELAY_1US; \
90
-    }
91
-#else
92
-  #define DELAY_US(X) delayMicroseconds(X) // May not be usable in CRITICAL_SECTION
93
-  #define DELAY_1US DELAY_US(1)
94
-#endif
95
-#define DELAY_2US  DELAY_US( 2)
96
-#define DELAY_3US  DELAY_US( 3)
97
-#define DELAY_4US  DELAY_US( 4)
98
-#define DELAY_5US  DELAY_US( 5)
99
-#define DELAY_6US  DELAY_US( 6)
100
-#define DELAY_7US  DELAY_US( 7)
101
-#define DELAY_8US  DELAY_US( 8)
102
-#define DELAY_9US  DELAY_US( 9)
103
-#define DELAY_10US DELAY_US(10)
104
-#define DELAY_20US DELAY_US(20)
53
+// Nanoseconds per cycle
54
+#define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
105
 
55
 
106
 // Remove compiler warning on an unused variable
56
 // Remove compiler warning on an unused variable
107
 #define UNUSED(x) (void) (x)
57
 #define UNUSED(x) (void) (x)

+ 3
- 6
Marlin/src/feature/Max7219_Debug_LEDs.cpp View File

60
 #include "../module/planner.h"
60
 #include "../module/planner.h"
61
 #include "../module/stepper.h"
61
 #include "../module/stepper.h"
62
 #include "../Marlin.h"
62
 #include "../Marlin.h"
63
+#include "../HAL/Delay.h"
63
 
64
 
64
 static uint8_t LEDs[8] = { 0 };
65
 static uint8_t LEDs[8] = { 0 };
65
 
66
 
66
 #ifdef CPU_32_BIT
67
 #ifdef CPU_32_BIT
67
   // Approximate a 1µs delay on 32-bit ARM
68
   // 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
-  }
69
+  #define SIG_DELAY() DELAY_US(1)
73
 #else
70
 #else
74
   // Delay for 0.1875µs (16MHz AVR) or 0.15µs (20MHz AVR)
71
   // Delay for 0.1875µs (16MHz AVR) or 0.15µs (20MHz AVR)
75
-  #define SIG_DELAY() DELAY_3_NOP
72
+  #define SIG_DELAY() DELAY_NS(188)
76
 #endif
73
 #endif
77
 
74
 
78
 void Max7219_PutByte(uint8_t data) {
75
 void Max7219_PutByte(uint8_t data) {

+ 12
- 11
Marlin/src/feature/dac/dac_dac084s085.cpp View File

12
 
12
 
13
 #include "../../Marlin.h"
13
 #include "../../Marlin.h"
14
 #include "../../module/stepper.h"
14
 #include "../../module/stepper.h"
15
+#include "../../HAL/Delay.h"
15
 
16
 
16
 dac084s085::dac084s085() { }
17
 dac084s085::dac084s085() { }
17
 
18
 
27
   spiBegin();
28
   spiBegin();
28
 
29
 
29
   //init onboard DAC
30
   //init onboard DAC
30
-  delayMicroseconds(2U);
31
+  DELAY_US(2);
31
   WRITE(DAC0_SYNC, LOW);
32
   WRITE(DAC0_SYNC, LOW);
32
-  delayMicroseconds(2U);
33
+  DELAY_US(2);
33
   WRITE(DAC0_SYNC, HIGH);
34
   WRITE(DAC0_SYNC, HIGH);
34
-  delayMicroseconds(2U);
35
+  DELAY_US(2);
35
   WRITE(DAC0_SYNC, LOW);
36
   WRITE(DAC0_SYNC, LOW);
36
 
37
 
37
   spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
38
   spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
39
 
40
 
40
   #if EXTRUDERS > 1
41
   #if EXTRUDERS > 1
41
     //init Piggy DAC
42
     //init Piggy DAC
42
-    delayMicroseconds(2U);
43
+    DELAY_US(2);
43
     WRITE(DAC1_SYNC, LOW);
44
     WRITE(DAC1_SYNC, LOW);
44
-    delayMicroseconds(2U);
45
+    DELAY_US(2);
45
     WRITE(DAC1_SYNC, HIGH);
46
     WRITE(DAC1_SYNC, HIGH);
46
-    delayMicroseconds(2U);
47
+    DELAY_US(2);
47
     WRITE(DAC1_SYNC, LOW);
48
     WRITE(DAC1_SYNC, LOW);
48
 
49
 
49
     spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
50
     spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
66
 
67
 
67
   if (channel > 3) {        // DAC Piggy E1,E2,E3
68
   if (channel > 3) {        // DAC Piggy E1,E2,E3
68
     WRITE(DAC1_SYNC, LOW);
69
     WRITE(DAC1_SYNC, LOW);
69
-    delayMicroseconds(2U);
70
+    DELAY_US(2);
70
     WRITE(DAC1_SYNC, HIGH);
71
     WRITE(DAC1_SYNC, HIGH);
71
-    delayMicroseconds(2U);
72
+    DELAY_US(2);
72
     WRITE(DAC1_SYNC, LOW);
73
     WRITE(DAC1_SYNC, LOW);
73
   }
74
   }
74
   else {                    // DAC onboard X,Y,Z,E0
75
   else {                    // DAC onboard X,Y,Z,E0
75
     WRITE(DAC0_SYNC, LOW);
76
     WRITE(DAC0_SYNC, LOW);
76
-    delayMicroseconds(2U);
77
+    DELAY_US(2);
77
     WRITE(DAC0_SYNC, HIGH);
78
     WRITE(DAC0_SYNC, HIGH);
78
-    delayMicroseconds(2U);
79
+    DELAY_US(2);
79
     WRITE(DAC0_SYNC, LOW);
80
     WRITE(DAC0_SYNC, LOW);
80
   }
81
   }
81
 
82
 
82
-  delayMicroseconds(2U);
83
+  DELAY_US(2);
83
   spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
84
   spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
84
 }
85
 }
85
 
86
 

+ 3
- 3
Marlin/src/inc/Conditionals_LCD.h View File

120
 
120
 
121
   #define REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER
121
   #define REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER
122
   #ifndef ST7920_DELAY_1
122
   #ifndef ST7920_DELAY_1
123
-    #define ST7920_DELAY_1 DELAY_2_NOP
123
+    #define ST7920_DELAY_1 DELAY_NS(125)
124
   #endif
124
   #endif
125
   #ifndef ST7920_DELAY_2
125
   #ifndef ST7920_DELAY_2
126
-    #define ST7920_DELAY_2 DELAY_2_NOP
126
+    #define ST7920_DELAY_2 DELAY_NS(125)
127
   #endif
127
   #endif
128
   #ifndef ST7920_DELAY_3
128
   #ifndef ST7920_DELAY_3
129
-    #define ST7920_DELAY_3 DELAY_2_NOP
129
+    #define ST7920_DELAY_3 DELAY_NS(125)
130
   #endif
130
   #endif
131
 
131
 
132
 #elif ENABLED(MKS_12864OLED)
132
 #elif ENABLED(MKS_12864OLED)

+ 21
- 20
Marlin/src/lcd/dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp View File

24
 // file u8g_dev_st7920_128x64_HAL.cpp for the HAL version.
24
 // file u8g_dev_st7920_128x64_HAL.cpp for the HAL version.
25
 
25
 
26
 #include "../../inc/MarlinConfig.h"
26
 #include "../../inc/MarlinConfig.h"
27
+#include "../../HAL/Delay.h"
27
 
28
 
28
 #if ENABLED(U8GLIB_ST7920)
29
 #if ENABLED(U8GLIB_ST7920)
29
 
30
 
46
 #pragma GCC optimize (3)
47
 #pragma GCC optimize (3)
47
 
48
 
48
 // If you want you can define your own set of delays in Configuration.h
49
 // If you want you can define your own set of delays in Configuration.h
49
-//#define ST7920_DELAY_1 DELAY_0_NOP
50
-//#define ST7920_DELAY_2 DELAY_0_NOP
51
-//#define ST7920_DELAY_3 DELAY_0_NOP
50
+//#define ST7920_DELAY_1 DELAY_NS(0)
51
+//#define ST7920_DELAY_2 DELAY_NS(0)
52
+//#define ST7920_DELAY_3 DELAY_NS(0)
52
 
53
 
53
 #if F_CPU >= 20000000
54
 #if F_CPU >= 20000000
54
-  #define CPU_ST7920_DELAY_1 DELAY_0_NOP
55
-  #define CPU_ST7920_DELAY_2 DELAY_0_NOP
56
-  #define CPU_ST7920_DELAY_3 DELAY_1_NOP
55
+  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
56
+  #define CPU_ST7920_DELAY_2 DELAY_NS(0)
57
+  #define CPU_ST7920_DELAY_3 DELAY_NS(50)
57
 #elif MB(3DRAG) || MB(K8200) || MB(K8400) || MB(SILVER_GATE)
58
 #elif MB(3DRAG) || MB(K8200) || MB(K8400) || MB(SILVER_GATE)
58
-  #define CPU_ST7920_DELAY_1 DELAY_0_NOP
59
-  #define CPU_ST7920_DELAY_2 DELAY_3_NOP
60
-  #define CPU_ST7920_DELAY_3 DELAY_0_NOP
59
+  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
60
+  #define CPU_ST7920_DELAY_2 DELAY_NS(188)
61
+  #define CPU_ST7920_DELAY_3 DELAY_NS(0)
61
 #elif MB(MINIRAMBO)
62
 #elif MB(MINIRAMBO)
62
-  #define CPU_ST7920_DELAY_1 DELAY_0_NOP
63
-  #define CPU_ST7920_DELAY_2 DELAY_4_NOP
64
-  #define CPU_ST7920_DELAY_3 DELAY_0_NOP
63
+  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
64
+  #define CPU_ST7920_DELAY_2 DELAY_NS(250)
65
+  #define CPU_ST7920_DELAY_3 DELAY_NS(0)
65
 #elif MB(RAMBO)
66
 #elif MB(RAMBO)
66
-  #define CPU_ST7920_DELAY_1 DELAY_0_NOP
67
-  #define CPU_ST7920_DELAY_2 DELAY_0_NOP
68
-  #define CPU_ST7920_DELAY_3 DELAY_0_NOP
67
+  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
68
+  #define CPU_ST7920_DELAY_2 DELAY_NS(0)
69
+  #define CPU_ST7920_DELAY_3 DELAY_NS(0)
69
 #elif F_CPU == 16000000
70
 #elif F_CPU == 16000000
70
-  #define CPU_ST7920_DELAY_1 DELAY_0_NOP
71
-  #define CPU_ST7920_DELAY_2 DELAY_0_NOP
72
-  #define CPU_ST7920_DELAY_3 DELAY_1_NOP
71
+  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
72
+  #define CPU_ST7920_DELAY_2 DELAY_NS(0)
73
+  #define CPU_ST7920_DELAY_3 DELAY_NS(63)
73
 #else
74
 #else
74
   #error "No valid condition for delays in 'ultralcd_st7920_u8glib_rrd.h'"
75
   #error "No valid condition for delays in 'ultralcd_st7920_u8glib_rrd.h'"
75
 #endif
76
 #endif
101
   ST7920_SND_BIT; // 8
102
   ST7920_SND_BIT; // 8
102
 }
103
 }
103
 
104
 
104
-#if defined(DOGM_SPI_DELAY_US) && DOGM_SPI_DELAY_US > 0
105
-  #define U8G_DELAY() delayMicroseconds(DOGM_SPI_DELAY_US)
105
+#if DOGM_SPI_DELAY_US > 0
106
+  #define U8G_DELAY() DELAY_US(DOGM_SPI_DELAY_US)
106
 #else
107
 #else
107
   #define U8G_DELAY() u8g_10MicroDelay()
108
   #define U8G_DELAY() u8g_10MicroDelay()
108
 #endif
109
 #endif

+ 19
- 15
Marlin/src/module/stepper.cpp View File

41
  * along with Grbl.  If not, see <http://www.gnu.org/licenses/>.
41
  * along with Grbl.  If not, see <http://www.gnu.org/licenses/>.
42
  */
42
  */
43
 
43
 
44
-/* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
45
-   and Philipp Tiefenbacher. */
44
+/**
45
+ * Timer calculations informed by the 'RepRap cartesian firmware' by Zack Smith
46
+ * and Philipp Tiefenbacher.
47
+ */
46
 
48
 
47
-/* Jerk controlled movements planner added by Eduardo José Tagle in April
48
-   2018, Equations based on Synthethos TinyG2 sources, but the fixed-point
49
-   implementation is a complete new one, as we are running the ISR with a
50
-   variable period.
51
-   Also implemented the Bézier velocity curve evaluation in ARM assembler,
52
-   to avoid impacting ISR speed. */
49
+/**
50
+ * Jerk controlled movements planner added Apr 2018 by Eduardo José Tagle.
51
+ * Equations based on Synthethos TinyG2 sources, but the fixed-point
52
+ * implementation is new, as we are running the ISR with a variable period.
53
+ * Also implemented the Bézier velocity curve evaluation in ARM assembler,
54
+ * to avoid impacting ISR speed.
55
+ */
53
 
56
 
54
 #include "stepper.h"
57
 #include "stepper.h"
55
 
58
 
67
 #include "../gcode/queue.h"
70
 #include "../gcode/queue.h"
68
 #include "../sd/cardreader.h"
71
 #include "../sd/cardreader.h"
69
 #include "../Marlin.h"
72
 #include "../Marlin.h"
73
+#include "../HAL/Delay.h"
70
 
74
 
71
 #if MB(ALLIGATOR)
75
 #if MB(ALLIGATOR)
72
   #include "../feature/dac/dac_dac084s085.h"
76
   #include "../feature/dac/dac_dac084s085.h"
1471
       while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
1475
       while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
1472
       pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
1476
       pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
1473
     #elif EXTRA_CYCLES_XYZE > 0
1477
     #elif EXTRA_CYCLES_XYZE > 0
1474
-      DELAY_NOPS(EXTRA_CYCLES_XYZE);
1478
+      DELAY_NS(EXTRA_CYCLES_XYZE * NANOSECONDS_PER_CYCLE);
1475
     #endif
1479
     #endif
1476
 
1480
 
1477
     #if HAS_X_STEP
1481
     #if HAS_X_STEP
1506
     #if EXTRA_CYCLES_XYZE > 20
1510
     #if EXTRA_CYCLES_XYZE > 20
1507
       if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
1511
       if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
1508
     #elif EXTRA_CYCLES_XYZE > 0
1512
     #elif EXTRA_CYCLES_XYZE > 0
1509
-      if (i) DELAY_NOPS(EXTRA_CYCLES_XYZE);
1513
+      if (i) DELAY_NS(EXTRA_CYCLES_XYZE * NANOSECONDS_PER_CYCLE);
1510
     #endif
1514
     #endif
1511
 
1515
 
1512
   } // steps_loop
1516
   } // steps_loop
1739
         while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
1743
         while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
1740
         pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
1744
         pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
1741
       #elif EXTRA_CYCLES_E > 0
1745
       #elif EXTRA_CYCLES_E > 0
1742
-        DELAY_NOPS(EXTRA_CYCLES_E);
1746
+        DELAY_NS(EXTRA_CYCLES_E * NANOSECONDS_PER_CYCLE);
1743
       #endif
1747
       #endif
1744
 
1748
 
1745
       switch (LA_active_extruder) {
1749
       switch (LA_active_extruder) {
1762
       #if EXTRA_CYCLES_E > 20
1766
       #if EXTRA_CYCLES_E > 20
1763
         if (e_steps) while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
1767
         if (e_steps) while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
1764
       #elif EXTRA_CYCLES_E > 0
1768
       #elif EXTRA_CYCLES_E > 0
1765
-        if (e_steps) DELAY_NOPS(EXTRA_CYCLES_E);
1769
+        if (e_steps) DELAY_NS(EXTRA_CYCLES_E * NANOSECONDS_PER_CYCLE);
1766
       #endif
1770
       #endif
1767
 
1771
 
1768
     } // e_steps
1772
     } // e_steps
2146
   #else
2150
   #else
2147
     #define _SAVE_START NOOP
2151
     #define _SAVE_START NOOP
2148
     #if EXTRA_CYCLES_BABYSTEP > 0
2152
     #if EXTRA_CYCLES_BABYSTEP > 0
2149
-      #define _PULSE_WAIT DELAY_NOPS(EXTRA_CYCLES_BABYSTEP)
2153
+      #define _PULSE_WAIT DELAY_NS(EXTRA_CYCLES_BABYSTEP * NANOSECONDS_PER_CYCLE)
2150
     #elif STEP_PULSE_CYCLES > 0
2154
     #elif STEP_PULSE_CYCLES > 0
2151
       #define _PULSE_WAIT NOOP
2155
       #define _PULSE_WAIT NOOP
2152
     #elif ENABLED(DELTA)
2156
     #elif ENABLED(DELTA)
2153
-      #define _PULSE_WAIT delayMicroseconds(2);
2157
+      #define _PULSE_WAIT DELAY_US(2);
2154
     #else
2158
     #else
2155
-      #define _PULSE_WAIT delayMicroseconds(4);
2159
+      #define _PULSE_WAIT DELAY_US(4);
2156
     #endif
2160
     #endif
2157
   #endif
2161
   #endif
2158
 
2162
 

+ 5
- 4
Marlin/src/module/temperature.cpp View File

30
 #include "../lcd/ultralcd.h"
30
 #include "../lcd/ultralcd.h"
31
 #include "planner.h"
31
 #include "planner.h"
32
 #include "../core/language.h"
32
 #include "../core/language.h"
33
+#include "../HAL/Delay.h"
33
 
34
 
34
 #if ENABLED(HEATER_0_USES_MAX6675)
35
 #if ENABLED(HEATER_0_USES_MAX6675)
35
   #include "../libs/private_spi.h"
36
   #include "../libs/private_spi.h"
681
         #if ENABLED(PID_EXTRUSION_SCALING)
682
         #if ENABLED(PID_EXTRUSION_SCALING)
682
           cTerm[HOTEND_INDEX] = 0;
683
           cTerm[HOTEND_INDEX] = 0;
683
           if (_HOTEND_TEST) {
684
           if (_HOTEND_TEST) {
684
-            long e_position = stepper.position(E_AXIS);
685
+            const long e_position = stepper.position(E_AXIS);
685
             if (e_position > last_e_position) {
686
             if (e_position > last_e_position) {
686
               lpq[lpq_ptr] = e_position - last_e_position;
687
               lpq[lpq_ptr] = e_position - last_e_position;
687
               last_e_position = e_position;
688
               last_e_position = e_position;
688
             }
689
             }
689
-            else {
690
+            else
690
               lpq[lpq_ptr] = 0;
691
               lpq[lpq_ptr] = 0;
691
-            }
692
+
692
             if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
693
             if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
693
             cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
694
             cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
694
             pid_output += cTerm[HOTEND_INDEX];
695
             pid_output += cTerm[HOTEND_INDEX];
1629
 
1630
 
1630
     WRITE(MAX6675_SS, 0); // enable TT_MAX6675
1631
     WRITE(MAX6675_SS, 0); // enable TT_MAX6675
1631
 
1632
 
1632
-    DELAY_100NS;          // Ensure 100ns delay
1633
+    DELAY_NS(100);       // Ensure 100ns delay
1633
 
1634
 
1634
     // Read a big-endian temperature value
1635
     // Read a big-endian temperature value
1635
     max6675_temp = 0;
1636
     max6675_temp = 0;

+ 3
- 3
Marlin/src/pins/pins_ANET_10.h View File

177
     #define BTN_EN2          10
177
     #define BTN_EN2          10
178
     #define BTN_ENC          16
178
     #define BTN_ENC          16
179
     #ifndef ST7920_DELAY_1
179
     #ifndef ST7920_DELAY_1
180
-      #define ST7920_DELAY_1 DELAY_0_NOP
180
+      #define ST7920_DELAY_1 DELAY_NS(0)
181
     #endif
181
     #endif
182
     #ifndef ST7920_DELAY_2
182
     #ifndef ST7920_DELAY_2
183
-      #define ST7920_DELAY_2 DELAY_1_NOP
183
+      #define ST7920_DELAY_2 DELAY_NS(63)
184
     #endif
184
     #endif
185
     #ifndef ST7920_DELAY_3
185
     #ifndef ST7920_DELAY_3
186
-      #define ST7920_DELAY_3 DELAY_2_NOP
186
+      #define ST7920_DELAY_3 DELAY_NS(125)
187
     #endif
187
     #endif
188
     #define STD_ENCODER_PULSES_PER_STEP 4
188
     #define STD_ENCODER_PULSES_PER_STEP 4
189
     #define STD_ENCODER_STEPS_PER_MENU_ITEM 1
189
     #define STD_ENCODER_STEPS_PER_MENU_ITEM 1

+ 3
- 3
Marlin/src/pins/pins_MELZI_CREALITY.h View File

55
 
55
 
56
 // Alter timing for graphical display
56
 // Alter timing for graphical display
57
 #ifndef ST7920_DELAY_1
57
 #ifndef ST7920_DELAY_1
58
-  #define ST7920_DELAY_1 DELAY_2_NOP
58
+  #define ST7920_DELAY_1 DELAY_NS(125)
59
 #endif
59
 #endif
60
 #ifndef ST7920_DELAY_2
60
 #ifndef ST7920_DELAY_2
61
-  #define ST7920_DELAY_2 DELAY_2_NOP
61
+  #define ST7920_DELAY_2 DELAY_NS(125)
62
 #endif
62
 #endif
63
 #ifndef ST7920_DELAY_3
63
 #ifndef ST7920_DELAY_3
64
-  #define ST7920_DELAY_3 DELAY_2_NOP
64
+  #define ST7920_DELAY_3 DELAY_NS(125)
65
 #endif
65
 #endif
66
 
66
 
67
 #if ENABLED(MINIPANEL)
67
 #if ENABLED(MINIPANEL)

+ 3
- 3
Marlin/src/pins/pins_MELZI_MALYAN.h View File

44
 
44
 
45
 // Alter timing for graphical display
45
 // Alter timing for graphical display
46
 #ifndef ST7920_DELAY_1
46
 #ifndef ST7920_DELAY_1
47
-  #define ST7920_DELAY_1 DELAY_2_NOP
47
+  #define ST7920_DELAY_1 DELAY_NS(125)
48
 #endif
48
 #endif
49
 #ifndef ST7920_DELAY_2
49
 #ifndef ST7920_DELAY_2
50
-  #define ST7920_DELAY_2 DELAY_2_NOP
50
+  #define ST7920_DELAY_2 DELAY_NS(125)
51
 #endif
51
 #endif
52
 #ifndef ST7920_DELAY_3
52
 #ifndef ST7920_DELAY_3
53
-  #define ST7920_DELAY_3 DELAY_2_NOP
53
+  #define ST7920_DELAY_3 DELAY_NS(125)
54
 #endif
54
 #endif

+ 3
- 3
Marlin/src/pins/pins_MELZI_TRONXY.h View File

51
 #define BTN_ENC         26
51
 #define BTN_ENC         26
52
 
52
 
53
 #ifndef ST7920_DELAY_1
53
 #ifndef ST7920_DELAY_1
54
-  #define ST7920_DELAY_1 DELAY_0_NOP
54
+  #define ST7920_DELAY_1 DELAY_NS(0)
55
 #endif
55
 #endif
56
 #ifndef ST7920_DELAY_2
56
 #ifndef ST7920_DELAY_2
57
-  #define ST7920_DELAY_2 DELAY_2_NOP
57
+  #define ST7920_DELAY_2 DELAY_NS(125)
58
 #endif
58
 #endif
59
 #ifndef ST7920_DELAY_3
59
 #ifndef ST7920_DELAY_3
60
-  #define ST7920_DELAY_3 DELAY_0_NOP
60
+  #define ST7920_DELAY_3 DELAY_NS(0)
61
 #endif
61
 #endif

+ 3
- 3
Marlin/src/pins/pins_PRINTRBOARD_REVF.h View File

244
 
244
 
245
     // increase delays
245
     // increase delays
246
     #ifndef ST7920_DELAY_1
246
     #ifndef ST7920_DELAY_1
247
-      #define ST7920_DELAY_1 DELAY_5_NOP
247
+      #define ST7920_DELAY_1 DELAY_NS(313)
248
     #endif
248
     #endif
249
     #ifndef ST7920_DELAY_2
249
     #ifndef ST7920_DELAY_2
250
-      #define ST7920_DELAY_2 DELAY_5_NOP
250
+      #define ST7920_DELAY_2 DELAY_NS(313)
251
     #endif
251
     #endif
252
     #ifndef ST7920_DELAY_3
252
     #ifndef ST7920_DELAY_3
253
-      #define ST7920_DELAY_3 DELAY_5_NOP
253
+      #define ST7920_DELAY_3 DELAY_NS(313)
254
     #endif
254
     #endif
255
 
255
 
256
   #else
256
   #else

+ 3
- 3
Marlin/src/pins/pins_SANGUINOLOLU_11.h View File

239
     #define BTN_EN2             30
239
     #define BTN_EN2             30
240
 
240
 
241
     #ifndef ST7920_DELAY_1
241
     #ifndef ST7920_DELAY_1
242
-      #define ST7920_DELAY_1 DELAY_0_NOP
242
+      #define ST7920_DELAY_1 DELAY_NS(0)
243
     #endif
243
     #endif
244
     #ifndef ST7920_DELAY_2
244
     #ifndef ST7920_DELAY_2
245
-      #define ST7920_DELAY_2 DELAY_3_NOP
245
+      #define ST7920_DELAY_2 DELAY_NS(188)
246
     #endif
246
     #endif
247
     #ifndef ST7920_DELAY_3
247
     #ifndef ST7920_DELAY_3
248
-      #define ST7920_DELAY_3 DELAY_0_NOP
248
+      #define ST7920_DELAY_3 DELAY_NS(0)
249
     #endif
249
     #endif
250
 
250
 
251
   #elif ENABLED(ZONESTAR_LCD) // For the Tronxy Melzi boards
251
   #elif ENABLED(ZONESTAR_LCD) // For the Tronxy Melzi boards

+ 3
- 8
Marlin/src/pins/pins_STM32F4.h View File

176
 //
176
 //
177
 // ST7920 Delays
177
 // ST7920 Delays
178
 //
178
 //
179
-
180
-#define STM_NOP __asm__("nop\n\t")
181
-#define STM_DELAY_SHORT { STM_NOP; STM_NOP; STM_NOP; STM_NOP; }
182
-#define STM_DELAY_LONG  { STM_DELAY_SHORT; STM_DELAY_SHORT; STM_NOP; STM_NOP; }
183
-
184
 #ifndef ST7920_DELAY_1
179
 #ifndef ST7920_DELAY_1
185
-  #define ST7920_DELAY_1 { STM_DELAY_SHORT; STM_DELAY_SHORT; }
180
+  #define ST7920_DELAY_1 DELAY_NS(96)
186
 #endif
181
 #endif
187
 
182
 
188
 #ifndef ST7920_DELAY_2
183
 #ifndef ST7920_DELAY_2
189
-  #define ST7920_DELAY_2 { STM_DELAY_SHORT; }
184
+  #define ST7920_DELAY_2 DELAY_NS(48)
190
 #endif
185
 #endif
191
 
186
 
192
 #ifndef ST7920_DELAY_3
187
 #ifndef ST7920_DELAY_3
193
-  #define ST7920_DELAY_3 { STM_DELAY_LONG; STM_DELAY_LONG; STM_DELAY_LONG; STM_DELAY_LONG; STM_DELAY_LONG; STM_DELAY_LONG; }
188
+  #define ST7920_DELAY_3 DELAY_NS(715)
194
 #endif
189
 #endif

Loading…
Cancel
Save