1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-03 02:22:25 +01:00

[mcp2515, canbus] error handling improvments (#10526)

This commit is contained in:
Patrick
2025-09-30 14:56:28 +02:00
committed by GitHub
parent 6018f5f5d1
commit 0e623055df
4 changed files with 28 additions and 13 deletions

View File

@@ -21,7 +21,7 @@ void Canbus::dump_config() {
} }
} }
void Canbus::send_data(uint32_t can_id, bool use_extended_id, bool remote_transmission_request, canbus::Error Canbus::send_data(uint32_t can_id, bool use_extended_id, bool remote_transmission_request,
const std::vector<uint8_t> &data) { const std::vector<uint8_t> &data) {
struct CanFrame can_message; struct CanFrame can_message;
@@ -45,13 +45,15 @@ void Canbus::send_data(uint32_t can_id, bool use_extended_id, bool remote_transm
ESP_LOGVV(TAG, " data[%d]=%02x", i, can_message.data[i]); ESP_LOGVV(TAG, " data[%d]=%02x", i, can_message.data[i]);
} }
if (this->send_message(&can_message) != canbus::ERROR_OK) { canbus::Error error = this->send_message(&can_message);
if (error != canbus::ERROR_OK) {
if (use_extended_id) { if (use_extended_id) {
ESP_LOGW(TAG, "send to extended id=0x%08" PRIx32 " failed!", can_id); ESP_LOGW(TAG, "send to extended id=0x%08" PRIx32 " failed with error %d!", can_id, error);
} else { } else {
ESP_LOGW(TAG, "send to standard id=0x%03" PRIx32 " failed!", can_id); ESP_LOGW(TAG, "send to standard id=0x%03" PRIx32 " failed with error %d!", can_id, error);
} }
} }
return error;
} }
void Canbus::add_trigger(CanbusTrigger *trigger) { void Canbus::add_trigger(CanbusTrigger *trigger) {

View File

@@ -70,11 +70,11 @@ class Canbus : public Component {
float get_setup_priority() const override { return setup_priority::HARDWARE; } float get_setup_priority() const override { return setup_priority::HARDWARE; }
void loop() override; void loop() override;
void send_data(uint32_t can_id, bool use_extended_id, bool remote_transmission_request, canbus::Error send_data(uint32_t can_id, bool use_extended_id, bool remote_transmission_request,
const std::vector<uint8_t> &data); const std::vector<uint8_t> &data);
void send_data(uint32_t can_id, bool use_extended_id, const std::vector<uint8_t> &data) { canbus::Error send_data(uint32_t can_id, bool use_extended_id, const std::vector<uint8_t> &data) {
// for backwards compatibility only // for backwards compatibility only
this->send_data(can_id, use_extended_id, false, data); return this->send_data(can_id, use_extended_id, false, data);
} }
void set_can_id(uint32_t can_id) { this->can_id_ = can_id; } void set_can_id(uint32_t can_id) { this->can_id_ = can_id; }
void set_use_extended_id(bool use_extended_id) { this->use_extended_id_ = use_extended_id; } void set_use_extended_id(bool use_extended_id) { this->use_extended_id_ = use_extended_id; }

View File

@@ -155,7 +155,7 @@ void MCP2515::prepare_id_(uint8_t *buffer, const bool extended, const uint32_t i
canid = (uint16_t) (id >> 16); canid = (uint16_t) (id >> 16);
buffer[MCP_SIDL] = (uint8_t) (canid & 0x03); buffer[MCP_SIDL] = (uint8_t) (canid & 0x03);
buffer[MCP_SIDL] += (uint8_t) ((canid & 0x1C) << 3); buffer[MCP_SIDL] += (uint8_t) ((canid & 0x1C) << 3);
buffer[MCP_SIDL] |= TXB_EXIDE_MASK; buffer[MCP_SIDL] |= SIDL_EXIDE_MASK;
buffer[MCP_SIDH] = (uint8_t) (canid >> 5); buffer[MCP_SIDH] = (uint8_t) (canid >> 5);
} else { } else {
buffer[MCP_SIDH] = (uint8_t) (canid >> 3); buffer[MCP_SIDH] = (uint8_t) (canid >> 3);
@@ -258,7 +258,7 @@ canbus::Error MCP2515::send_message(struct canbus::CanFrame *frame) {
} }
} }
return canbus::ERROR_FAILTX; return canbus::ERROR_ALLTXBUSY;
} }
canbus::Error MCP2515::read_message_(RXBn rxbn, struct canbus::CanFrame *frame) { canbus::Error MCP2515::read_message_(RXBn rxbn, struct canbus::CanFrame *frame) {
@@ -272,7 +272,7 @@ canbus::Error MCP2515::read_message_(RXBn rxbn, struct canbus::CanFrame *frame)
bool use_extended_id = false; bool use_extended_id = false;
bool remote_transmission_request = false; bool remote_transmission_request = false;
if ((tbufdata[MCP_SIDL] & TXB_EXIDE_MASK) == TXB_EXIDE_MASK) { if ((tbufdata[MCP_SIDL] & SIDL_EXIDE_MASK) == SIDL_EXIDE_MASK) {
id = (id << 2) + (tbufdata[MCP_SIDL] & 0x03); id = (id << 2) + (tbufdata[MCP_SIDL] & 0x03);
id = (id << 8) + tbufdata[MCP_EID8]; id = (id << 8) + tbufdata[MCP_EID8];
id = (id << 8) + tbufdata[MCP_EID0]; id = (id << 8) + tbufdata[MCP_EID0];
@@ -315,6 +315,17 @@ canbus::Error MCP2515::read_message(struct canbus::CanFrame *frame) {
rc = canbus::ERROR_NOMSG; rc = canbus::ERROR_NOMSG;
} }
#ifdef ESPHOME_LOG_HAS_DEBUG
uint8_t err = get_error_flags_();
// The receive flowchart in the datasheet says that if rollover is set (BUKT), RX1OVR flag will be set
// once both buffers are full. However, the RX0OVR flag is actually set instead.
// We can just check for both though because it doesn't break anything.
if (err & (EFLG_RX0OVR | EFLG_RX1OVR)) {
ESP_LOGD(TAG, "receive buffer overrun");
clear_rx_n_ovr_flags_();
}
#endif
return rc; return rc;
} }

View File

@@ -130,7 +130,9 @@ static const uint8_t CANSTAT_ICOD = 0x0E;
static const uint8_t CNF3_SOF = 0x80; static const uint8_t CNF3_SOF = 0x80;
static const uint8_t TXB_EXIDE_MASK = 0x08; // applies to RXBn_SIDL, TXBn_SIDL and RXFn_SIDL
static const uint8_t SIDL_EXIDE_MASK = 0x08;
static const uint8_t DLC_MASK = 0x0F; static const uint8_t DLC_MASK = 0x0F;
static const uint8_t RTR_MASK = 0x40; static const uint8_t RTR_MASK = 0x40;