- vesc_can: poll twai_get_status_info() every 500ms; auto-recover from bus-off (twai_initiate_recovery) and stopped state (twai_start) - vesc_can: expose g_twai_bus_off / g_twai_tx_err_count / g_twai_rx_err_count - main: set flags bit2 when TWAI is bus-off (visible in TELEM_STATUS) - sdkconfig: switch console from UART0 (conflicts with binary protocol at 460800) to USB serial JTAG — eliminates log corruption on Orin Flags byte: bit0=estop, bit1=hb_timeout, bit2=twai_bus_off Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
42 lines
1.6 KiB
C
42 lines
1.6 KiB
C
#pragma once
|
||
/* vesc_can.h — VESC CAN TWAI driver for ESP32-S3 BALANCE (bd-66hx)
|
||
*
|
||
* VESC extended CAN ID: (packet_type << 8) | vesc_node_id
|
||
* Physical layer: TWAI peripheral → SN65HVD230 → 500 kbps shared bus
|
||
*/
|
||
|
||
#include <stdint.h>
|
||
#include <stdbool.h>
|
||
#include "freertos/FreeRTOS.h"
|
||
#include "freertos/queue.h"
|
||
|
||
/* ── VESC packet types ── */
|
||
#define VESC_PKT_SET_RPM 3u
|
||
#define VESC_PKT_STATUS 9u /* int32 erpm, int16 I×10, int16 duty×1000 */
|
||
#define VESC_PKT_STATUS_4 16u /* int16 T_fet×10, T_mot×10, I_in×10 */
|
||
#define VESC_PKT_STATUS_5 27u /* int32 tacho, int16 V_in×10 */
|
||
|
||
/* ── VESC telemetry snapshot ── */
|
||
typedef struct {
|
||
int32_t erpm; /* electrical RPM (STATUS) */
|
||
int16_t current_x10; /* phase current A×10 (STATUS) */
|
||
int16_t voltage_x10; /* bus voltage V×10 (STATUS_5) */
|
||
int16_t temp_mot_x10; /* motor temp °C×10 (STATUS_4) */
|
||
uint32_t last_rx_ms; /* esp_timer ms of last STATUS frame */
|
||
} vesc_state_t;
|
||
|
||
/* ── Globals (two VESC nodes: index 0 = VESC_ID_A=56, 1 = VESC_ID_B=68) ── */
|
||
extern vesc_state_t g_vesc[2];
|
||
|
||
/* TWAI bus health — set by vesc_can_rx_task, read by telem_task for flags bit2 */
|
||
extern volatile bool g_twai_bus_off;
|
||
extern volatile uint32_t g_twai_tx_err_count;
|
||
extern volatile uint32_t g_twai_rx_err_count;
|
||
|
||
/* ── API ── */
|
||
void vesc_can_init(void);
|
||
void vesc_can_send_rpm(uint8_t vesc_id, int32_t erpm);
|
||
|
||
/* RX task — pass tx_queue as arg; forwards STATUS frames to Orin over serial */
|
||
void vesc_can_rx_task(void *arg); /* arg = QueueHandle_t orin_tx_queue */
|