Industrial bus

TWAI / CAN

Industrial and automotive bus. The chip is a controller; you still need a transceiver.

Industrial bus CAN / TWAI ESP32 / S3 / C3 / C6 class with TWAI

Official path: examples/peripherals/twai

01 Overview

TWAI is Espressif's implementation of the CAN protocol, compatible with CAN 2.0 standard and extended frames. The official example demonstrates basic frame transmission and reception, serving as the starting point for board-level CAN communication.

It solves the problem of directly interfacing ESP32 series with a CAN bus, exchanging data with automotive ECUs, industrial PLCs, sensor gateways, etc., without needing an external SPI-to-CAN module.

Typical use cases: vehicle connectivity, industrial fieldbus, device status reporting, multi-node control networks.

It is not: a USB-to-CAN debug tool, nor a full high-layer protocol stack (like CANopen or J1939). It only provides low-level frame transmission; protocol parsing must be implemented by the user.

On the hardware side, the chip only contains a CAN controller; an external CAN transceiver (e.g., SN65HVD230) is mandatory to generate the differential signal.

02 Hardware

  • Transceiver: An external CAN transceiver (e.g., SN65HVD230) is mandatory. Connect the chip's TWAI pins (e.g., GPIO5/GPIO4) to the transceiver's TXD/RXD.
  • Bus termination: Connect a 120Ω resistor between CANH and CANL, one at each end of the bus.
  • Common ground: All nodes must share a common ground; otherwise, common-mode voltage may damage the transceiver.
  • Isolation: In noisy environments, use isolated transceivers (e.g., ISO1050) or isolated power supplies to avoid ground loops.
  • Power: The transceiver typically requires 3.3V or 5V supply; ensure voltage level compatibility, add level shifting if necessary.
  • Wiring: Use twisted pair for CANH/CANL, keep it short, and route away from high-power lines.
  • Schematic: Refer to the schematic for specific pin connections and decoupling capacitors.

03 Software flow

1. Configure the TWAI driver: set bitrate (e.g., 500kbps), mode (normal/listen-only), acceptance filter (receive all frames or filter by ID).

2. Install the driver: call twai_driver_install() to initialize the controller.

3. Start TWAI: call twai_start() to begin communication.

4. Transmit frames: construct a twai_message_t, fill ID, data length, and data, then call twai_transmit().

5. Receive frames: call twai_receive() for blocking reception, or use event queues for asynchronous reception.

6. Error handling: monitor error counters (tx_err_counter/rx_err_counter); if they exceed thresholds, the controller enters bus-off state, requiring twai_initiate_recovery() to recover.

7. Stop and uninstall: twai_stop() and twai_driver_uninstall() are used to shut down.

04 Core points

  • No transceiver, no CAN: Connecting the chip pins directly to the bus will damage the chip; an external transceiver is mandatory.
  • Bit timing is sensitive: Different chips (e.g., S3/C3) have different clock sources, so bit timing parameters may vary; adjust according to the chip's datasheet.
  • Acceptance filter: The default receives all frames; in real applications, configure filters to reduce CPU load.
  • Bus-off recovery: After bus-off, manual recovery is required; otherwise, communication will not resume.
  • Custom work: Bitrate, filters, and protocol layers (e.g., CANopen) need customization per project.
  • Don't copy across chips: TWAI pins may differ across ESP32 series, and bit timing parameters must be adjusted per chip.

中文

工业总线

TWAI / CAN

工业与汽车总线。芯片是 TWAI 控制器,外面还要收发器。

工业总线 CAN / TWAI ESP32 / S3 / C3 / C6 class with TWAI

官方路径: examples/peripherals/twai

01 项目概述

TWAI 是 Espressif 对 CAN 协议的实现,兼容 CAN 2.0 标准帧与扩展帧。官方示例演示了最基本的收发流程,是评估板级 CAN 通信的起点。

解决什么问题:在 ESP32 系列上直接接入 CAN 总线,与车载 ECU、工业 PLC、传感器网关等设备交换数据,无需外部 SPI 转 CAN 模块。

典型场景:车机互联、工业现场总线、设备状态上报、多节点控制网络。

它不是:不是 USB 转 CAN 调试工具,也不是完整的高层协议栈(如 CANopen、J1939)。它只提供底层帧收发,协议解析需要自己实现。

硬件上,芯片内部只有 CAN 控制器,必须外接 CAN 收发器(如 SN65HVD230)才能产生差分信号。

02 项目硬件描述

  • 收发器: 必须外接 CAN 收发器(如 SN65HVD230),芯片的 TWAI 引脚(如 GPIO5/GPIO4)连接收发器的 TXD/RXD。
  • 总线终端: CANH 与 CANL 之间需接 120Ω 终端电阻,总线两端各一个。
  • 共地: 所有节点必须共地,否则共模电压可能损坏收发器。
  • 隔离: 现场干扰大时,建议使用隔离收发器(如 ISO1050)或隔离电源,避免地环路。
  • 电源: 收发器通常需要 3.3V 或 5V 供电,注意电平匹配,必要时加电平转换。
  • 接线: CANH/CANL 双绞线,尽量短,远离强电。
  • 原理图: 具体引脚连接和去耦电容见原理图。

03 项目软件流程描述

1. 配置 TWAI 驱动:设置位速率(如 500kbps)、模式(正常/只听)、验收滤波(接收所有帧或过滤 ID)。

2. 安装驱动:调用 twai_driver_install() 初始化控制器。

3. 启动 TWAI:调用 twai_start() 开始通信。

4. 发送帧:构造 twai_message_t,填充 ID、数据长度和数据,调用 twai_transmit()。

5. 接收帧:调用 twai_receive() 阻塞接收,或使用事件队列异步接收。

6. 错误处理:监控错误计数(tx_err_counter/rx_err_counter),超过阈值进入 bus-off 状态,需调用 twai_initiate_recovery() 恢复。

7. 停止与卸载:twai_stop() 和 twai_driver_uninstall() 用于关闭。

04 项目核心点

  • 没有收发器就没有 CAN: 芯片引脚直接接总线会损坏芯片,必须外接收发器。
  • 位时序敏感: 不同芯片(如 S3/C3)的时钟源不同,位时序参数可能不同,需根据芯片手册调整。
  • 验收滤波: 默认接收所有帧,实际应用需配置滤波以减轻 CPU 负担。
  • 总线关闭恢复: bus-off 后必须手动恢复,否则无法通信。
  • 可定制: 波特率、滤波、协议层(如 CANopen)都需要根据项目定制。
  • 不要照搬: 不同 ESP32 系列的 TWAI 引脚可能不同,且位时序参数需按芯片调整。