ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
frames.cpp
Go to the documentation of this file.
1// Raw frames for other components: frame listeners, send_frame(), heartbeat / EMCY callbacks for every node.
2#include "canopen.h"
3
4#include "esphome/core/log.h"
5
6namespace esphome::canopen {
7
8ESPHOME_LOG_TAG(TAG, "canopen");
9
11 if ((listener.can_id & listener.mask) > canbus::CAN_STANDARD_ID_MAX) {
12 ESP_LOGW(TAG, "frame listener 0x%x/0x%x matches no 11-bit CAN ID, not added", listener.can_id, listener.mask);
13 return;
14 }
15 // adding it twice would link it to itself
16 for (const FrameListener *l = this->frame_listeners_; l != nullptr; l = l->next) {
17 if (l == &listener)
18 return;
19 }
20 // the bits above 0x7FF are 0 in both the frame's id and can_id & mask, so they can go
21 listener.can_id &= canbus::CAN_STANDARD_ID_MAX;
22 listener.mask &= canbus::CAN_STANDARD_ID_MAX;
23 listener.next = this->frame_listeners_;
24 this->frame_listeners_ = &listener;
25 if (this->co_ != nullptr)
26 this->check_frame_listener_(listener.can_id, listener.mask);
27}
28
29// A listener on an ID CANopenNode receives itself (NMT, heartbeats it consumes, ...) is almost always a mistake.
30void CANopenComponent::check_frame_listener_(uint16_t can_id, uint16_t mask) {
31 CO_CANmodule_t *mod = this->co_->CANmodule;
32 for (uint16_t i = 0; i < mod->rxSize; i++) {
33 const CO_CANrx_t *rx = &mod->rxArray[i];
34 if (rx->CANrx_callback == nullptr || (rx->ident & 0x800))
35 continue;
36 if (((can_id ^ rx->ident) & mask & rx->mask) == 0) {
37 ESP_LOGW(TAG, "frame listener 0x%03x/0x%03x overlaps CAN ID 0x%03x used by CANopenNode", can_id, mask,
38 rx->ident & 0x7FF);
39 }
40 }
41}
42
43bool CANopenComponent::send_frame(uint32_t can_id, const uint8_t *data, size_t len) {
44 if (can_id > canbus::CAN_STANDARD_ID_MAX || len > canbus::CAN_MAX_DATA_LENGTH)
45 return false;
46 return this->transmit(can_id, data, static_cast<uint8_t>(len));
47}
48
49void CANopenComponent::dispatch_frame_(const CO_CANrxMsg_t &msg) {
50 if (msg.ident & 0x800)
51 return; // RTR
52 for (const FrameListener *l = this->frame_listeners_; l != nullptr; l = l->next) {
53 if (((msg.ident ^ l->can_id) & l->mask) == 0)
54 l->callback(l->context, msg.ident, msg.data, msg.DLC);
55 }
56 uint8_t node;
57#ifdef USE_CANOPEN_HEARTBEAT
59 if (decode_heartbeat(msg.ident, msg.data, msg.DLC, node, state)) {
60 this->heartbeat_callback_.call(node, state);
61 return;
62 }
63#endif
64 Emergency emcy;
65 if (!decode_emcy(msg.ident, msg.data, msg.DLC, node, emcy))
66 return;
67 // code 0x0000: an error cleared; the register shows the errors still active
68 if (emcy.error_code == 0) {
69 ESP_LOGI(TAG, "node 0x%02x EMCY: error reset, register 0x%02x", node, emcy.error_register);
70 } else {
71 ESP_LOGW(TAG, "node 0x%02x EMCY: error code 0x%04x, register 0x%02x", node, emcy.error_code, emcy.error_register);
72 }
73#ifdef USE_CANOPEN_EMCY
74 this->emcy_callback_.call(node, emcy);
75#endif
76}
77
78} // namespace esphome::canopen
uint8_t l
Definition bl0906.h:0
CallbackManager< void(uint8_t, const Emergency &)> emcy_callback_
Definition canopen.h:407
bool transmit(uint16_t ident, const uint8_t *data, uint8_t len)
void add_frame_listener(FrameListener &listener)
Definition frames.cpp:10
bool send_frame(uint32_t can_id, const uint8_t *data, size_t len)
Definition frames.cpp:43
void check_frame_listener_(uint16_t can_id, uint16_t mask)
Definition frames.cpp:30
void dispatch_frame_(const CO_CANrxMsg_t &msg)
Definition frames.cpp:49
CallbackManager< void(uint8_t, NMTState)> heartbeat_callback_
Definition canopen.h:404
bool state
Definition fan.h:2
bool decode_heartbeat(uint32_t can_id, const uint8_t *data, size_t len, uint8_t &node, NMTState &state)
Definition types.h:125
ESPHOME_LOG_TAG(TAG, "canopen")
bool decode_emcy(uint32_t can_id, const uint8_t *data, size_t len, uint8_t &node, Emergency &emcy)
Definition types.h:140
const void size_t len
Definition hal.h:68
static void uint32_t