Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarifications on the operation of the code and changing pins #71

Open
brightproject opened this issue Nov 11, 2024 · 0 comments
Open

Comments

@brightproject
Copy link

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.

esp32_s3_can_once

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).

esp32_s3_can_miltiple

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant