mirror of
https://github.com/esphome/esphome.git
synced 2025-10-06 20:03:46 +01:00
[esp32_can] support multiple CAN instances for platforms that support it (#10712)
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
This commit is contained in:
@@ -67,8 +67,16 @@ static bool get_bitrate(canbus::CanSpeed bitrate, twai_timing_config_t *t_config
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ESP32Can::setup_internal() {
|
bool ESP32Can::setup_internal() {
|
||||||
|
static int next_twai_ctrl_num = 0;
|
||||||
|
if (next_twai_ctrl_num >= SOC_TWAI_CONTROLLER_NUM) {
|
||||||
|
ESP_LOGW(TAG, "Maximum number of esp32_can components created already");
|
||||||
|
this->mark_failed();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
twai_general_config_t g_config =
|
twai_general_config_t g_config =
|
||||||
TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, TWAI_MODE_NORMAL);
|
TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, TWAI_MODE_NORMAL);
|
||||||
|
g_config.controller_id = next_twai_ctrl_num++;
|
||||||
if (this->tx_queue_len_.has_value()) {
|
if (this->tx_queue_len_.has_value()) {
|
||||||
g_config.tx_queue_len = this->tx_queue_len_.value();
|
g_config.tx_queue_len = this->tx_queue_len_.value();
|
||||||
}
|
}
|
||||||
@@ -86,14 +94,14 @@ bool ESP32Can::setup_internal() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Install TWAI driver
|
// Install TWAI driver
|
||||||
if (twai_driver_install(&g_config, &t_config, &f_config) != ESP_OK) {
|
if (twai_driver_install_v2(&g_config, &t_config, &f_config, &(this->twai_handle_)) != ESP_OK) {
|
||||||
// Failed to install driver
|
// Failed to install driver
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start TWAI driver
|
// Start TWAI driver
|
||||||
if (twai_start() != ESP_OK) {
|
if (twai_start_v2(this->twai_handle_) != ESP_OK) {
|
||||||
// Failed to start driver
|
// Failed to start driver
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return false;
|
return false;
|
||||||
@@ -102,6 +110,11 @@ bool ESP32Can::setup_internal() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
canbus::Error ESP32Can::send_message(struct canbus::CanFrame *frame) {
|
canbus::Error ESP32Can::send_message(struct canbus::CanFrame *frame) {
|
||||||
|
if (this->twai_handle_ == nullptr) {
|
||||||
|
// not setup yet or setup failed
|
||||||
|
return canbus::ERROR_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
if (frame->can_data_length_code > canbus::CAN_MAX_DATA_LENGTH) {
|
if (frame->can_data_length_code > canbus::CAN_MAX_DATA_LENGTH) {
|
||||||
return canbus::ERROR_FAILTX;
|
return canbus::ERROR_FAILTX;
|
||||||
}
|
}
|
||||||
@@ -124,7 +137,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 (twai_transmit(&message, this->tx_enqueue_timeout_ticks_) == ESP_OK) {
|
if (twai_transmit_v2(this->twai_handle_, &message, this->tx_enqueue_timeout_ticks_) == ESP_OK) {
|
||||||
return canbus::ERROR_OK;
|
return canbus::ERROR_OK;
|
||||||
} else {
|
} else {
|
||||||
return canbus::ERROR_ALLTXBUSY;
|
return canbus::ERROR_ALLTXBUSY;
|
||||||
@@ -132,9 +145,14 @@ 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) {
|
||||||
|
if (this->twai_handle_ == nullptr) {
|
||||||
|
// not setup yet or setup failed
|
||||||
|
return canbus::ERROR_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
twai_message_t message;
|
twai_message_t message;
|
||||||
|
|
||||||
if (twai_receive(&message, 0) != ESP_OK) {
|
if (twai_receive_v2(this->twai_handle_, &message, 0) != ESP_OK) {
|
||||||
return canbus::ERROR_NOMSG;
|
return canbus::ERROR_NOMSG;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -5,6 +5,8 @@
|
|||||||
#include "esphome/components/canbus/canbus.h"
|
#include "esphome/components/canbus/canbus.h"
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
|
|
||||||
|
#include <driver/twai.h>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace esp32_can {
|
namespace esp32_can {
|
||||||
|
|
||||||
@@ -29,6 +31,7 @@ class ESP32Can : public canbus::Canbus {
|
|||||||
TickType_t tx_enqueue_timeout_ticks_{};
|
TickType_t tx_enqueue_timeout_ticks_{};
|
||||||
optional<uint32_t> tx_queue_len_{};
|
optional<uint32_t> tx_queue_len_{};
|
||||||
optional<uint32_t> rx_queue_len_{};
|
optional<uint32_t> rx_queue_len_{};
|
||||||
|
twai_handle_t twai_handle_{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esp32_can
|
} // namespace esp32_can
|
||||||
|
89
tests/components/esp32_can/test.esp32-c6-idf.yaml
Normal file
89
tests/components/esp32_can/test.esp32-c6-idf.yaml
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
esphome:
|
||||||
|
on_boot:
|
||||||
|
then:
|
||||||
|
- canbus.send:
|
||||||
|
# Extended ID explicit
|
||||||
|
canbus_id: esp32_internal_can
|
||||||
|
use_extended_id: true
|
||||||
|
can_id: 0x100
|
||||||
|
data: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
|
||||||
|
- canbus.send:
|
||||||
|
# Standard ID by default
|
||||||
|
canbus_id: esp32_internal_can
|
||||||
|
can_id: 0x100
|
||||||
|
data: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
|
||||||
|
- canbus.send:
|
||||||
|
# Extended ID explicit
|
||||||
|
canbus_id: esp32_internal_can_2
|
||||||
|
use_extended_id: true
|
||||||
|
can_id: 0x100
|
||||||
|
data: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
|
||||||
|
- canbus.send:
|
||||||
|
# Standard ID by default
|
||||||
|
canbus_id: esp32_internal_can_2
|
||||||
|
can_id: 0x100
|
||||||
|
data: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
|
||||||
|
|
||||||
|
canbus:
|
||||||
|
- platform: esp32_can
|
||||||
|
id: esp32_internal_can
|
||||||
|
rx_pin: GPIO8
|
||||||
|
tx_pin: GPIO7
|
||||||
|
can_id: 4
|
||||||
|
bit_rate: 50kbps
|
||||||
|
on_frame:
|
||||||
|
- can_id: 500
|
||||||
|
then:
|
||||||
|
- lambda: |-
|
||||||
|
std::string b(x.begin(), x.end());
|
||||||
|
ESP_LOGD("canbus1", "canid 500 %s", b.c_str() );
|
||||||
|
- can_id: 0b00000000000000000000001000000
|
||||||
|
can_id_mask: 0b11111000000000011111111000000
|
||||||
|
use_extended_id: true
|
||||||
|
then:
|
||||||
|
- lambda: |-
|
||||||
|
auto pdo_id = can_id >> 14;
|
||||||
|
switch (pdo_id)
|
||||||
|
{
|
||||||
|
case 117:
|
||||||
|
ESP_LOGD("canbus1", "exhaust_fan_duty");
|
||||||
|
break;
|
||||||
|
case 118:
|
||||||
|
ESP_LOGD("canbus1", "supply_fan_duty");
|
||||||
|
break;
|
||||||
|
case 119:
|
||||||
|
ESP_LOGD("canbus1", "supply_fan_flow");
|
||||||
|
break;
|
||||||
|
// to be continued...
|
||||||
|
}
|
||||||
|
- platform: esp32_can
|
||||||
|
id: esp32_internal_can_2
|
||||||
|
rx_pin: GPIO10
|
||||||
|
tx_pin: GPIO9
|
||||||
|
can_id: 4
|
||||||
|
bit_rate: 50kbps
|
||||||
|
on_frame:
|
||||||
|
- can_id: 500
|
||||||
|
then:
|
||||||
|
- lambda: |-
|
||||||
|
std::string b(x.begin(), x.end());
|
||||||
|
ESP_LOGD("canbus2", "canid 500 %s", b.c_str() );
|
||||||
|
- can_id: 0b00000000000000000000001000000
|
||||||
|
can_id_mask: 0b11111000000000011111111000000
|
||||||
|
use_extended_id: true
|
||||||
|
then:
|
||||||
|
- lambda: |-
|
||||||
|
auto pdo_id = can_id >> 14;
|
||||||
|
switch (pdo_id)
|
||||||
|
{
|
||||||
|
case 117:
|
||||||
|
ESP_LOGD("canbus2", "exhaust_fan_duty");
|
||||||
|
break;
|
||||||
|
case 118:
|
||||||
|
ESP_LOGD("canbus2", "supply_fan_duty");
|
||||||
|
break;
|
||||||
|
case 119:
|
||||||
|
ESP_LOGD("canbus2", "supply_fan_flow");
|
||||||
|
break;
|
||||||
|
// to be continued...
|
||||||
|
}
|
Reference in New Issue
Block a user