#ifndef CONFIG_H
#define CONFIG_H

// ============================================
// SaltyLab Balance Bot — MAMBA F722S FC
// Pin assignments from Betaflight: DIAT-MAMBAF722_2022B
// ============================================

// --- IMU: MPU6000 (SPI1) ---
// SPI1: PA5=SCK, PA6=MISO, PA7=MOSI
// WHO_AM_I = 0x68
#define MPU_SPI             SPI1
#define MPU_CS_PORT         GPIOA
#define MPU_CS_PIN          GPIO_PIN_4    // GYRO_CS 1
#define MPU_EXTI_PORT       GPIOC
#define MPU_EXTI_PIN        GPIO_PIN_4    // GYRO_EXTI 1 (data ready IRQ)
#define GYRO_ALIGN          CW270         // gyro_1_sensor_align = CW270

// --- Barometer: BMP280 or DPS310 (I2C1) ---
#define BARO_I2C            I2C1
#define BARO_SCL_PORT       GPIOB
#define BARO_SCL_PIN        GPIO_PIN_8    // I2C_SCL 1
#define BARO_SDA_PORT       GPIOB
#define BARO_SDA_PIN        GPIO_PIN_9    // I2C_SDA 1
// Magnetometer also on I2C1 (external, header only)

// --- LEDs ---
#define LED1_PORT           GPIOC
#define LED1_PIN            GPIO_PIN_15   // LED 1 (active low)
#define LED2_PORT           GPIOC
#define LED2_PIN            GPIO_PIN_14   // LED 2 (active low)

// --- Buzzer ---
#define BEEPER_PORT         GPIOB
#define BEEPER_PIN          GPIO_PIN_2    // BEEPER 1
#define BEEPER_INVERTED     1             // beeper_inversion = ON
// beeper_od = OFF (push-pull)

// --- Battery Monitoring (ADC3) ---
#define ADC_VBAT_PORT       GPIOC
#define ADC_VBAT_PIN        GPIO_PIN_1    // ADC_BATT 1
#define ADC_CURR_PORT       GPIOC
#define ADC_CURR_PIN        GPIO_PIN_3    // ADC_CURR 1
#define ADC_IBAT_SCALE      115           // ibata_scale

// --- LED Strip (WS2812 NeoPixel, Issue #193) ---
// TIM3_CH1 PWM on PB4 for 8-LED ring status indicator
#define LED_STRIP_TIM       TIM3
#define LED_STRIP_CHANNEL   TIM_CHANNEL_1
#define LED_STRIP_PORT      GPIOB
#define LED_STRIP_PIN       GPIO_PIN_4    // LED_STRIP 1 (TIM3_CH1)
#define LED_STRIP_AF        GPIO_AF2_TIM3  // Alternate function
#define LED_STRIP_NUM_LEDS  8u            // 8-LED ring
#define LED_STRIP_FREQ_HZ   800000u       // 800 kHz PWM for NeoPixel (1.25 µs per bit)

// --- Servo Pan-Tilt (Issue #206) ---
// TIM4_CH1 (PB6) for pan servo, TIM4_CH2 (PB7) for tilt servo
#define SERVO_TIM           TIM4
#define SERVO_PAN_PORT      GPIOB
#define SERVO_PAN_PIN       GPIO_PIN_6    // TIM4_CH1
#define SERVO_PAN_CHANNEL   TIM_CHANNEL_1
#define SERVO_TILT_PORT     GPIOB
#define SERVO_TILT_PIN      GPIO_PIN_7    // TIM4_CH2
#define SERVO_TILT_CHANNEL  TIM_CHANNEL_2
#define SERVO_AF            GPIO_AF2_TIM4  // Alternate function
#define SERVO_FREQ_HZ       50u            // 50 Hz (20ms period, standard servo)
#define SERVO_MIN_US        500u           // 500µs = 0°
#define SERVO_MAX_US        2500u          // 2500µs = 180°
#define SERVO_CENTER_US     1500u          // 1500µs = 90°

// --- OSD: MAX7456 (SPI2) ---
#define OSD_SPI             SPI2
#define OSD_CS_PORT         GPIOB
#define OSD_CS_PIN          GPIO_PIN_12   // OSD_CS 1
// SPI2: PB13=SCK, PB14=MISO, PB15=MOSI

// --- Blackbox Flash: M25P16 (SPI3) ---
#define FLASH_SPI           SPI3
#define FLASH_CS_PORT       GPIOA
#define FLASH_CS_PIN        GPIO_PIN_15   // FLASH_CS 1
// SPI3: PC10=SCK, PC11=MISO, PB5=MOSI

