/** * @file ethercat_pdo.cpp * @brief EtherCAT 过程数据(PDO)收发模块实现(1.5.55)。 */ #include "comm/ethercat_pdo.h" #include "comm/ethercat_can_bridge.h" #include "sys/cpu_load.h" #include uint8_t EthercatPdo::s_out[EthercatPdo::kPdoOutBytes] = {0}; uint8_t EthercatPdo::s_in[EthercatPdo::kPdoInBytes] = {0}; volatile uint32_t EthercatPdo::s_outputCycles = 0U; volatile uint32_t EthercatPdo::s_inputCycles = 0U; void EthercatPdo::readOutputs(const uint8_t *esc) { if (esc == nullptr) { return; } memcpy(s_out, esc, kPdoOutBytes); ++s_outputCycles; } void EthercatPdo::process() { /* PDO ↔ CAN 转发:输出 PDO 的 seq 字节变化沿触发一次 CAN 发送, * 输入 PDO 回填桥状态 + 电机状态窗口(布局见 ethercat_can_bridge.h)。 * * 顺序必须是先 processOutput 后 buildInput:buildInput 会把本周期刚执行的 * seq 回显成 ack,主站因此能在下一周期立刻确认送达;反过来则 ack 恒滞后一拍。 * processOutput 还负责取走主站请求的状态窗口下标,也必须在 buildInput 之前。 */ EthercatCanBridge::processOutput(s_out); EthercatCanBridge::buildInput(s_in); fillDiagTail(s_in); } /** * @brief 填充输入 PDO 段 5 设备诊断区(632..639,1.5.61)。 * @param[out] in 全长输入过程数据缓冲(buildInput 刚清零+填完段 1~4)。 * @note 负载率 1s 滚动一个窗口,seq 随窗口递增——主站只认"seq 在变"的负载值。 * 每周期重写 6 字节比"值变化才写"的分支判断更便宜,也不引入隐藏状态。 */ void EthercatPdo::fillDiagTail(uint8_t *in) { const uint16_t load = CpuLoad::loadPermille(); in[ECAT_DIAG_OFF_LOAD] = (uint8_t)(load & 0xFFU); in[ECAT_DIAG_OFF_LOAD + 1] = (uint8_t)(load >> 8); const uint32_t seq = CpuLoad::windowSeq(); in[ECAT_DIAG_OFF_SEQ] = (uint8_t)(seq & 0xFFU); in[ECAT_DIAG_OFF_SEQ + 1] = (uint8_t)((seq >> 8) & 0xFFU); in[ECAT_DIAG_OFF_SEQ + 2] = (uint8_t)((seq >> 16) & 0xFFU); in[ECAT_DIAG_OFF_SEQ + 3] = (uint8_t)((seq >> 24) & 0xFFU); } void EthercatPdo::writeInputs(uint8_t *esc) { if (esc == nullptr) { return; } memcpy(esc, s_in, kPdoInBytes); ++s_inputCycles; } void EthercatPdo::snapshotOutput(uint8_t *dst) { if (dst == nullptr) { return; } memcpy(dst, s_out, kPdoOutBytes); } extern "C" { void EcatPdo_ReadOutputs(const uint8_t *esc) { EthercatPdo::readOutputs(esc); } void EcatPdo_Process(void) { EthercatPdo::process(); } void EcatPdo_WriteInputs(uint8_t *esc) { EthercatPdo::writeInputs(esc); } } /* extern "C" */