|
HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
|
ESP32 CAN (TWAI) implementation using ESP-IDF v5.5+ node-based API. More...
#include <EspCan.h>
Public Member Functions | |
| EspCan (const hf_esp_can_config_t &config) noexcept | |
| Constructor with TWAI node configuration. | |
| ~EspCan () noexcept override | |
| Destructor - ensures proper cleanup and resource deallocation. | |
| hf_can_err_t | Initialize () noexcept override |
| Initialize the TWAI node and allocate resources. | |
| hf_can_err_t | Deinitialize () noexcept override |
| Deinitialize the TWAI node and free resources. | |
| hf_can_err_t | SendMessage (const hf_can_message_t &message, uint32_t timeout_ms=1000) noexcept override |
| Send a CAN message. | |
| hf_can_err_t | ReceiveMessage (hf_can_message_t &message, uint32_t timeout_ms=0) noexcept override |
| Receive a CAN message (legacy polling interface). | |
| hf_can_err_t | SetReceiveCallback (hf_can_receive_callback_t callback) noexcept override |
| Set callback for received messages (legacy support removed - use RegisterReceiveCallback). | |
| void | ClearReceiveCallback () noexcept override |
| Clear the receive callback (legacy support removed - use UnregisterCallback). | |
| hf_can_err_t | SetReceiveCallbackEx (hf_esp_can_receive_callback_t cb, void *user_data=nullptr) noexcept |
| void | ClearReceiveCallbackEx () noexcept |
| hf_can_err_t | SetErrorCallback (hf_esp_can_error_callback_t cb, void *user_data=nullptr) noexcept |
| void | ClearErrorCallback () noexcept |
| hf_can_err_t | SetStateChangeCallback (hf_esp_can_state_callback_t cb, void *user_data=nullptr) noexcept |
| void | ClearStateChangeCallback () noexcept |
| hf_can_err_t | SetTxCompleteCallback (hf_esp_can_tx_callback_t cb, void *user_data=nullptr) noexcept |
| void | ClearTxCompleteCallback () noexcept |
| hf_can_err_t | GetStatus (hf_can_status_t &status) noexcept override |
| Get current CAN bus status. | |
| hf_can_err_t | Reset () noexcept override |
| Reset the CAN controller. | |
| hf_can_err_t | SetAcceptanceFilter (uint32_t id, uint32_t mask, bool extended=false) noexcept override |
| Set acceptance filter for incoming messages. | |
| hf_can_err_t | ClearAcceptanceFilter () noexcept override |
| Clear all acceptance filters (accept all messages). | |
| hf_can_err_t | GetStatistics (hf_can_statistics_t &stats) noexcept override |
| Get detailed statistics. | |
| hf_can_err_t | ResetStatistics () noexcept override |
| Reset statistics counters. | |
| hf_can_err_t | GetDiagnostics (hf_can_diagnostics_t &diagnostics) noexcept override |
| Get diagnostic information. | |
| hf_can_err_t | ConfigureAdvancedTiming (const hf_esp_can_timing_config_t &timing_config) noexcept |
| Configure advanced bit timing parameters. | |
| hf_can_err_t | ConfigureAdvancedFilter (const hf_esp_can_filter_config_t &filter_config) noexcept |
| Configure hardware acceptance filter with advanced options. | |
| hf_can_err_t | InitiateBusRecovery () noexcept |
| Initiate bus recovery from bus-off state. | |
| hf_can_err_t | GetNodeInfo (twai_node_record_t &node_info) noexcept |
| Get detailed node information including error states. | |
| uint32_t | SendMessageBatch (const hf_can_message_t *messages, uint32_t count, uint32_t timeout_ms=1000) noexcept |
| Send multiple messages in a batch for improved performance. | |
Public Member Functions inherited from BaseCan | |
| virtual | ~BaseCan () noexcept=default |
| Virtual destructor ensures proper cleanup in derived classes. | |
| BaseCan (const BaseCan &)=delete | |
| BaseCan & | operator= (const BaseCan &)=delete |
| BaseCan (BaseCan &&)=delete | |
| BaseCan & | operator= (BaseCan &&)=delete |
| bool | EnsureInitialized () noexcept |
| Ensures that the CAN bus is initialized (lazy initialization). | |
| bool | EnsureDeinitialized () noexcept |
| Ensures that the CAN bus is deinitialized. | |
| bool | IsInitialized () const noexcept |
| Checks if the class is initialized. | |
| virtual hf_can_err_t | SetReceiveCallbackFD (hf_can_fd_receive_callback_t callback) noexcept |
| Set CAN-FD receive callback with enhanced information. | |
| virtual bool | SupportsCanFD () const noexcept |
| Check if CAN-FD is supported by this controller. | |
| virtual bool | SetCanFDMode (bool enable, hf_u32_t data_baudrate=2000000, hf_u32_t timeout_ms=1000) noexcept |
| Enable or disable CAN-FD mode. | |
| virtual bool | ConfigureCanFDTiming (hf_u16_t nominal_prescaler, hf_u8_t nominal_tseg1, hf_u8_t nominal_tseg2, hf_u16_t data_prescaler, hf_u8_t data_tseg1, hf_u8_t data_tseg2, hf_u8_t sjw=1) noexcept |
| Configure CAN-FD timing parameters. | |
| virtual bool | SetTransmitterDelayCompensation (hf_u8_t tdc_offset, hf_u8_t tdc_filter) noexcept |
| Set transmitter delay compensation (CAN-FD feature). | |
| virtual hf_u32_t | ReceiveMessageBatch (hf_can_message_t *messages, hf_u32_t max_count, hf_u32_t timeout_ms=100) noexcept |
| Receive multiple messages in a batch. | |
| virtual hf_can_err_t | ResetDiagnostics () noexcept |
| Reset CAN diagnostic information. | |
Private Member Functions | |
| hf_can_err_t | ConvertToTwaiFrame (const hf_can_message_t &hf_message, twai_frame_t &twai_frame) noexcept |
| Convert HF CAN message to ESP-IDF v5.5 TWAI frame. | |
| hf_can_err_t | ConvertFromTwaiFrame (const twai_frame_t &twai_frame, hf_can_message_t &hf_message) noexcept |
| Convert ESP-IDF v5.5 TWAI frame to HF CAN message. | |
| hf_can_err_t | ConvertEspError (esp_err_t esp_err) noexcept |
| Convert ESP-IDF error to HF error code. | |
| void | UpdateStatistics (hf_can_operation_type_t operation_type, bool success) noexcept |
| Update statistics after an operation. | |
| void | ProcessReceivedMessage (const twai_frame_t &frame) noexcept |
| Process received message through callback system. | |
| void | UpdateErrorStatistics (uint32_t error_type) noexcept |
| Update error statistics from error event. | |
| void | DispatchReceiveCallbacks (const twai_frame_t &frame) noexcept |
| Dispatch message to all registered receive callbacks. | |
| void | DispatchErrorCallbacks (const hf_esp_can_error_info_t &error_info) noexcept |
| Dispatch error event to all registered error callbacks. | |
| void | DispatchStateChangeCallbacks (const hf_esp_can_state_info_t &state_info) noexcept |
| Dispatch state change to all registered state callbacks. | |
| void | DispatchTxCompleteCallbacks (const hf_esp_can_tx_info_t &tx_info) noexcept |
| Dispatch transmission complete to all registered TX callbacks. | |
Static Private Member Functions | |
| static bool | InternalReceiveCallback (twai_node_handle_t handle, const twai_rx_done_event_data_t *event_data, void *user_ctx) noexcept |
| Internal receive event callback for ESP-IDF v5.5 event system. | |
| static bool | InternalErrorCallback (twai_node_handle_t handle, const twai_error_event_data_t *event_data, void *user_ctx) noexcept |
| Internal error event callback for comprehensive error handling. | |
| static bool | InternalStateChangeCallback (twai_node_handle_t handle, const twai_state_change_event_data_t *event_data, void *user_ctx) noexcept |
| Internal state change callback for bus recovery monitoring. | |
Private Attributes | |
| const hf_esp_can_config_t | config_ |
| TWAI node configuration. | |
| std::atomic< bool > | is_enabled_ |
| Node enabled state. | |
| std::atomic< bool > | is_recovering_ |
| Bus recovery state. | |
| PlatformMutex | config_mutex_ |
| Configuration mutex. | |
| PlatformMutex | stats_mutex_ |
| Statistics mutex. | |
| PlatformMutex | callback_mutex_ |
| Callback registry mutex. | |
| twai_node_handle_t | twai_node_handle_ |
| Native TWAI node handle. | |
| hf_esp_can_receive_callback_t | receive_cb_ {nullptr} |
| void * | receive_ud_ = nullptr |
| hf_esp_can_error_callback_t | error_cb_ {nullptr} |
| void * | error_ud_ = nullptr |
| hf_esp_can_state_callback_t | state_cb_ {nullptr} |
| void * | state_ud_ = nullptr |
| hf_esp_can_tx_callback_t | tx_cb_ {nullptr} |
| void * | tx_ud_ = nullptr |
| hf_can_statistics_t | statistics_ |
| Performance statistics. | |
| hf_can_diagnostics_t | diagnostics_ |
| Diagnostic information. | |
| hf_esp_can_timing_config_t | advanced_timing_ |
| Advanced timing configuration. | |
| hf_esp_can_filter_config_t | current_filter_ |
| Current filter configuration. | |
| bool | filter_configured_ |
| Filter configuration state. | |
Additional Inherited Members | |
Protected Member Functions inherited from BaseCan | |
| BaseCan () noexcept | |
| Protected constructor. | |
Protected Attributes inherited from BaseCan | |
| bool | initialized_ |
| Initialization status. | |
| hf_can_statistics_t | statistics_ |
| CAN operation statistics. | |
| hf_can_diagnostics_t | diagnostics_ |
| CAN diagnostic information. | |
ESP32 CAN (TWAI) implementation using ESP-IDF v5.5+ node-based API.
This class provides clean, comprehensive CAN communication using the ESP32's TWAI (Two-Wire Automotive Interface) controllers with modern ESP-IDF v5.5+ node-based APIs. The implementation supports ESP32-C6 and external SN65 CAN transceivers.
Key Features:
|
explicitnoexcept |
Constructor with TWAI node configuration.
| config | TWAI node configuration parameters |
LAZY INITIALIZATION: The TWAI node is NOT physically configured until Initialize() is called. This follows the same pattern as EspAdc.
|
overridenoexcept |
Destructor - ensures proper cleanup and resource deallocation.
|
overridevirtualnoexcept |
Clear all acceptance filters (accept all messages).
Reimplemented from BaseCan.
|
noexcept |
|
overridevirtualnoexcept |
Clear the receive callback (legacy support removed - use UnregisterCallback).
Reimplemented from BaseCan.
|
noexcept |
|
noexcept |
|
noexcept |
|
noexcept |
Configure hardware acceptance filter with advanced options.
| filter_config | Filter configuration including dual filter support |
|
noexcept |
Configure advanced bit timing parameters.
| timing_config | Advanced timing configuration |
|
privatenoexcept |
Convert ESP-IDF error to HF error code.
| esp_err | ESP-IDF error code |
|
privatenoexcept |
Convert ESP-IDF v5.5 TWAI frame to HF CAN message.
| twai_frame | Source TWAI frame |
| hf_message | Destination HF message |
|
privatenoexcept |
Convert HF CAN message to ESP-IDF v5.5 TWAI frame.
| hf_message | Source HF message |
| twai_frame | Destination TWAI frame |
|
overridevirtualnoexcept |
Deinitialize the TWAI node and free resources.
Implements BaseCan.
|
privatenoexcept |
Dispatch error event to all registered error callbacks.
| error_info | Error information |
|
privatenoexcept |
Dispatch message to all registered receive callbacks.
| frame | Received TWAI frame |
|
privatenoexcept |
Dispatch state change to all registered state callbacks.
| state_info | State change information |
|
privatenoexcept |
Dispatch transmission complete to all registered TX callbacks.
| tx_info | Transmission information |
|
overridevirtualnoexcept |
Get diagnostic information.
| diagnostics | Reference to store diagnostic data |
Reimplemented from BaseCan.
|
noexcept |
Get detailed node information including error states.
| node_info | Reference to store node information |
|
overridevirtualnoexcept |
Get detailed statistics.
| stats | Reference to store statistics |
Reimplemented from BaseCan.
|
overridevirtualnoexcept |
Get current CAN bus status.
| status | Reference to store status information |
Implements BaseCan.
|
overridevirtualnoexcept |
Initialize the TWAI node and allocate resources.
Implements BaseCan.
|
noexcept |
Initiate bus recovery from bus-off state.
|
staticprivatenoexcept |
Internal error event callback for comprehensive error handling.
| handle | TWAI node handle |
| event_data | Error event data |
| user_ctx | User context |
|
staticprivatenoexcept |
Internal receive event callback for ESP-IDF v5.5 event system.
| handle | TWAI node handle |
| event_data | Event data |
| user_ctx | User context |
|
staticprivatenoexcept |
Internal state change callback for bus recovery monitoring.
| handle | TWAI node handle |
| event_data | State change event data |
| user_ctx | User context |
|
privatenoexcept |
Process received message through callback system.
| frame | Received TWAI frame |
|
overridevirtualnoexcept |
Receive a CAN message (legacy polling interface).
| message | Reference to store received message |
| timeout_ms | Timeout in milliseconds (0 = non-blocking) |
Implements BaseCan.
|
overridevirtualnoexcept |
|
overridevirtualnoexcept |
|
overridevirtualnoexcept |
Send a CAN message.
| message | CAN message to send |
| timeout_ms | Timeout in milliseconds (0 = non-blocking) |
Implements BaseCan.
|
virtualnoexcept |
Send multiple messages in a batch for improved performance.
| messages | Array of messages to send |
| count | Number of messages |
| timeout_ms | Timeout for each message |
Reimplemented from BaseCan.
|
overridevirtualnoexcept |
Set acceptance filter for incoming messages.
| id | CAN ID to accept |
| mask | Acceptance mask (0 = don't care bits) |
| extended | true for extended frames, false for standard |
Implements BaseCan.
|
noexcept |
|
overridevirtualnoexcept |
Set callback for received messages (legacy support removed - use RegisterReceiveCallback).
| callback | Callback function to handle received messages |
Implements BaseCan.
|
noexcept |
|
noexcept |
|
noexcept |
|
privatenoexcept |
Update error statistics from error event.
| error_type | Error type from ESP-IDF |
|
privatenoexcept |
Update statistics after an operation.
| operation_type | Type of operation |
| success | Whether operation was successful |
|
private |
Advanced timing configuration.
|
mutableprivate |
Callback registry mutex.
|
private |
TWAI node configuration.
|
mutableprivate |
Configuration mutex.
|
private |
Current filter configuration.
|
private |
Diagnostic information.
|
private |
|
private |
|
private |
Filter configuration state.
|
private |
Node enabled state.
|
private |
Bus recovery state.
|
private |
|
private |
|
private |
|
private |
|
private |
Performance statistics.
|
mutableprivate |
Statistics mutex.
|
private |
Native TWAI node handle.
|
private |
|
private |