nopnop2002 / esp-idf-CANBus-Monitor

Monitor Canbus traffic
MIT License
118 stars 25 forks source link

Unable to transmit can Data #12

Open pegasus-automation opened 2 years ago

pegasus-automation commented 2 years ago

Hello, I am trying to use the CANBus Monitor. I am able to successfully receive message on the CAN but while transmitting I am facing the below error as shown in the screenshot. twai_transmit Fail ESP_ERR_INVALID_STATE

canbus

I have checked it with 120 ohm and 150 ohm terminating resistors individually. Same error in both. I am using TJA1050 CAN IC [Connected as follow CTX-21 CRX-22 VCC-5v Gnd-Gnd]

nopnop2002 commented 2 years ago

If the transmission fails, these are the possible causes.

There is no receiving app on CanBus. The speed does not match the receiver. There is no terminating resistor on the CanBus. There are three terminating resistors on the CanBus. The resistance value of the terminating resistor is incorrect. Stub length in CAN bus is too long. Though the ISO11898 Standard specifies a maximum bus length of 40m and maximum stub length of 0.3m at 1Mbps. See here. https://e2e.ti.com/support/interface-group/interface/f/interface-forum/378932/iso1050-can-bus-stub-length


Sometimes the transmission seems to succeed. 182618017-f27c5701-a043-451f-93c9-f001b73e2874

pegasus-automation commented 2 years ago

I have tested the same setup with CAN analyser tool and we're able to send the data to the ECU and get the response from the ECU.

Also tried 150ohm and 120ohm termination resistance but the issue remains the same.

Also little bit confused with 3 termination resistance? If we have 1 master and 2 slaves in that case we'll have 3 termination resistance so, also in vehicle there is communication between Multiple ECU so all ECU will have their own termination resistance.

nopnop2002 commented 2 years ago

1 master and 2 slaves in that case we'll have 3 termination resistance

CAN-BUS only needs to have terminating resistors at both ends of the BUS.

https://support.maxongroup.com/hc/en-us/articles/360009241840-CAN-bus-topology-and-bus-termination

pegasus-automation commented 2 years ago

Just a silly doubt. in code include of twai it is written update from IDF v4.2 means we have to take twai.c and twai.h from v4.2? or it depicts that the twai files are updated from idf v4.2 to v4.4

nopnop2002 commented 2 years ago

Before esp-idf v4.2-dev-2243 it was called can driver.

Now The name was changed to twai (Two-Wire Automotive Interface) driver.

Now this gives an error.

#include "driver/can.h"
pegasus-automation commented 2 years ago

Thanks, that clears the doubt. However I am still unable to communicate with the device. The device with which I am trying to communicate is working on logic level of 5 while ESP32 has logic level of 3v3. Do you think this could be the reason for failure in transmission of data?

nopnop2002 commented 2 years ago

The device with which I am trying to communicate is working on logic level of 5 while ESP32 has logic level of 3v3.

3V CAN Trasnceviers are fully interoperable with 5V CAN trasnceviers.

https://www.ti.com/lit/an/slla337/slla337.pdf

When 3.3V and 5V are mixed, empirically, the value of termination resistance becomes very severe.

This is my test circuit. 3.3V and 5V are mixed. The length of the Stub is very short.

   +-----------+   +-----------+   +-----------+ 
   | 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)
sboicu commented 1 year ago

I also try to communicate to other can devices, no success. image I tried separatelly the HW connection and I was able to communicate by CAN with an arduino schetch and also sending serial data. HW looks ok as connection. I am not even allowed to send the test callback function. Am I missing something? HW connection is identicaly to the one mentioned in readme. Look forward for receiving your inputs. Thanks.

nopnop2002 commented 1 year ago

You need two ESP32s, will this official sample work?

https://github.com/espressif/esp-idf/tree/master/examples/peripherals/twai/twai_network

twai_network_listen_only works only 1 ESP32.

sboicu commented 1 year ago

Hello @nopnop2002

thanks for helping.

this is the log from your example flash + monitor

