rename esp32 CAN to TWAI, so it compiles again (#4334)

fixes https://github.com/esphome/issues/issues/4023
This commit is contained in:
Stephan Martin 2023-01-26 00:08:55 +01:00 committed by Jesse Hills
parent 2d0a08442e
commit aa7f3569ec
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A

View file

@ -2,7 +2,7 @@
#include "esp32_can.h" #include "esp32_can.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include <driver/can.h> #include <driver/twai.h>
// WORKAROUND, because CAN_IO_UNUSED is just defined as (-1) in this version // WORKAROUND, because CAN_IO_UNUSED is just defined as (-1) in this version
// of the framework which does not work with -fpermissive // of the framework which does not work with -fpermissive
@ -14,25 +14,25 @@ namespace esp32_can {
static const char *const TAG = "esp32_can"; static const char *const TAG = "esp32_can";
static bool get_bitrate(canbus::CanSpeed bitrate, can_timing_config_t *t_config) { static bool get_bitrate(canbus::CanSpeed bitrate, twai_timing_config_t *t_config) {
switch (bitrate) { switch (bitrate) {
case canbus::CAN_50KBPS: case canbus::CAN_50KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_50KBITS(); *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_50KBITS();
return true; return true;
case canbus::CAN_100KBPS: case canbus::CAN_100KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_100KBITS(); *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_100KBITS();
return true; return true;
case canbus::CAN_125KBPS: case canbus::CAN_125KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_125KBITS(); *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_125KBITS();
return true; return true;
case canbus::CAN_250KBPS: case canbus::CAN_250KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_250KBITS(); *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_250KBITS();
return true; return true;
case canbus::CAN_500KBPS: case canbus::CAN_500KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_500KBITS(); *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_500KBITS();
return true; return true;
case canbus::CAN_1000KBPS: case canbus::CAN_1000KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_1MBITS(); *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_1MBITS();
return true; return true;
default: default:
return false; return false;
@ -40,10 +40,10 @@ static bool get_bitrate(canbus::CanSpeed bitrate, can_timing_config_t *t_config)
} }
bool ESP32Can::setup_internal() { bool ESP32Can::setup_internal() {
can_general_config_t g_config = twai_general_config_t g_config =
CAN_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, CAN_MODE_NORMAL); TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, TWAI_MODE_NORMAL);
can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL(); twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
can_timing_config_t t_config; twai_timing_config_t t_config;
if (!get_bitrate(this->bit_rate_, &t_config)) { if (!get_bitrate(this->bit_rate_, &t_config)) {
// invalid bit rate // invalid bit rate
@ -51,15 +51,15 @@ bool ESP32Can::setup_internal() {
return false; return false;
} }
// Install CAN driver // Install TWAI driver
if (can_driver_install(&g_config, &t_config, &f_config) != ESP_OK) { if (twai_driver_install(&g_config, &t_config, &f_config) != ESP_OK) {
// Failed to install driver // Failed to install driver
this->mark_failed(); this->mark_failed();
return false; return false;
} }
// Start CAN driver // Start TWAI driver
if (can_start() != ESP_OK) { if (twai_start() != ESP_OK) {
// Failed to start driver // Failed to start driver
this->mark_failed(); this->mark_failed();
return false; return false;
@ -72,15 +72,15 @@ canbus::Error ESP32Can::send_message(struct canbus::CanFrame *frame) {
return canbus::ERROR_FAILTX; return canbus::ERROR_FAILTX;
} }
uint32_t flags = CAN_MSG_FLAG_NONE; uint32_t flags = TWAI_MSG_FLAG_NONE;
if (frame->use_extended_id) { if (frame->use_extended_id) {
flags |= CAN_MSG_FLAG_EXTD; flags |= TWAI_MSG_FLAG_EXTD;
} }
if (frame->remote_transmission_request) { if (frame->remote_transmission_request) {
flags |= CAN_MSG_FLAG_RTR; flags |= TWAI_MSG_FLAG_RTR;
} }
can_message_t message = { twai_message_t message = {
.flags = flags, .flags = flags,
.identifier = frame->can_id, .identifier = frame->can_id,
.data_length_code = frame->can_data_length_code, .data_length_code = frame->can_data_length_code,
@ -89,7 +89,7 @@ canbus::Error ESP32Can::send_message(struct canbus::CanFrame *frame) {
memcpy(message.data, frame->data, frame->can_data_length_code); memcpy(message.data, frame->data, frame->can_data_length_code);
} }
if (can_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) { if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
return canbus::ERROR_OK; return canbus::ERROR_OK;
} else { } else {
return canbus::ERROR_ALLTXBUSY; return canbus::ERROR_ALLTXBUSY;
@ -97,15 +97,15 @@ canbus::Error ESP32Can::send_message(struct canbus::CanFrame *frame) {
} }
canbus::Error ESP32Can::read_message(struct canbus::CanFrame *frame) { canbus::Error ESP32Can::read_message(struct canbus::CanFrame *frame) {
can_message_t message; twai_message_t message;
if (can_receive(&message, 0) != ESP_OK) { if (twai_receive(&message, 0) != ESP_OK) {
return canbus::ERROR_NOMSG; return canbus::ERROR_NOMSG;
} }
frame->can_id = message.identifier; frame->can_id = message.identifier;
frame->use_extended_id = message.flags & CAN_MSG_FLAG_EXTD; frame->use_extended_id = message.flags & TWAI_MSG_FLAG_EXTD;
frame->remote_transmission_request = message.flags & CAN_MSG_FLAG_RTR; frame->remote_transmission_request = message.flags & TWAI_MSG_FLAG_RTR;
frame->can_data_length_code = message.data_length_code; frame->can_data_length_code = message.data_length_code;
if (!frame->remote_transmission_request) { if (!frame->remote_transmission_request) {