/****************************************************************************** 版权所有 (C), 2018-2099, Radkil ****************************************************************************** 文 件 名 : Spoolend.c 版 本 号 : 初稿 作 者 : radkil 生成日期 : 2026年6月10日 最近修改 : 移植自 Spoolold 功能描述 : 线轴端控制板 — Modbus 从机/主机 + DMK 电机 + 继电器 + 电源管理 修改历史 : 1.日 期 : 2026年6月10日 作 者 : radkil 修改内容 : 创建文件 2.日 期 : 2026年7月2日 作 者 : radkil 修改内容 : 从 Spoolold 移植完整功能 ******************************************************************************/ #include "bsp_uart.h" #include "bsp_CAN.h" #include "com.h" #include "fdcan.h" #include "modbus.h" #include "dmk.h" #include "flash_operation.h" #include "iwdg.h" #include "gpio.h" /*----------------------------------------------* * 全局变量 * *----------------------------------------------*/ TComCtrl *g_ptRS485_1; /* Modbus 从机 */ TComCtrl *g_ptRS485_2; /* Modbus 主机(连 DMK 驱动器)*/ TComCtrl *g_ptFDCAN1; /*----------------------------------------------* * 模块级变量 * *----------------------------------------------*/ static uint32_t s_uwRxPreTime = 0; /*----------------------------------------------* * RS485 硬件发送函数 * *----------------------------------------------*/ static int RS485_1_Send(char *_pBuffer, uint32_t _iSize) { HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_SET); TUartUserData *ptUartUserData = (TUartUserData *)g_ptRS485_1->m_pUserData; int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100); HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_RESET); return iRet; } static int RS485_2_Send(char *_pBuffer, uint32_t _iSize) { HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_SET); TUartUserData *ptUartUserData = (TUartUserData *)g_ptRS485_2->m_pUserData; int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100); HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_RESET); return iRet; } /*----------------------------------------------* * UART 接收中断回调 * *----------------------------------------------*/ #ifndef CONFIG_UART_IT_IDLEDMA void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) { UART_RX_IRQHandler(g_ptRS485_1); } else if (huart->Instance == USART3) { UART_RX_IRQHandler(g_ptRS485_2); } else if (huart->Instance == LPUART1) { // 预留 } } #endif /*----------------------------------------------* * CAN 回调 * *----------------------------------------------*/ static int check_Spoolend(char *_pBuffer, uint32_t _iSize) { return _iSize; } static void decode_Spoolend(const char *_pBuffer, uint32_t _iSize) { TCANUserData *ptCANUserData = (TCANUserData *)g_ptFDCAN1->m_pUserData; uint32_t iCANId = (uint32_t)ptCANUserData->m_canrx->Identifier; rd_ComIDWrite(g_ptFDCAN1, iCANId, (char *)_pBuffer, _iSize); } static int FDCAN1_Send(char *_pBuffer, uint32_t _iSize) { return CAN_TX_FIFOQ(g_ptFDCAN1, _pBuffer, _iSize); } void HAL_FDCAN_ErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t ErrorStatusITs) { if (hfdcan->Instance == FDCAN1) { MX_FDCAN1_Init(); } } void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) { if (hfdcan->Instance == FDCAN1) { CAN_RX_IRQHandler(g_ptFDCAN1); } } /*----------------------------------------------* * 电源管理 * *----------------------------------------------*/ static void handle_power_on_off(void) { if (holdingRegisters[5] != 0) /* 手动模式 */ { if (holdingRegisters[6] == 0) { if (holdingRegisters[4] == 0) HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET); else HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET); } else { if (holdingRegisters[4] == 0) HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET); else HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET); } return; } /* 自动模式:根据通信超时判断 */ if (HAL_GetTick() >= s_uwRxPreTime && HAL_GetTick() - s_uwRxPreTime > holdingRegisters[7]) { if (holdingRegisters[4] == 0) HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET); else HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET); } else { if (holdingRegisters[4] == 0) HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET); else HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET); } } /*----------------------------------------------* * 初始化 * *----------------------------------------------*/ void SpoolendInit(void) { /* RS485_1: Modbus 从机 — check/decode 回调由 rd_ComRead 自动调用 */ TUartUserData *ptUartUserData = UART_userdata_init(1, 115200, 512); g_ptRS485_1 = rd_ComCreate(Modbus_Slave_Check, Modbus_Slave_Decode, RS485_1_Send, ptUartUserData->m_buf_size, ptUartUserData); UART_IT_init(g_ptRS485_1); /* RS485_2: Modbus 主机(DMK 驱动器)*/ TUartUserData *ptUartUserData2 = UART_userdata_init(3, 115200, 512); g_ptRS485_2 = rd_ComCreate(NULL, NULL, RS485_2_Send, ptUartUserData2->m_buf_size, ptUartUserData2); UART_IT_init(g_ptRS485_2); Modbus_Master_Init(g_ptRS485_2); /* FDCAN1 */ TCANUserData *ptCANUserData = CAN_userdata_init(&hfdcan1, 8, 0); g_ptFDCAN1 = rd_ComCreate(check_Spoolend, decode_Spoolend, FDCAN1_Send, CONFIG_UART_BUFFER_SIZE, ptCANUserData); CAN_IT_init(g_ptFDCAN1); /* 从 Flash 恢复参数 */ ReadParametersFromFlash((int16_t *)holdingRegisters); holdingRegisters[8] = 2000; /* 默认电机速度 */ s_uwRxPreTime = HAL_GetTick(); } /*----------------------------------------------* * 主任务 * *----------------------------------------------*/ void SpoolendTask(void) { char pcBuffer1[52] = {0}; char pcBuffer2[52] = {0}; /* 喂狗 */ HAL_IWDG_Refresh(&hiwdg); /* 读取 Modbus 从机帧(rd_ComRead 内部自动调用 Modbus_Slave_Decode)*/ __disable_irq(); int i485_1 = rd_ComRead(g_ptRS485_1, pcBuffer1, sizeof(pcBuffer1)); int i485_2 = rd_ComRead(g_ptRS485_2, pcBuffer2, sizeof(pcBuffer2)); rd_ComRead(g_ptFDCAN1, pcBuffer2, sizeof(pcBuffer2)); __enable_irq(); (void)i485_1; (void)i485_2; /* K1-K4 继电器控制 */ HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, holdingRegisters[0] ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, holdingRegisters[1] ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(K3_GPIO_Port, K3_Pin, holdingRegisters[2] ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(K4_GPIO_Port, K4_Pin, holdingRegisters[3] ? GPIO_PIN_SET : GPIO_PIN_RESET); /* 通信接收标志处理 */ if (RS485_1_RX) { RS485_1_RX = 0; s_uwRxPreTime = HAL_GetTick(); HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); } if (RS485_1_Host_Existed) { RS485_1_Host_Existed = 0; s_uwRxPreTime = HAL_GetTick(); } /* 通信超时判断 + 电机控制 */ if (HAL_GetTick() >= s_uwRxPreTime && HAL_GetTick() - s_uwRxPreTime > holdingRegisters[7]) { MB_WriteHoldingReg(1, 0x033, 0x00); /* 超时停止电机 */ } else { handle_dmk_motor(holdingRegisters[8], holdingRegisters[9]); } /* 电源管理 */ handle_power_on_off(); /* Flash 保存触发 */ if (holdingRegisters[10] == 55) { WriteParametersToFlash((int16_t *)holdingRegisters); holdingRegisters[10] = 1; } /* 发送处理 */ rd_ComSendProc(g_ptRS485_1); rd_ComSendProc(g_ptRS485_2); rd_ComSendProc(g_ptFDCAN1); }