Example code for CAN receive using ESP-IDF.
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.
esp-idf v4.2-dev-2243 or later.
Use twai(Two-Wire Automotive Interface) driver instead of can driver.
- SN65HVD23x CAN-BUS Transceiver
SN65HVD23x | ESP32 | ESP32-S2 | ESP32-C3 | ||
---|---|---|---|---|---|
D(CTX) | -- | GPIO21 | GPIO17 | GPIO9 | (*1) |
GND | -- | GND | GND | GND | |
Vcc | -- | 3.3V | 3.3V | 3.3V | |
R(CRX) | -- | GPIO22 | GPIO18 | GPIO10 | (*1) |
Vref | -- | N/C | N/C | N/C | |
CANL | -- | To CAN Bus | |||
CANH | -- | To CAN Bus | |||
RS | -- | GND | GND | GND | (*2) |
(*1) You can change using menuconfig. But it may not work with other GPIOs.
(*2) N/C for SN65HVD232
- Termination resistance
I used 150 ohms.
+-----------+ +-----------+ +-----------+
| 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)
NOTE
3V CAN Trasnceviers like VP230 are fully interoperable with 5V CAN trasnceviers like MCP2551.
Check here.
git clone https://github.com/nopnop2002/esp-idf-candump
cd esp-idf-candump
idf.py set-target esp32
idf.py menuconfig
idf.py flash
git clone https://github.com/nopnop2002/esp-idf-candump
cd esp-idf-candump
idf.py set-target esp32s2
idf.py menuconfig
idf.py flash
git clone https://github.com/nopnop2002/esp-idf-candump
cd esp-idf-candump
idf.py set-target esp32c3
idf.py menuconfig
idf.py flash
You have to set this config value with menuconfig.
- CAN Bitrate
- CTX GPIO number
- CRX GPIO number
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.
https://github.com/nopnop2002/esp-idf-can2http