main
Drew Owens Publish Microgoose Rev A full-stack hardware, CAD, datasheet, and 7-stage RL stack e843249
/*
 * board_pins.h
 * Hardware pin assignments and bus topology for microgoose-spine.
 * Matches finalized design/netlist.json and design/bom.csv 1:1.
 * Target MCU: STM32G431CBT6 (U4, 170 MHz Cortex-M4F, 128 KB Flash, 32 KB SRAM, LQFP-48).
 */

#ifndef MICROGOOSE_BOARD_PINS_H
#define MICROGOOSE_BOARD_PINS_H

#include <stdint.h>

/* Core clock and peripheral bus frequencies (16 MHz HSI -> PLL 170 MHz Boost Mode) */
#define MG_SYSCLK_HZ                170000000UL
#define MG_HCLK_HZ                  170000000UL
#define MG_PCLK1_HZ                 170000000UL
#define MG_PCLK2_HZ                 170000000UL

/* Real-time Spine control loop rate: 200 Hz (5000 us period) */
#define MG_SPINE_LOOP_HZ            200U
#define MG_SPINE_PERIOD_US          5000U

/* Safety thresholds enforced on the Spine MCU */
#define MG_HOST_WATCHDOG_TIMEOUT_US 100000UL   /* 100 ms host silence trips watchdog */
#define MG_VBAT_UNDERVOLT_MV        6100U      /* 6.10 V 2S Li-ion cutoff */
#define MG_BUS_OVERCURRENT_MA       4200       /* 4.20 A continuous limit on 5V_BUS */
#define MG_OVERCURRENT_TRIP_US      100000UL   /* 100 ms overcurrent persistence */

/*
 * ============================================================================
 * STM32G431CBT6 (U4, LQFP-48) Exact Netlist Pin Assignments
 * ============================================================================
 *
 * 1. Host Radxa Zero 3W Link (J4: BM05B-GHS-TBT, 2x 5V_BUS + GND + TX + RX)
 *    - PA2 (U4.10): USART2_TX (AF7, HOST_TX -> J4.4), DMA1_Channel2
 *    - PA3 (U4.11): USART2_RX (AF7, HOST_RX <- J4.5), DMA1_Channel1
 *    - Supports 1 Mbps default and 2 Mbps high-speed full-duplex framing
 *
 * 2. Dynamixel XL330-M288-T Half-Duplex TTL Bus (J1, J2, J3: BM03B-GHS-TBT)
 *    - PB10 (U4.22): USART3_TX (AF7, DXL_TX -> U6.2), 1 Mbps 8N1
 *    - PB11 (U4.25): USART3_RX (AF7, DXL_RX <- R18 100R <- DXL_DATA), 1 Mbps 8N1
 *    - PB12 (U4.26): GPIO output DXL_DIR driving U6 (SN74LVC1G126DBVR) OE
 *                    with R10 10 kOhm pulldown to GND
 *    - J1 (LEG_L_DXL) : Left Leg branch (DXL IDs 1..5)
 *    - J2 (LEG_R_DXL) : Right Leg branch (DXL IDs 6..10)
 *    - J3 (HEAD_DXL)  : Neck/Head (DXL IDs 11..14) and Beak (DXL ID 15)
 *
 * 3. Onboard + Head Sensor I2C Bus (400 kHz Fast-Mode I2C1)
 *    - PB6 (U4.43): I2C1_SCL (AF4 open-drain, R16 4.7 kOhm pull-up to 3V3)
 *    - PB7 (U4.44): I2C1_SDA (AF4 open-drain, R17 4.7 kOhm pull-up to 3V3)
 *    - 0x68: U5 onboard torso 6-axis IMU Bosch BMI270 (SDO tied to GND)
 *    - 0x40: U3 onboard high-side monitor TI INA226AIDGST across R20
 *            Bourns CSS2H-2512R-L300F (0.30 mOhm / 0.0003 Ohm, 1% shunt on 5V_BUS)
 *    - J5 (HEAD_I2C, BM05B-GHS-TBT: 3V3, GND, I2C_SCL, I2C_SDA, HEAD_INT):
 *            - 0x29: ST VL53L5CX 8x8 multizone ToF ground rangefinder
 *            - 0x69: Head 6-axis IMU Bosch BMI270 (SDO tied to 3V3)
 *
 * 4. Analog Battery Divider Monitor
 *    - PA0 (U4.8): ADC1_IN1 reading VBAT_ADC (R11 100 kOhm CQ03WAF1003T5E /
 *                  R12 33 kOhm CQ03SAF3302T5E + C25 100 nF filter)
 *
 * 5. Addressable RGB Status Pixel + Digital Sense/Interrupt Inputs
 *    - PA1 (U4.9) : TIM2_CH2 (AF1, LED_PWM_3V3) -> U7 (SN74AHCT1G125DBVR) ->
 *                   R19 100R -> D2 (WS2812B-2020 STATUS_RGB) at 800 kHz
 *    - PB0 (U4.16): GPIO input BUCK_PG (from U1.4 LMR33640DDDAR PGOOD, R7 10k pull-up)
 *    - PB1 (U4.17): GPIO input IMU_INT1 (from U5.4 BMI270 INT1)
 *    - PB2 (U4.18): GPIO input INA_ALERT (active-low from U3.3 INA226 ALERT, R15 10k pull-up)
 *    - PA8 (U4.30): GPIO input HEAD_INT (from J5.5 head sensor interrupt)
 *
 * 6. Debug, Reset, and Boot Pins (MachineContactMedium edge pads MC6..MC8)
 *    - PA13 (U4.37): SYS_JTMS-SWDIO (AF0 -> MC6)
 *    - PA14 (U4.38): SYS_JTCK-SWCLK (AF0 -> MC7)
 *    - PG10 (U4.7) : NRST (active-low reset -> MC8, R8 10k pull-up, C24 100nF)
 *    - PB8  (U4.45): BOOT0 (R9 10 kOhm pulldown to GND)
 */

