Implements TIM3_CH1 PWM driver for 8-LED NeoPixel ring with: - 6 state-based animations: boot (blue chase), armed (solid green), error (red blink), low battery (yellow pulse), charging (green breathe), e_stop (red strobe) - Non-blocking via 1 ms tick callback - GRB byte order encoding (WS2812B standard) - PWM duty values for "0" (~40%) and "1" (~56%) bit encoding - 10 unit tests covering state transitions, animations, color encoding Driver integrated into main.c initialization and main loop tick. Includes buzzer driver (Issue #189) integration. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
226 lines
9.2 KiB
C
226 lines
9.2 KiB
C
#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)
|
|
|
|
// --- 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: USART6 (PC6=TX, PC7=RX)
|
|
// Debug: UART5 (PC12=TX, PD2=RX)
|
|
|
|
// --- 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
|
|
|
|
#endif // CONFIG_H
|