101 lines
2.5 KiB
C++
101 lines
2.5 KiB
C++
/**
|
|
* can_driver.h
|
|
*
|
|
* CAN Bus driver for ESP32-S3-Touch-LCD-5B using TWAI (Two-Wire Automotive Interface)
|
|
* Reads BMW CAN messages at 500 Kbps in LISTEN_ONLY mode
|
|
*
|
|
* Hardware: GPIO 15 (TX), GPIO 16 (RX)
|
|
* Mode: LISTEN_ONLY (safe, won't transmit on vehicle bus)
|
|
* Baud: 500 Kbps
|
|
*/
|
|
|
|
#ifndef _CAN_DRIVER_H
|
|
#define _CAN_DRIVER_H
|
|
|
|
#include <Arduino.h>
|
|
#include <lvgl.h>
|
|
#include "driver/twai.h"
|
|
#include "protocol/automotive_data.h"
|
|
|
|
// ============================================================================
|
|
// HARDWARE CONFIGURATION
|
|
// ============================================================================
|
|
|
|
#define CAN_TX_PIN 15
|
|
#define CAN_RX_PIN 16
|
|
#define CAN_BAUD_RATE TWAI_TIMING_CONFIG_500KBITS()
|
|
|
|
// ============================================================================
|
|
// TASK CONFIGURATION
|
|
// ============================================================================
|
|
|
|
#define CAN_TASK_STACK_SIZE 4096
|
|
#define CAN_TASK_PRIORITY 1
|
|
#define CAN_TASK_CORE 0 // Run on Core 0 with UART task
|
|
|
|
// ============================================================================
|
|
// CAN EVENT TYPES
|
|
// ============================================================================
|
|
|
|
typedef enum {
|
|
CAN_EVENT_START,
|
|
CAN_EVENT_STOP
|
|
} can_event_type_t;
|
|
|
|
typedef struct {
|
|
can_event_type_t type;
|
|
} can_queue_event_t;
|
|
|
|
// ============================================================================
|
|
// CAN STATISTICS
|
|
// ============================================================================
|
|
|
|
typedef struct {
|
|
uint32_t messages_received;
|
|
uint32_t messages_parsed;
|
|
uint32_t parse_errors;
|
|
uint32_t last_receive_time;
|
|
} can_stats_t;
|
|
|
|
// ============================================================================
|
|
// PUBLIC API
|
|
// ============================================================================
|
|
|
|
namespace CANDriver {
|
|
/**
|
|
* Initialize CAN driver and create FreeRTOS task
|
|
* @param auto_start If true, CAN bus starts listening immediately
|
|
*/
|
|
void init(bool auto_start = false);
|
|
|
|
/**
|
|
* Start CAN bus reception
|
|
*/
|
|
void start();
|
|
|
|
/**
|
|
* Stop CAN bus reception
|
|
*/
|
|
void stop();
|
|
|
|
/**
|
|
* Get current CAN statistics
|
|
* @return Statistics structure
|
|
*/
|
|
can_stats_t getStats();
|
|
|
|
/**
|
|
* Check if CAN is currently active
|
|
* @return true if CAN is receiving messages
|
|
*/
|
|
bool isActive();
|
|
|
|
/**
|
|
* UI event handler for CAN toggle switch
|
|
* @param e LVGL event
|
|
*/
|
|
void onCANToggle(lv_event_t *e);
|
|
}
|
|
|
|
#endif // _CAN_DRIVER_H
|