AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
twai_plugin.cpp
Go to the documentation of this file.
1//================================================================================================
8//================================================================================================
9#ifdef ESP_PLATFORM
13#include "isobus/utility/system_timing.hpp"
14#include "isobus/utility/to_string.hpp"
15
16#include <algorithm>
17#include <chrono>
18#include <cstdint>
19#include <cstring>
20#include <limits>
21
22namespace isobus
23{
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)
28 {
29 }
30
31 TWAIPlugin::~TWAIPlugin()
32 {
33 close();
34 }
35
36 bool TWAIPlugin::get_is_valid() const
37 {
38 twai_status_info_t status;
39 esp_err_t error = twai_get_status_info(&status);
40 if (ESP_OK == error)
41 {
42 return status.state == TWAI_STATE_RUNNING;
43 }
44 else
45 {
46 LOG_ERROR("[TWAI] Error getting status: " + isobus::to_string(esp_err_to_name(error)));
47 }
48 return false;
49 }
50
51 void TWAIPlugin::close()
52 {
53 esp_err_t error = twai_stop();
54 if (ESP_OK != error)
55 {
56 LOG_ERROR("[TWAI] Error stopping driver: " + isobus::to_string(esp_err_to_name(error)));
57 }
58 error = twai_driver_uninstall();
59 if (ESP_OK != error)
60 {
61 LOG_ERROR("[TWAI] Error uninstalling driver: " + isobus::to_string(esp_err_to_name(error)));
62 }
63 }
64
65 void TWAIPlugin::open()
66 {
67 esp_err_t error = twai_driver_install(generalConfig, timingConfig, filterConfig);
68 if (ESP_OK != error)
69 {
70 LOG_CRITICAL("[TWAI] Error installing driver: " + isobus::to_string(esp_err_to_name(error)));
71 }
72 error = twai_start();
73 if (ESP_OK != error)
74 {
75 LOG_CRITICAL("[TWAI] Error starting driver: " + isobus::to_string(esp_err_to_name(error)));
76 }
77 }
78
79 bool TWAIPlugin::read_frame(isobus::CANMessageFrame &canFrame)
80 {
81 bool retVal = false;
82
83 //Wait for message to be received
84 twai_message_t message = {};
85 esp_err_t error = twai_receive(&message, pdMS_TO_TICKS(100));
86 if (ESP_OK == error)
87 {
88 // Process received message
89 if (!(message.rtr))
90 {
91 canFrame.identifier = message.identifier;
92 canFrame.isExtendedFrame = message.extd;
93 canFrame.dataLength = message.data_length_code;
94 if (isobus::CAN_DATA_LENGTH >= canFrame.dataLength)
95 {
96 memset(canFrame.data, 0, sizeof(canFrame.data));
97 memcpy(canFrame.data, message.data, canFrame.dataLength);
98 retVal = true;
99 }
100 }
101 }
102 else if (ESP_ERR_TIMEOUT != error)
103 {
104 LOG_ERROR("[TWAI] Error receiving message: " + isobus::to_string(esp_err_to_name(error)));
105 }
106
107 return retVal;
108 }
109
110 bool TWAIPlugin::write_frame(const isobus::CANMessageFrame &canFrame)
111 {
112 bool retVal = false;
113 twai_message_t message = {};
114
115 message.identifier = canFrame.identifier;
116 message.extd = canFrame.isExtendedFrame;
117 message.data_length_code = canFrame.dataLength;
118 memcpy(message.data, canFrame.data, canFrame.dataLength);
119
120 esp_err_t error = twai_transmit(&message, pdMS_TO_TICKS(100));
121 if (ESP_OK == error)
122 {
123 retVal = true;
124 }
125 else
126 {
127 LOG_ERROR("[TWAI] Error sending message: " + isobus::to_string(esp_err_to_name(error)));
128 }
129 return retVal;
130 }
131}
132#endif // ESP_PLATFORM
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.