Chapter 2. API Reference
Listen Only Mode: This mode will prevent the TWAI controller from influencing the bus. Therefore, transmission of
messages/acknowledgement/error frames will be disabled. However the TWAI controller will still be able to receive
messages but will not acknowledge the message. This mode is suited for bus monitor applications.
Alerts The TWAI driver contains an alert feature that is used to notify the application layer of certain TWAI con-
troller or TWAI bus events. Alerts are selectively enabled when the TWAI driver is installed, but can be reconfigured
during runtime by calling twai_reconfigure_alerts(). The application can then wait for any enabled alerts
to occur by calling twai_read_alerts(). The TWAI driver supports the following alerts:
Table 4: TWAI Driver Alerts
Alert Flag Description
TWAI_ALERT_TX_IDLE No more messages queued for transmission
TWAI_ALERT_TX_SUCCESS The previous transmission was successful
TWAI_ALERT_RX_DATA A frame has been received and added to the RX queue
TWAI_ALERT_BELOW_ERR_WARN Both error counters have dropped below error warning limit
TWAI_ALERT_ERR_ACTIVE TWAI controller has become error active
TWAI_ALERT_RECOVERY_IN_PROGRESSTWAI controller is undergoing bus recovery
TWAI_ALERT_BUS_RECOVERED TWAI controller has successfully completed bus recovery
TWAI_ALERT_ARB_LOST The previous transmission lost arbitration
TWAI_ALERT_ABOVE_ERR_WARN One of the error counters have exceeded the error warning limit
TWAI_ALERT_BUS_ERROR A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus
TWAI_ALERT_TX_FAILED The previous transmission has failed
TWAI_ALERT_RX_QUEUE_FULL The RX queue is full causing a received frame to be lost
TWAI_ALERT_ERR_PASS TWAI controller has become error passive
TWAI_ALERT_BUS_OFF Bus-off condition occurred. TWAI controller can no longer influ-
ence bus
Note: The TWAI controller’s error warning limit is used to preemptively warn the application of bus errors
before the error passive state is reached. By default, the TWAI driver sets the error warning limit to 96. The
TWAI_ALERT_ABOVE_ERR_WARN is raised when the TEC or REC becomes larger then or equal to the error
warning limit. The TWAI_ALERT_BELOW_ERR_WARN is raised when both TEC and REC return back to values
below 96.
Note: When enabling alerts, the TWAI_ALERT_AND_LOG flag can be used to cause the TWAI driver to
log any raised alerts to UART. However, alert logging is disabled and TWAI_ALERT_AND_LOG if the CON-
FIG_TWAI_ISR_IN_IRAM option is enabled (see Placing ISR into IRAM).
Note: The TWAI_ALERT_ALL and TWAI_ALERT_NONE macros can also be used to enable/disable all alerts
during configuration/reconfiguration.
Bit Timing The operating bit rate of the TWAI driver is configured using the twai_timing_config_t struc-
ture. The period of each bit is made up of multiple time quanta, and the period of a time quantum is determined
by a prescaled version of the TWAI controller’s source clock. A single bit contains the following segments in the
following order:
1. The Synchronization Segment consists of a single time quantum
2. Timing Segment 1 consists of 1 to 16 time quanta before sample point
3. Timing Segment 2 consists of 1 to 8 time quanta after sample point
The Baudrate Prescaler is used to determine the period of each time quantum by dividing the TWAI controller’s
source clock (80 MHz APB clock). On the ESP32-S2, the brp can be any even number from 2 to 32768.
Espressif Systems 445
Submit Document Feedback
Release v4.4