13static constexpr uint8_t PDO_COMM_HIGHEST_SUB = 5;
15static const LogString *pdo_kind(
bool tpdo) {
return tpdo ? LOG_STR(
"TPDO") : LOG_STR(
"RPDO"); }
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);
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);
28 uint16_t event_timer_ms, uint16_t inhibit_time_100us, uint8_t transmission_type) {
31 for (
const auto &
m : mappings) {
34 aligned = aligned &&
m.bits > 0 && (
m.bits % 8) == 0;
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);
45 ESP_LOGE(TAG,
"%s %u: not reserved (canopen.request_pdos() declares it)", LOG_STR_ARG(pdo_kind(tpdo)), n);
53 p.
cob_id = cob_id | (mappings.size() == 0 ? PDO_DISABLED.cob_id : 0);
59 for (
const auto &
m : mappings)
65#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
72static constexpr uint8_t RPDO_COMM_SUBS = 4;
73static constexpr uint8_t TPDO_COMM_SUBS = 5;
77 auto *highest =
const_cast<uint8_t *
>(&PDO_COMM_HIGHEST_SUB);
78 for (
size_t i = 0; i < this->
pdo_count_(); i++) {
81 const uint16_t n = tpdo ? i - this->
rpdo_count_ : i;
84 {1, ODA_SDO_RW, &p.
cob_id, 4},
88 od.add_record((tpdo ? 0x1800 : 0x1400) + n, comm, tpdo ? TPDO_COMM_SUBS : RPDO_COMM_SUBS);
90 od.add_array((tpdo ? 0x1A00 : 0x1600) + n, ODA_SDO_RW, &p.
map_count, p.
map.data(),
sizeof(
uint32_t), p.
map.size(),
98 cfg.ENTRY_H1400 = this->
od_.
find(0x1400);
99 cfg.ENTRY_H1600 = this->
od_.
find(0x1600);
101 cfg.ENTRY_H1800 = this->
od_.
find(0x1800);
102 cfg.ENTRY_H1A00 = this->
od_.
find(0x1A00);
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);
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)
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);
127#if ((CO_CONFIG_PDO) & CO_CONFIG_RPDO_ENABLE) != 0
129 log_if_invalid(
false, n, this->
pdos_[n], this->
co_->RPDO[n].PDO_common);
131#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
133 log_if_invalid(
true, n, this->
pdos_[this->rpdo_count_ + n], this->
co_->TPDO[n].PDO_common);
139#if ((CO_CONFIG_PDO) & CO_CONFIG_RPDO_ENABLE) != 0
141 CO_process_RPDO(this->
co_,
false, dt_us,
nullptr);
143#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
145 CO_process_TPDO(this->
co_,
false, dt_us,
nullptr);
150#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
154 CO_TPDOsendRequest(&this->
co_->TPDO[n]);
162#if ((CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE) != 0
166 OD_entry_t *entry = this->
od_.
find(index);
168 if (entry ==
nullptr || entry->extension ==
nullptr || sub >= OD_FLAGS_PDO_SIZE * 8)
170 OD_requestTPDO(entry, sub);
size_t pdo_count_() const
void process_pdo_(uint32_t dt_us)
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)
void discard_pending_tpdos_()
void config_pdo_(CO_config_t &cfg)
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)
bool request_tpdo(uint8_t n)
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)
OD_entry_t * find(uint16_t index)
ESPHOME_LOG_TAG(TAG, "canopen")
void discard_pending_tx(CO_CANmodule_t *mod, CO_CANtx_t *buf)
std::array< uint32_t, 8 > map
uint8_t transmission_type