From 3066655727a5b19cb3fd0fae4a558de813d4e442 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Sun, 19 Nov 2017 20:26:53 -0600
Subject: [PATCH] Try port*100+pin, fix config dependency

---
 Marlin/src/HAL/HAL_AVR/MarlinSerial.h         |   2 +-
 .../HAL/HAL_LPC1768/HAL_LCD_I2C_routines.h    |   8 +-
 .../HAL/HAL_LPC1768/HAL_LCD_pin_routines.c    |  81 +++++----
 Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp |   2 +-
 Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.h   |   2 +-
 Marlin/src/HAL/HAL_LPC1768/fastio.h           |   2 +-
 Marlin/src/HAL/HAL_LPC1768/main.cpp           |   2 +-
 Marlin/src/HAL/HAL_LPC1768/pinmapping.cpp     |  65 ++++---
 Marlin/src/HAL/HAL_LPC1768/pinmapping.h       | 161 +++++++++---------
 Marlin/src/gcode/control/M42.cpp              |   4 +-
 Marlin/src/inc/MarlinConfig.h                 |   9 +-
 Marlin/src/inc/MarlinConfigPre.h              |  34 ++++
 Marlin/src/inc/Version.h                      |   8 +-
 13 files changed, 212 insertions(+), 168 deletions(-)
 create mode 100644 Marlin/src/inc/MarlinConfigPre.h

diff --git a/Marlin/src/HAL/HAL_AVR/MarlinSerial.h b/Marlin/src/HAL/HAL_AVR/MarlinSerial.h
index 6a9fd776fa..ec647f9b33 100644
--- a/Marlin/src/HAL/HAL_AVR/MarlinSerial.h
+++ b/Marlin/src/HAL/HAL_AVR/MarlinSerial.h
@@ -32,7 +32,7 @@
 #ifndef _MARLINSERIAL_H_
 #define _MARLINSERIAL_H_
 
-#include "../../inc/MarlinConfig.h"
+#include "../../inc/MarlinConfigPre.h"
 
 #include <WString.h>
 
diff --git a/Marlin/src/HAL/HAL_LPC1768/HAL_LCD_I2C_routines.h b/Marlin/src/HAL/HAL_LPC1768/HAL_LCD_I2C_routines.h
index f53161f6d0..c02ae6e964 100644
--- a/Marlin/src/HAL/HAL_LPC1768/HAL_LCD_I2C_routines.h
+++ b/Marlin/src/HAL/HAL_LPC1768/HAL_LCD_I2C_routines.h
@@ -20,16 +20,12 @@
  *
  */
 
-#if defined(TARGET_LPC1768)
-
-   void u8g_i2c_init(uint8_t options);
+#ifdef TARGET_LPC1768
 
+  void u8g_i2c_init(uint8_t options);
   uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos);
-
   uint8_t u8g_i2c_start(uint8_t sla);
-
   uint8_t u8g_i2c_send_byte(uint8_t data);
-
   void u8g_i2c_stop(void);
 
 #endif
diff --git a/Marlin/src/HAL/HAL_LPC1768/HAL_LCD_pin_routines.c b/Marlin/src/HAL/HAL_LPC1768/HAL_LCD_pin_routines.c
index 35c02ffec1..f4e5a76b47 100644
--- a/Marlin/src/HAL/HAL_LPC1768/HAL_LCD_pin_routines.c
+++ b/Marlin/src/HAL/HAL_LPC1768/HAL_LCD_pin_routines.c
@@ -30,34 +30,34 @@
  * resulted in using about about 25% of the CPU's time.
  */
 
-#if defined(TARGET_LPC1768)
+#ifdef TARGET_LPC1768
 
-  #include <LPC17xx.h>
-  #include <lpc17xx_pinsel.h>
-  #include "src/core/macros.h"
-//  #include "pinmapping.h"
+#include <LPC17xx.h>
+#include <lpc17xx_pinsel.h>
+#include "src/core/macros.h"
+//#include "pinmapping.h"
 
-  #define LPC_PORT_OFFSET         (0x0020)
-  #define LPC_PIN(pin)            (1UL << pin)
-  #define LPC_GPIO(port)          ((volatile LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + LPC_PORT_OFFSET * port))
+#define LPC_PORT_OFFSET         (0x0020)
+#define LPC_PIN(pin)            (1UL << pin)
+#define LPC_GPIO(port)          ((volatile LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + LPC_PORT_OFFSET * port))
 
