/*
motor_pins.h - pin mappings resolver for ganged/squared/ABC axes
NOTE: This file is not used by the core, it may be used by drivers to simplify board map files
Part of grblHAL
Copyright (c) 2021-2026 Terje Io
grblHAL 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.
grblHAL 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 grblHAL. If not, see .
*/
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wexpansion-to-defined"
#endif
#define CAT(a, b) CAT_(a, b)
#define CAT_(a, b) a##b
#define MOTOR_IO(n, t) CAT(M, CAT(n, t))
#define mn_has_limit(a) \
(a == 3 ? defined(M3_LIMIT_PIN) : \
(a == 4 ? defined(M4_LIMIT_PIN) : \
(a == 5 ? defined(M5_LIMIT_PIN) : \
(a == 6 ? defined(M6_LIMIT_PIN) : \
(a == 7 ? defined(M7_LIMIT_PIN) : 0)))))
#define mn_has_home(a) \
(a == 3 ? defined(M3_HOME_PIN) : \
(a == 4 ? defined(M4_HOME_PIN) : \
(a == 5 ? defined(M5_HOME_PIN) : \
(a == 6 ? defined(M6_HOME_PIN) : \
(a == 7 ? defined(M7_HOME_PIN) : 0)))))
#define mn_has_limit_max(a) \
(a == 3 ? defined(M3_LIMIT_PIN_MAX) : \
(a == 4 ? defined(M4_LIMIT_PIN_MAX) : \
(a == 5 ? defined(M5_LIMIT_PIN_MAX) : \
(a == 6 ? defined(M6_LIMIT_PIN_MAX) : \
(a == 7 ? defined(M7_LIMIT_PIN_MAX) : 0)))))
#define mn_has_fault(a) \
(a == 3 ? defined(M3_MOTOR_FAULT_PIN) : \
(a == 4 ? defined(M4_MOTOR_FAULT_PIN) : \
(a == 5 ? defined(M5_MOTOR_FAULT_PIN) : \
(a == 6 ? defined(M6_MOTOR_FAULT_PIN) : \
(a == 7 ? defined(M7_MOTOR_FAULT_PIN) : 0)))))
#define mn_has_enable(a) \
(a == 3 ? defined(M3_ENABLE_PIN) : \
(a == 4 ? defined(M4_ENABLE_PIN) : \
(a == 5 ? defined(M5_ENABLE_PIN) : \
(a == 6 ? defined(M6_ENABLE_PIN) : \
(a == 7 ? defined(M7_ENABLE_PIN) : 0)))))
#define N_MOTORS (3 + defined(M3_AVAILABLE) + defined(M4_AVAILABLE) + defined(M5_AVAILABLE) + defined(M6_AVAILABLE) + defined(M7_AVAILABLE))
#if N_AXIS > 3
#if !defined(M3_STEP_BIT)
#define M3_STEP_BIT (1<>M3_LIMIT_PIN)
#else
#define M3_LIMIT_BIT 0
#endif
#endif
#if !defined(M3_LIMIT_MAX_BIT)
#ifdef M3_LIMIT_MAX_PIN
#define M3_LIMIT_MAX_BIT (1< 4
#if !defined(M4_STEP_BIT)
#define M4_STEP_BIT (1<>M4_LIMIT_PIN)
#else
#define M4_LIMIT_BIT 0
#endif
#endif
#if !defined(M4_LIMIT_MAX_BIT)
#ifdef M4_LIMIT_MAX_PIN
#define M4_LIMIT_MAX_BIT (1< 5
#if !defined(M5_STEP_BIT)
#define M5_STEP_BIT (1<>M5_LIMIT_PIN)
#else
#define M5_LIMIT_BIT 0
#endif
#endif
#if !defined(M5_LIMIT_MAX_BIT)
#ifdef M5_LIMIT_MAX_PIN
#define M5_LIMIT_MAX_BIT (1< 6
#if !defined(M6_STEP_BIT)
#define M6_STEP_BIT (1<>M6_LIMIT_PIN)
#else
#define M6_LIMIT_BIT 0
#endif
#endif
#if !defined(M6_LIMIT_MAX_BIT)
#ifdef M6_LIMIT_MAX_PIN
#define M6_LIMIT_MAX_BIT (1< 7
#if !defined(M7_STEP_BIT)
#define M7_STEP_BIT (1<>M7_LIMIT_PIN)
#else
#define M7_LIMIT_BIT 0
#endif
#endif
#if !defined(M7_LIMIT_MAX_BIT)
#ifdef M7_LIMIT_MAX_PIN
#define M7_LIMIT_MAX_BIT (1< N_ABC_MOTORS
#error "Axis configuration is not supported!"
#endif
#if N_AUTO_SQUARED
#define SQUARING_ENABLED
#endif
#if N_GANGED
#define GANGING_ENABLED
#endif
#if Z_GANGED
#if N_MOTORS == 8
#define Z2_MOTOR_IDX 7
#elif N_MOTORS == 7
#define Z2_MOTOR_IDX 6
#elif N_MOTORS == 6
#define Z2_MOTOR_IDX 5
#elif N_MOTORS == 5
#define Z2_MOTOR_IDX 4
#elif N_MOTORS == 4
#define Z2_MOTOR_IDX 3
#endif
#elif Y_GANGED
#if N_MOTORS == 8
#define Y2_MOTOR_IDX 7
#elif N_MOTORS == 7
#define Y2_MOTOR_IDX 6
#elif N_MOTORS == 6
#define Y2_MOTOR_IDX 5
#elif N_MOTORS == 5
#define Y2_MOTOR_IDX 4
#elif N_MOTORS == 4
#define Y2_MOTOR_IDX 3
#endif
#elif X_GANGED
#if N_MOTORS == 8
#define X2_MOTOR_IDX 7
#elif N_MOTORS == 7
#define X2_MOTOR_IDX 6
#elif N_MOTORS == 6
#define X2_MOTOR_IDX 5
#elif N_MOTORS == 5
#define X2_MOTOR_IDX 4
#elif N_MOTORS == 4
#define X2_MOTOR_IDX 3
#endif
#endif
#if Y_GANGED && !defined(Y2_MOTOR_IDX)
#if N_MOTORS == 8
#define Y2_MOTOR_IDX 6
#elif N_MOTORS == 7
#define Y2_MOTOR_IDX 5
#elif N_MOTORS == 6
#define Y2_MOTOR_IDX 4
#elif N_MOTORS == 5
#define Y2_MOTOR_IDX 3
#endif
#elif X_GANGED && !defined(X2_MOTOR_IDX)
#if N_MOTORS == 8
#define X2_MOTOR_IDX 6
#elif N_MOTORS == 7
#define X2_MOTOR_IDX 5
#elif N_MOTORS == 6
#define X2_MOTOR_IDX 4
#elif N_MOTORS == 5
#define X2_MOTOR_IDX 3
#endif
#endif
#if X_GANGED && !defined(X2_MOTOR_IDX)
#if N_MOTORS == 8
#define X2_MOTOR_IDX 5
#elif N_MOTORS == 7
#define X2_MOTOR_IDX 4
#elif N_MOTORS == 6
#define X2_MOTOR_IDX 3
#endif
#endif
#ifdef X2_MOTOR_IDX
#define X2_STEP_PORT MOTOR_IO(X2_MOTOR_IDX, _STEP_PORT)
#define X2_STEP_PIN MOTOR_IO(X2_MOTOR_IDX, _STEP_PIN)
#define X2_STEP_BIT (1<= N_MOTORS
#error "No pins are available for A axis motor"
#endif
#define A_STEP_PORT MOTOR_IO(A_AXIS_IDX, _STEP_PORT)
#define A_STEP_PIN MOTOR_IO(A_AXIS_IDX, _STEP_PIN)
#define A_STEP_BIT (1<= N_MOTORS
#error "No pins are available for B axis motor"
#endif
#define B_STEP_PORT MOTOR_IO(B_AXIS_IDX, _STEP_PORT)
#define B_STEP_PIN MOTOR_IO(B_AXIS_IDX, _STEP_PIN)
#define B_STEP_BIT (1<= N_MOTORS
#error "No pins are available for C axis motor"
#endif
#define C_STEP_PORT MOTOR_IO(C_AXIS_IDX, _STEP_PORT)
#define C_STEP_PIN MOTOR_IO(C_AXIS_IDX, _STEP_PIN)
#define C_STEP_BIT (1<= N_MOTORS
#error "No pins are available for U axis motor"
#endif
#define U_STEP_PORT MOTOR_IO(U_AXIS_IDX, _STEP_PORT)
#define U_STEP_PIN MOTOR_IO(U_AXIS_IDX, _STEP_PIN)
#define U_STEP_BIT (1<= N_MOTORS
#error "No pins are available for V axis motor"
#endif
#define V_STEP_PORT MOTOR_IO(V_AXIS_IDX, _STEP_PORT)
#define V_STEP_PIN MOTOR_IO(V_AXIS_IDX, _STEP_PIN)
#define V_STEP_BIT (1<= N_MOTORS
#error "No pins are available for W axis motor"
#endif
#define W_STEP_PORT MOTOR_IO(W_AXIS_IDX, _STEP_PORT)
#define W_STEP_PIN MOTOR_IO(W_AXIS_IDX, _STEP_PIN)
#define W_STEP_BIT (1<