Search code examples
uartinterrupt-handlinghal

HAL_UART_Receive_IT only runs once


Am using HAL library but the reception interrupt only fires once.

I've tried debugging it but i still cant figure out where to start from.

Am not sure which status flag that is set so that i could re-enable it or disable it to make it run another round. The datasheet is a bit sheety coz STM isn't providing a detailed copy. Am using an 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;
        }
    }
}

Solution

  • I had the same problem with you and had many tests in five days, but the results were not good. But I solved this problem with the help of someone I learned through someone' blog. So I want yout to share the solution. My problem was that I used printf in interrupt route and callback. This is wrong behaviour. Also, the debugger only works once. Therefore, the results received should be verified by the trasmit command. I used TX with UART3 and RX with UART6. If there was a lot of RX data, there was a problem with sending TX as it is, so I had to print the 100th data. I confirmed that 3000Bytes are working without any problem. If you want my test code, send me the following email address. nicehans72@gmail.com. I'll let you go.

    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 */
    }