使用DMA中断接收串口 不定量数据
设置如下:
1. 接收中断增加 ?? ?void HAL_UART_IRQHandler(UART_HandleTypeDef *huart) ?? ?增加 ?? ?if(((isrflags & USART_ISR_IDLE) != RESET) && ((cr1its & USART_CR1_IDLEIE) != RESET)) ?? ? { ?? ?//Record the received bytes number ?? ? Rev_Size = huart->RxXferSize - huart->hdmarx->Instance->CNDTR; ?? ?//clear the IDLE flag ?? ?__HAL_UART_CLEAR_IDLEFLAG(huart); ?? ?//Abord the received process ?? ?HAL_UART_AbortReceive_IT(huart); ?? ? return; ?? ? }
2. DMA接收增加 ?? ?HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
?? ?//Modification 1 ?? ?SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3. 空闲中断改动 ?? ?HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
?? ?//ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); ?? ?CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE| USART_CR1_IDLEIE));
4. 重写空闲接收 void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart) { ?? ?if(huart->Instance == LPUART1)?? ?// 判断是由哪个串口触发的中断 ?? ?{ ?? ??? ?UartReady1 = SET; ?? ?} ?? ?else?? ?if(huart->Instance == USART2)?? ?// 判断是由哪个串口触发的中断 ?? ?{ ?? ??? ?UartReady2 = SET; ?? ??? ? ?? ??? ?for(uint16_t i = 0; i < Rev_Size; i++) ?? ??? ?{ ?? ??? ?//printf(" 0x%02X", DMARxBuf1[i]); ?? ??? ?printf("%c", DMARxBuf1[i]); ?? ??? ?} ?? ??? ? ?? ??? ?//Re-start receiving ?? ??? ?HAL_UART_Receive_DMA(&huart2,(uint8_t*)DMARxBuf1,BUFFERSIZE); ?? ?} ?? ?else?? ?if(huart->Instance == USART4)?? ?// 判断是由哪个串口触发的中断 ?? ?{ ?? ??? ?UartReady4 = SET; ?? ?} }
测试效果:
?
|