Home

Awesome

esp-idf-candump

Example code for CAN receive using ESP-IDF.

The ESP32 intregates a CAN controller which is compatible with the NXP SJA1000 CAN controller.
Thus, it is CAN 2.0B specification compliant and supports two message formats:

ESP-IDF contains three example for TWAI, but you need two ESP32s running master and slave, or master and listen_only at the same time.
So, I made a reception-only tool with Raspberry or Arduino as the sender.

Note for ESP-IDF V4.4 can bitrate
In ESP32 whose chip version is Rev2 or higher, the role of register bits related to CAN communication has been changed to mean "halve the communication speed".
This is to support slower bitrates below 25Kbps.
This fuature can be controlled by CONFIG_ESP32_REV_MIN.
This fuature is enabled when CONFIG_ESP32_REV_MIN >= 2.
See here for detail.

Software requirement

ESP-IDF V4.4/V5.x.
ESP-IDF V5.1 is required when using ESP32C6.

Hardware requirements

Wireing

SN65HVD23xESP32ESP32-S2/S3ESP32-C3/C6
D(CTX)--GPIO21GPIO17GPIO0(*1)
GND--GNDGNDGND
Vcc--3.3V3.3V3.3V
R(CRX)--GPIO22GPIO18GPIO1(*1)
Vref--N/CN/CN/C
CANL--To CAN Bus
CANH--To CAN Bus
RS--GNDGNDGND(*2)

(*1) You can change using menuconfig. But it may not work with other GPIOs.

(*2) N/C for SN65HVD232

Test Circuit

   +-----------+   +-----------+   +-----------+ 
   | Atmega328 |   | Atmega328 |   |   ESP32   | 
   |           |   |           |   |           | 
   | Transmit  |   | Receive   |   | 21    22  | 
   +-----------+   +-----------+   +-----------+ 
     |       |      |        |       |       |   
   +-----------+   +-----------+     |       |   
   |           |   |           |     |       |   
   |  MCP2515  |   |  MCP2515  |     |       |   
   |           |   |           |     |       |   
   +-----------+   +-----------+     |       |   
     |      |        |      |        |       |   
   +-----------+   +-----------+   +-----------+ 
   |           |   |           |   | D       R | 
   |  MCP2551  |   |  MCP2551  |   |   VP230   | 
   | H      L  |   | H      L  |   | H       L | 
   +-----------+   +-----------+   +-----------+ 
     |       |       |       |       |       |   
     +--^^^--+       |       |       +--^^^--+
     |   R1  |       |       |       |   R2  |   
 |---+-------|-------+-------|-------+-------|---| BackBorn H
             |               |               |
             |               |               |
             |               |               |
 |-----------+---------------+---------------+---| BackBorn L

      +--^^^--+:Terminaror register
      R1:120 ohms
      R2:150 ohms(Not working at 120 ohms)

ESP32-CAN-3V3

NOTE
3V CAN Trasnceviers like VP230 are fully interoperable with 5V CAN trasnceviers like MCP2551.
Check here.

Installation

git clone https://github.com/nopnop2002/esp-idf-candump
cd esp-idf-candump/stdout
idf.py set-target {esp32/esp32s2/esp32s3/esp32c3/esp32c6}
idf.py menuconfig
idf.py flash

Configuration

You have to set this config value with menuconfig.

config-top config-app-1 config-app-2

ScreenShot

stdout

Acceptance Filter

The explanation about the acceptance filter is available here.
There are diagrams of Bit layout of single filter mode and Bit layout of dual filter mode, but they are described in Right side MSBit.
I've never seen a Right side MSBit before, so I thought it was a Left side MSBit.
I was very confused.

Reference

https://github.com/nopnop2002/esp-idf-can2http

https://github.com/nopnop2002/esp-idf-can2mqtt

https://github.com/nopnop2002/esp-idf-can2usb

https://github.com/nopnop2002/esp-idf-can2websocket

https://github.com/nopnop2002/esp-idf-can2socket

https://github.com/nopnop2002/esp-idf-CANBus-Monitor