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

ESP32 actual baud rate is half of the configured #149

Open
thefatmoop opened this issue Sep 19, 2024 · 1 comment
Open

ESP32 actual baud rate is half of the configured #149

thefatmoop opened this issue Sep 19, 2024 · 1 comment

Comments

@thefatmoop
Copy link

thefatmoop commented Sep 19, 2024

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);
}
@thefatmoop thefatmoop changed the title ESP32 actual baud rate is double of the configured ESP32 actual baud rate is half of the configured Sep 19, 2024
@drnet-at
Copy link

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.

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

2 participants