sandeepmistry / arduino-CAN

An Arduino library for sending and receiving data using CAN bus.
MIT License
691 stars 236 forks source link

ESP32 actual baud rate is half of the configured #149

Open thefatmoop opened 1 month ago

thefatmoop commented 1 month ago

I haven't taken a dig into why, but curious if someone can shed some light on this.

Hardware: ESP32 using the built in CAN transceiver. Using external can PHY chip. Monitoring the TX line straight out of the esp32.

Measurements: scope - Setting the baud rate as 500kbaud on esp32 results in a single bit width of 250khz. salae logic analyzer - Setting the baud rate as 500kbaud on esp32, have to set salae logic to 250kbaud to decode packets.

conclusion It looks like whatever baud is set in can.begin(baud) gets halved. Am I doing something wrong?

#include <Arduino.h>
//https://github.com/sandeepmistry/arduino-CAN/tree/master
#include <CAN.h>

#define rx 26
#define tx 27

void setup() {
  Serial.begin(115200);

  Serial.println("CAN Sender");
  CAN.setPins (rx, tx);

  // start the CAN bus at 250 kbps
  if (!CAN.begin(500E3)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }

}

void loop() {
  // send packet: id is 11 bits, packet can contain up to 8 bytes of data
  Serial.print("Sending packet ... ");

  CAN.beginPacket(0x12);
  CAN.write('h');
  CAN.write('e');
  CAN.write('l');
  CAN.write('l');
  CAN.write('o');
  CAN.endPacket();
  Serial.println("done");
  delay(1000);

  // send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
  Serial.print("Sending extended packet ... ");

  CAN.beginExtendedPacket(0xabcdef);
  CAN.write('w');
  CAN.write('o');
  CAN.write('r');
  CAN.write('l');
  CAN.write('d');
  CAN.endPacket();
  Serial.println("done");
  delay(1000);
}
drnet-at commented 5 days ago

In V3 of the ESP32 , Espressif added a bit in the interrupt register (Bit 4, 0x10), that actually doesn't control an Interrupt, but adds a "prescale by 2" factor to allow lower bitrates with the ESP32 CAN controller.

In line 135, we now have

writeRegister(REG_IER, 0xff); // enable all interrupts

which sets the prescale bit, thus dividing bitrates by 2. Replacing this with

writeRegister(REG_IER, 0xef); // enable all interrupts

resolves the issue.