Description
Good day and thanks for the repository @collin80 😄
At the very beginning I wanted to ask, why does the code use the can_common.h
library if CAN
bus is based on TWAI
esp32?
I use the ESP32-S3
microcontroller + module SN65HVD230
The config from the .pio
is as follows:
[env:ESP32_S3_BOARD]
platform =
espressif32
board = esp32-s3-devkitc-1-n16r8
framework = arduino
I have a custom board that I developed and there the MCU
controls the encoder and display.
Of the free pins, only 17
and 18
are UART2 (PX2/TX2)
by default
The file esp32_can.cpp
contains the default code.
From the code example
it is clear that pin 16
needs to be pull to ground and then other contacts reassigned.
My code:
#include <esp32_can.h>
void setup() {
Serial0.begin(115200);
Serial0.println("Initializing");
pinMode(GPIO_NUM_16, OUTPUT);
digitalWrite(GPIO_NUM_16, LOW); //enable CAN transceiver
CAN0.setCANPins(GPIO_NUM_17, GPIO_NUM_18);
CAN0.begin(500000);
Serial0.println("Ready");
CAN_FRAME txFrame;
txFrame.rtr = 0;
txFrame.id = 0x123;
txFrame.extended = false;
txFrame.length = 5;
txFrame.data.uint8[0] = 0x1A;
txFrame.data.uint8[1] = 0x2B;
txFrame.data.uint8[2] = 0x3C;
txFrame.data.uint8[4] = 0x4D;
txFrame.data.uint8[5] = 0x5E;
CAN0.sendFrame(txFrame);
}
void loop() {
}
This code is executed once and at the end of the Error
frame.
If I change the pins in the esp32_can.cpp
file forcibly to
ESP32CAN attribute((weak)) CAN0(GPIO_NUM_17, GPIO_NUM_18) ;
Or I write them in the main code in the SETUP
section
void setup() {
Serial0.begin(115200);
Serial0.println("Initializing");
CAN0.setCANPins(GPIO_NUM_17, GPIO_NUM_18);
CAN0.begin(500000);
Serial0.println("Ready");
CAN_FRAME txFrame;
txFrame.rtr = 0;
txFrame.id = 0x123;
txFrame.extended = false;
txFrame.length = 5;
txFrame.data.uint8[0] = 0x1A;
txFrame.data.uint8[1] = 0x2B;
txFrame.data.uint8[2] = 0x3C;
txFrame.data.uint8[4] = 0x4D;
txFrame.data.uint8[5] = 0x5E;
CAN0.sendFrame(txFrame);
}
then the frames will start to be sent continuously with breaks between sendings of 54 us
and each frame ends with a NAK
(didn't fit in the screenshot).
What am I doing wrong or right?
Why does the pin reassignment example recommend doing it this way, and sending only once
pinMode(GPIO_NUM_16, OUTPUT);
digitalWrite(GPIO_NUM_16, LOW); //enable CAN transceiver
CAN0.setCANPins(GPIO_NUM_17, GPIO_NUM_18);
But my code worked like this and sending is continuous
CAN0.setCANPins(GPIO_NUM_17,
GPIO_NUM_18);
I would like to understand this issue.