#define MG_PIN_VBAT_DIV_PORT        'A'
#define MG_PIN_VBAT_DIV_NUM         0U   /* PA0 (U4.8): ADC1_IN1 VBAT_ADC */

#define MG_PIN_WS2812_PORT          'A'
#define MG_PIN_WS2812_NUM           1U   /* PA1 (U4.9): TIM2_CH2 AF1 -> U7 -> D2 */
#define MG_PIN_WS2812_AF            1U
#define MG_WS2812_LED_COUNT         1U   /* D2 STATUS_RGB (WS2812B-2020) */

#define MG_PIN_HOST_TX_PORT         'A'
#define MG_PIN_HOST_TX_NUM          2U   /* PA2 (U4.10): USART2_TX AF7 -> J4.4 */
#define MG_PIN_HOST_RX_PORT         'A'
#define MG_PIN_HOST_RX_NUM          3U   /* PA3 (U4.11): USART2_RX AF7 <- J4.5 */
#define MG_PIN_HOST_UART_AF         7U
#define MG_HOST_UART_BAUD_DEFAULT   1000000UL
#define MG_HOST_UART_BAUD_FAST      2000000UL

#define MG_PIN_HEAD_INT_PORT        'A'
#define MG_PIN_HEAD_INT_NUM         8U   /* PA8 (U4.30): GPIO input HEAD_INT <- J5.5 */

#define MG_PIN_BUCK_PG_PORT         'B'
#define MG_PIN_BUCK_PG_NUM          0U   /* PB0 (U4.16): GPIO input BUCK_PG <- U1.4 */
#define MG_PIN_IMU_INT1_PORT        'B'
#define MG_PIN_IMU_INT1_NUM         1U   /* PB1 (U4.17): GPIO input IMU_INT1 <- U5.4 */
#define MG_PIN_INA_ALERT_PORT       'B'
#define MG_PIN_INA_ALERT_NUM        2U   /* PB2 (U4.18): GPIO input INA_ALERT <- U3.3 */

#define MG_PIN_I2C1_SCL_PORT        'B'
#define MG_PIN_I2C1_SCL_NUM         6U   /* PB6 (U4.43): I2C1_SCL AF4 */
#define MG_PIN_I2C1_SDA_PORT        'B'
#define MG_PIN_I2C1_SDA_NUM         7U   /* PB7 (U4.44): I2C1_SDA AF4 */
#define MG_PIN_I2C1_AF              4U
#define MG_I2C1_SPEED_HZ            400000UL

#define MG_PIN_BOOT0_PORT           'B'
#define MG_PIN_BOOT0_NUM            8U   /* PB8 (U4.45): BOOT0 10k pulldown */

#define MG_PIN_DXL_TX_PORT          'B'
#define MG_PIN_DXL_TX_NUM           10U  /* PB10 (U4.22): USART3_TX AF7 */
#define MG_PIN_DXL_RX_PORT          'B'
#define MG_PIN_DXL_RX_NUM           11U  /* PB11 (U4.25): USART3_RX AF7 */
#define MG_PIN_DXL_UART_AF          7U
#define MG_PIN_DXL_DIR_PORT         'B'
#define MG_PIN_DXL_DIR_NUM          12U  /* PB12 (U4.26): GPIO output DXL_DIR -> U6.1 */
#define MG_DXL_UART_BAUD            1000000UL

