From 9a950e3a5a1893248609f5c0687b578ebe1a0240 Mon Sep 17 00:00:00 2001
From: Chris Pepper <p3p@p3psoft.co.uk>
Date: Sat, 5 Aug 2017 01:20:01 +0100
Subject: [PATCH] Update the delay functions and change the default pinmap for
 character displays (#7434)

---
 Marlin/pins_RAMPS_RE_ARM.h             |  6 +++---
 Marlin/src/HAL/HAL_LPC1768/arduino.cpp | 25 +++++++++++++++++++------
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/Marlin/pins_RAMPS_RE_ARM.h b/Marlin/pins_RAMPS_RE_ARM.h
index 6dda906c62..a188880a9b 100644
--- a/Marlin/pins_RAMPS_RE_ARM.h
+++ b/Marlin/pins_RAMPS_RE_ARM.h
@@ -275,11 +275,11 @@
   #define LCD_PINS_ENABLE     51  // (MOSI) J3-10 & AUX-3
   #define LCD_PINS_D4         52  // (SCK)  J3-9 & AUX-3
 
-  #define LCD_PINS_D5         59  // J3-8 & AUX-2
+  #define LCD_PINS_D5         71  // ENET_MDIO
   #define DOGLCD_A0           59  // J3-8 & AUX-2
-  #define LCD_PINS_D6         63  // J5-3 & AUX-2
+  #define LCD_PINS_D6         73  // ENET_RX_ER
   #define DOGLCD_CS           63  // J5-3 & AUX-2
-  #define LCD_PINS_D7          6  // (SERVO1) J5-1 & SERVO connector
+  #define LCD_PINS_D7         75  // ENET_RXD1
 
 
   //#define MISO                50  // system defined J3-10 & AUX-3
diff --git a/Marlin/src/HAL/HAL_LPC1768/arduino.cpp b/Marlin/src/HAL/HAL_LPC1768/arduino.cpp
index 0d32ea193b..3f2e101a5c 100644
--- a/Marlin/src/HAL/HAL_LPC1768/arduino.cpp
+++ b/Marlin/src/HAL/HAL_LPC1768/arduino.cpp
@@ -43,17 +43,28 @@ uint32_t millis() {
   return _millis;
 }
 
-//todo: recheck all of this
 void delayMicroseconds(uint32_t us) {
-  if (us < 2) return; // function jump, compare, return about 1us
-  us--;
-  static const int nop_factor = (SystemCoreClock / 10000000); // measured accurate at 10us
+  static const int nop_factor = (SystemCoreClock / 11000000);
   static volatile int loops = 0;
-  if (us < 20) { // burn cycles
+
+  //previous ops already burned most of 1us, burn the rest
+  loops = nop_factor / 4; //measured at 1us
+  while (loops > 0) --loops;
+
+  if (us < 2) return;
+  us--;
+
+  //redirect to delay for large values, then set new delay to remainder
+  if (us > 1000) {
+    delay(us / 1000);
+    us = us % 1000;
+  }
+
+  if (us < 5) { // burn cycles, time in interrupts will not be taken into account
     loops = us * nop_factor;
     while (loops > 0) --loops;
   }
-  else { // poll systick
+  else { // poll systick, more accurate through interrupts
     int32_t start = SysTick->VAL;
     int32_t load = SysTick->LOAD;
     int32_t end = start - (load / 1000) * us;
@@ -67,6 +78,8 @@ void delayMicroseconds(uint32_t us) {
 
 extern "C" void delay(int msec) {
    volatile int32_t end = _millis + msec;
+   SysTick->VAL = SysTick->LOAD; // reset systick counter so next systick is in exactly 1ms
+                                 // this could extend the time between systicks by upto 1ms
    while (_millis < end) __WFE();
 }
 
-- 
GitLab