ca13734bf0
Add kernel (Cortex-M0/M3/M4, Tricore, S32K, RISC-V ports), drivers, middleware (CAN stack, diagnostics, safety), applications, board support, build/test tooling, and documentation.
121 lines
3.3 KiB
C
121 lines
3.3 KiB
C
/**
|
|
* @file can_driver.h
|
|
* @brief CAN (Controller Area Network) driver interface
|
|
* @note Supports classical CAN and CAN FD
|
|
*/
|
|
|
|
#ifndef CAN_DRIVER_H
|
|
#define CAN_DRIVER_H
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include "kernel.h"
|
|
|
|
/* CAN Configuration Constants */
|
|
#define CAN_MAX_MESSAGE_LENGTH 8 /* Classical CAN */
|
|
#define CAN_FD_MAX_MESSAGE_LENGTH 64 /* CAN FD */
|
|
#define CAN_MAX_FILTERS 32
|
|
#define CAN_MAX_TX_MAILBOXES 3
|
|
#define CAN_MAX_RX_FIFO_DEPTH 16
|
|
|
|
/* CAN Baudrates */
|
|
typedef enum {
|
|
CAN_BAUD_125K = 125000,
|
|
CAN_BAUD_250K = 250000,
|
|
CAN_BAUD_500K = 500000,
|
|
CAN_BAUD_1M = 1000000,
|
|
CAN_FD_BAUD_2M = 2000000,
|
|
CAN_FD_BAUD_5M = 5000000
|
|
} CanBaudrate_t;
|
|
|
|
/* CAN Message Types */
|
|
typedef enum {
|
|
CAN_FRAME_CLASSIC = 0,
|
|
CAN_FRAME_FD = 1,
|
|
CAN_FRAME_FD_BRS = 2 /* FD with Bit Rate Switch */
|
|
} CanFrameType_t;
|
|
|
|
/* CAN Frame Format */
|
|
typedef enum {
|
|
CAN_FORMAT_STANDARD = 0, /* 11-bit identifier */
|
|
CAN_FORMAT_EXTENDED = 1 /* 29-bit identifier */
|
|
} CanFrameFormat_t;
|
|
|
|
/* CAN Message ID */
|
|
typedef struct {
|
|
uint32_t id : 29;
|
|
CanFrameFormat_t format : 1;
|
|
bool is_remote : 1;
|
|
bool is_extended : 1;
|
|
} CanMessageId_t;
|
|
|
|
/* CAN Message Structure */
|
|
typedef struct {
|
|
CanMessageId_t id;
|
|
CanFrameType_t type;
|
|
uint8_t data[CAN_FD_MAX_MESSAGE_LENGTH];
|
|
uint8_t length;
|
|
uint32_t timestamp;
|
|
bool is_rx;
|
|
} CanMessage_t;
|
|
|
|
/* CAN Filter Configuration */
|
|
typedef struct {
|
|
uint32_t filter_id;
|
|
uint32_t filter_mask;
|
|
CanFrameFormat_t format;
|
|
bool enable;
|
|
} CanFilterConfig_t;
|
|
|
|
/* CAN Statistics */
|
|
typedef struct {
|
|
uint32_t tx_messages;
|
|
uint32_t rx_messages;
|
|
uint32_t tx_errors;
|
|
uint32_t rx_errors;
|
|
uint32_t bus_off_count;
|
|
uint32_t error_warning_count;
|
|
uint32_t error_passive_count;
|
|
uint32_t tx_overflow;
|
|
uint32_t rx_overflow;
|
|
} CanStatistics_t;
|
|
|
|
/* CAN Callbacks */
|
|
typedef void (*CanRxCallback_t)(const CanMessage_t* message);
|
|
typedef void (*CanTxCallback_t)(uint32_t mailbox, bool success);
|
|
typedef void (*CanErrorCallback_t)(uint32_t error_code);
|
|
|
|
/* CAN Configuration Structure */
|
|
typedef struct {
|
|
CanBaudrate_t nominal_baudrate;
|
|
CanBaudrate_t data_baudrate; /* For CAN FD */
|
|
CanFrameType_t frame_type;
|
|
bool enable_fd;
|
|
bool enable_automatic_retransmission;
|
|
CanFilterConfig_t filters[CAN_MAX_FILTERS];
|
|
uint8_t filter_count;
|
|
CanRxCallback_t rx_callback;
|
|
CanTxCallback_t tx_callback;
|
|
CanErrorCallback_t error_callback;
|
|
} CanConfig_t;
|
|
|
|
/* CAN Driver Interface */
|
|
KernelStatus_t can_init(CanConfig_t* config);
|
|
KernelStatus_t can_deinit(void);
|
|
KernelStatus_t can_send_message(const CanMessage_t* message, uint32_t timeout_ms);
|
|
KernelStatus_t can_receive_message(CanMessage_t* message, uint32_t timeout_ms);
|
|
KernelStatus_t can_configure_filter(const CanFilterConfig_t* filter);
|
|
KernelStatus_t can_set_baudrate(CanBaudrate_t baudrate);
|
|
KernelStatus_t can_get_statistics(CanStatistics_t* stats);
|
|
KernelStatus_t can_clear_statistics(void);
|
|
bool can_is_bus_off(void);
|
|
KernelStatus_t can_recover_bus_off(void);
|
|
void can_process_interrupt(void);
|
|
|
|
/* CAN FD Specific Functions */
|
|
KernelStatus_t can_fd_set_data_baudrate(CanBaudrate_t baudrate);
|
|
KernelStatus_t can_fd_set_transmit_delay(uint16_t delay_us);
|
|
bool can_fd_is_enabled(void);
|
|
|
|
#endif /* CAN_DRIVER_H */
|