13#include "isobus/utility/system_timing.hpp"
14#include "isobus/utility/to_string.hpp"
24 TWAIPlugin::TWAIPlugin(
const twai_general_config_t *generalConfig,
const twai_timing_config_t *timingConfig,
const twai_filter_config_t *filterConfig) :
25 generalConfig(generalConfig),
26 timingConfig(timingConfig),
27 filterConfig(filterConfig)
31 TWAIPlugin::~TWAIPlugin()
36 bool TWAIPlugin::get_is_valid()
const
38 twai_status_info_t status;
39 esp_err_t error = twai_get_status_info(&status);
42 return status.state == TWAI_STATE_RUNNING;
46 LOG_ERROR(
"[TWAI] Error getting status: " + isobus::to_string(esp_err_to_name(error)));
51 void TWAIPlugin::close()
53 esp_err_t error = twai_stop();
56 LOG_ERROR(
"[TWAI] Error stopping driver: " + isobus::to_string(esp_err_to_name(error)));
58 error = twai_driver_uninstall();
61 LOG_ERROR(
"[TWAI] Error uninstalling driver: " + isobus::to_string(esp_err_to_name(error)));
65 void TWAIPlugin::open()
67 esp_err_t error = twai_driver_install(generalConfig, timingConfig, filterConfig);
70 LOG_CRITICAL(
"[TWAI] Error installing driver: " + isobus::to_string(esp_err_to_name(error)));
75 LOG_CRITICAL(
"[TWAI] Error starting driver: " + isobus::to_string(esp_err_to_name(error)));
84 twai_message_t message = {};
85 esp_err_t error = twai_receive(&message, pdMS_TO_TICKS(100));
93 canFrame.
dataLength = message.data_length_code;
96 memset(canFrame.
data, 0,
sizeof(canFrame.
data));
102 else if (ESP_ERR_TIMEOUT != error)
104 LOG_ERROR(
"[TWAI] Error receiving message: " + isobus::to_string(esp_err_to_name(error)));
113 twai_message_t message = {};
117 message.data_length_code = canFrame.
dataLength;
120 esp_err_t error = twai_transmit(&message, pdMS_TO_TICKS(100));
127 LOG_ERROR(
"[TWAI] Error sending message: " + isobus::to_string(esp_err_to_name(error)));
General constants used throughout this library.
A class that acts as a logging sink. The intent is that someone could make their own derived class of...
A CAN frame for interfacing with a hardware layer, like socket CAN or other interface.
std::uint8_t dataLength
The length of the data used in the frame.
std::uint32_t identifier
The 32 bit identifier of the frame.
bool isExtendedFrame
Denotes if the frame is extended format.
std::uint8_t data[8]
The data payload of the frame.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
constexpr std::uint8_t CAN_DATA_LENGTH
The length of a classical CAN frame.
A driver for using the Two-Wire Automotive Interface (TWAI) with the stack.