我使用STM32CubeIDE中的STM32L451RCTx。项目中需要通过usart2对接收到的请求进行响应。我能够一次性接收请求并成功响应,但在响应传输完成后,无法再次接收请求。当usart线路上收到数据时,usart2接收中断未被触发。
--> 初始化部分如下:
static void MX_USART2_UART_Init(void) { /* USER CODE BEGIN USART2_Init 0 */ /* USER CODE END USART2_Init 0 */ /* USER CODE BEGIN USART2_Init 1 */ /* USER CODE END USART2_Init 1 */ huart2.Instance = USART2; huart2.Init.BaudRate = 9600; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART2_Init 2 */ if(HAL_UART_Receive_IT(&huart2, &uart2_buffer_u8, 1)) { Error_Handler(); } /* USER CODE END USART2_Init 2 */ } --> In Interrupt handler side, 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 */ CLEAR_BIT(huart2.Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ CLEAR_BIT(huart2.Instance->CR3, USART_CR3_EIE); /* Rx process is completed, restore huart->RxState to Ready */ huart2.RxState = HAL_UART_STATE_READY; /* Clear RxISR function pointer */ huart2.RxISR = NULL; uint8_t status = 0; status = HAL_UART_Receive_IT(&huart2, &uart2_buffer_u8, 1); if (status || huart2.ErrorCode != 0 || huart2.ErrorCode != 34) { /* error handler */ /* Re-Initialize to avoid missing of messages */ status = HAL_UART_Receive_IT(&huart2, &uart2_buffer_u8, 1); if (status) { /* error handler */ } } //USART_ClearITPendingBit(&huart2,USART_IT_TC); /* USER CODE END USART2_IRQn 1 */ } --> Interrupt Call back function void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance == USART2) { mgmtLocalRx = uart2_buffer_u8; if ((mgmtLocalRx == FRAMEHEAD) && (mgmtBMS_RxCounter != 0)) { mgmtBMS_RxCounter = 0; mgmtBMS_Rx_Buffer[mgmtBMS_RxCounter++] = mgmtLocalRx; }
else if ((mgmtLocalRx == FRAMEHEAD) && (mgmtBMS_RxCounter == 0)) { mgmtBMS_Rx_Buffer[mgmtBMS_RxCounter++] = mgmtLocalRx; } else if ((mgmtBMS_RxCounter < 5) && (mgmtBMS_RxCounter != 0)){ mgmtBMS_Rx_Buffer[mgmtBMS_RxCounter++] = mgmtLocalRx;
} else if ((mgmtLocalRx == FRAMEEND) && (mgmtBMS_RxCounter >= 5)){ mgmtBMS_Rx_Buffer[mgmtBMS_RxCounter++] = mgmtLocalRx; uartRxSection = true; mgmtBMS_RxCounter = 0; } } }
|