-
Notifications
You must be signed in to change notification settings - Fork 7.7k
ESP32 CANBUS Communication #7046
-
Hi,
- When I try the communication between ESP32 board, it works fine.
- When I try KV58(from NXP, ISO 11898-1 standard and CAN 2.0 B protocol specifications) with K64F(ISO 11898-1 standard and CAN 2.0 B protocol specifications) CANBUS, it works fine as well.
- Then, I try KV58 CANBUS with ESP32, I cannot get it work. The difference between K64F CANBUS and ESP32 CANBUS is just MCU, all others(transceiver, connection, and etc) are exactly same. I am wondering if it is related to timing settings?
Can you tell me how to fix this problem? Any suggestion?
The code for ESP32 is below...
Thanks,
Christie
void setup() {
// put your setup code here, to run once:
Serial.begin(115200, SERIAL_8N1);
delay(1000);
Serial.println("Hello World!");
//-----For CANBUS testing-----
Serial.println("Basic Demo - ESP32-Arduino-CAN");
CAN_cfg.speed = CAN_SPEED_200KBPS; //CAN_SPEED_100KBPS; //CAN_SPEED_250KBPS; //CAN_SPEED_200KBPS; //CAN_SPEED_125KBPS;
CAN_cfg.tx_pin_id = GPIO_NUM_18; //(gpio_num_t)CAN_TX_PIN; //(gpio_num_t)(18); // GPIO_NUM_18; //GPIO_NUM_19; //GPIO_NUM_5;
CAN_cfg.rx_pin_id = GPIO_NUM_5; // (gpio_num_t)CAN_RX_PIN; //(gpio_num_t)(5); // GPIO_NUM_5; //GPIO_NUM_18; //GPIO_NUM_4;
CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));
// Init CAN Module
ESP32Can.CANInit();
}
void loop() {
CAN_frame_t rx_frame;
unsigned long currentMillis = millis();
// Receive next CAN frame from queue
if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) {
if (rx_frame.FIR.B.FF == CAN_frame_std) {
printf("New standard frame");
}
else {
printf("New extended frame");
}
if (rx_frame.FIR.B.RTR == CAN_RTR) {
printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC);
}
else {
printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC);
for (int i = 0; i < rx_frame.FIR.B.DLC; i++) {
printf("0x%02X ", rx_frame.data.u8[i]);
}
printf("\n");
}
}
// Send CAN Message
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
CAN_frame_t tx_frame;
tx_frame.FIR.B.FF = CAN_frame_std;
tx_frame.MsgID = 0x058;
tx_frame.FIR.B.DLC = 8;
tx_frame.data.u8[0] = 0x00;
tx_frame.data.u8[1] = 0x01;
tx_frame.data.u8[2] = 0x02;
tx_frame.data.u8[3] = 0x03;
tx_frame.data.u8[4] = 0x04;
tx_frame.data.u8[5] = 0x05;
tx_frame.data.u8[6] = 0x06;
tx_frame.data.u8[7] = 0x07;
ESP32Can.CANWriteFrame(&tx_frame);
Serial.println("Send_CAN_Packet");
}
}
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 5 replies
-
@guoquan1963 The code you have provided doesn't appear to be using any APIs from Espressif but instead a third-party CAN library. I'd suggest talking with the author of the library.
Beta Was this translation helpful? Give feedback.
All reactions
-
There is no sample code for Arduino IDE. That is what I found on Web.
Can you tell me where I can find "TWAI_TIMING_CONFIG_250KBITS()" definition?
Beta Was this translation helpful? Give feedback.
All reactions
-
There may not be Arduino specific examples as nobody has submitted or requested it. There are ESP-IDF based examples which will work in Arduino IDE with minimal modifications (app_main code mostly goes into setup method).
https://github.com/espressif/esp-idf/tree/release/v4.4/examples/peripherals/twai
Can you tell me where I can find "TWAI_TIMING_CONFIG_250KBITS()" definition?
https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/esp32/include/driver/include/driver/twai.h
https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/esp32/include/hal/include/hal/twai_types.h#L77
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks, Mike.
I installed esp-idf-v4.4 for VS code. But, I can't these files, please let me know where I can find these?
How about Arduino IDE, where are these files are located?
Beta Was this translation helpful? Give feedback.
All reactions
-
I don't know for sure if these examples are included in VSCode or not, but they are available via the link above.
For Arduino IDE the linked include files are present under the directory where the esp32 core is installed to (typically $HOME/.arduino15/packages/esp32/hardware/esp32/{version}/tools/sdk/ ...
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks, Mike
Beta Was this translation helpful? Give feedback.
All reactions
-
Hello, you may want to see the TWAI (CAN) example: #7430
Beta Was this translation helpful? Give feedback.