8000 Dual-TWAI only working with one bus at a time · Issue #80 · collin80/esp32_can · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Dual-TWAI only working with one bus at a time #80
Open
@TechOverflow

Description

@TechOverflow

Hi Collin,
I've been collaborating with Outlandnish who did a great job updating to the newer _V2 IDF features, however there's some underlying issue when both buses are used at the same time.

Here's my setup. The ESP32-C6 gateway should simply forward any message from one bus to the other.

Image

Code:

#include <Arduino.h>
#include <SmartLeds.h>
#include <esp32_can.h>

const int LED_COUNT = 1;
const int DATA_PIN = 8;
const int CHANNEL = 0;

SmartLed leds(LED_WS2812B, LED_COUNT, DATA_PIN, CHANNEL, DoubleBuffer);

#define CAN0_SHUTDOWN 3
#define CAN0_STANDBY 0

#define CAN1_SHUTDOWN 2
#define CAN1_STANDBY 1

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

  // setup status LED
  leds[0] = Rgb { 255, 0, 0 };
  leds.show();
  leds.wait();

  // setup CAN pins
  pinMode(CAN0_SHUTDOWN, OUTPUT);
  pinMode(CAN0_STANDBY, OUTPUT);

  // enable CAN0 transceiver
  digitalWrite(CAN0_SHUTDOWN, LOW);
  digitalWrite(CAN0_STANDBY, LOW);

  pinMode(CAN1_SHUTDOWN, OUTPUT);
  pinMode(CAN1_STANDBY, OUTPUT);

  // enable CAN1 transceiver
  digitalWrite(CAN1_SHUTDOWN, LOW);
  digitalWrite(CAN1_STANDBY, LOW);

  // setup CAN buses
  CAN0.setCANPins(GPIO_NUM_16, GPIO_NUM_17);
  CAN0.begin(500000);
  CAN0.watchFor();
  CAN1.setCANPins(GPIO_NUM_18, GPIO_NUM_19);
  CAN1.begin(500000);
  CAN1.watchFor();
}

auto lastCan0Frame = 0;
auto lastCan1Frame = 0;

void loop() {
  // check for can messages on each bus
  CAN_FRAME frame;
  
  if (CAN0.available() && CAN0.read(frame)) {
    // print out bus and frame
    printFrame(&frame, 0);
    CAN1.sendFrame(frame);
    Serial.println("Frame sent from CAN0 to CAN1");
    lastCan0Frame = millis();
  }

  if (CAN1.available() && CAN1.read(frame)) {
    // print out bus and frame
    printFrame(&frame, 1);
    CAN0.sendFrame(frame);
    Serial.println("Frame sent from CAN1 to CAN0");
    lastCan1Frame = millis();
  }

  // show green led when receiving CAN0 and blue led when receiving CAN1
  auto now = millis();
  bool showGreen = now - lastCan0Frame < 1000;
  bool showBlue = now - lastCan1Frame < 1000;

  leds[0] = Rgb { showGreen || showGreen ? 0 : 5, showGreen ? 5 : 0, showBlue ? 5 : 0 };
  leds.show();
  leds.wait();
}

void printFrame(CAN_FRAME *message, int busNum) {
  Serial.printf("CAN %d - ID: %08X, DLC: %d, Data: %02X %02X %02X %02X %02X %02X %02X %02X\n",
    busNum,
    message->id, 
    message->length, 
    message->data.byte[0], 
    message->data.byte[1], 
    message->data.byte[2], 
    message->data.byte[3], 
    message->data.byte[4], 
    message->data.byte[5], 
    message->data.byte[6], 
    message->data.byte[7]
  );
}

And the output:

CAN 1 - ID: 000007DF, DLC: 8, Data: 02 01 00 00 00 00 00 00
Frame sent from CAN1 to CAN0
CAN 1 - ID: 18DB33F1, DLC: 8, Data: 02 01 00 00 00 00 00 00
Frame sent from CAN1 to CAN0
CAN 0 - ID: 000007DF, DLC: 8, Data: 02 01 00 00 00 00 00 00
Frame sent from CAN0 to CAN1
CAN 0 - ID: 18DB33F1, DLC: 8, Data: 02 01 00 00 00 00 00 00
Frame sent from CAN0 to CAN1
CAN 0 - ID: 000007DF, DLC: 8, Data: 02 01 00 00 00 00 00 00
Frame sent from CAN0 to CAN1
CAN 0 - ID: 000007DF, DLC: 8, Data: 02 01 00 00 00 00 00 00
Frame sent from CAN0 to CAN1
CAN 0 - ID: 000007DF, DLC: 8, Data: 03 22 F8 10 00 00 00 00
Frame sent from CAN0 to CAN1
CAN 1 - ID: 000007DF, DLC: 8, Data: 03 22 F8 10 00 00 00 00
Frame sent from CAN1 to CAN0
CAN 1 - ID: 18DB33F1, DLC: 8, Data: 02 01 00 00 00 00 00 00
Frame sent from CAN1 to CAN0
CAN 0 - ID: 18DB33F1, DLC: 8, Data: 02 01 00 00 00 00 00 00
Frame sent from CAN0 to CAN1
CAN 0 - ID: 18DB33F1, DLC: 8, Data: 03 22 F8 10 00 00 00 00
Frame sent from CAN0 to CAN1
CAN 1 - ID: 18DB33F1, DLC: 8, Data: 03 22 F8 10 00 00 00 00
Frame sent from CAN1 to CAN0

The messages in the serial output that say they are on CAN0 (and then sent from CAN0 to CAN1) definitely aren't. Both 7DF and 18DB33F1 are request IDs coming from the OBD reader, trying to establish communication. The ECU sim should send a 7E8 message as a reply to 7DF.
When it says "Frame sent from CAN1 to CAN0", there is no actual traffic on CAN0 (verified with scope).

Is there anything you can identify in the dual-TWAI updates as a potential bug or have any advice on what the try out?
Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0