// --- Motor Outputs (PWM/DShot) ---
#define MOTOR1_PORT         GPIOC
#define MOTOR1_PIN          GPIO_PIN_8    // TIM8_CH3
#define MOTOR2_PORT         GPIOC
#define MOTOR2_PIN          GPIO_PIN_9    // TIM8_CH4
#define MOTOR3_PORT         GPIOA
#define MOTOR3_PIN          GPIO_PIN_8    // TIM1_CH1
#define MOTOR4_PORT         GPIOA
#define MOTOR4_PIN          GPIO_PIN_9    // TIM1_CH2
#define MOTOR5_PORT         GPIOB
#define MOTOR5_PIN          GPIO_PIN_0    // TIM3_CH3
#define MOTOR6_PORT         GPIOB
#define MOTOR6_PIN          GPIO_PIN_1    // TIM3_CH4
#define MOTOR7_PORT         GPIOA
#define MOTOR7_PIN          GPIO_PIN_10   // TIM1_CH3
#define MOTOR8_PORT         GPIOB
#define MOTOR8_PIN          GPIO_PIN_4    // TIM3_CH1

// --- UARTs ---
// USART1: PB6=TX, PB7=RX   (serial 0, SmartAudio/VTX)
#define UART1_TX_PORT       GPIOB
#define UART1_TX_PIN        GPIO_PIN_6
#define UART1_RX_PORT       GPIOB
#define UART1_RX_PIN        GPIO_PIN_7

// USART2: PA2=TX, PA3=RX   (serial 1)
#define UART2_TX_PORT       GPIOA
#define UART2_TX_PIN        GPIO_PIN_2
#define UART2_RX_PORT       GPIOA
#define UART2_RX_PIN        GPIO_PIN_3

// USART3: PB10=TX, PB11=RX (serial 2, SBUS RX default)
#define UART3_TX_PORT       GPIOB
#define UART3_TX_PIN        GPIO_PIN_10
#define UART3_RX_PORT       GPIOB
#define UART3_RX_PIN        GPIO_PIN_11

// UART4: PA0=TX, PA1=RX    (serial 3)
#define UART4_TX_PORT       GPIOA
#define UART4_TX_PIN        GPIO_PIN_0
#define UART4_RX_PORT       GPIOA
#define UART4_RX_PIN        GPIO_PIN_1

// UART5: PC12=TX, PD2=RX   (serial 4)
#define UART5_TX_PORT       GPIOC
#define UART5_TX_PIN        GPIO_PIN_12
#define UART5_RX_PORT       GPIOD
#define UART5_RX_PIN        GPIO_PIN_2

// USART6: PC6=TX, PC7=RX   (serial 5)
#define UART6_TX_PORT       GPIOC
#define UART6_TX_PIN        GPIO_PIN_6
#define UART6_RX_PORT       GPIOC
#define UART6_RX_PIN        GPIO_PIN_7

// --- PINIO (switchable outputs, e.g. VTX power) ---
#define PINIO1_PORT         GPIOC
#define PINIO1_PIN          GPIO_PIN_2    // pinio_config = 129 (USER1)
#define PINIO2_PORT         GPIOC
#define PINIO2_PIN          GPIO_PIN_0    // pinio_config = 129 (USER2)

// --- JLink: Jetson Serial Binary Protocol (USART1, Issue #120) ---
#define JLINK_BAUD              921600  /* USART1 baud rate */
#define JLINK_HB_TIMEOUT_MS     1000    /* Jetson heartbeat timeout (ms) */
#define JLINK_TLM_HZ            50      /* STATUS telemetry TX rate (Hz) */

// --- Firmware Version ---
#define FW_MAJOR                1
#define FW_MINOR                0
#define FW_PATCH                0

// --- SaltyLab Assignments ---
// Hoverboard ESC: USART2 (PA2=TX, PA3=RX) or USART3
// ELRS Receiver: UART4 (PA0=TX, PA1=RX) — CRSF 420000 baud
// Jetson (JLink binary protocol, Issue #120): USART1 (PB6=TX, PB7=RX) @ 921600
// USART6 (PC6=TX, PC7=RX): legacy Jetson CDC path — reserved for VESC (Issue #383)
// Debug: UART5 (PC12=TX, PD2=RX)

// --- ESC Backend Selection (Issue #388) ---
// Pluggable ESC abstraction layer — supports multiple backends:
//   HOVERBOARD: EFeru FOC (USART2 @ 115200) — current default
//   VESC:       FSESC 4.20 Plus (USART6 @ 921600, balance mode) — future
#define ESC_BACKEND             HOVERBOARD  /* HOVERBOARD or VESC */

// --- CRSF / ExpressLRS ---
// CH1[0]=steer  CH2[1]=throttle  CH5[4]=arm  CH6[5]=mode
#define CRSF_ARM_THRESHOLD      1750    /* CH5 raw value; > threshold = armed */
#define CRSF_STEER_MAX          400     /* CH1 range: -400..+400 motor counts */
#define CRSF_FAILSAFE_MS        500     /* Disarm after this ms without a frame (Issue #103) */

