7#ifdef CANOPEN_SDO_BLOCK_SIZE
8#include "301/crc16-ccitt.h"
21#ifdef USE_CANOPEN_NODE
24static constexpr uint32_t RESET_TX_GRACE_MS = 60;
26static constexpr uint32_t RESET_FLUSH_EXTRA_MS = 80;
31static_assert(
NMT_STATES.size() == NMTStateStrings::LAST_INDEX);
34 for (
size_t i = 0; i <
NMT_STATES.size(); i++) {
36 return NMTStateStrings::get_log_str(
static_cast<uint8_t
>(i), NMTStateStrings::LAST_INDEX);
38 return NMTStateStrings::get_log_str(NMTStateStrings::LAST_INDEX, NMTStateStrings::LAST_INDEX);
45static_assert(
static_cast<uint8_t
>(CO_NMT_PRE_OPERATIONAL) ==
58#ifdef USE_CANOPEN_NODE
66#ifdef USE_CANOPEN_NODE_MONITOR
70#ifdef USE_CANOPEN_PDO_STORAGE
77#ifdef USE_CANOPEN_SDO_CLIENT
81 this->
fail_setup_(LOG_STR(
"invalid object dictionary"));
90#ifdef USE_CANOPEN_NODE
93 cfg.ENTRY_H1017 = od.find(0x1017);
95 cfg.ENTRY_H1001 = od.find(0x1001);
96 cfg.ENTRY_H1014 = od.find(0x1014);
99#ifdef USE_CANOPEN_NODE_MONITOR
102#ifdef USE_CANOPEN_PDO
107#ifdef USE_CANOPEN_SDO_CLIENT
112 this->
co_ = CO_new(&cfg, &heap);
113 if (this->
co_ ==
nullptr) {
117 CO_ReturnError_t err = CO_CANinit(this->
co_,
this, 0);
118 if (err != CO_ERROR_NO) {
119 ESP_LOGE(TAG,
"CO_CANinit failed: %d", err);
123#ifdef USE_CANOPEN_NODE
125#ifdef USE_CANOPEN_PDO_STORAGE
127 this->
fail_setup_(LOG_STR(
"PDO storage init failed"));
133 err = CO_CANopenInit(this->
co_,
nullptr,
nullptr, od.od(),
nullptr, nmt_control, 500 , 1000, 0,
134 false, this->node_id_, &err_info);
135 if (err != CO_ERROR_NO) {
136 ESP_LOGE(TAG,
"CO_CANopenInit failed: %d (OD 0x%04" PRIx32
")", err, err_info);
137 this->
fail_setup_(LOG_STR(
"CO_CANopenInit failed"));
140#ifdef USE_CANOPEN_PDO
146#ifdef USE_CANOPEN_NODE_MONITOR
152#ifdef USE_CANOPEN_SDO_CLIENT
155 this->
fail_setup_(LOG_STR(
"SDO client init failed"));
162#ifdef USE_CANOPEN_LOCAL_NODES
168 ESP_LOGV(TAG,
"CANopenNode initialized, node 0x%02x, heap %" PRIu32
" B", this->
node_id_, heap);
173#ifdef USE_CANOPEN_SDO_CLIENT
180#ifdef USE_CANOPEN_SDO_CLIENT
182 "CANopen (CANopenNode):\n"
183 " Mode: SDO client only (no node ID)\n"
184 " SDO client: response timeout %u ms",
189#ifdef USE_CANOPEN_NODE
191 "CANopen (CANopenNode):\n"
193 " Heartbeat interval: %u ms\n"
196 this->
start_operational_ ? LOG_STR_LITERAL(
"operational") : LOG_STR_LITERAL(
"pre-operational"));
197#ifdef CANOPEN_SDO_BLOCK_SIZE
198 ESP_LOGCONFIG(TAG,
" SDO server: block transfer, download block size %u", CANOPEN_SDO_BLOCK_SIZE);
202#ifdef USE_CANOPEN_PDO
205#ifdef USE_CANOPEN_SDO_CLIENT
208#ifdef USE_CANOPEN_NODE_MONITOR
215#ifdef USE_CANOPEN_SDO_CLIENT
216#ifdef USE_CANOPEN_LOCAL_NODES
233#ifdef USE_CANOPEN_LOCAL_NODES
237#ifdef USE_CANOPEN_SDO_CLIENT
240 CO_SDOclient_t *cli = this->
co_->SDOclient;
241 const bool unsent = cli->CANtxBuff->bufferFull;
247 cli->timeoutTimer / 1000 + elapsed >= 2u * this->response_timeout_ms_) {
249 this->sdo_last_ms_ = now;
254#ifdef USE_CANOPEN_SDO_CLIENT
256 if (unsent && !cli->CANtxBuff->bufferFull)
257 cli->timeoutTimer = 0;
262#if defined(USE_CANOPEN_NODE) && defined(CANOPEN_SDO_BLOCK_SIZE)
274#ifdef USE_CANOPEN_NODE
278 this->last_us_ = now;
279 CO_NMT_reset_cmd_t
reset = CO_process(this->
co_,
false, dt,
nullptr);
280#ifdef USE_CANOPEN_NODE_MONITOR
286 ESP_LOGW(TAG,
"NMT reset node");
289 }
else if (
reset == CO_RESET_COMM) {
290 ESP_LOGW(TAG,
"NMT reset communication (not implemented)");
295 if (this->
co_->CANmodule->CANtxCount == 0 &&
296 static_cast<int32_t
>(now_ms + RESET_TX_GRACE_MS - this->reset_deadline_ms_) < 0)
301#ifdef USE_CANOPEN_PDO
308#ifdef USE_CANOPEN_PDO
320#if defined(USE_CANOPEN_NODE) && defined(CANOPEN_SDO_BLOCK_SIZE)
325 CO_SDOserver_t *srv = this->
co_->SDOserver;
326 if (srv->state != CO_SDO_ST_UPLOAD_BLK_SUBBLOCK_SREQ)
328 const CO_NMT_internalState_t nmt = CO_NMT_getInternalState(this->
co_->NMT);
329 const bool pre_or_operational = nmt == CO_NMT_PRE_OPERATIONAL || nmt == CO_NMT_OPERATIONAL;
331 while (srv->state == CO_SDO_ST_UPLOAD_BLK_SUBBLOCK_SREQ && !srv->CANtxBuff->bufferFull) {
332#ifdef USE_CANOPEN_LOCAL_NODES
338 CO_SDOserver_process(srv, pre_or_operational, 0,
nullptr);
343#if defined(USE_CANOPEN_LOCAL_NODES) && defined(CANOPEN_SDO_BLOCK_SIZE)
346 if (node->local_queue_.capacity() != 0 && node->local_queue_.size() >=
LOCAL_QUEUE_SIZE / 2)
353#ifdef USE_CANOPEN_NODE
368#ifdef USE_CANOPEN_NODE
371 CO_NMT_command_t command;
374 command = CO_NMT_ENTER_OPERATIONAL;
377 command = CO_NMT_ENTER_PRE_OPERATIONAL;
380 command = CO_NMT_ENTER_STOPPED;
386 ESP_LOGW(TAG,
"NMT state %s rejected: the previous command is still pending",
390 CO_NMT_sendInternalCommand(this->
co_->NMT, command);
400#ifdef CANOPEN_SDO_BLOCK_SIZE
403static_assert(CO_CONFIG_SDO_SRV_BUFFER_SIZE <= UINT16_MAX);
405extern "C" uint16_t
crc16_ccitt(
const uint8_t block[],
size_t blockLength, uint16_t crc) {
uint16_t crc16_ccitt(const uint8_t block[], size_t blockLength, uint16_t crc)
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
void disable_loop()
Disable this component's loop.
bool rx_capped() const
The last receive pass stopped at max_frames_per_loop; more frames may be waiting.
void add_frame_callback(F &&callback)
The frame is only valid during the call.
void send_upload_block_()
void process_sdo_(uint32_t dt_us)
void add_sdo_client_od_()
uint16_t response_timeout_ms_
void process_pdo_(uint32_t dt_us)
bool local_queues_busy_() const
static constexpr uint8_t LOCAL_QUEUE_SIZE
void add_device_info_od_()
LazyCallbackManager< void(NMTState)> nmt_state_callback_
void dump_device_info_config_()
void apply_nmt_command_()
void discard_pending_tpdos_()
void config_pdo_(CO_config_t &cfg)
void dump_node_monitor_config_()
void dump_config() override
static constexpr uint32_t UPLOAD_BURST_US
FrameListener * frame_listeners_
void fail_setup_(const LogString *reason)
FixedRingBuffer< LocalFrame, LOCAL_QUEUE_SIZE > local_queue_
void on_frame_(const canbus::CanFrame &frame)
void add_pdo_storage_od_()
void add_node_monitor_od_()
void check_frame_listener_(uint16_t can_id, uint16_t mask)
void init_node_monitor_()
void dispatch_node_events_()
void config_sdo_client_(CO_config_t &cfg)
bool set_nmt_state(NMTState state)
void process_node_monitor_(uint32_t dt_us)
CANopenComponent * next_local_
bool is_client_only() const
uint32_t reset_deadline_ms_
void config_device_info_(CO_config_t &cfg)
void config_node_monitor_(CO_config_t &cfg)
bool nmt_command_pending_() const
void reject_queued_sdo_()
void dump_usage(const char *tag) const
void CO_CANmodule_process(CO_CANmodule_t *CANmodule)
void CO_CANsetNormalMode(CO_CANmodule_t *CANmodule)
PROGMEM_STRING_TABLE(NMTStateStrings, "bootup", "stopped", "operational", "pre-operational", "unknown")
ESPHOME_LOG_TAG(TAG, "canopen")
@ NMT_STATE_PRE_OPERATIONAL
NMTState to_nmt_state(CO_NMT_internalState_t state)
const LogString * nmt_state_to_string(NMTState state)
constexpr std::array< NMTState, 4 > NMT_STATES
uint32_t IRAM_ATTR HOT micros()
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
Application App
Global storage of Application pointer - only one Application can exist.
One CAN frame as passed between the bus and its driver.