`esptool.py v4.4 Serial port com15 Connecting... Failed to get PID of a device on com15, using standard reset sequence. .. Chip is ESP32-D0WD (revision v1.0) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 34:ab:95:8d:b7:d0 Uploading stub... Running stub... Stub running... Changing baud rate to 460800 Changed. Configuring flash size... Flash will be erased from 0x00001000 to 0x00007fff... Flash will be erased from 0x00010000 to 0x00048fff... Flash will be erased from 0x00008000 to 0x00008fff... Compressed 26368 bytes to 16418... Writing at 0x00001000... (50 %) Writing at 0x000076e7... (100 %) Wrote 26368 bytes (16418 compressed) at 0x00001000 in 0.8 seconds (effective 264.3 kbit/s)... Hash of data verified. Compressed 231488 bytes to 124907... Writing at 0x00010000... (12 %) Writing at 0x0001d460... (25 %) Writing at 0x00022fd1... (37 %) Writing at 0x00028c11... (50 %) Writing at 0x0002e56c... (62 %) Writing at 0x000370f1... (75 %) Writing at 0x0003f1ee... (87 %) Writing at 0x00044c81... (100 %) Wrote 231488 bytes (124907 compressed) at 0x00010000 in 3.2 seconds (effective 579.4 kbit/s)... Hash of data verified. Compressed 3072 bytes to 103... Writing at 0x00008000... (100 %) Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.1 seconds (effective 416.5 kbit/s)... Hash of data verified.

Leaving... Hard resetting via RTS pin... Executing action: monitor Running idf_monitor in directory c:\espressif\frameworks\esp-idf-v5.0-2\examples\esp-idf-canbus-monitor Executing "C:\Espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe C:\Espressif\frameworks\esp-idf-v5.0-2\tools/idf_monitor.py -p com15 -b 115200 --toolchain-prefix xtensa-esp32-elf- --target esp32 --revision 0 c:\espressif\frameworks\esp-idf-v5.0-2\examples\esp-idf-canbus-monitor\build\esp-idf-CANBus-Monitor.elf --force-color -m 'C:\Espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe' 'C:\Espressif\frameworks\esp-idf-v5.0-2\tools\idf.py'"... --- idf_monitor on com15 115200 --- --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0030,len:6992 load:0x40078000,len:15452 load:0x40080400,len:3840 0x40080400: _init at ??:?

entry 0x4008064c I (27) boot: ESP-IDF v5.0 2nd stage bootloader I (27) boot: compile time 01:17:53 I (27) boot: chip revision: v1.0 I (30) boot_comm: chip revision: 1, min. bootloader chip revision: 0 I (37) boot.esp32: SPI Speed : 40MHz I (42) boot.esp32: SPI Mode : DIO I (46) boot.esp32: SPI Flash Size : 2MB I (51) boot: Enabling RNG early entropy source... I (56) boot: Partition Table: I (60) boot: ## Label Usage Type ST Offset Length I (67) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (74) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (82) boot: 2 factory factory app 00 00 00010000 00100000 I (89) boot: End of partition table I (94) boot_comm: chip revision: 1, min. application chip revision: 0 I (101) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=0b0c4h ( 45252) map I (126) esp_image: segment 1: paddr=0001b0ec vaddr=3ffb0000 size=02668h ( 9832) load I (130) esp_image: segment 2: paddr=0001d75c vaddr=40080000 size=028bch ( 10428) load I (136) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=1dbdch (121820) map I (184) esp_image: segment 4: paddr=0003dc04 vaddr=400828bc size=0ac00h ( 44032) load I (202) esp_image: segment 5: paddr=0004880c vaddr=50000000 size=00010h ( 16) load I (209) boot: Loaded app from partition at offset 0x10000 I (209) boot: Disabling RNG early entropy source... I (222) cpu_start: Pro cpu up. I (222) cpu_start: Starting app cpu, entry point is 0x400812b8 0x400812b8: call_start_cpu1 at C:/Espressif/frameworks/esp-idf-v5.0-2/components/esp_system/port/cpu_start.c:142

I (0) cpu_start: App cpu up. I (237) cpu_start: Pro cpu start user code I (237) cpu_start: cpu freq: 160000000 Hz I (237) cpu_start: Application information: I (241) cpu_start: Project name: esp-idf-CANBus-Monitor I (248) cpu_start: App version: e15a592-dirty I (253) cpu_start: Compile time: Jan 22 2023 01:26:27 I (259) cpu_start: ELF file SHA256: cfd6f32d6da00d6e... I (265) cpu_start: ESP-IDF: v5.0 I (270) heap_init: Initializing. RAM available for dynamic allocation: I (277) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM I (283) heap_init: At 3FFB2FD8 len 0002D028 (180 KiB): DRAM I (289) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM I (296) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM I (302) heap_init: At 4008D4BC len 00012B44 (74 KiB): IRAM I (310) spi_flash: detected chip: generic I (313) spi_flash: flash io: dio W (317) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header. I (331) cpu_start: Starting scheduler on PRO CPU. I (0) cpu_start: Starting scheduler on APP CPU. I (10) UART_TX_TASK: Start I (364) CONTROL_TASK: Start I (364) TWAI_RX_TASK: Start I (364) UART_RX_TASK: Start I (364) TWAI_TX_TASK: Start I (107774) UART_RX_TASK: Read 20 I (107774) UART_RX_TASK: 0x3ffb4794 aa 55 12 07 01 00 00 00 00 00 00 00 00 03 00 00 |.U..............| I (107774) UART_RX_TASK: 0x3ffb47a4 00 00 00 1d |....| I (107794) CONTROL_TASK: uartBuf.bytes=20 I (107794) CONTROL_TASK: uartBuf.data[0]=0xaa I (107794) CONTROL_TASK: _cmd=[5] _len==[5] I (107794) CONTROL_TASK: Standard filter code=0x000 mask=0x000 I (107804) CONTROL_TASK: Speed = 125Kbps I (107804) CONTROL_TASK: Driver installed I (107814) CONTROL_TASK: Driver started I (111334) UART_RX_TASK: Read 60 I (111334) UART_RX_TASK: 0x3ffb4794 aa 55 12 07 01 00 00 00 00 00 00 00 00 03 00 00 |.U..............| I (111334) UART_RX_TASK: 0x3ffb47a4 00 00 00 1d aa 55 12 07 01 00 00 00 00 00 00 00 |.....U..........| I (111344) UART_RX_TASK: 0x3ffb47b4 00 03 00 00 00 00 00 1d aa 55 12 07 01 00 00 00 |.........U......| I (111354) UART_RX_TASK: 0x3ffb47c4 00 00 00 00 00 03 00 00 00 00 00 1d |............| I (111374) CONTROL_TASK: uartBuf.bytes=60 I (111374) CONTROL_TASK: uartBuf.data[0]=0xaa I (111374) CONTROL_TASK: _cmd=[5] _len==[5] I (111834) CONTROL_TASK: Driver stopped I (111834) CONTROL_TASK: Driver uninstalled I (111834) CONTROL_TASK: Standard filter code=0x000 mask=0x000 I (111834) CONTROL_TASK: Speed = 125Kbps I (111834) CONTROL_TASK: Driver installed I (111844) CONTROL_TASK: Driver started I (180004) UART_RX_TASK: Read 6 I (180004) UART_RX_TASK: 0x3ffb4794 aa c1 70 01 aa 55 |..p..U| I (180014) CONTROL_TASK: uartBuf.bytes=6 I (180014) CONTROL_TASK: uartBuf.data[0]=0xaa I (180014) CONTROL_TASK: _cmd=[c] _len==[1] I (180014) TWAI_TX_TASK: tx_msg.identifier=[0x170] I (182824) UART_RX_TASK: Read 6 I (182824) UART_RX_TASK: 0x3ffb4794 aa c1 70 01 aa 55 |..p..U| I (182834) CONTROL_TASK: uartBuf.bytes=6 I (182834) CONTROL_TASK: uartBuf.data[0]=0xaa I (182834) CONTROL_TASK: _cmd=[c] _len==[1]`

sboicu commented 1 year ago

And here I used 2xESP32 1 flashed with twai network master 1 flashed with twai network listener only

everything seems to be working. but your example I am not able to mae it work.

image

sboicu commented 1 year ago

then I tried with the listener to listen what your example is sending. and then nothing: image

sboicu commented 1 year ago

but with one esp flashed with twai master which is transmitting ping the app receives. only the TX is not working. image

nopnop2002 commented 1 year ago

CANID=0xa2 Length=0 packets are received at around 5Hz. what is this packet?

As a trial, is it possible to set the transmission cycle of this packet to 1Hz on the transmitting side?

nopnop2002 commented 1 year ago

@sboicu

I think this problem is a "spinlock acquisition race condition".

ESP-IDF V4.4.1 and later have a bug regarding this situation.

There is a possibility that it can be fixed by correcting here.

https://github.com/nopnop2002/esp-idf-CANBus-Monitor/blob/master/main/main.c#L782

//xTaskCreate(twai_transmit_task, "twai_tx", 1024*4, NULL, 2, NULL);
xTaskCreate(twai_transmit_task, "twai_tx", 1024*4, NULL, 3, NULL);