Ive tried a bunch of different things, but cant get it to compile...
![Confused :?](./images/smilies/icon_e_confused.gif)
Code: Select all
static const can_timing_config_t t_config = CAN_TIMING_CONFIG_25KBITS();
//Filter all other IDs except MSG_ID
static const can_filter_config_t f_config = {.acceptance_code = (uint32_t)(MSG_ID << 21),
.acceptance_mask = (uint32_t)~(CAN_STD_ID_MASK << 21),
.single_filter = true};
//Set to NO_ACK mode due to self testing with single module
static const can_general_config_t g_config = {.mode = CAN_MODE_NO_ACK,
.tx_io = (gpio_num_t)TX_GPIO_NUM,
.rx_io = (gpio_num_t)RX_GPIO_NUM,
.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
};
static SemaphoreHandle_t tx_sem;
static SemaphoreHandle_t rx_sem;
static SemaphoreHandle_t ctrl_sem;
static SemaphoreHandle_t done_sem;
/* --------------------------- Tasks and Functions -------------------------- */
static void can_transmit_task(void *arg)
{
can_message_t tx_msg = {.flags = CAN_MSG_FLAG_SELF, .identifier = MSG_ID, .data_length_code = 1};
Could explain what steps you have taken to port the driver over to Arduino, and also the compilation error log you are seeing.dmaxben wrote: Ive tried a bunch of different things, but cant get it to compile
The miwagner arduino driver and Thomas Barthe's arduino ESP-32 CAN driver are significantly different than the current ESP-IDF CAN driver. They are *NOT* direct ports.MStackoverflow wrote: ↑Tue Oct 08, 2019 3:26 pmHave you seen this library from miwagner?
https://github.com/miwagner/ESP32-Arduino-CAN
It is a port from the official CAN driver, it work as expected.
Code: Select all
#include <driver/can.h>
#include driver/gpio.h>
#include <esp_system.h>
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
// define your CAN messages here, OR you can define them locally...
// standard 11-bit frame ID = 0
// extended 29-bit frame ID = 1
// format: can_message_t (name of your message) = {std/ext frame, message ID, message DLC, {data bytes here}};
can_message_t myMessageToSend = {0, 0x123, 8, {0x01, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x99}};
uint32_t previousMillis;
const uint32_t interval = 1000;
//=================================================
void setup()
{
// anything else you might need to do....
setup_can_driver();
}
void setup_can_driver()
{
can_general_config_t general_config = {
.mode = CAN_MODE_NORMAL,
.tx_io = (gpio_num_t)GPIO_NUM_5,
.rx_io = (gpio_num_t)GPIO_NUM_4,
.clkout_io = (gpio_num_t)CAN_IO_UNUSED,
.bus_off_io = (gpio_num_t)CAN_IO_UNUSED,
.tx_queue_len = 100,
.rx_queue_len = 65,
.alerts_enabled = CAN_ALERT_NONE,
.clkout_divider = 0};
can_timing_config_t timing_config = CAN_TIMING_CONFIG_500KBITS();
can_filter_config_t filter_config = CAN_FILTER_CONFIG_ACCEPT_ALL();
esp_err_t error;
error = can_driver_install(&general_config, &timing_config, &filter_config);
if (error == ESP_OK)
{
Serial.println("CAN Driver installation success...");
}
else
{
Serial.println("CAN Driver installation fail...");
return;
}
// start CAN driver
error = can_start();
if (error == ESP_OK)
{
Serial.println("CAN Driver start success...");
}
else
{
Serial.println("CAN Driver start FAILED...");
return;
}
}
//=======================================================================
void loop()
{
can_message_t rx_frame;
if (can_receive(&rx_frame, pdMS_TO_TICKS(1000)) == ESP_OK)
{
//do whatever you need with your received CAN messages here
// follow the can_message_t struct to learn how to decode/process the received frame.
}
if (millis() - previousMillis > interval) // send out your CAN frame once every second
{
previousMillis = millis();
can_transmit(&myMessageToSend, pdMS_TO_TICKS(1000));
}
}
Code: Select all
can_message_t my_message = {
.identifier = 0xABCD,
.data_length_code = 8,
.flags = CAN_MSG_FLAG_EXTD | CAN_MSG_FLAG_SS,
.data = {1, 2 , 3 , 4 ,5 ,6 ,7 ,8}
};
I agree on using the actual member names when defining the messsages...however my project has hundreds of CAN messages, and I didnt want to type everything out by hand, making my program hundreds of lines longer haha.ESP_Dazz wrote: ↑Wed Oct 09, 2019 4:44 pm@ dmaxben Just a few pointers regarding IDF CAN driver usage
It's probably a good idea to use the member names when initializing a CAN message structure in case the order of the members change in the future versions of the driver. There are also a bunch of message flags documented here to set/check to a certain type (e.g., extended ID, RTR etc).The CAN driver was designed to be used in a multi-threaded manner, so general practice is to split receiving and transmitting into separate tasks (threads). This way, the RX queue can be emptied and the TX queue filled simultaneously by the two threads, and you won't need such a large TX and RX queue. Default queue length setting by the driver is 5, a length of 100 seems abnormally long.Code: Select all
can_message_t my_message = { .identifier = 0xABCD, .data_length_code = 8, .flags = CAN_MSG_FLAG_EXTD | CAN_MSG_FLAG_SS, .data = {1, 2 , 3 , 4 ,5 ,6 ,7 ,8} };
You should still be able to directly call FreeRTOS functions and types to create tasks in Arduino. I suggest you take a look at the Self-Test example for a start. You should be able to easily adapt that to Arduino. In theory, it should work by just copying the code, renaming app_main() to setup(), and leaving loop() empty.
dmaxben wrote: ↑Wed Oct 09, 2019 3:41 pmAlso, as a update, I was able to get the official ESP-IDF CAN driver working perfectly in Arduino
This is just basic stuff to get anyone started.
Code: Select all
#include <driver/can.h> #include driver/gpio.h> #include <esp_system.h> .... [/quote] As you, I need to keep Arduino with my ESP32 project with CAN bus. I tried your basic exemple (Serial.begin(115200); must be added in Setup()), but it does not work. My CAN sniffer does not received anything, nor the ESP32. What CAN interface did you used? SN65hvd2xx? See bellow, it seems having issue with SN65hvd230 while 232 works perfectly. https://esp32.com/viewtopic.php?t=380&start=170 Bad luck for me, I do have a VP230. This may be the problem :-( Don't you a full running exdemple please?
Users browsing this forum: No registered users and 30 guests