/****************************************************************************** 版权所有 (C), 2018-2099, Radkil ****************************************************************************** 文 件 名 : drv_interface.c 版 本 号 : 初稿 作 者 : radkil 生成日期 : 2026年5月27日 最近修改 : 功能描述 : 对外接口层 修改历史 : 1.日 期 : 2026年5月27日 作 者 : radkil 修改内容 : 创建文件 ******************************************************************************/ #include "BHBF.h" #include "cmsis_os.h" #include "FreeRTOS.h" #include "msg_center.h" /*----------------------------------------------* * 外部变量说明 * *----------------------------------------------*/ /*----------------------------------------------* * 外部函数原型说明 * *----------------------------------------------*/ /*----------------------------------------------* * 内部函数原型说明 * *----------------------------------------------*/ /*----------------------------------------------* * 全局变量 * *----------------------------------------------*/ TComCtrl *g_ptRS485_1; TComCtrl *g_ptRS485_2; TComCtrl *g_ptRS485_3; TComCtrl *g_ptRS485_4; TComCtrl *g_ptLTE_7S0; TComCtrl *g_ptE28_SBUS; TComCtrl *g_ptInterCall; TComCtrl *g_ptFDCAN1; TComCtrl *g_ptFDCAN2; /*----------------------------------------------* * 模块级变量 * *----------------------------------------------*/ /*----------------------------------------------* * 常量定义 * *----------------------------------------------*/ /*----------------------------------------------* * 宏定义 * *----------------------------------------------*/ 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; } int LTE_7S0_Send(char *_pBuffer, uint32_t _iSize) { TUartUserData *ptUartUserData = (TUartUserData *)g_ptLTE_7S0->m_pUserData; int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100); return iRet; } 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; } int InterCall_Send(char *_pBuffer, uint32_t _iSize) { TUartUserData *ptUartUserData = (TUartUserData *)g_ptInterCall->m_pUserData; int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100); return iRet; } int E28_SBUS_Send(char *_pBuffer, uint32_t _iSize) { TUartUserData *ptUartUserData = (TUartUserData *)g_ptE28_SBUS->m_pUserData; int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100); return iRet; } int RS485_3_Send(char *_pBuffer, uint32_t _iSize) { HAL_GPIO_WritePin(RS485_3_DIR_GPIO_Port, RS485_3_DIR_Pin, GPIO_PIN_SET); TUartUserData *ptUartUserData = (TUartUserData *)g_ptRS485_3->m_pUserData; int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100); HAL_GPIO_WritePin(RS485_3_DIR_GPIO_Port, RS485_3_DIR_Pin, GPIO_PIN_RESET); return iRet; } int RS485_4_Send(char *_pBuffer, uint32_t _iSize) { HAL_GPIO_WritePin(RS485_4_DIR_GPIO_Port, RS485_4_DIR_Pin, GPIO_PIN_SET); TUartUserData *ptUartUserData = (TUartUserData *)g_ptRS485_4->m_pUserData; int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100); HAL_GPIO_WritePin(RS485_4_DIR_GPIO_Port, RS485_4_DIR_Pin, GPIO_PIN_RESET); return iRet; } int FDCAN1_Send(char *_pBuffer, uint32_t _iSize) { return CAN_TX_FIFOQ(g_ptFDCAN1, _pBuffer, _iSize); } int FDCAN2_Send(char *_pBuffer, uint32_t _iSize) { return CAN_TX_FIFOQ(g_ptFDCAN2, _pBuffer, _iSize); } #ifndef CONFIG_UART_IT_IDLEDMA extern TComCtrl *g_ptUartCtrl; void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) { UART_RX_IRQHandler(g_ptRS485_1); } else if (huart->Instance == USART2) { UART_RX_IRQHandler(g_ptLTE_7S0); } else if (huart->Instance == USART3) { #ifdef USE_PRINT UART_RX_IRQHandler(g_ptUartCtrl); #else UART_RX_IRQHandler(g_ptRS485_2); #endif } else if (huart->Instance == UART4) { UART_RX_IRQHandler(g_ptInterCall); } else if (huart->Instance == UART5) { UART_RX_IRQHandler(g_ptE28_SBUS); } else if (huart->Instance == USART6) { UART_RX_IRQHandler(g_ptRS485_3); } else if (huart->Instance == UART7) { UART_RX_IRQHandler(g_ptRS485_4); } } #endif void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) //**RS485_1 { HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_RESET); } else if (huart->Instance == USART3) //**RS485_2 { HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_RESET); } else if (huart->Instance == USART6) //**RS485_3 { HAL_GPIO_WritePin(RS485_3_DIR_GPIO_Port, RS485_3_DIR_Pin, GPIO_PIN_RESET); } else if (huart->Instance == UART7) //**RS485_4 { HAL_GPIO_WritePin(RS485_4_DIR_GPIO_Port, RS485_4_DIR_Pin, GPIO_PIN_RESET); } } void HAL_FDCAN_ErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t ErrorStatusITs) { if (hfdcan->Instance == FDCAN1) { MX_FDCAN1_Init(); } else if (hfdcan->Instance == FDCAN2) { MX_FDCAN2_Init(); } } void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) { if (hfdcan->Instance == FDCAN1) { CAN_RX_IRQHandler(g_ptFDCAN1); } else if (hfdcan->Instance == FDCAN2) { CAN_RX_IRQHandler(g_ptFDCAN2); } } void Read_RS485_1(void *argument) { char pcBuffer[52] = {0}; while(1) { __disable_irq(); rd_ComRead(g_ptRS485_1, pcBuffer, 25); __enable_irq(); Rd_Delay(1); } } void SendAll_Task(void *argument) { while(1) { rd_ComSendProc(g_ptRS485_1); Rd_Delay(1); #ifdef USE_PRINT rd_ComSendProc(g_ptUartCtrl); #else rd_ComSendProc(g_ptRS485_2); #endif Rd_Delay(1); rd_ComSendProc(g_ptRS485_3); Rd_Delay(1); rd_ComSendProc(g_ptRS485_4); Rd_Delay(1); rd_ComSendProc(g_ptLTE_7S0); Rd_Delay(1); rd_ComSendProc(g_ptE28_SBUS); Rd_Delay(1); rd_ComSendProc(g_ptInterCall); Rd_Delay(1); rd_ComSendProc(g_ptFDCAN1); Rd_Delay(1); rd_ComSendProc(g_ptFDCAN2); Rd_Delay(1); } } extern void RBcore_Init(void); extern void MotorInit(void); extern void MotorMgr_ParseResponse(uint8_t ucMotorID, const uint8_t *pucData, uint32_t uiLen); // 雷赛电机CAN响应检查函数 // 雷赛响应ID范围:0x581 - 0x5FF (0x580 + MotorID) static int check_LeiSaiMotor(char *_pBuffer, uint32_t _iSize) { if (NULL == _pBuffer || _iSize < 4) { return 0; } // 从buffer中提取CAN ID(前4字节) uint32_t uiFrameID = (uint32_t)(_pBuffer[0] & 0xFF) | ((_pBuffer[1] & 0xFF) << 8) | ((_pBuffer[2] & 0xFF) << 16) | ((_pBuffer[3] & 0xFF) << 24); // 雷赛响应ID范围:0x581 - 0x5FF if (uiFrameID >= 0x581 && uiFrameID <= 0x5FF) { return 8 + 4; // 数据长度8 + CAN ID长度4 } return 0; } // 雷赛电机CAN响应解码函数 static void decode_LeiSaiMotor(const char *_pBuffer, uint32_t _iSize) { if (NULL == _pBuffer || _iSize < 12) // 4字节CAN ID + 8字节数据 { return; } // 从buffer中提取CAN ID uint32_t uiFrameID = (uint32_t)(_pBuffer[0] & 0xFF) | ((_pBuffer[1] & 0xFF) << 8) | ((_pBuffer[2] & 0xFF) << 16) | ((_pBuffer[3] & 0xFF) << 24); // 提取电机ID (0x580 + MotorID) uint8_t ucMotorID = (uint8_t)(uiFrameID - 0x580); // 调用电机管理模块解析响应 MotorMgr_ParseResponse(ucMotorID, (const uint8_t *)&_pBuffer[4], 8); } void Drv_InterfaceInit(void) { const osThreadAttr_t Read_RS485_1_attributes = { .name = "RS485_1", .stack_size = 512, .priority = (osPriority_t) osPriorityHigh6, }; (void)osThreadNew(Read_RS485_1, NULL, &Read_RS485_1_attributes); const osThreadAttr_t send_task_attributes = { .name = "send_task", .stack_size = 512, .priority = (osPriority_t) osPriorityHigh7, }; (void)osThreadNew(SendAll_Task, NULL, &send_task_attributes); MsgCenter_Init(); // 初始化FDCAN1,使用雷赛电机的回调 TCANUserData *ptCANUserData = CAN_userdata_init(&hfdcan1, 8, 0); g_ptFDCAN1 = rd_ComCreate(check_LeiSaiMotor, decode_LeiSaiMotor, FDCAN1_Send, CONFIG_UART_BUFFER_SIZE, ptCANUserData); CAN_IT_init(g_ptFDCAN1); MotorInit(); }