HAL_UART_Receive_IT 只运行一次

HAL_UART_Receive_IT only runs once

我正在使用 HAL 库,但接收中断只触发一次。

我试过调试它,但我仍然不知道从哪里开始。

我不确定设置了哪个状态标志,以便我可以重新启用它或禁用它以使其 运行 另一轮。数据表有点厚,因为 STM 没有提供详细的副本。我正在使用 STM32F303K8。

uint8_t rcvd, count = 0, reception_complete = FALSE;
char buffer[100];

int main(void)
{
    HAL_Init();
    SystemClockConfig();
    UART2_Init();

    __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

    while(1){

        if(reception_complete == FALSE) {
            HAL_UART_Receive_IT(&huart2, &rcvd, 1);
        }
        else {
            ReturnProcessedString();
            reception_complete = FALSE;
        }
    }

    return 0;
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART2)
    {
        if(rcvd == '\r'){
            reception_complete = TRUE;
            buffer[count++] = '\r';

            count = 0;

        }else {
            buffer[count++] = rcvd;
        }
    }
}

我和你有同样的问题,五天内测试了很多次,结果都不好。但是我在通过某人的博客了解到的人的帮助下解决了这个问题。所以我希望你分享解决方案。 我的问题是我在中断路由和回调中使用了 printf。这是错误的行为。此外,调试器只工作一次。因此,收到的结果应该由 trasmit 命令验证。我将 TX 与 UART3 配合使用,将 RX 与 UART6 配合使用。如果RX数据很多,直接发送TX就会有问题,所以我不得不打印第100个数据。我确认 3000Bytes 没有任何问题。如果您想要我的测试代码,请将以下电子邮件地址发送给我。 nicehans72@gmail.com。我放过你

main.c

volatile uint8_t rxd[10];
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */


  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_UART7_Init();
  MX_UART8_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  MX_USART6_UART_Init();
  /* USER CODE BEGIN 2 */

    HAL_UART_Receive_IT(&huart6, (uint8_t *)rxd, 1);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

int a=0;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
    a++;
    if(UartHandle->Instance == USART6)
    {
        if (a%100==0)
            HAL_UART_Transmit(&huart3, (uint8_t *)rxd, 1, 0xFFFF);
        HAL_UART_Receive_IT(&huart6, (uint8_t *)rxd, 1);
    }
}

static void MX_USART3_UART_Init(void)
{

  /* USER CODE BEGIN USART3_Init 0 */

  /* USER CODE END USART3_Init 0 */

  /* USER CODE BEGIN USART3_Init 1 */

  /* USER CODE END USART3_Init 1 */
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 9600;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART3_Init 2 */

  /* USER CODE END USART3_Init 2 */

}

static void MX_USART6_UART_Init(void)
{

  /* USER CODE BEGIN USART6_Init 0 */

  /* USER CODE END USART6_Init 0 */

  /* USER CODE BEGIN USART6_Init 1 */

  /* USER CODE END USART6_Init 1 */
  huart6.Instance = USART6;
  huart6.Init.BaudRate = 9600;
  huart6.Init.WordLength = UART_WORDLENGTH_8B;
  huart6.Init.StopBits = UART_STOPBITS_1;
  huart6.Init.Parity = UART_PARITY_NONE;
  huart6.Init.Mode = UART_MODE_TX_RX;
  huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart6.Init.OverSampling = UART_OVERSAMPLING_16;
  huart6.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart6.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart6) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART6_Init 2 */

  /* USER CODE END USART6_Init 2 */

}

stm32f7xx_it.c

void USART3_IRQHandler(void)
{
  /* USER CODE BEGIN USART3_IRQn 0 */

  /* USER CODE END USART3_IRQn 0 */
  HAL_UART_IRQHandler(&huart3);
  /* USER CODE BEGIN USART3_IRQn 1 */

  /* USER CODE END USART3_IRQn 1 */
}

void USART6_IRQHandler(void)
{
  /* USER CODE BEGIN USART6_IRQn 0 */

  /* USER CODE END USART6_IRQn 0 */
  //
    HAL_UART_IRQHandler(&huart6);
  /* USER CODE BEGIN USART6_IRQn 1 */

  /* USER CODE END USART6_IRQn 1 */
}