examples/peripherals/twai/twai_error_recovery/README.md
| Supported Targets | ESP32 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-H21 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
|---|
This example demonstrates how to recover a TWAI node from a Bus-Off error condition and resume communication. The recovery is triggered by physically inducing bus errors and handled using the ESP TWAI on-chip driver with callback support.
Modify TX_GPIO_NUM and RX_GPIO_NUM in the twai_recovery_main.c if needed. Connect these pins to your TWAI transceiver's TX and RX pins respectively.
#define TX_GPIO_NUM GPIO_NUM_4
#define RX_GPIO_NUM GPIO_NUM_5
To trigger bus errors and test the recovery mechanism, you can use either of these methods:
Connection Disruption: Briefly disconnect the TX or RX wire between the ESP32 and the TWAI transceiver while frames are being transmitted.
Bus Short Circuit: Briefly short-circuit the TWAI H and TWAI L lines on the bus. This simulates a bus fault condition.
⚠️ Important: The disconnection or short-circuit should be brief enough to trigger bus error messages in the log output. Too long may cause permanent damage to the transceiver or affect other nodes on the bus.
idf.py set-target esp32 build
idf.py -p PORT flash monitor
install twai success
node started
sending frame 0, please trigger error during sending
sending frame 1, please trigger error during sending
...
sending frame 4, please trigger error during sending
// Manually trigger error here (disconnect TX/RX or short H/L)
bus error: 0x2
state changed: error_active -> error_warning
...
sending frame 9, please trigger error during sending
// Trigger error again
bus error: 0x2
state changed: error_passive -> bus_off
node offline, start recover ...
waiting ... 0
...
state changed: bus_off -> error_active
node recovered! continue
sending frame 0, please trigger error during sending
If the example doesn't enter the error_warning/error_passive/bus_off states after manual error triggering:
If errors are not being detected: