Archive STM32 firmware to legacy/stm32/: - src/, include/, lib/USB_CDC/, platformio.ini, test stubs, flash_firmware.py - test/test_battery_adc.c, test_hw_button.c, test_pid_schedule.c, test_vesc_can.c, test_can_watchdog.c - USB_CDC_BUG.md Rename: stm32_protocol → esp32_protocol, mamba_protocol → balance_protocol, stm32_cmd_node → esp32_cmd_node, stm32_cmd_params → esp32_cmd_params, stm32_cmd.launch.py → esp32_cmd.launch.py, test_stm32_protocol → test_esp32_protocol, test_stm32_cmd_node → test_esp32_cmd_node Content cleanup across all files: - Mamba F722S → ESP32-S3 BALANCE - BlackPill → ESP32-S3 IO - STM32F722/F7xx → ESP32-S3 - stm32Mode/Version/Port → esp32Mode/Version/Port - STM32 State/Mode labels → ESP32 State/Mode - Jetson Nano → Jetson Orin Nano Super - /dev/stm32 → /dev/esp32 - stm32_bridge → esp32_bridge - STM32 HAL → ESP-IDF docs/SALTYLAB.md: - Update "Drone FC Details" to describe ESP32-S3 BALANCE board (Waveshare ESP32-S3 Touch LCD 1.28) - Replace verbose "Self-Balancing Control" STM32 section with brief note pointing to SAUL-TEE-SYSTEM-REFERENCE.md TEAM.md: Update Embedded Firmware Engineer role to ESP32-S3 / ESP-IDF No new functionality — cleanup only. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
184 lines
5.5 KiB
C
184 lines
5.5 KiB
C
#include "esc_backend.h"
|
|
#include "config.h"
|
|
#include "stm32f7xx_hal.h"
|
|
#ifdef DEBUG_MOTOR_TEST
|
|
#include <stdio.h>
|
|
#endif
|
|
|
|
/*
|
|
* Hoverboard ESC Backend Implementation
|
|
*
|
|
* Adapts the Hoverboard EFeru FOC protocol to the ESC backend vtable.
|
|
* UART2: PA2=TX, PA3=RX @ 115200 baud
|
|
*
|
|
* Packet: [0xABCD] [steer:i16] [speed:i16] [checksum:u16]
|
|
* Checksum = start ^ steer ^ speed
|
|
* Speed range: -1000 to +1000
|
|
* Must send at >=50Hz or ESC times out.
|
|
*/
|
|
|
|
#define HOVERBOARD_START_FRAME 0xABCD
|
|
#define HOVERBOARD_BAUD 115200
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
uint16_t start;
|
|
int16_t steer;
|
|
int16_t speed;
|
|
uint16_t checksum;
|
|
} hoverboard_cmd_t;
|
|
|
|
#ifdef DEBUG_MOTOR_TEST
|
|
UART_HandleTypeDef huart2; /* non-static: exposed for jetson_uart.c R command */
|
|
#else
|
|
static UART_HandleTypeDef huart2;
|
|
#endif
|
|
|
|
/* Backend vtable instance */
|
|
static const esc_backend_t hoverboard_backend;
|
|
|
|
/*
|
|
* Initialize UART2 for hoverboard communication.
|
|
* Called once at startup via backend registration.
|
|
*/
|
|
static void hoverboard_backend_init(void) {
|
|
/* Enable clocks */
|
|
__HAL_RCC_UART5_CLK_ENABLE();
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
|
|
/* PA2=TX, PA3=RX, AF7 for USART2 */
|
|
GPIO_InitTypeDef gpio = {0};
|
|
// UART5: PC12=TX, PD2=RX
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
gpio.Pin = GPIO_PIN_12;
|
|
gpio.Mode = GPIO_MODE_AF_PP;
|
|
gpio.Pull = GPIO_PULLUP;
|
|
gpio.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
gpio.Alternate = GPIO_AF8_UART5;
|
|
HAL_GPIO_Init(GPIOC, &gpio);
|
|
|
|
// RX: PD2
|
|
gpio.Pin = GPIO_PIN_2;
|
|
gpio.Alternate = GPIO_AF8_UART5;
|
|
HAL_GPIO_Init(GPIOD, &gpio);
|
|
|
|
/* USART2 config */
|
|
huart2.Instance = UART5;
|
|
huart2.Init.BaudRate = HOVERBOARD_BAUD;
|
|
huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart2.Init.StopBits = UART_STOPBITS_1;
|
|
huart2.Init.Parity = UART_PARITY_NONE;
|
|
huart2.Init.Mode = UART_MODE_TX_RX;
|
|
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
HAL_UART_Init(&huart2);
|
|
|
|
#ifdef DEBUG_MOTOR_TEST
|
|
/* Diagnostic: report UART5 register state on USART6 after init */
|
|
{
|
|
extern void jetson_uart_send(const uint8_t *data, uint16_t len);
|
|
char diag[128];
|
|
uint32_t brr = UART5->BRR;
|
|
uint32_t cr1 = UART5->CR1;
|
|
uint32_t isr = UART5->ISR;
|
|
uint32_t apb1 = HAL_RCC_GetPCLK1Freq();
|
|
/* Also read GPIOC MODER to verify PC12 is in AF mode (bits 25:24 = 10) */
|
|
uint32_t moder = GPIOC->MODER;
|
|
uint32_t pc12_mode = (moder >> 24) & 0x3; /* 0=input 1=output 2=AF 3=analog */
|
|
uint32_t afr = GPIOC->AFR[1]; /* AFR high for pins 8-15 */
|
|
uint32_t pc12_af = (afr >> 16) & 0xF; /* AF for pin 12 */
|
|
int n = snprintf(diag, sizeof(diag),
|
|
"UART5: BRR=%lu CR1=0x%lX ISR=0x%lX APB1=%luHz PC12mode=%lu AF=%lu\n",
|
|
(unsigned long)brr, (unsigned long)cr1, (unsigned long)isr,
|
|
(unsigned long)apb1, (unsigned long)pc12_mode, (unsigned long)pc12_af);
|
|
/* Delay to let USART6 finish boot banner first */
|
|
HAL_Delay(100);
|
|
jetson_uart_send((uint8_t*)diag, n);
|
|
}
|
|
#endif /* DEBUG_MOTOR_TEST */
|
|
}
|
|
|
|
/*
|
|
* Send motor command via hoverboard protocol.
|
|
* Called at ~50Hz from motor_driver_update().
|
|
*/
|
|
#ifdef DEBUG_MOTOR_TEST
|
|
static volatile uint32_t hover_tx_count = 0;
|
|
#endif
|
|
|
|
static void hoverboard_backend_send(int16_t speed, int16_t steer) {
|
|
hoverboard_cmd_t cmd;
|
|
cmd.start = HOVERBOARD_START_FRAME;
|
|
cmd.steer = steer;
|
|
cmd.speed = speed;
|
|
cmd.checksum = cmd.start ^ cmd.steer ^ cmd.speed;
|
|
|
|
#ifdef DEBUG_MOTOR_TEST
|
|
HAL_StatusTypeDef rc = HAL_UART_Transmit(&huart2, (uint8_t *)&cmd, sizeof(cmd), 5);
|
|
hover_tx_count++;
|
|
|
|
/* Debug: every 50th send, report status on USART6 */
|
|
if (hover_tx_count % 50 == 1) {
|
|
extern void jetson_uart_send(const uint8_t *data, uint16_t len);
|
|
char dbg[64];
|
|
int n = snprintf(dbg, sizeof(dbg), "ESC tx=%lu rc=%d spd=%d str=%d\n",
|
|
(unsigned long)hover_tx_count, (int)rc, speed, steer);
|
|
jetson_uart_send((uint8_t*)dbg, n);
|
|
}
|
|
#else
|
|
HAL_UART_Transmit(&huart2, (uint8_t *)&cmd, sizeof(cmd), 5);
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
* Emergency stop: send zero and disable motors.
|
|
* Hoverboard will disable outputs on repeated zero packets.
|
|
*/
|
|
static void hoverboard_backend_estop(void) {
|
|
hoverboard_backend_send(0, 0);
|
|
}
|
|
|
|
/*
|
|
* Resume after estop (optional, hoverboard auto-resumes on non-zero command).
|
|
*/
|
|
static void hoverboard_backend_resume(void) {
|
|
/* No action needed — next non-zero send will resume */
|
|
}
|
|
|
|
/*
|
|
* Query telemetry from hoverboard.
|
|
* Hoverboard protocol is command-only (no RX in this implementation).
|
|
* Return zero telemetry stub; future: add RX feedback.
|
|
*/
|
|
static void hoverboard_backend_get_telemetry(esc_telemetry_t *out) {
|
|
if (out) {
|
|
out->speed = 0;
|
|
out->steer = 0;
|
|
out->voltage_mv = 0;
|
|
out->current_ma = 0;
|
|
out->temperature_c = 0;
|
|
out->fault = 0;
|
|
}
|
|
}
|
|
|
|
/* Hoverboard backend vtable */
|
|
static const esc_backend_t hoverboard_backend = {
|
|
.init = hoverboard_backend_init,
|
|
.send = hoverboard_backend_send,
|
|
.estop = hoverboard_backend_estop,
|
|
.resume = hoverboard_backend_resume,
|
|
.get_telemetry = hoverboard_backend_get_telemetry,
|
|
};
|
|
|
|
/*
|
|
* Public functions for backward compatibility.
|
|
* These remain for existing code that calls hoverboard_init/hoverboard_send directly.
|
|
*/
|
|
|
|
void hoverboard_init(void) {
|
|
esc_backend_register(&hoverboard_backend);
|
|
}
|
|
|
|
void hoverboard_send(int16_t speed, int16_t steer) {
|
|
esc_send(speed, steer);
|
|
}
|