Moving CAN project to ESP32 - Need Help

luckhermsen
Posts: 2
Joined: Thu Feb 28, 2019 10:19 am

Moving CAN project to ESP32 - Need Help

Postby luckhermsen » Thu Feb 28, 2019 10:40 am

Hi everyone,

I've been working on a canbus project for a while now.
I started based on a blog from CuriousNinja (http://curious.ninja/project/bmw-e46/e4 ... ace-intro/)
I've got everything set up and running. Implemented many cool features. The next step I would love to take is move the project from the Arduino Nano to an ESP32 which will allow many more features such as control over WiFi.

The problem I'm facing is that my programming skills aren't good enough to get the code working on the ESP32.
It uses a library written by someone from the arduino forum. The library uses interrupt.h which doesn't work on the ESP32 because of the different chip architecture.

Currently I'm using a TH3122.4 transceiver but I've read there are numerous other chips.

What I'm looking for is a way to get the code working on an ESP32.
The main code I use reads and writes full hex messages passing over the bus. So for example:

Code: Select all

const byte LOCK_ALL_DOORS_2 [7] PROGMEM = {
  0x3F, 0x05, 0x00, 0x0C, 0x34, 0x01, 0x03 // Locks all doors
};
So I can check for messages by comparing the received byte array to a stored byte array.
Also I can send a message to the car by addressing a stored byte array.

would it be possible to do the same thing with an ESP32?
Or would I have to use something entirely different?

I would like to point out that this is a hobby project, but I'm also trying to make it possible to sell it as a product once it is finished.
It's for a specific older type of BMW so there's not a huge marktet but maybe I can sell a few.
Thus if anyone is willing to do more than just point me in the right direction and maybe even write some of the code that is needed, I could compensate it with a fair amount of money.

Really hoping that you guys can help me out so the project can become much more exiting with WiFi/Bluetooth functionality!
All help appreciated!

Cheers,
Luck

peterglen
Posts: 27
Joined: Thu Mar 22, 2018 5:55 pm

Re: Moving CAN project to ESP32 - Need Help

Postby peterglen » Sat Mar 02, 2019 7:42 pm

We put a CAN chip on our board, below, the tested working code to drive it:

static const char *TAG = "my_can";

#define RX_TASK_PRIO 8 //Receiving task priority
#define TX_TASK_PRIO 9 //Sending task priority

#define MSG_ID 0x555

#define TX_GPIO_NUM 21
#define RX_GPIO_NUM 22

static SemaphoreHandle_t tx_sem;

// Initialize configuration structures using macro initializers

//can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(
// GPIO_NUM_21, GPIO_NUM_22, CAN_MODE_NORMAL);

can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(
GPIO_NUM_21, GPIO_NUM_22, CAN_MODE_NO_ACK);

//can_timing_config_t t_config = CAN_TIMING_CONFIG_25KBITS();
//can_timing_config_t t_config = CAN_TIMING_CONFIG_50KBITS();
//can_timing_config_t t_config = CAN_TIMING_CONFIG_100KBITS();
//can_timing_config_t t_config = CAN_TIMING_CONFIG_125KBITS();
//can_timing_config_t t_config = CAN_TIMING_CONFIG_250KBITS();
//can_timing_config_t t_config = CAN_TIMING_CONFIG_500KBITS();
//can_timing_config_t t_config = CAN_TIMING_CONFIG_800KBITS();
can_timing_config_t t_config = CAN_TIMING_CONFIG_1MBITS();

can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL();
//can_filter_config_t f_config = {.acceptance_code = (MSG_ID << 21),
// .acceptance_mask = ~(CAN_STD_ID_MASK << 21),
// .single_filter = true};

static void canr_task(void *pvParameter)

{
// Set the random number generator
vTaskDelay(10 / portTICK_RATE_MS);
srand(esp_timer_get_time());

while(1)
{
// Wait for message to be received
can_message_t message;

int err = can_receive(&message, pdMS_TO_TICKS(10000));
if (err == ESP_OK) {
printf("Message received\n");
}
else
{
ESP_LOGE(TAG, "Failed to receive message %d (%s).",
err, esp_err_to_name(err));
continue;
}

// Process received message
if (message.flags & CAN_MSG_FLAG_EXTD) {
//printf("Message is in Extended Format\n");
}
else {
//printf("Message is in Standard Format\n");
}
printf("ID is %d\n", message.identifier);
if (!(message.flags & CAN_MSG_FLAG_RTR)) {
for (int i = 0; i < message.data_length_code; i++) {
printf("Data byte %d = %d\n", i, message.data);
}
}
vTaskDelay(500 / portTICK_RATE_MS);
}
vTaskDelete(NULL);
}


static void cans_task(void *pvParameter)

{
// Set the random number generator
vTaskDelay(10 / portTICK_RATE_MS);
srand(esp_timer_get_time());

GET_MAC(self_mac);

//Start CAN driver
if (can_start() == ESP_OK) {
printf("CAN Driver started\n");

#if 0
while (true)
{
can_status_info_t status_info; can_get_status_info(&status_info);
if(status_info.state == CAN_STATE_RUNNING)
break;
printf("Waiting for CAN port\n"); vTaskDelay(pdMS_TO_TICKS(100));
}
printf("CAN Driver started2\n");
#endif
}
else
{
ESP_LOGE(TAG, "Failed to start CAN driver.");
}
//can_message_t tx_msg =
// {.data_length_code = 1, .identifier = MSG_ID, .flags = CAN_MSG_FLAG_SELF};

vTaskDelay(500 / portTICK_RATE_MS);

//Configure message to transmit
can_message_t message;

while(1)
{
message.identifier = MSG_ID;
//message.flags = CAN_MSG_FLAG_EXTD;
//message.flags = CAN_MSG_FLAG_SELF;
message.flags = CAN_MSG_FLAG_NONE;
message.data_length_code = 6;

//for (int i = 0; i < message.data_length_code; i++) {
// message.data = (char)rand();
// }

memcpy(message.data, self_mac, sizeof(self_mac));

// Wait for CAN to complete
can_status_info_t status_info;
can_get_status_info(&status_info);
while (status_info.msgs_to_tx >= g_config.tx_queue_len) {
vTaskDelay(pdMS_TO_TICKS(100));
can_get_status_info(&status_info);
}
//Queue message for transmission
if (can_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
printf("Message queued for transmission\n");
//vTaskDelay(pdMS_TO_TICKS(10));
}
else
{
ESP_LOGE(TAG, "Failed to queue message for transmission.");
}
vTaskDelay(500 / portTICK_RATE_MS);
}

vTaskDelete(NULL);
}

//////////////////////////////////////////////////////////////////////////
//

void init_can()

{
tx_sem = xSemaphoreCreateBinary();

//Install CAN driver
if (can_driver_install(&g_config, &t_config, &f_config) == ESP_OK)
{
printf("CAN Driver installed\n");
}
else
{
printf("Failed to install CAN driver.\n");
}

// (Dumb) waiting for port initialize
vTaskDelay(500 / portTICK_RATE_MS);

xTaskCreatePinnedToCore(cans_task, "CAN_tx", 4096, NULL, TX_TASK_PRIO, NULL, tskNO_AFFINITY);
xTaskCreatePinnedToCore(canr_task, "CAN_rx", 4096, NULL, RX_TASK_PRIO, NULL, tskNO_AFFINITY);
}

// EOF

luckhermsen
Posts: 2
Joined: Thu Feb 28, 2019 10:19 am

Re: Moving CAN project to ESP32 - Need Help

Postby luckhermsen » Sun Mar 03, 2019 2:45 pm

Thanks, I'll definitely test this out!
What transceiver did you use?

Who is online

Users browsing this forum: No registered users and 102 guests