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

Properly reset the module #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

danielkucera
Copy link

It looks like the module is much more stable when reset bit is first set and then cleared.
Fixes #36

It looks like the module is much more stable when reset bit is first set and then cleared.
Fixes #36
@hicham-vv
Copy link

hicham-vv commented Feb 28, 2024

@miwagner
@danielkucera hello daniel , had the same issue, thank for your help ,
two days trying to find the issue, i knew there was something with some registers initialisation , because after hardreset the system work properly, and with soft reset the system still not working
thanks again

@danielkucera
Copy link
Author

Hello @hicham-vv ,
you are welcome.
But this is almost 2 years old so nowadays I would recommend you to use twai functions from esp-idf.

@hicham-vv
Copy link

hello @danielkucera i am already working with this arduino library with an exesting,
i will try to migrate it to the TWAI library when i have time to that,

can you suggest a arduino library that support TWAI ?

thank you again

@danielkucera
Copy link
Author

It should just work when you include driver/twai.h from arduino:
https://github.com/danielkucera/esp32-can/blob/epb/src/can-idf.cpp.inc

@hicham-vv
Copy link

It should just work when you include driver/twai.h from arduino: https://github.com/danielkucera/esp32-can/blob/epb/src/can-idf.cpp.inc

hello @danielkucera i worked with the TWAI library,
i am facing an issue when i am trying to sniff DATA CAN from my vehicule ,

i only receive 1 ID, but this library's miwagner i can sniff many IDs
i think there is a bug in TWAI library , did you ever worked with it ??

miwagner output:

0x0CF00400, 8, 0x00 0x7D 0x7D 0x00 0x00 0xFF 0xF0 0xFF
0x0CFE5A2F, 8, 0x0F 0x33 0x33 0x0F 0x3F 0xF7 0xFC 0xF0
0x0CF00300, 8, 0xF1 0x00 0x00 0xFF 0xFF 0xFF 0x00 0xFF
0x0CFE6CEE, 8, 0x00 0xFF 0xFF 0xC0 0x00 0x00 0x00 0x00
0x18FDC40B, 8, 0x1F 0x00 0x31 0xF1 0xFF 0xFF 0xFF 0xFF
0x18FEC4C8, 8, 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0x18FEBF0B, 8, 0x00 0x00 0x7D 0x7D 0x7D 0x7D 0xFF 0xFF
0x0CF00400, 8, 0x00 0x7D 0x7D 0x00 0x00 0xFF 0xF0 0xFF
0x0CF00203, 8, 0xFC 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF
0x18FEBF0B, 8, 0x00 0x00 0x7D 0x7D 0x7D 0x7D 0xFF 0xFF
0x0CF00203, 8, 0xFC 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF
0x0CF00400, 8, 0x00 0x7D 0x7D 0x00 0x00 0xFF 0xF0 0xFF
0x0CF00203, 8, 0xFC 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF
0x18FEBF0B, 8, 0x00 0x00 0x7D 0x7D 0x7D 0x7D 0xFF 0xFF
0x0CF00203, 8, 0xFC 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF
0x0CF00400, 8, 0x00 0x7D 0x7D 0x00 0x00 0xFF 0xF0 0xFF
0x0CF00203, 8, 0xFC 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF

but with the TWAI, i got only that

Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff
Message is in Extended Format ID: cf00203 Byte: fc 00 00 ff ff ff ff ff

@danielkucera
Copy link
Author

share your code

@hicham-vv
Copy link

hicham-vv commented Mar 13, 2024

This one for miwagner

CAN_device_t CAN_cfg;               // CAN Config
unsigned long previousMillis = 0;   // will store last time a CAN Message was send
const int interval = 1000;          // interval at which send CAN Messages (milliseconds)
const int rx_queue_size = 5;       // Receive Queue size

void setup() {
  Serial.begin(115200);
  while(!Serial);
  delay(1500);
  Serial.println("Basic Demo - ESP32-Arduino-CAN");
  #ifdef FUSOCANTER
  CAN_cfg.speed = CAN_SPEED_500KBPS;
  #endif
  #ifndef FUSOCANTER
  CAN_cfg.speed = CAN_SPEED_250KBPS;
  #endif
  CAN_cfg.tx_pin_id = GPIO_NUM_18;
  CAN_cfg.rx_pin_id = GPIO_NUM_19;
  CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));
  // Init CAN Module
  bool a = ESP32Can.CANInit();
  Serial.println(a);

}