-  #define INPUT 0
-  #define OUTPUT 1
-  #define INPUT_PULLUP 2
+#define INPUT 0
+#define OUTPUT 1
+#define INPUT_PULLUP 2
 
 
 uint8_t LPC1768_PIN_PORT(const uint8_t pin);
 uint8_t LPC1768_PIN_PIN(const uint8_t pin);
 
-  #ifdef __cplusplus
-      extern "C" {
-  #endif
+#ifdef __cplusplus
+    extern "C" {
+#endif
 
-// IO functions
+// I/O functions
 // As defined by Arduino INPUT(0x0), OUPUT(0x1), INPUT_PULLUP(0x2)
 void pinMode_LCD(uint8_t pin, uint8_t mode) {
-#define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111))
-#define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111))
+  #define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111))
+  #define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111))
   PINSEL_CFG_Type config = { LPC1768_PIN_PORT(pin),
                              LPC1768_PIN_PIN(pin),
                              PINSEL_FUNC_0,
@@ -82,35 +82,32 @@ void pinMode_LCD(uint8_t pin, uint8_t mode) {
   }
 }
 
+void u8g_SetPinOutput(uint8_t internal_pin_number) {
+   pinMode_LCD(internal_pin_number, 1);  // OUTPUT
+}
 
-  void u8g_SetPinOutput(uint8_t internal_pin_number) {
-     pinMode_LCD(internal_pin_number, 1);  // OUTPUT
-  }
-
-  void u8g_SetPinInput(uint8_t internal_pin_number) {
-     pinMode_LCD(internal_pin_number, 0);  // INPUT
-  }
+void u8g_SetPinInput(uint8_t internal_pin_number) {
+   pinMode_LCD(internal_pin_number, 0);  // INPUT
+}
 
+void u8g_SetPinLevel(uint8_t  pin, uint8_t  pin_status) {
+  #define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111))
+  #define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111))
+  if (pin_status)
+    LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET = LPC_PIN(LPC1768_PIN_PIN(pin));
+  else
+    LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR = LPC_PIN(LPC1768_PIN_PIN(pin));
+}
 
+uint8_t u8g_GetPinLevel(uint8_t pin) {
+  #define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111))
+  #define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111))
+  return (uint32_t)LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOPIN & LPC_PIN(LPC1768_PIN_PIN(pin)) ? 1 : 0;
+}
 
-  void u8g_SetPinLevel(uint8_t  pin, uint8_t  pin_status) {
-#define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111))
-#define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111))
-    if (pin_status)
-      LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET = LPC_PIN(LPC1768_PIN_PIN(pin));
-    else
-      LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR = LPC_PIN(LPC1768_PIN_PIN(pin));
-  }
 
-  uint8_t u8g_GetPinLevel(uint8_t pin) {
-#define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111))
-#define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111))
-    return (uint32_t)LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOPIN & LPC_PIN(LPC1768_PIN_PIN(pin)) ? 1 : 0;
+#ifdef __cplusplus
   }
-
-
-  #ifdef __cplusplus
-    }
-  #endif
-
 #endif
+
+#endif // TARGET_LPC1768
diff --git a/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp b/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp
index 1ea90310f3..61cb23203d 100644
--- a/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp
+++ b/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp
@@ -39,7 +39,7 @@
 #include "../../inc/MarlinConfig.h"
 #include <stdint.h>
 #include <stdarg.h>
-#include "arduino.h"
+#include "include/arduino.h"
 #include "pinmapping.h"
 #include "fastio.h"
 #include "SoftwareSerial.h"
diff --git a/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.h b/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.h
index fe01e8147e..fc3dcfe0c8 100644
--- a/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.h
+++ b/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.h
@@ -33,7 +33,7 @@
 #ifndef SOFTWARESERIAL_H
 #define SOFTWARESERIAL_H
 
-#include "arduino.h"
+#include "include/arduino.h"
 #include <stdint.h>
 //#include "serial.h"
 #include <Stream.h>
diff --git a/Marlin/src/HAL/HAL_LPC1768/fastio.h b/Marlin/src/HAL/HAL_LPC1768/fastio.h
index c459823cdb..2da989a9c8 100644
--- a/Marlin/src/HAL/HAL_LPC1768/fastio.h
+++ b/Marlin/src/HAL/HAL_LPC1768/fastio.h
@@ -36,7 +36,7 @@
 #define _FASTIO_LPC1768_H
 
 #include <LPC17xx.h>
-#include "arduino.h"
+#include "include/arduino.h"
 #include "pinmapping.h"
 
 bool useable_hardware_PWM(pin_t pin);
diff --git a/Marlin/src/HAL/HAL_LPC1768/main.cpp b/Marlin/src/HAL/HAL_LPC1768/main.cpp
index 7fe2ba0bb2..dd6d24681d 100644
--- a/Marlin/src/HAL/HAL_LPC1768/main.cpp
+++ b/Marlin/src/HAL/HAL_LPC1768/main.cpp
@@ -30,7 +30,7 @@ extern "C" {
 #include "HAL_timers.h"
 #include <stdio.h>
 #include <stdarg.h>
-#include "arduino.h"
+#include "include/arduino.h"
 #include "serial.h"
 #include "LPC1768_PWM.h"
 
diff --git a/Marlin/src/HAL/HAL_LPC1768/pinmapping.cpp b/Marlin/src/HAL/HAL_LPC1768/pinmapping.cpp
index 42a71b80a0..461705092e 100644
--- a/Marlin/src/HAL/HAL_LPC1768/pinmapping.cpp
+++ b/Marlin/src/HAL/HAL_LPC1768/pinmapping.cpp
@@ -22,32 +22,53 @@
 
 #ifdef TARGET_LPC1768
 
-#include "../../inc/MarlinConfig.h"
+#include "pinmapping.h"
+
 #include "../../gcode/parser.h"
 
-int16_t GET_PIN_MAP_INDEX(pin_t pin) {
-  const uint8_t pin_port = LPC1768_PIN_PORT(pin),
-                pin_pin = LPC1768_PIN_PIN(pin);
-  for (size_t i = 0; i < NUM_DIGITAL_PINS; ++i)
-    if (LPC1768_PIN_PORT(pin_map[i]) == pin_port && LPC1768_PIN_PIN(pin_map[i]) == pin_pin)
-      return i;
+// Get the digital pin for an analog index
+pin_t analogInputToDigitalPin(const uint8_t p) {
+  return (p < COUNT(adc_pin_table) ? adc_pin_table[p] : P_NC);
+}
+
+// Return the index of a pin number
+// The pin number given here is in the form ppp:nnnnn
+int16_t GET_PIN_MAP_INDEX(const pin_t pin) {
+  const uint16_t index = (LPC1768_PIN_PORT(pin) << 5) | LPC1768_PIN_PIN(pin);
+  return (index < NUM_DIGITAL_PINS && pin_map[index] != P_NC) ? index : -1;
+}
+
+// Test whether the pin is valid
+bool VALID_PIN(const pin_t p) {
+  const int16_t ind = GET_PIN_MAP_INDEX(p);
+  return ind >= 0 && pin_map[ind] >= 0;
+}
+
+// Get the analog index for a digital pin
+int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p) {
+  return (VALID_PIN(p) ? LPC1768_PIN_ADC(p) : -1);
+}
+
+// Test whether the pin is PWM
+bool PWM_PIN(const pin_t p) {
+  return VALID_PIN(p) && LPC1768_PIN_PWM(p);
+}
+
+// Test whether the pin is interruptable
+bool INTERRUPT_PIN(const pin_t p) {
+  return VALID_PIN(p) && LPC1768_PIN_INTERRUPT(p);
+}
 
-  return -1;
+// Get the pin number at the given index
+pin_t GET_PIN_MAP_PIN(const int16_t ind) {
+  return WITHIN(ind, 0, NUM_DIGITAL_PINS - 1) ? pin_map[ind] : P_NC;
 }
 
-int16_t PARSED_PIN_INDEX(char code, int16_t dval) {  // treats 1.2 and 1.20 as the same thing
-  if (parser.seenval(code)) {
-    int port, pin;
-    char pin_string[3] = {"  "};
-    if (sscanf(parser.strval(code), "%d.%2s", &port, pin_string) == 2) {
-      if (pin_string[1] == '\0') pin_string[1] = '0';   // add trailing zero if a null is found
-      pin = (10 * (pin_string[0] - '0')) + (pin_string[1] - '0');  // convert string to number
-      for (size_t i = 0; i < NUM_DIGITAL_PINS; ++i)
-        if (LPC1768_PIN_PORT(pin_map[i]) == port && LPC1768_PIN_PIN(pin_map[i]) == pin)
-          return i;
-    }
-  }
-  return dval;
+int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) {
+  const uint16_t val = (uint16_t)parser.intval(code), port = val / 100, pin = val % 100;
+  const  int16_t ind = (port < (NUM_DIGITAL_PINS >> 5) && (pin < 32))
+                      ? GET_PIN_MAP_INDEX(port << 5 | pin) : -2;
+  return ind > -2 ? ind : dval;
 }
 
-#endif // TARGET_LPC1768
\ No newline at end of file
+#endif // TARGET_LPC1768
diff --git a/Marlin/src/HAL/HAL_LPC1768/pinmapping.h b/Marlin/src/HAL/HAL_LPC1768/pinmapping.h
index 701557b388..f47ea561f6 100644
--- a/Marlin/src/HAL/HAL_LPC1768/pinmapping.h
+++ b/Marlin/src/HAL/HAL_LPC1768/pinmapping.h
@@ -20,10 +20,10 @@
  *
  */
 
-#ifndef __HAL_PINMAPPING_H__
-#define __HAL_PINMAPPING_H__
+#ifndef _PINMAPPING_H_
+#define _PINMAPPING_H_
 
-#include "../../core/macros.h"
+#include "../../inc/MarlinConfigPre.h"
 
 #include <stdint.h>
 
@@ -94,6 +94,7 @@ typedef int16_t pin_t;
 #define INTERRUPT(b)  BOOL_(b)
 #define PWM(b)        BOOL_(b)
 
+// Combine elements into pin bits: 0b00AAAAWIPPPNNNNN
 #define LPC1768_PIN_(port, pin, int, pwm, adc)  0b00##adc##pwm##int##port##pin
 #define LPC1768_PIN(port, pin, int, pwm, adc)   LPC1768_PIN_(port, pin, int, pwm, adc)
 
@@ -106,7 +107,7 @@ constexpr int8_t LPC1768_PIN_ADC(const pin_t pin) { return (int8_t)((pin >> 10)
 // ******************
 // Runtime pinmapping
 // ******************
-#define P_NC   -1
+#define P_NC -1
 
 #if SERIAL_PORT != 3
   #define P0_00 LPC1768_PIN(PORT(0), PIN( 0), INTERRUPT(1), PWM(0), ADC_NONE)
@@ -187,97 +188,95 @@ constexpr int8_t LPC1768_PIN_ADC(const pin_t pin) { return (int8_t)((pin >> 10)
 #define P4_28   LPC1768_PIN(PORT(4), PIN(28), INTERRUPT(0), PWM(0), ADC_NONE)
 #define P4_29   LPC1768_PIN(PORT(4), PIN(29), INTERRUPT(0), PWM(0), ADC_NONE)
 
-constexpr bool VALID_PIN(const pin_t p) {
-  return (
-    #if SERIAL_PORT == 0
-      (LPC1768_PIN_PORT(p) == 0 && LPC1768_PIN_PIN(p) <= 1)             ||
-      (LPC1768_PIN_PORT(p) == 0 && WITHIN(LPC1768_PIN_PIN(p), 4, 11))   ||
-    #elif SERIAL_PORT == 2
-      (LPC1768_PIN_PORT(p) == 0 && LPC1768_PIN_PIN(p) <= 9)             ||
-    #elif SERIAL_PORT == 3
-      (LPC1768_PIN_PORT(p) == 0 && WITHIN(LPC1768_PIN_PIN(p), 2, 11))   ||
-    #else
-      (LPC1768_PIN_PORT(p) == 0 && LPC1768_PIN_PIN(p) <= 11)            ||
-    #endif
-    #if SERIAL_PORT == 1
-      (LPC1768_PIN_PORT(p) == 0 && WITHIN(LPC1768_PIN_PIN(p), 17, 30))  ||
-    #else
-      (LPC1768_PIN_PORT(p) == 0 && WITHIN(LPC1768_PIN_PIN(p), 15, 30))  ||
-    #endif
-    (LPC1768_PIN_PORT(p) == 1 && LPC1768_PIN_PIN(p) == 1)             ||
-    (LPC1768_PIN_PORT(p) == 1 && LPC1768_PIN_PIN(p) == 4)             ||
-    (LPC1768_PIN_PORT(p) == 1 && WITHIN(LPC1768_PIN_PIN(p), 8, 10))   ||
-    (LPC1768_PIN_PORT(p) == 1 && WITHIN(LPC1768_PIN_PIN(p), 14, 31))  ||
-    (LPC1768_PIN_PORT(p) == 2 && LPC1768_PIN_PIN(p) <= 13)            ||
-    (LPC1768_PIN_PORT(p) == 3 && WITHIN(LPC1768_PIN_PIN(p), 25, 26))  ||
-    (LPC1768_PIN_PORT(p) == 4 && WITHIN(LPC1768_PIN_PIN(p), 28, 29))
-  );
-}
-
-constexpr bool PWM_PIN(const pin_t p) {
-  return (VALID_PIN(p) && LPC1768_PIN_PWM(p));
-}
-
-constexpr bool INTERRUPT_PIN(const pin_t p) {
-  return (VALID_PIN(p) && LPC1768_PIN_INTERRUPT(p));
-}
-
-#if SERIAL_PORT == 0
-  #define NUM_ANALOG_INPUTS 6
-#else
-  #define NUM_ANALOG_INPUTS 8
-#endif
-
-constexpr pin_t adc_pin_table[] = {
-  P0_23, P0_24, P0_25, P0_26, P1_30, P1_31,
-  #if SERIAL_PORT != 0
-    P0_03, P0_02
-  #endif
-};
-
-constexpr pin_t analogInputToDigitalPin(const uint8_t p) {
-  return (p < COUNT(adc_pin_table) ? adc_pin_table[p] : P_NC);
-}
-
-constexpr int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p) {
-  return (VALID_PIN(p) ? LPC1768_PIN_ADC(p) : -1);
-}
-
-// P0.6 thru P0.9 are for the onboard SD card
-// P0.29 and P0.30 are for the USB port
-#define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09, P0_29, P0_30
-
-// Pin map for M43 and M226
-const pin_t pin_map[] = {
+// Pin index for M43 and M226
+constexpr pin_t pin_map[] = {
   #if SERIAL_PORT != 3
     P0_00, P0_01,
+  #else
+    P_NC,  P_NC,
   #endif
   #if SERIAL_PORT != 0
-    P0_02, P0_03,
+                  P0_02, P0_03,
+  #else
+                  P_NC,  P_NC,
   #endif
-  P0_04, P0_05, P0_06, P0_07, P0_08, P0_09,
+                                P0_04, P0_05, P0_06, P0_07,
+    P0_08, P0_09,
   #if SERIAL_PORT != 2
-    P0_10, P0_11,
+                  P0_10, P0_11,
+  #else
+                  P_NC,  P_NC,
   #endif
+                                P_NC,  P_NC,  P_NC,
   #if SERIAL_PORT != 1
-    P0_15, P0_16,
+                                                     P0_15,
+    P0_16,
+  #else
+                                                     P_NC,
+    P_NC,
   #endif
-  P0_17, P0_18, P0_19, P0_20, P0_21, P0_22, P0_23, P0_24,
-  P0_25, P0_26, P0_27, P0_28, P0_29, P0_30,
-  P1_00, P1_01, P1_04, P1_08, P1_09, P1_10, P1_14, P1_15,
+           P0_17, P0_18, P0_19, P0_20, P0_21, P0_22, P0_23,
+    P0_24, P0_25, P0_26, P0_27, P0_28, P0_29, P0_30, P_NC,
+
+  P1_00, P1_01, P_NC,  P_NC,  P1_04, P_NC,  P_NC,  P_NC,
+  P1_08, P1_09, P1_10, P_NC,  P_NC,  P_NC,  P1_14, P1_15,
   P1_16, P1_17, P1_18, P1_19, P1_20, P1_21, P1_22, P1_23,
   P1_24, P1_25, P1_26, P1_27, P1_28, P1_29, P1_30, P1_31,
+
   P2_00, P2_01, P2_02, P2_03, P2_04, P2_05, P2_06, P2_07,
-  P2_08, P2_09, P2_10, P2_11, P2_12, P2_13,
-  P3_25, P3_26,
-  P4_28, P4_29
+  P2_08, P2_09, P2_10, P2_11, P2_12, P2_13, P_NC,  P_NC,
+  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+
+  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+  P_NC,  P3_25, P3_26, P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+
+  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,  P_NC,
+  P_NC,  P_NC,  P_NC,  P_NC,  P4_28, P4_29, P_NC,  P_NC
 };
 
-#define NUM_DIGITAL_PINS COUNT(pin_map)
+constexpr int16_t NUM_DIGITAL_PINS = COUNT(pin_map);
+
+constexpr pin_t adc_pin_table[] = {
+  P0_23, P0_24, P0_25, P0_26, P1_30, P1_31,
+  #if SERIAL_PORT != 0
+    P0_03, P0_02
+  #endif
+};
+
+constexpr int16_t NUM_ANALOG_INPUTS = COUNT(adc_pin_table);
+
+// P0.6 thru P0.9 are for the onboard SD card
+// P0.29 and P0.30 are for the USB port
+#define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09, P0_29, P0_30
+
+// Get the digital pin for an analog index
+pin_t analogInputToDigitalPin(const uint8_t p);
+
+// Return the index of a pin number
+// The pin number given here is in the form ppp:nnnnn
+int16_t GET_PIN_MAP_INDEX(const pin_t pin);
+
+// Test whether the pin is valid
+bool VALID_PIN(const pin_t p);
+
+// Get the analog index for a digital pin
+int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p);
+
+// Test whether the pin is PWM
+bool PWM_PIN(const pin_t p);
+
+// Test whether the pin is interruptable
+bool INTERRUPT_PIN(const pin_t p);
 
-#define GET_PIN_MAP_PIN(i) (WITHIN(i, 0, (int)NUM_DIGITAL_PINS - 1) ? pin_map[i] : -1)
+// Get the pin number at the given index
+pin_t GET_PIN_MAP_PIN(const int16_t ind);
 
-int16_t GET_PIN_MAP_INDEX(pin_t pin);
-int16_t PARSED_PIN_INDEX(char code, int16_t dval = 0);
+// Parse a G-code word into a pin index
+int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
 
-#endif // __HAL_PINMAPPING_H__
+#endif // _PINMAPPING_H_
diff --git a/Marlin/src/gcode/control/M42.cpp b/Marlin/src/gcode/control/M42.cpp
index 7ddd1c0026..18da0d12b1 100644
--- a/Marlin/src/gcode/control/M42.cpp
+++ b/Marlin/src/gcode/control/M42.cpp
@@ -28,7 +28,9 @@
  * M42: Change pin status via GCode
  *
  *  P<pin>  Pin number (LED if omitted)
- *            For LPC1768 enter pin P1_20 as M42 P1.20
+ *          For LPC1768 specify pin P1_02 as M42 P102,
+ *                                  P1_20 as M42 P120, etc.
+ *
  *  S<byte> Pin status from 0 - 255
  */
 void GcodeSuite::M42() {
diff --git a/Marlin/src/inc/MarlinConfig.h b/Marlin/src/inc/MarlinConfig.h
index c52c319ec6..cb8e54af18 100644
--- a/Marlin/src/inc/MarlinConfig.h
+++ b/Marlin/src/inc/MarlinConfig.h
@@ -23,13 +23,8 @@
 #ifndef MARLIN_CONFIG_H
 #define MARLIN_CONFIG_H
 
-#include "../core/boards.h"
-#include "../core/macros.h"
-#include "Version.h"
-#include "../../Configuration.h"
-#include "Conditionals_LCD.h"
-#include "../../Configuration_adv.h"
-#include "Conditionals_adv.h"
+#include "MarlinConfigPre.h"
+
 #include "../HAL/HAL.h"
 #include "../pins/pins.h"
 #if defined(__AVR__) && !defined(USBCON)
diff --git a/Marlin/src/inc/MarlinConfigPre.h b/Marlin/src/inc/MarlinConfigPre.h
new file mode 100644
index 0000000000..62831e37ab
--- /dev/null
+++ b/Marlin/src/inc/MarlinConfigPre.h
@@ -0,0 +1,34 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef MARLIN_CONFIGPRE_H
+#define MARLIN_CONFIGPRE_H
+
+#include "../core/boards.h"
+#include "../core/macros.h"
+#include "Version.h"
+#include "../../Configuration.h"
+#include "Conditionals_LCD.h"
+#include "../../Configuration_adv.h"
+#include "Conditionals_adv.h"
+
+#endif // MARLIN_CONFIGPRE_H
diff --git a/Marlin/src/inc/Version.h b/Marlin/src/inc/Version.h
index 6a08df0fb1..f33a8d73c8 100644
--- a/Marlin/src/inc/Version.h
+++ b/Marlin/src/inc/Version.h
@@ -23,12 +23,12 @@
 #ifndef _VERSION_H_
 #define _VERSION_H_
 
-#include "MarlinConfig.h"
+#include "../core/macros.h" // for ENABLED
 
 /**
- * This file is the standard Marlin version identifier file, all fields can be
- * overriden by the ones defined in _Version.h by using the Configuration.h
- * directive USE_AUTOMATIC_VERSIONING.
+ * This file is the standard Marlin version identifier file.
+ * Use -DUSE_AUTOMATIC_VERSIONING=1 and a custom _Version.h
+ * to override these values.
  */
 
 #if ENABLED(USE_AUTOMATIC_VERSIONING)
-- 
GitLab