Pinout mapping in cpu_map.h - fra589/grbl-Mega-5X GitHub Wiki

How-to change the hardware mapping of Grbl in cpu_map.h

First, search for Arduino Mega pinout picture on the web, perhaps you can find a picture like this (or a better one :smile:):
Arduino Mega pinout diagram In this picture, you will find all you need to translate mappings between AVR ATmega2560 CPU physical pins and port pins, Arduino pin names and pins numbers, pins functions, ...
Another good document for this is here: https://content.arduino.cc/assets/Pinout-Mega2560rev3_latest.pdf

In the file cpu_map.h, the mapping used by the 4 to 6 axis version is defined in the #define CPU_MAP_2560_RAMPS_BOARD section.
For example, by default, the step pin of the axis number 0 (by default X axis) is defined on Arduino port A0. Searching for A0 in the Arduino Mega pinout diagram picture, you will find Arduino's A0 is the CPU port pin PF0. This mean the CPU port number is F and the CPU port bit is 0. To define this in cpu_map.h, there is 2 lines definitions :

   #define STEP_PORT_0 F  
   #define STEP_BIT_0 0  // X Step - Pin A0  

For steppers and limit switchs, there is macros definitions wich define the DDR automaticaly.

Another example for the definition of the "spindle enable" on Arduino port D4:

Arduino mega pinout detail

In the picture, D4 is finded on port G, bit 5 (PG5). So, the cpu_map.h definition is:

  #define SPINDLE_ENABLE_DDR      DDRG
  #define SPINDLE_ENABLE_PORT     PORTG
  #define SPINDLE_ENABLE_BIT      5 // MEGA2560 Digital Pin 4 - Ramps 1.4 Servo 4 Signal pin (D4)

The last definition in the #define CPU_MAP_2560_RAMPS_BOARD section is the spindle output PWM pin, wich is by default on port D8 (PH5 on pinout picture):

  // Define spindle output pins.
  #define SPINDLE_PWM_DDR   DDRH
  #define SPINDLE_PWM_PORT  PORTH
  #define SPINDLE_PWM_BIT   5 // MEGA2560 Digital Pin 8