ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pdo.cpp
Go to the documentation of this file.
1// Our own PDOs: configuration (configure_tpdo/rpdo()), the communication and mapping records, processing.
2#include "canopen.h"
3
4#ifdef USE_CANOPEN_PDO
5
6#include "esphome/core/log.h"
7
8namespace esphome::canopen {
9
10ESPHOME_LOG_TAG(TAG, "canopen");
11
12// sub 0 of the communication records (0x1400 / 0x1800 + n), read only
13static constexpr uint8_t PDO_COMM_HIGHEST_SUB = 5;
14
15static const LogString *pdo_kind(bool tpdo) { return tpdo ? LOG_STR("TPDO") : LOG_STR("RPDO"); }
16
17bool CANopenComponent::configure_tpdo(uint8_t n, uint32_t cob_id, std::initializer_list<PdoMapping> mappings,
18 uint16_t event_timer_ms, uint16_t inhibit_time_100us, uint8_t transmission_type) {
19 return this->configure_pdo_(true, n, cob_id, mappings, event_timer_ms, inhibit_time_100us, transmission_type);
20}
21
22bool CANopenComponent::configure_rpdo(uint8_t n, uint32_t cob_id, std::initializer_list<PdoMapping> mappings,
23 uint16_t event_timer_ms, uint8_t transmission_type) {
24 return this->configure_pdo_(false, n, cob_id, mappings, event_timer_ms, 0, transmission_type);
25}
26
27bool CANopenComponent::configure_pdo_(bool tpdo, uint8_t n, uint32_t cob_id, std::initializer_list<PdoMapping> mappings,
28 uint16_t event_timer_ms, uint16_t inhibit_time_100us, uint8_t transmission_type) {
29 unsigned bits = 0;
30 bool aligned = true;
31 for (const auto &m : mappings) {
32 bits += m.bits;
33 // no CO_CONFIG_PDO_BITWISE_MAPPING: whole bytes
34 aligned = aligned && m.bits > 0 && (m.bits % 8) == 0;
35 }
36 // a node's PDOs (a client-only hub has no PDO objects), 11-bit IDs CiA 301 doesn't reserve, event-driven
37 // transmission (no SYNC)
38 if (this->od_.finalized() || this->is_client_only() || mappings.size() > 8 || bits > 64 || !aligned ||
39 cob_id > 0x7FF || CO_IS_RESTRICTED_CAN_ID(cob_id) || transmission_type < CO_PDO_TRANSM_TYPE_SYNC_EVENT_LO) {
40 ESP_LOGE(TAG, "%s %u: invalid configuration (or after setup), ignored", LOG_STR_ARG(pdo_kind(tpdo)), n);
41 return false;
42 }
43 // not reserved with request_pdos(): fails the hub's setup()
44 if (n >= (tpdo ? this->tpdo_count_ : this->rpdo_count_)) {
45 ESP_LOGE(TAG, "%s %u: not reserved (canopen.request_pdos() declares it)", LOG_STR_ARG(pdo_kind(tpdo)), n);
46 this->pdo_not_reserved_ = true;
47 return false;
48 }
49 PdoParams &p = this->pdos_[tpdo ? this->rpdo_count_ + n : n];
50 // configured again: no mapping entries left from before (CANopenNode checks all 8, also those past map_count)
51 p = PDO_DISABLED;
52 // without mappings the PDO would be invalid (CANopenNode sends an EMCY): disabled, for the host to map later
53 p.cob_id = cob_id | (mappings.size() == 0 ? PDO_DISABLED.cob_id : 0);
54 p.transmission_type = transmission_type;
55 p.inhibit_time = inhibit_time_100us;
56 p.event_timer = event_timer_ms;
57 p.map_count = mappings.size();
58 size_t i = 0;
59 for (const auto &m : mappings)
60 p.map[i++] = (uint32_t(m.index) << 16) | (uint32_t(m.sub) << 8) | m.bits;
61 return true;
62}
63
65#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
66 for (size_t n = 0; n < this->tpdo_count_; n++)
67 discard_pending_tx(this->co_->CANmodule, this->co_->TPDO[n].CANtxBuff);
68#endif
69}
70
71// communication record subs: RPDO 0, 1, 2, 5; TPDO also 3 (_RPDO/_TPDO_OD_RECORD_SUBS in __init__.py)
72static constexpr uint8_t RPDO_COMM_SUBS = 4;
73static constexpr uint8_t TPDO_COMM_SUBS = 5;
74
76 auto &od = this->od_;
77 auto *highest = const_cast<uint8_t *>(&PDO_COMM_HIGHEST_SUB);
78 for (size_t i = 0; i < this->pdo_count_(); i++) {
79 PdoParams &p = this->pdos_[i];
80 const bool tpdo = i >= this->rpdo_count_;
81 const uint16_t n = tpdo ? i - this->rpdo_count_ : i;
82 // OD_getSub() finds record subs by subindex, so the TPDO's inhibit time (sub 3) can come last
83 const ObjectDictionary::RecordSub comm[TPDO_COMM_SUBS] = {{0, ODA_SDO_R, highest, 1},
84 {1, ODA_SDO_RW, &p.cob_id, 4},
85 {2, ODA_SDO_RW, &p.transmission_type, 1},
86 {5, ODA_SDO_RW, &p.event_timer, 2},
87 {3, ODA_SDO_RW, &p.inhibit_time, 2}};
88 od.add_record((tpdo ? 0x1800 : 0x1400) + n, comm, tpdo ? TPDO_COMM_SUBS : RPDO_COMM_SUBS);
89 // the mapping count is writable: CiA 301 remapping sets it to 0 and back
90 od.add_array((tpdo ? 0x1A00 : 0x1600) + n, ODA_SDO_RW, &p.map_count, p.map.data(), sizeof(uint32_t), p.map.size(),
91 ODA_SDO_RW);
92 }
93}
94
95void CANopenComponent::config_pdo_(CO_config_t &cfg) {
96 // CO_CANopenInitPDO() steps from the first record: 0x1400.. / 0x1600.. / 0x1800.. / 0x1A00.. are contiguous
97 cfg.CNT_RPDO = this->rpdo_count_;
98 cfg.ENTRY_H1400 = this->od_.find(0x1400);
99 cfg.ENTRY_H1600 = this->od_.find(0x1600);
100 cfg.CNT_TPDO = this->tpdo_count_;
101 cfg.ENTRY_H1800 = this->od_.find(0x1800);
102 cfg.ENTRY_H1A00 = this->od_.find(0x1A00);
103}
104
106 uint32_t err_info = 0;
107 CO_ReturnError_t err = CO_CANopenInitPDO(this->co_, this->co_->em, this->od_.od(), this->node_id_, &err_info);
108 if (err != CO_ERROR_NO) {
109 ESP_LOGE(TAG, "CO_CANopenInitPDO failed: %d (OD 0x%04" PRIx32 ")", err, err_info);
110 return false;
111 }
112 // a mapping that doesn't match the OD isn't an init error: CANopenNode disables the PDO and only sends an EMCY
113 this->log_invalid_pdos_();
114 return true;
115}
116
117#if ((CO_CONFIG_PDO) & (CO_CONFIG_RPDO_ENABLE | CO_CONFIG_TPDO_ENABLE)) != 0
118static void log_if_invalid(bool tpdo, unsigned n, const PdoParams &p, const CO_PDO_common_t &common) {
119 if ((p.cob_id & PDO_DISABLED.cob_id) != 0 || common.valid)
120 return; // disabled as configured, or running
121 ESP_LOGE(TAG, "%s %u disabled: its mapping (0x%04X) doesn't match the OD", LOG_STR_ARG(pdo_kind(tpdo)), n,
122 (tpdo ? 0x1A00u : 0x1600u) + n);
123}
124#endif
125
127#if ((CO_CONFIG_PDO) & CO_CONFIG_RPDO_ENABLE) != 0
128 for (unsigned n = 0; n < this->rpdo_count_; n++)
129 log_if_invalid(false, n, this->pdos_[n], this->co_->RPDO[n].PDO_common);
130#endif
131#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
132 for (unsigned n = 0; n < this->tpdo_count_; n++)
133 log_if_invalid(true, n, this->pdos_[this->rpdo_count_ + n], this->co_->TPDO[n].PDO_common);
134#endif
135}
136
138 // no SYNC: event-driven PDOs only
139#if ((CO_CONFIG_PDO) & CO_CONFIG_RPDO_ENABLE) != 0
140 if (this->rpdo_count_ > 0)
141 CO_process_RPDO(this->co_, false, dt_us, nullptr);
142#endif
143#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
144 if (this->tpdo_count_ > 0)
145 CO_process_TPDO(this->co_, false, dt_us, nullptr);
146#endif
147}
148
150#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
151 // before setup(), after a failed one, or past the reserved TPDOs
152 if (this->co_ == nullptr || this->is_failed() || n >= this->tpdo_count_)
153 return false;
154 CO_TPDOsendRequest(&this->co_->TPDO[n]);
155 return true;
156#else
157 return false;
158#endif
159}
160
161bool CANopenComponent::request_tpdo(uint16_t index, uint8_t sub) {
162#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
163 // no TPDOs reserved, or a failed setup()
164 if (this->tpdo_count_ == 0 || this->is_failed())
165 return false;
166 OD_entry_t *entry = this->od_.find(index);
167 // the request flags live in the entry's extension (its handler)
168 if (entry == nullptr || entry->extension == nullptr || sub >= OD_FLAGS_PDO_SIZE * 8)
169 return false;
170 OD_requestTPDO(entry, sub);
171 return true;
172#else
173 return false;
174#endif
175}
176
177} // namespace esphome::canopen
178
179#endif // USE_CANOPEN_PDO
uint8_t m
Definition bl0906.h:1
bool is_failed() const
Definition component.h:288
void process_pdo_(uint32_t dt_us)
Definition pdo.cpp:137
bool configure_pdo_(bool tpdo, uint8_t n, uint32_t cob_id, std::initializer_list< PdoMapping > mappings, uint16_t event_timer_ms, uint16_t inhibit_time_100us, uint8_t transmission_type)
Definition pdo.cpp:27
void config_pdo_(CO_config_t &cfg)
Definition pdo.cpp:95
bool configure_tpdo(uint8_t n, uint32_t cob_id, std::initializer_list< PdoMapping > mappings, uint16_t event_timer_ms=0, uint16_t inhibit_time_100us=0, uint8_t transmission_type=254)
Definition pdo.cpp:17
bool configure_rpdo(uint8_t n, uint32_t cob_id, std::initializer_list< PdoMapping > mappings, uint16_t event_timer_ms=0, uint8_t transmission_type=254)
Definition pdo.cpp:22
OD_entry_t * find(uint16_t index)
Definition od.h:73
ESPHOME_LOG_TAG(TAG, "canopen")
void discard_pending_tx(CO_CANmodule_t *mod, CO_CANtx_t *buf)
static void uint32_t
std::array< uint32_t, 8 > map
Definition canopen.h:28