#define MG_PIN_SWDIO_PORT           'A'
#define MG_PIN_SWDIO_NUM            13U  /* PA13 (U4.37): SWDIO -> MC6 */
#define MG_PIN_SWCLK_PORT           'A'
#define MG_PIN_SWCLK_NUM            14U  /* PA14 (U4.38): SWCLK -> MC7 */
#define MG_PIN_NRST_PORT            'G'
#define MG_PIN_NRST_NUM             10U  /* PG10 (U4.7) : NRST -> MC8 */

/* I2C1 7-bit Slave Addresses */
#define MG_I2C_ADDR_VL53L5CX        0x29U  /* J5 Head 8x8 ToF */
#define MG_I2C_ADDR_INA226          0x40U  /* U3 Onboard INA226AIDGST */
#define MG_I2C_ADDR_BMI270_TORSO    0x68U  /* U5 Onboard torso BMI270 (SDO = GND) */
#define MG_I2C_ADDR_BMI270_HEAD     0x69U  /* J5 Head BMI270 (SDO = 3V3) */

/* VBAT_ADC resistive divider on PA0 (ADC1_IN1): R11 = 100 kOhm, R12 = 33 kOhm */
#define MG_VBAT_DIV_R_TOP_KOHM      100U
#define MG_VBAT_DIV_R_BOT_KOHM      33U
#define MG_ADC_VREF_MV              3300U
#define MG_ADC_MAX_COUNTS           4095U

/* High-side shunt R20: Bourns CSS2H-2512R-L300F (0.30 mOhm = 0.0003 Ohm = 300 uOhm, 1%) */
#define MG_INA226_SHUNT_UOHM        300U
#define MG_INA226_CAL_0P3MOHM_1MA   17067U /* 0x42AB = round(0.00512 / (0.001 * 0.0003)) */

/* Joint topology: 14 locomotion DoF + 1 expressive beak DoF = 15 XL330 servos */
#define MG_LOCO_DOF_COUNT           14U
#define MG_AUX_DOF_COUNT            1U
#define MG_TOTAL_SERVO_COUNT        15U

/*
 * Canonical Dynamixel IDs per connector branch:
 *   - J1 Left Leg  : IDs 1..5   (RL joint indices 0..4)
 *   - J2 Right Leg : IDs 6..10  (RL joint indices 9..13)
 *   - J3 Neck/Head : IDs 11..14 (RL joint indices 5..8) + Beak ID 15 (aux index 14)
 */
typedef enum {
    MG_DXL_ID_LEFT_HIP_YAW      = 1,   /* J1 Left Leg   RL index 0  */
    MG_DXL_ID_LEFT_HIP_ROLL     = 2,   /* J1 Left Leg   RL index 1  */
    MG_DXL_ID_LEFT_HIP_PITCH    = 3,   /* J1 Left Leg   RL index 2  */
    MG_DXL_ID_LEFT_KNEE_PITCH   = 4,   /* J1 Left Leg   RL index 3  */
    MG_DXL_ID_LEFT_ANKLE_PITCH  = 5,   /* J1 Left Leg   RL index 4  */
    MG_DXL_ID_RIGHT_HIP_YAW     = 6,   /* J2 Right Leg  RL index 9  */
    MG_DXL_ID_RIGHT_HIP_ROLL    = 7,   /* J2 Right Leg  RL index 10 */
    MG_DXL_ID_RIGHT_HIP_PITCH   = 8,   /* J2 Right Leg  RL index 11 */
    MG_DXL_ID_RIGHT_KNEE_PITCH  = 9,   /* J2 Right Leg  RL index 12 */
    MG_DXL_ID_RIGHT_ANKLE_PITCH = 10,  /* J2 Right Leg  RL index 13 */
    MG_DXL_ID_NECK_PITCH        = 11,  /* J3 Neck/Head  RL index 5  */
    MG_DXL_ID_HEAD_PITCH        = 12,  /* J3 Neck/Head  RL index 6  */
    MG_DXL_ID_HEAD_YAW          = 13,  /* J3 Neck/Head  RL index 7  */
    MG_DXL_ID_HEAD_ROLL         = 14,  /* J3 Neck/Head  RL index 8  */
    MG_DXL_ID_BEAK_PITCH        = 15   /* J3 Beak aux   RL index 14 */
} mg_dxl_id_t;

#endif /* MICROGOOSE_BOARD_PINS_H */