Compiler thinks I'm giving an int to the CAN_GENERAL_CONFIG_DEFAULT macro

jrveale
Posts: 9
Joined: Mon Apr 01, 2019 9:30 am

Compiler thinks I'm giving an int to the CAN_GENERAL_CONFIG_DEFAULT macro

Postby jrveale » Mon Jan 18, 2021 10:36 am

I've used CAN in the ESP-IDF succesfully and am currently porting my CAN tester program for use in the ESP32 Arduino environment.

I'm having issues with the CAN_GENERAL_CONFIG_DEFAULT() macro that don't make any sense to me. See the below function (which simply wraps up a default can_driver_install().

Code: Select all

esp_err_t InstallCAN(gpio_num_t canTxPin, gpio_num_t canRxPin, can_mode_t canMode)
{
  // Initialise config structures using macro inits
  can_general_config_t gConfig = CAN_GENERAL_CONFIG_DEFAULT(canTxPin, canRxPin, canMode);
  can_timing_config_t tConfig = CAN_TIMING_CONFIG_25KBITS();
  can_filter_config_t fConfig = CAN_FILTER_CONFIG_ACCEPT_ALL();

  // Install CAN driver and return error code
  return can_driver_install(&gConfig, &tConfig, &fConfig);
}
My compiler (I'm using PlatformIO) takes issue with the config macro, saying " a value of type "int" cannot be used to initialize an entity of type "gpio_num_t" ". The function ensures that I could only possibly be passing gpio_num_ts rather than ints.

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: Compiler thinks I'm giving an int to the CAN_GENERAL_CONFIG_DEFAULT macro

Postby chegewara » Mon Jan 18, 2021 11:56 am

Its the difference between C and C++. You have to cast it with:

Code: Select all

(int)canTxPin, (int) canRxPin

jrveale
Posts: 9
Joined: Mon Apr 01, 2019 9:30 am

Re: Compiler thinks I'm giving an int to the CAN_GENERAL_CONFIG_DEFAULT macro

Postby jrveale » Mon Jan 18, 2021 3:33 pm

Solution: Expanded the macro and found the issue is that CAN_IO_UNUSED is defined as an int rather than a gpio_num_t. Replaced the macro with the expanded form, and casting CAN_IO_UNUSED to gpio_num_t.

So

Code: Select all

can_general_config_t gConfig = CAN_GENERAL_CONFIG_DEFAULT(canTxPin, canRxPin, canMode);
is replaced by

Code: Select all

can_general_config_t gConfig = {
      .mode = canMode,
      .tx_io = canTxPin,
      .rx_io = canRxPin,
      .clkout_io = (gpio_num_t)CAN_IO_UNUSED,
      .bus_off_io = (gpio_num_t)CAN_IO_UNUSED,
      .tx_queue_len = 5,
      .rx_queue_len = 5,
      .alerts_enabled = CAN_ALERT_NONE,
      .clkout_divider = 0,
  };

jrveale
Posts: 9
Joined: Mon Apr 01, 2019 9:30 am

Re: Compiler thinks I'm giving an int to the CAN_GENERAL_CONFIG_DEFAULT macro

Postby jrveale » Mon Jan 18, 2021 3:37 pm

chegewara wrote:
Mon Jan 18, 2021 11:56 am
Its the difference between C and C++. You have to cast it with:

Code: Select all

(int)canTxPin, (int) canRxPin
Thanks chegewara, but the macro is expecting gpio_num_t not int.

The issue was due to C vs C++ differences but was hidden within the macro and had nothing to do with my inputs. CAN_IO_UNUSED is defined as an int rather than a gpio_num_t - turns out this has already been reported. https://github.com/espressif/esp-idf/issues/2825

Who is online

Users browsing this forum: No registered users and 89 guests