// --- Battery ADC (ADC3, PC1 = ADC123_IN11) ---
/* Mamba F722: 10kΩ + 1kΩ voltage divider → 11:1 ratio */
#define VBAT_SCALE_NUM          11      /* Numerator of divider ratio */
#define VBAT_AREF_MV            3300    /* ADC reference in mV */
#define VBAT_ADC_BITS           12      /* 12-bit ADC → 4096 counts */
/* Filtered Vbat in mV: (raw * 3300 * 11) / 4096, updated at 10Hz */

// --- CRSF Telemetry TX (uplink: FC → ELRS module → pilot handset) ---
#define CRSF_TELEMETRY_HZ       1       /* Telemetry TX rate (Hz) */

// --- PID Tuning ---
#define PID_KP              35.0f
#define PID_KI              1.0f
#define PID_KD              1.0f
#define PID_INTEGRAL_MAX    500.0f
#define PID_LOOP_HZ         1000

// --- Safety ---
#define MAX_TILT_DEG        25.0f
#define RC_TIMEOUT_MS       500
#define ARMING_HOLD_MS      3000
#define MAX_SPEED_LIMIT     100
#define WATCHDOG_TIMEOUT_MS 50

// --- Motor Driver ---
#define MOTOR_CMD_MAX           1000    /* ESC range: -1000..+1000 */
#define MOTOR_STEER_RAMP_RATE   20      /* counts/ms — steer ramp only */

// --- IMU Calibration ---
#define GYRO_CAL_SAMPLES        1000    /* gyro bias samples (~1s at 1ms/sample) */

// --- RC / Mode Manager ---
/* CRSF channel indices (0-based; CRSF range 172-1811, center 992) */
#define CRSF_CH_STEER           0       /* CH1 — right stick horizontal (steer) */
#define CRSF_CH_SPEED           1       /* CH2 — right stick vertical (throttle) */
#define CRSF_CH_ARM             4       /* CH5 — arm switch (2-pos) */
#define CRSF_CH_MODE            5       /* CH6 — mode switch (3-pos) */
/* Deadband around CRSF center (992) in raw counts (~2% of range) */
#define CRSF_DEADBAND           30
/* CH6 mode thresholds (raw CRSF counts) */
#define CRSF_MODE_LOW_THRESH    600     /* <= → RC_MANUAL */
#define CRSF_MODE_HIGH_THRESH   1200    /* >= → AUTONOMOUS */
/* Max speed bias RC can add to balance PID output (counts, same scale as ESC) */
#define MOTOR_RC_SPEED_MAX      300
/* Full blend transition time: MANUAL→AUTO takes this many ms */
#define MODE_BLEND_MS           500

// --- Power Management (STOP mode, Issue #178) ---
#define PM_IDLE_TIMEOUT_MS      30000u  // 30s no activity → PM_SLEEP_PENDING
#define PM_FADE_MS               3000u  // LED fade-out duration before STOP entry
#define PM_LED_PERIOD_MS         2000u  // sleep-pending triangle-wave period (ms)
// Estimated per-subsystem currents (mA) — used for JLINK_TLM_POWER telemetry
#define PM_CURRENT_BASE_MA         30   // SPI1(IMU)+UART4(CRSF)+USART1(JLink)+core
#define PM_CURRENT_AUDIO_MA         8   // I2S3 + amplifier quiescent
#define PM_CURRENT_OSD_MA           5   // SPI2 OSD (MAX7456)
#define PM_CURRENT_DEBUG_MA         1   // UART5 + USART6
#define PM_CURRENT_STOP_MA          1   // MCU in STOP mode (< 1 mA)
#define PM_TLM_HZ                   1   // JLINK_TLM_POWER transmit rate (Hz)

// --- Audio Amplifier (I2S3, Issue #143) ---
// SPI3 repurposed as I2S3; blackbox flash unused on balance bot
#define AUDIO_BCLK_PORT         GPIOC
#define AUDIO_BCLK_PIN          GPIO_PIN_10   // I2S3_CK (PC10, AF6)
#define AUDIO_LRCK_PORT         GPIOA
#define AUDIO_LRCK_PIN          GPIO_PIN_15   // I2S3_WS (PA15, AF6)
#define AUDIO_DOUT_PORT         GPIOB
#define AUDIO_DOUT_PIN          GPIO_PIN_5    // I2S3_SD (PB5, AF6)
#define AUDIO_MUTE_PORT         GPIOC
#define AUDIO_MUTE_PIN          GPIO_PIN_5    // active-high = amp enabled
// PLLI2S: N=192, R=2 → I2S clock=96 MHz → FS≈22058 Hz (< 0.04% error)
#define AUDIO_SAMPLE_RATE       22050u        // nominal sample rate (Hz)
#define AUDIO_BUF_HALF          441u          // DMA half-buffer: 20ms at 22050 Hz
#define AUDIO_VOLUME_DEFAULT    80u           // default volume 0-100

