Skip to content

Commit a1b6182

Browse files
Add status check and recovery from BUS_OFF (#130)
1 parent 7d11b86 commit a1b6182

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

main/modules/can.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,24 @@ void Can::send(const uint32_t id, const uint8_t data[8], const bool rtr, uint8_t
127127
message.data[i] = data[i];
128128
}
129129
if (twai_transmit(&message, pdMS_TO_TICKS(0)) != ESP_OK) {
130-
if (twai_stop() != ESP_OK || twai_start() != ESP_OK) {
131-
throw std::runtime_error("could not send CAN message and could not restart twai driver");
130+
twai_status_info_t status_info;
131+
132+
if (twai_get_status_info(&status_info) != ESP_OK) {
133+
throw std::runtime_error("could not get twai status");
134+
}
135+
if (status_info.state == TWAI_STATE_BUS_OFF) {
136+
if (twai_initiate_recovery() != ESP_OK) {
137+
throw std::runtime_error("could not initiate recovery");
138+
}
139+
vTaskDelay(pdMS_TO_TICKS(100)); // Wait for recovery to start
140+
}
141+
if (status_info.state != TWAI_STATE_STOPPED) {
142+
if (twai_stop() != ESP_OK) {
143+
throw std::runtime_error("could not stop twai driver");
144+
}
145+
}
146+
if (twai_start() != ESP_OK) {
147+
throw std::runtime_error("could not restart twai driver");
132148
}
133149
throw std::runtime_error("could not send CAN message");
134150
}

0 commit comments

Comments
 (0)