Having trouble with Can Bus

orbitcoms
Posts: 149
Joined: Fri Aug 03, 2018 10:08 pm
Location: Sydney, Australia

Having trouble with Can Bus

Postby orbitcoms » Mon Oct 12, 2020 12:53 am

I am trying to get the Can Bus running on ESP32 (WROOM).
The documentation described twai driver but I could not find this, only "/drivers?can.h" So I used the Can driver and followed example code in documentation on ESP-IDF site.
The can appears to configure and load without error but when attempting to transmit I get error 259 (0x0103) and connect understand what this error is. My code is below (I am using idf.py inside vscode). TX pin is GPIO 26 and RX pin is GPIO27.

Thanks in advance for any help. This is first attempt at using CANBUS

Code: Select all

void ConfigCanBus(void)
{
  can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(GPIO_NUM_26,GPIO_NUM_27,CAN_MODE_NORMAL);
  can_timing_config_t t_config = CAN_TIMING_CONFIG_500KBITS();
  can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL();

  if(can_driver_install(&g_config, &t_config, &f_config) == ESP_OK)
  {
    printf("Can Bus driver installed ok\n");
  }
  else
  {
    printf("Failed to install CanBus Drive \n");
  }

  if(can_start() == ESP_OK)
  {
    printf("CanBus driver started ok\n");
  }
  else
  {
      printf("CanBus driver failed to start\n");
  }
}

  void SendCanBusMessage(void)
  {
    can_message_t message;
    message.flags = CAN_MSG_FLAG_NONE;
    message.identifier = 0xAA;  //std fram = 2 byte ID, extended = 4 byte ID
    message.data_length_code = 4;
    for(int i = 0; i < 4; i++)
    {
      message.data[i] = i;
    }
    
    esp_err_t result = can_transmit(&message,pdMS_TO_TICKS(1000));
    if(result == ESP_OK)
    {
      printf("CanBus message sent\n");
    }
    else
    {
        printf("CanBus failed to send, error code %d  \n",result);
    }
  }

ESP_Sprite
Posts: 9619
Joined: Thu Nov 26, 2015 4:08 am

Re: Having trouble with Can Bus

Postby ESP_Sprite » Tue Oct 13, 2020 3:53 am

Wrt the first part of your post: CAN was renamed to TWAI at some point, so you're using the correct driver. I don't know too much about CAN itself, but do you actually have a CAN transceiver on these pins? From memory, the driver doesn't work unless you have.

orbitcoms
Posts: 149
Joined: Fri Aug 03, 2018 10:08 pm
Location: Sydney, Australia

Re: Having trouble with Can Bus

Postby orbitcoms » Tue Oct 13, 2020 4:21 am

Thanks for your reply.

I found a hardware problem. Initially I was going to place TCAN337 on board and this has pull-down on the RX_Enable pin but I fitted MAX3051 and it does not have that feature so it was in diel mode. I grounded the pin and it is working.

My components/driver folder only has can driver and not TWAI, is TWAI the latest driver and can I get it from github somewhere?

ESP_Sprite
Posts: 9619
Joined: Thu Nov 26, 2015 4:08 am

Re: Having trouble with Can Bus

Postby ESP_Sprite » Wed Oct 14, 2020 2:06 am

It depends on the ESP-IDF version you use. Current master uses TWAI, if you use a release version it probably still is CAN.

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 64 guests