void loop() {

  CAN_frame_t rx_frame;

  unsigned long currentMillis = millis();

  // Receive next CAN frame from queue
  if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) {

    // if (rx_frame.FIR.B.FF == CAN_frame_std) {
    //   printf("Standard:");
    // }
    // else {
    //   printf("Extended:");
    // }

    if (rx_frame.FIR.B.RTR == CAN_RTR) {
      printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID,  rx_frame.FIR.B.DLC);
    }
    else {
        printf("0x%08X, %d,  ", rx_frame.MsgID,  rx_frame.FIR.B.DLC);
        for (int i = 0; i < rx_frame.FIR.B.DLC; i++) {
          printf("0x%02X ", rx_frame.data.u8[i]);
        }

      printf("\n");
    }
  }
}

This one for TWAI receiver

/* ESP32 TWAI receive example.
  Receive messages and sends them over serial.

  Connect a CAN bus transceiver to the RX/TX pins.
  For example: SN65HVD230

  TWAI_MODE_LISTEN_ONLY is used so that the TWAI controller will not influence the bus.

  The API gives other possible speeds and alerts:
  https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/twai.html

  Example output from a can bus message:
  -> Message received
  -> Message is in Standard Format
  -> ID: 604
  -> Byte: 0 = 00, 1 = 0f, 2 = 13, 3 = 02, 4 = 00, 5 = 00, 6 = 08, 7 = 00

  Example output with alerts:
  -> Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.
  -> Bus error count: 171
  -> Alert: The RX queue is full causing a received frame to be lost.
  -> RX buffered: 4  RX missed: 46 RX overrun 0

  created 05-11-2022 by Stephan Martin (designer2k2)
*/

#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#include "driver/twai.h"

// Pins used to connect to CAN bus transceiver:
#define RX_PIN 19
#define TX_PIN 18

// Intervall:
#define POLLING_RATE_MS 1000

static bool driver_installed = false;

void setup() {
  // Start Serial:
  Serial.begin(115200);

  // Initialize configuration structures using macro initializers
  twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_LISTEN_ONLY);
  twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS();  //Look in the api-reference for other speed sets.
  twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

  // Install TWAI driver
  if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
    Serial.println("Driver installed");
  } else {
    Serial.println("Failed to install driver");
    return;
  }

  // Start TWAI driver
  if (twai_start() == ESP_OK) {
    Serial.println("Driver started");
  } else {
    Serial.println("Failed to start driver");
    return;
  }

  // Reconfigure alerts to detect frame receive, Bus-Off error and RX queue full states
  uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_RX_QUEUE_FULL;
  if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK) {
    Serial.println("CAN Alerts reconfigured");
  } else {
    Serial.println("Failed to reconfigure alerts");
    return;
  }

  // TWAI driver is now successfully installed and started
  driver_installed = true;
}

static void handle_rx_message(twai_message_t& message) {
  // Process received message
  if (message.extd) {
    Serial.println("Message is in Extended Format");
  } else {
    Serial.println("Message is in Standard Format");
  }
  Serial.printf("ID: %lx\nByte:", message.identifier);
  if (!(message.rtr)) {
    for (int i = 0; i < message.data_length_code; i++) {
      Serial.printf(" %d = %02x,", i, message.data[i]);
    }
    Serial.println("");
  }
}

void loop() {
  if (!driver_installed) {
    // Driver not installed
    delay(1000);
    return;
  }
  // Check if alert happened
  uint32_t alerts_triggered;
  twai_read_alerts(&alerts_triggered, pdMS_TO_TICKS(POLLING_RATE_MS));
  twai_status_info_t twaistatus;
  twai_get_status_info(&twaistatus);

  // Handle alerts
  if (alerts_triggered & TWAI_ALERT_ERR_PASS) {
    Serial.println("Alert: TWAI controller has become error passive.");
  }
  if (alerts_triggered & TWAI_ALERT_BUS_ERROR) {
    Serial.println("Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.");
    Serial.printf("Bus error count: %lu\n", twaistatus.bus_error_count);
  }
  if (alerts_triggered & TWAI_ALERT_RX_QUEUE_FULL) {
    Serial.println("Alert: The RX queue is full causing a received frame to be lost.");
    Serial.printf("RX buffered: %lu\t", twaistatus.msgs_to_rx);
    Serial.printf("RX missed: %lu\t", twaistatus.rx_missed_count);
    Serial.printf("RX overrun %lu\n", twaistatus.rx_overrun_count);
  }

  // Check if message is received
  if (alerts_triggered & TWAI_ALERT_RX_DATA) {
    // One or more messages received. Handle all.
    twai_message_t message;
    while (twai_receive(&message, 0) == ESP_OK) {
      handle_rx_message(message);
    }
  }
}

@hicham-vv
Copy link

@danielkucera i found it , i just need to switch to mode NORMAL, thanks

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

Successfully merging this pull request may close these issues.

2 participants