|
发表于 2018-4-18 15:55:29
|
显示全部楼层
hal库的串口中断里面确实没有写如空闲中断 但是hal库预留了接口 自己手动启用 自己写个空闲回掉函数
以下是我的代码 F030的 增加了防溢出的功能 请硬汉大哥评断
/**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET)
{
HAL_UART_RxIdleCallback(&huart1);
}
/* USER CODE END USART1_IRQn 1 */
}
/**
* @brief This function handles USART2 global interrupt.
*/
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE) != RESET)
{
HAL_UART_RxIdleCallback(&huart2);
}
/* USER CODE END USART2_IRQn 1 */
}
/*********************************************************************************************************
** @FunctionName HAL_UART_RxCpltCallback
** @Description 串口接收完成中断回调函数
** @InputParam huart: 串口句柄
** @OutputParam NONE
** @ReturnValue NONE
**
*********************************************************************************************************/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint8_t i = 0;
uint8_t len = 0;
if(huart->Instance == USART1)
{
len = UART1_REV_LEN - huart->hdmarx->Instance->CNDTR;
for(i=0; i<len; i++)
{
Uart1_RevData.UartRevData[Usart1_RevLen+i] = Usart1RxBuffer[i];
}
Usart1_RevLen += len;
HAL_UART_Receive_DMA(huart, Usart1RxBuffer, UART1_REV_LEN);
}
if(huart->Instance == USART2)
{
len = UART2_REV_LEN - huart->hdmarx->Instance->CNDTR;
for(i=0; i<len; i++)
{
Uart2_RevData.UartRevData[Usart2_RevLen+i] = Usart2RxBuffer[i];
}
Usart2_RevLen += len;
HAL_UART_Receive_DMA(huart, Usart2RxBuffer, UART2_REV_LEN);
}
}
/*********************************************************************************************************
** @FunctionName HAL_UART_RxIdleCallback
** @Description 串口空闲中断回调函数
** @InputParam huart: 串口句柄
** @OutputParam NONE
** @ReturnValue NONE
**
*********************************************************************************************************/
void HAL_UART_RxIdleCallback(UART_HandleTypeDef *huart)
{
uint8_t i = 0;
uint8_t len = 0;
if(huart->Instance == USART1)
{
__HAL_UART_CLEAR_IDLEFLAG(huart);
HAL_UART_DMAStop(huart);
len = UART1_REV_LEN - huart->hdmarx->Instance->CNDTR;
for(i=0; i<len; i++)
{
Uart1_RevData.UartRevData[Usart1_RevLen+i] = Usart1RxBuffer[i];
}
// memcpy(&Uart1_RevData.UartRevData[Usart_RevLen], Usart1RxBuffer, len);
Usart1_RevLen += len;
Uart1_RevData.UartRevLen = Usart1_RevLen;
if(osMessagePut(Queue_Usart1RevHandle, (uint32_t)&Uart1_RevData, 0) != osOK)
{
printf("Q Send Err\r\n");
}
// printf("idle rev len:%d\r\n", len);
Usart1_RevLen = 0;
HAL_UART_Receive_DMA(huart, Usart1RxBuffer, UART1_REV_LEN);
}
if(huart->Instance == USART2)
{
// printf("uart2 idle it\r\n");
__HAL_UART_CLEAR_IDLEFLAG(huart);
HAL_UART_DMAStop(huart);
len = UART2_REV_LEN - huart->hdmarx->Instance->CNDTR;
for(i=0; i<len; i++)
{
Uart2_RevData.UartRevData[Usart2_RevLen+i] = Usart2RxBuffer[i];
}
Usart2_RevLen += len;
Uart2_RevData.UartRevLen = Usart2_RevLen;
if(osMessagePut(Queue_Usart2RevHandle, (uint32_t)&Uart2_RevData, 0) != osOK)
{
printf("Q Send Err\r\n");
}
Usart2_RevLen = 0;
HAL_UART_Receive_DMA(huart, Usart2RxBuffer, UART2_REV_LEN);
}
}
|
|