#include "bsp_UART.h" #include "main.h" #include #include "DLT/DLTuc.h" void GF_UART_Send_List_Send(struct UARTHandler *handler); void Dispatcher_List_Add(struct UARTHandler *uartHandler, void (*dispache)(void)); void UARTHandlerAddTxList(struct UARTHandler *uartHandler, uint8_t *data, uint16_t length, uint32_t txListTimePeriod, void (*UART_Decode)(uint8_t*, uint16_t)); //Store all the data in these buffer uint8_t RS485_1_RxMA_buf[UART_Receive_MAX_NUM]; uint8_t RS485_2_RxDMA_buf[UART_Receive_MAX_NUM]; uint8_t RS485_3_RxDMA_buf[UART_Receive_MAX_NUM]; uint8_t RS485_4_RxDMA_buf[UART_Receive_MAX_NUM]; uint8_t Debug_RxDMA_buf[UART_Receive_MAX_NUM]; uint8_t ExternSerial_RxDMA_buf[UART_Receive_MAX_NUM]; uint8_t RS485_1_TxDMA_buf[UART_Transmit_MAX_NUM]; uint8_t RS485_2_TxDMA_buf[UART_Transmit_MAX_NUM]; uint8_t RS485_3_TxDMA_buf[UART_Transmit_MAX_NUM]; uint8_t RS485_4_TxDMA_buf[UART_Transmit_MAX_NUM]; uint8_t Debug_TxDMA_buf[UART_Transmit_MAX_NUM]; uint8_t ExternSerial_TxDMA_buf[UART_Transmit_MAX_NUM]; #define InterCall_DEBUG_UART huart4 #define E28_SBUS_UART huart5 #define RS485_4_UART huart7 #define RS485_1_UART huart1 #define LTE_7S0_Serial_UART huart2 #define RS485_2_UART huart3 #define RS485_3_UART huart6 struct UARTHandler RS_485_1_UART_Handler; struct UARTHandler RS_485_2_UART_Handler; struct UARTHandler RS_485_3_UART_Handler; struct UARTHandler RS_485_4_UART_Handler; struct UARTHandler InterCall_DEBUG_UART_Handler; struct UARTHandler E28_SBUS_UART_Handler; struct UARTHandler LTE_7S0_Serial_UART_Handler; #if defined (hlpuart1Exit) #define LPUART1UART hlpuart1 struct UARTHandler LPUART1_UART_Handler; #endif void GF_BSP_UARTHandlers_Intialize( int32_t RS485_1_WaitTime, int32_t RS485_2_WaitTime, int32_t RS485_3_WaitTime, int32_t RS485_4_WaitTime, int32_t LTE_7S0_Serial_WaitTime, int32_t InterCall_DEBUG_WaitTime, int32_t E28_SBUS_WaitTime, int32_t LPUART1_UART_WaitTime, int32_t RS485_1_Dispacher_Time, int32_t RS485_2_Dispacher_Time, int32_t RS485_3_Dispacher_Time, int32_t RS485_4_Dispacher_Time, int32_t LTE_7S0_Serial_Dispacher_Time, int32_t InterCall_DEBUG_Dispacher_Time, int32_t E28_SBUS_Dispacher_Time, int32_t LPUART1_UART_Dispacher_Time ) { IntializeUARTHandler(&RS_485_1_UART_Handler, &RS485_1_UART, RS485_1_WaitTime, 2,RS485_1_Dispacher_Time); IntializeUARTHandler(&RS_485_2_UART_Handler, &RS485_2_UART, RS485_2_WaitTime, 2,RS485_2_Dispacher_Time); IntializeUARTHandler(&RS_485_3_UART_Handler, &RS485_3_UART, RS485_3_WaitTime, 2,RS485_3_Dispacher_Time); IntializeUARTHandler(&RS_485_4_UART_Handler, &RS485_4_UART, RS485_4_WaitTime, 2,RS485_4_Dispacher_Time); IntializeUARTHandler(<E_7S0_Serial_UART_Handler, <E_7S0_Serial_UART, LTE_7S0_Serial_WaitTime, 2,LTE_7S0_Serial_Dispacher_Time); IntializeUARTHandler(&InterCall_DEBUG_UART_Handler, &InterCall_DEBUG_UART, InterCall_DEBUG_WaitTime, 2,InterCall_DEBUG_Dispacher_Time); IntializeUARTHandler(&E28_SBUS_UART_Handler, &E28_SBUS_UART, E28_SBUS_WaitTime, 2,E28_SBUS_Dispacher_Time); HAL_UART_Receive_IT((E28_SBUS_UART_Handler.uart), (uint8_t*) &E28_SBUS_UART_Handler.tmp_Rx_Buf, 1); HAL_UART_Receive_IT((InterCall_DEBUG_UART_Handler.uart), (uint8_t*) &InterCall_DEBUG_UART_Handler.tmp_Rx_Buf, 1); HAL_UART_Receive_IT((RS_485_1_UART_Handler.uart), (uint8_t*) &RS_485_1_UART_Handler.tmp_Rx_Buf, 1); HAL_UART_Receive_IT((RS_485_2_UART_Handler.uart), (uint8_t*) &RS_485_2_UART_Handler.tmp_Rx_Buf, 1); HAL_UART_Receive_IT((RS_485_3_UART_Handler.uart), (uint8_t*) &RS_485_3_UART_Handler.tmp_Rx_Buf, 1); HAL_UART_Receive_IT((RS_485_4_UART_Handler.uart), (uint8_t*) &RS_485_4_UART_Handler.tmp_Rx_Buf, 1); HAL_UART_Receive_IT((LTE_7S0_Serial_UART_Handler.uart), (uint8_t*) <E_7S0_Serial_UART_Handler.tmp_Rx_Buf, 1); #if defined (hlpuart1Exit) IntializeUARTHandler(&LPUART1_UART_Handler, &LPUART1UART, LPUART1_UART_WaitTime, 2,LPUART1_UART_Dispacher_Time); //10ms 剩余2ms HAL_UART_Receive_IT((LPUART1_UART_Handler.uart), (uint8_t*) &LPUART1_UART_Handler.tmp_Rx_Buf, 1); #endif GF_BSP_Interrupt_Add_CallBack( DF_BSP_InterCall_TIM8_2ms_PeriodElapsedCallback, GF_BSP_UART_Timer); //RS_485_2_UART_Handler.send_finished=1; } //Count Every 2 ms void GF_BSP_UART_Timer() { Counting(&RS_485_1_UART_Handler); Counting(&RS_485_2_UART_Handler); Counting(&RS_485_3_UART_Handler); Counting(&RS_485_4_UART_Handler); Counting(<E_7S0_Serial_UART_Handler); Counting(&InterCall_DEBUG_UART_Handler); Counting(&E28_SBUS_UART_Handler); #if defined (hlpuart1Exit) Counting(&LPUART1_UART_Handler); #endif // RS_485_1_UART_Handler.Dispatcher_Run(&RS_485_1_UART_Handler); // RS_485_2_UART_Handler.Dispatcher_Run(&RS_485_2_UART_Handler); // RS_485_3_UART_Handler.Dispatcher_Run(&RS_485_3_UART_Handler); // RS_485_4_UART_Handler.Dispatcher_Run(&RS_485_4_UART_Handler); // E22_Serial_UART_Handler.Dispatcher_Run(&E22_Serial_UART_Handler); // InterCall_DEBUG_UART_Handler.Dispatcher_Run(&InterCall_DEBUG_UART_Handler); // InterCall_DEBUG_UART_Handler.Dispatcher_Run(&InterCall_DEBUG_UART_Handler); } void UARTHandlerAddTxList(struct UARTHandler *uartHandler, uint8_t *data, uint16_t length, uint32_t txListTimePeriod, void (*UART_Decode)(uint8_t*, uint16_t)) { UARTSendHandler *pTmp = NULL; //临时指针 //临时指针2用于逐个申请内存 pTmp = (UARTSendHandler*) malloc(sizeof(UARTSendHandler)); memcpy(pTmp->Tx_Buf, data, length); pTmp->pNext = NULL; pTmp->SendListTimePeriod=txListTimePeriod; pTmp->SendLength = length; pTmp->UART_Decode = UART_Decode; //if NULL, call intialize one if (uartHandler->pCurrentUARTSendHadler == NULL) { uartHandler->pCurrentUARTSendHadler = pTmp; //空链表 } else { char i = 0; //插到尾部 UARTSendHandler *phead = NULL; phead = uartHandler->pCurrentUARTSendHadler; while (phead->pNext != NULL) { i++; phead = phead->pNext; } phead->pNext = pTmp; } } void UARTHandlerTx(struct UARTHandler *uartHandler) { if (uartHandler->uart == NULL) { LOGFF(DL_ERROR, "the UART Hardware did not intialize,check the setting"); return; } uartHandler->RxCount=0;//设定RxCount为0 #if defined (hlpuart1Exit) if(uartHandler->uart->Instance == LPUART1) { HAL_UART_Transmit(uartHandler->uart, uartHandler->Tx_Buf, uartHandler->TxCount,100); }else { #endif if (uartHandler->uart->Instance == UART4) //**DEBUG { } else if (uartHandler->uart->Instance == USART1) //**RS485_1 { HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_SET); } else if (uartHandler->uart->Instance == USART3) //**RS485_2 { HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_SET); } else if (uartHandler->uart->Instance == USART6) //**RS485_3 { HAL_GPIO_WritePin(RS485_3_DIR_GPIO_Port, RS485_3_DIR_Pin, GPIO_PIN_SET); } else if (uartHandler->uart->Instance == UART7) //**RS485_4 { HAL_GPIO_WritePin(RS485_4_DIR_GPIO_Port, RS485_4_DIR_Pin, GPIO_PIN_SET); } else if (uartHandler->uart->Instance == USART2)//**E22 // External serial port { } else if (uartHandler->uart->Instance == UART5) //**E28 // SBUS { } SCB_CleanInvalidateDCache_by_Addr((uint32_t*) uartHandler->Tx_Buf, uartHandler->TxCount); HAL_UART_Transmit_DMA(uartHandler->uart, uartHandler->Tx_Buf, uartHandler->TxCount); #if defined (hlpuart1Exit) } #endif if (uartHandler->uart == UART4) //**DEBUG { } else if (uartHandler->uart == USART1) //**RS485_1 { HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_RESET); } else if (uartHandler->uart == USART3) //**RS485_2 { HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_RESET); } else if (uartHandler->uart == USART6) //**RS485_3 { HAL_GPIO_WritePin(RS485_3_DIR_GPIO_Port, RS485_3_DIR_Pin, GPIO_PIN_RESET); } else if (uartHandler->uart == UART7) //**RS485_4 { HAL_GPIO_WritePin(RS485_4_DIR_GPIO_Port, RS485_4_DIR_Pin, GPIO_PIN_RESET); } else if (uartHandler->uart == USART2) //**E22 // External serial port { } else if (uartHandler->uart == UART5) //**E28 // SBUS { } } void UARTHandlerRX(struct UARTHandler *uartHandler) { //uartHandler->Wait_Time_Count = 0; if (uartHandler->startCountFlag == 0) { uartHandler->startCountFlag = 1; uartHandler->RxCount = 0; } uartHandler->Rx_Buf[uartHandler->RxCount] = uartHandler->tmp_Rx_Buf[0]; uartHandler->RxCount++; HAL_UART_Receive_IT(uartHandler->uart, uartHandler->tmp_Rx_Buf, 1); uartHandler->Wait_Time_Count = 0; if(uartHandler->RxCount>=1024)//接收数据过多,直接抛弃 { uartHandler->RxCount=0; } //HAL_UART_Receive_IT(uartHandler->uart, RS_485_2_UART_Handler.tmp_Rx_Buf, 1); } void IntializeUARTHandler(struct UARTHandler *uartHandler, UART_HandleTypeDef *uart, int32_t WaitTime, unsigned char timeSpan,int32_t Dispacher_Time) { uartHandler->Wait_Time_Count = 0; uartHandler->Wait_time = WaitTime; uartHandler->uart = uart; uartHandler->UART_Rx = UARTHandlerRX; uartHandler->UART_Tx = UARTHandlerTx; uartHandler->AddSendList = UARTHandlerAddTxList; uartHandler->dispacherController = (DispacherController*) malloc( sizeof(DispacherController)); uartHandler->dispacherController->pHead = NULL; uartHandler->dispacherController->pTail = NULL; uartHandler->dispacherController->Dispacher_Enable = 0; uartHandler->dispacherController->DispacherCallTime = Dispacher_Time ; // call the function every 50 ms uartHandler->dispacherController->Dispacher_Counter = 0; uartHandler->dispacherController->DispacherNumber = 0; uartHandler->dispacherController->Add_Dispatcher_List = Dispatcher_List_Add_t; uartHandler->dispacherController->Dispatcher_Run = Dispatch_t; uartHandler->timeSpan = 2; uartHandler->SendList_time_Count = 0; uartHandler->SendList_Period = 100; } void Counting(struct UARTHandler *uartHandler) { if (uartHandler->UART_Decode == NULL) //不解析,直接返回true { uartHandler->decode_finished = 1; } else { if (uartHandler->startCountFlag == 1) { uartHandler->Wait_Time_Count++; if (uartHandler->timeSpan * uartHandler->Wait_Time_Count >= uartHandler->Wait_time) { uartHandler->Wait_Time_Count = 0; uartHandler->startCountFlag = 0; //启动解析函数 if (uartHandler->RxCount >= 1) { uartHandler->UART_Decode(uartHandler->Rx_Buf, uartHandler->RxCount); uartHandler->RxCount = 0; } } } } if (uartHandler->pCurrentUARTSendHadler != NULL) { GF_UART_Send_List_Send(uartHandler); } else { uartHandler->dispacherController->Dispatcher_Run( uartHandler->dispacherController); } // uartHandler->Send_Time_Count++; // if (uartHandler->timeSpan * uartHandler->Send_Time_Count // >= uartHandler->DispacherCallPeriod) // { // uartHandler->Send_Time_Count = 0; // if (uartHandler->pCurrentUARTSendHadler != NULL) // { // //拷贝数据到相关的代码中,然后发送 // uartHandler->UART_Decode = // uartHandler->pCurrentUARTSendHadler->UART_Decode; // // memcpy(uartHandler->Tx_Buf, // uartHandler->pCurrentUARTSendHadler->Tx_Buf, // uartHandler->pCurrentUARTSendHadler->TxCount); // // uartHandler->TxCount = uartHandler->pCurrentUARTSendHadler->TxCount; // uartHandler->UART_Tx(uartHandler); // // if (uartHandler->pCurrentUARTSendHadler->pNext != NULL) // { // UARTSendHandler *temp = // uartHandler->pCurrentUARTSendHadler->pNext; // free(uartHandler->pCurrentUARTSendHadler); //清除内存 // uartHandler->pCurrentUARTSendHadler = temp; // } else // { // free(uartHandler->pCurrentUARTSendHadler); //清除内存 // uartHandler->pCurrentUARTSendHadler = NULL; // } // // } // // } } //_weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == UART4) //**DEBUG { UARTHandlerRX(&InterCall_DEBUG_UART_Handler); //GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_DEBUG_RxCpltCallback); } else if (huart->Instance == USART1) //**RS485_1 { //GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_RS485_1_RxCpltCallback); UARTHandlerRX(&RS_485_1_UART_Handler); } else if (huart->Instance == USART3) //**RS485_2 { //GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_RS485_2_RxCpltCallback); UARTHandlerRX(&RS_485_2_UART_Handler); } else if (huart->Instance == USART6) //**RS485_3 { //GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_RS485_3_RxCpltCallback); UARTHandlerRX(&RS_485_3_UART_Handler); } else if (huart->Instance == UART7) //**RS485_4 { // UARTHandlerRX(&RS_485_4_UART_Handler); //GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_RS485_4_RxCpltCallback); UARTHandlerRX(&RS_485_4_UART_Handler); } else if (huart->Instance == USART2) //**E22 // External serial port { // GF_BSP_Interrupt_Run_CallBack( // DF_BSP_InterCall_E22_Serial_RxCpltCallback); UARTHandlerRX(<E_7S0_Serial_UART_Handler); } else if (huart->Instance == UART5) //**E28 // SBUS { // GF_BSP_Interrupt_Run_CallBack( // DF_BSP_InterCall_E28_SBUS_RxFifo0Callback); UARTHandlerRX(&E28_SBUS_UART_Handler); } #if defined (hlpuart1Exit) else if (huart->Instance == LPUART1) //**E28 // SBUS { UARTHandlerRX(&LPUART1_UART_Handler); } #endif } void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == UART4) //**DEBUG { } else 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); } else if (huart->Instance == USART2) //**E22 // External serial port { } else if (huart->Instance == UART5) //**E28 // SBUS { } } //RS485_Index:1-4 RS485; 5 ExternSerial; 6 Debug; void GF_BSP_UART_Transmit(const uint8_t RS485_Index, const uint8_t *pData, uint16_t Size) { UART_HandleTypeDef *huart; if (Size > UART_Transmit_MAX_NUM) Size = UART_Transmit_MAX_NUM; switch (RS485_Index) { case 1: huart = &huart1; HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_SET); memcpy(RS485_1_TxDMA_buf, pData, Size); SCB_CleanInvalidateDCache_by_Addr((uint32_t*) RS485_1_TxDMA_buf, Size); HAL_UART_Transmit_DMA(huart, RS485_1_TxDMA_buf, Size); //HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff); break; case 2: huart = &huart3; HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_SET); memcpy(RS485_2_TxDMA_buf, pData, Size); SCB_CleanInvalidateDCache_by_Addr((uint32_t*) RS485_2_TxDMA_buf, Size); HAL_UART_Transmit_DMA(huart, RS485_2_TxDMA_buf, Size); break; case 3: huart = &huart6; HAL_GPIO_WritePin(RS485_3_DIR_GPIO_Port, RS485_3_DIR_Pin, GPIO_PIN_SET); memcpy(RS485_3_TxDMA_buf, pData, Size); SCB_CleanInvalidateDCache_by_Addr((uint32_t*) RS485_3_TxDMA_buf, Size); HAL_UART_Transmit_DMA(huart, RS485_3_TxDMA_buf, Size); break; case 4: huart = &huart7; HAL_GPIO_WritePin(RS485_4_DIR_GPIO_Port, RS485_4_DIR_Pin, GPIO_PIN_SET); memcpy(RS485_4_TxDMA_buf, pData, Size); SCB_CleanInvalidateDCache_by_Addr((uint32_t*) RS485_4_TxDMA_buf, Size); HAL_UART_Transmit_DMA(huart, RS485_4_TxDMA_buf, Size); break; case 5: huart = &huart2; memcpy(ExternSerial_TxDMA_buf, pData, Size); SCB_CleanInvalidateDCache_by_Addr( (uint32_t*) ExternSerial_TxDMA_buf, Size); HAL_UART_Transmit_DMA(huart, ExternSerial_TxDMA_buf, Size); break; default: huart = &huart4; memcpy(Debug_TxDMA_buf, pData, Size); SCB_CleanInvalidateDCache_by_Addr((uint32_t*) Debug_TxDMA_buf, Size); HAL_UART_Transmit_DMA(huart, Debug_TxDMA_buf, Size); break; } } //void USART_Dispatch(struct UARTHandler *uartHandler) //{ ////2ms // if (uartHandler->Dispacher_Enable == 1) // { // uartHandler->Dispacher_Counter++; // if (uartHandler->DispacherNumber > 0) //列表中有数据 // { // if (uartHandler->Dispacher_Counter * uartHandler->timeSpan // >= uartHandler->DispacherCallPeriod // / uartHandler->DispacherNumber) //多长时间运行一次 // { // // uartHandler->Dispacher_Counter = 0; // if (uartHandler->pHead != NULL // && uartHandler->pHead->pNext != NULL) // { // uartHandler->pHead->dispache(); // uartHandler->pHead = uartHandler->pHead->pNext; // } else // { // // } // } // } // // } // //} // //void Dispatcher_List_Add(struct UARTHandler *uartHandler, // void (*dispache)(void)) //{ // Dispatcher *pTmp = NULL; //临时指针2 // if (uartHandler->pHead == NULL && uartHandler->pTail == NULL) //头尾部都为空 // { // uartHandler->pHead = uartHandler->pTail = (Dispatcher*) malloc( // sizeof(Dispatcher)); // uartHandler->pTail->dispache = dispache; // uartHandler->pTail->pNext = uartHandler->pHead; // uartHandler->DispacherNumber++; // } else // { // //临时指针2用于逐个申请内存 // pTmp = (Dispatcher*) malloc(sizeof(Dispatcher)); // pTmp->dispache = dispache; // pTmp->pNext = uartHandler->pHead; //set the new dispatcher .next to the header, thus make it a circle // //临时指针1的next指向刚分配内存的临时指针2 // uartHandler->pTail->pNext = pTmp; // // uartHandler->pTail = pTmp; //set pTail the last node of this ring // uartHandler->DispacherNumber++; // } //} void GF_UART_Send_List_Send(struct UARTHandler *handler) { handler->SendList_time_Count++; if (handler->timeSpan * handler->SendList_time_Count >= handler->SendList_Period) { handler->SendList_time_Count = 0; handler->SendListExists = 1; if (handler->pCurrentUARTSendHadler != NULL) { //拷贝数据到相关的代码中,然后发送 //handler->CAN_Decode = handler->pCurrentCANSendHadler->CAN_Decode; // memcpy(handler->Tx_Buf, handler->pCurrentUARTSendHadler->Tx_Buf, handler->pCurrentUARTSendHadler->SendLength); handler->SendList_Period = handler->pCurrentUARTSendHadler->SendListTimePeriod; handler->TxCount = handler->pCurrentUARTSendHadler->SendLength; handler->UART_Tx(handler); if (handler->pCurrentUARTSendHadler->pNext != NULL) { UARTSendHandler *temp = handler->pCurrentUARTSendHadler->pNext; free(handler->pCurrentUARTSendHadler); //清除内存 handler->pCurrentUARTSendHadler = temp; } else { free(handler->pCurrentUARTSendHadler); //清除内存 handler->pCurrentUARTSendHadler = NULL; } } else { handler->SendListExists = 0; } } }