lamaPLC: TWAI basics

TWAI basics The TWAI (Two-Wire Automotive Interface) was developed/applied by Espressif for the ESP32 microcontroller family, effectively implementing CAN Classic.

TWAI is a highly reliable, multi-master, real-time, serial asynchronous communication protocol designed for automotive and industrial applications.

The TWAI controller is not compatible with ISO11898-1 FD Format frames and will interpret them as errors. TWAI is effectively “CAN Classic,” not CAN-FD, which is what the automotive industry & promoters such as STMicroelectronics, Infineon, NXP, Texas Instruments, Kvaser, Bosch, Daimler, and GM wouldn't appreciate Espressif referring to their transceiver as “CAN,” which could confuse.

The TWAI standard supports both 11-bit and 29-bit identifiers. The protocol supports message prioritization with lossless arbitration, automatic retransmission, and fault confinement mechanisms. The ESP32 microcontroller includes TWAI controllers, allowing for the creation of one driver instance, as follows:

PlatformTWAI busses
ESP82660
ESP321
ESP32-S21
ESP32-S31
ESP32-C2(ESP8684)0
ESP32-C3(ESP8685)1
ESP32-C52
ESP32-C62
ESP32-C612
ESP32-H21
ESP32-P43

TWAI port specifically

  • TX/RX: For a single-node, you can directly short the TX and RX pins to omit the transceiver.
  • BUS_OFF: (optional): Outputs a low logic level (0 V) when the TWAI controller enters the bus-off state. Otherwise, it remains at a high logic level (3.3 V).
  • CLK_OUT: (optional) Outputs the controller's time quantum clock, which is a divided version of the source clock.

Transmitting Messages

TWAI messages come in different types, each defined by its header. A typical data frame mainly includes a header and a data payload, with a structure similar to the following:

TWAI Transmitting Messages

  • ID: specifies the message as 0x01. Message IDs are usually used to indicate the type of message in an application and also help in bus arbitration during transmission—lower values mean higher priority on the bus.
  • RTR: Shows the frame is a remote frame with no data payload.
  • IDE: Represents the use of a 29-bit extended ID format.
  • FDF: Indicates that the frame is in FD format, supporting up to 64 bytes of data.
  • BRS: Allows using a separate data-phase baud rate during transmission.
  • ESI: For received frames, indicates the error status of the transmitting node.
  • DLC: Data Length Code. For classic frames, values [0:8] represent lengths [0:8]; for FD format, values [0:15] correspond to lengths up to 64 bytes.

TWAI topics on lamaPLC