// --- Gimbal Servo Bus (ST3215, USART3 half-duplex, Issue #547) ---
// Half-duplex single-wire on PB10 (USART3_TX, AF7) at 1 Mbps.
// USART3 is available: not assigned to any active subsystem.
#define SERVO_BUS_UART          USART3
#define SERVO_BUS_PORT          GPIOB
#define SERVO_BUS_PIN           GPIO_PIN_10   // USART3_TX, AF7
#define SERVO_BUS_BAUD          1000000u      // 1 Mbps (ST3215 default)
#define GIMBAL_PAN_ID           1u            // ST3215 servo ID for pan
#define GIMBAL_TILT_ID          2u            // ST3215 servo ID for tilt
#define GIMBAL_TLM_HZ           50u           // position feedback rate (Hz)
#define GIMBAL_PAN_LIMIT_DEG    180.0f        // pan  soft limit (deg each side)
#define GIMBAL_TILT_LIMIT_DEG   90.0f         // tilt soft limit (deg each side)

// --- CAN Bus Driver (Issue #597, remapped Issue #676) ---
// CAN1 on PB8 (RX, AF9) / PB9 (TX, AF9) — SCL/SDA pads on Mamba F722S MK2
// I2C1 freed: BME280 moved to I2C2 (PB10/PB11); PB8/PB9 repurposed for CAN1
#define CAN_RPM_SCALE           10             // motor_cmd to RPM: 1 cmd count = 10 RPM
#define CAN_TLM_HZ              1u             // JLINK_TLM_CAN_STATS transmit rate (Hz)


// --- LVC: Low Voltage Cutoff (Issue #613) ---
// 3-stage undervoltage protection; voltages in mV
#define LVC_WARNING_MV          21000u  // 21.0 V -- buzzer alert, full power
#define LVC_CRITICAL_MV         19800u  // 19.8 V -- 50% motor power reduction
#define LVC_CUTOFF_MV           18600u  // 18.6 V -- motors disabled, latch until reboot
#define LVC_HYSTERESIS_MV         200u  // recovery hysteresis to prevent threshold chatter
#define LVC_TLM_HZ                 1u   // JLINK_TLM_LVC transmit rate (Hz)


// --- UART Command Protocol (Issue #629) ---
// Jetson-STM32 binary command protocol on UART5 (PC12/PD2)
// NOTE: Spec requested USART1 @ 115200; USART1 is occupied by JLink @ 921600.
#define UART_PROT_BAUD          115200u  // baud rate for UART5 Jetson protocol
#define UART_PROT_HB_TIMEOUT_MS   500u  // heartbeat timeout: Jetson considered lost after 500 ms

// --- Encoder Odometry (Issue #632) ---
// Left  encoder: TIM2 (32-bit), CH1=PA15 (AF1), CH2=PB3 (AF1)
// Right encoder: TIM3 (16-bit), CH1=PC6  (AF2), CH2=PC7 (AF2)
// Encoder mode 3: count on both A and B edges (x4 resolution)
#define ENC_LEFT_TIM            TIM2
#define ENC_LEFT_CH1_PORT       GPIOA
#define ENC_LEFT_CH1_PIN        GPIO_PIN_15   // TIM2_CH1, AF1
#define ENC_LEFT_CH2_PORT       GPIOB
#define ENC_LEFT_CH2_PIN        GPIO_PIN_3    // TIM2_CH2, AF1
#define ENC_LEFT_AF             GPIO_AF1_TIM2
#define ENC_RIGHT_TIM           TIM3
#define ENC_RIGHT_CH1_PORT      GPIOC
#define ENC_RIGHT_CH1_PIN       GPIO_PIN_6    // TIM3_CH1, AF2
#define ENC_RIGHT_CH2_PORT      GPIOC
#define ENC_RIGHT_CH2_PIN       GPIO_PIN_7    // TIM3_CH2, AF2
#define ENC_RIGHT_AF            GPIO_AF2_TIM3

// --- Hardware Button (Issue #682) ---
// Active-low push button on PC2 (internal pull-up)
#define BTN_PORT            GPIOC
#define BTN_PIN             GPIO_PIN_2
#define BTN_DEBOUNCE_MS     20u     // ms debounce window
#define BTN_LONG_MIN_MS     1500u   // ms threshold: LONG press
#define BTN_COMMIT_MS       500u    // ms quiet after lone SHORT -> PARK event
#define BTN_SEQ_TIMEOUT_MS  3000u   // ms: sequence window; expired buffer abandoned

#endif // CONFIG_H
