cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive_IT is not working

KaushikSTM32
Associate

Originally a reply in this thread - split as a separate question.


Hello 

My blocking mode is working with "HAL_UART_Receive(&huart4, temp, 1, 100);"

Nonblocking mode is not working

Can you suggest anything to check?

I have defined as said

 

/* USER CODE BEGIN 2 */

HAL_UART_Receive_IT(&huart4, temp, 1);

/* USER CODE END 2 */

 

and

 

/* USER CODE BEGIN 4 */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  HAL_UART_Receive_IT(&huart4, temp, 1);
  HAL_UART_Transmit(&huart4, temp, 1, 100);
}

/* USER CODE END 4 */

 

33 REPLIES 33

Sir,

Thanks for the support.

 

I tried to disable this while loop in Error_Handler also, but still when I send the first byte It stopped responding.

I think it never reaches to HAL_UART_RxCpltCallback this function, It stops before.

 

I did this, I don't know whether this can serve my logic to disable the error handler.

void Error_Handler(void)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

// __disable_irq();

// while (1)

{

}

/* USER CODE END Error_Handler_Debug */

}

 

Thanks,

Please follow the instructions for posting source code:

https://community.st.com/t5/community-guidelines/how-to-insert-source-code/ta-p/693413

 


@KaushikSTM32 wrote:

I tried to disable this while loop in Error_Handler ,


That's not a sensible thing to do!

If your code reaches that point, it means something has failed.

You need to find the reason for that failure - and fix that.

 

So the first thing you need to do is find where the Error Handler is being called, and why.

Andrew Sir,

Shall I know which ISR will be executed first when Controller receives the first byte?

I have chosen Word length 8 with no parity so I think this should be (UART_RxISR_8BIT)  executed first, am I right?

Shall I put toggling on that ISR to know whether it is executing or not with extern variable?

 

Thanks,

Before you do anything else, you need to find out where that error is occurring!

Thank you sir for the useful information.

KaushikSTM32
Associate

void UART4_IRQHandler(void) this ISR routine is not executed while receiving.

Check the stm32xxxhal_msp.c file to be sure any reference to UART4 are there. May look something like this though port pins might be different. 

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  if(huart->Instance==UART4)
  {
  /* USER CODE BEGIN UART4_MspInit 0 */

  /* USER CODE END UART4_MspInit 0 */

  /** Initializes the peripherals clock
  */
    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_UART4;
    PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
      Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_UART4_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**UART4 GPIO Configuration
    PA0     ------> UART4_TX
    PA1     ------> UART4_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* UART4 interrupt Init */
    HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(UART4_IRQn);
  /* USER CODE BEGIN UART4_MspInit 1 */

  /* USER CODE END UART4_MspInit 1 */
  }

 

Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.

@KaushikSTM32 wrote:

UART4_IRQHandler(void) this ISR routine is not executed while receiving.


You still haven't confirmed that you have enabled the interrupt.

Obviously, if the interrupt is not enabled, its handler will never get called!

 

In the code @Karl Yamashita just posted, this is where the interrupt gets enabled:

    /* UART4 interrupt Init */
    HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(UART4_IRQn);

Sir,

I went further into it I had monitored the RDR and it showed the received value correct. 

I also monitored RXNE flag it was setting at 1 while receiving the first byte.

And also RXNEIE is 1. Interrupt routine should be called but it reset RXNE and debugger got lost, not executing anything.

I have checked NVIC in startup file all seems ok.

 

Yes Sir, Interrupt is enabled. I have introduced it also just before while loop.

 

   HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(USART3_IRQn);  // Example for USART1
   USART3->CR1 |= USART_CR1_RXNEIE;  // Enable RX interrupt

 

 

rx.png

Thanks,

 

Yes sir its enabled.

Port pins are also correct.

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  if(huart->Instance==USART3)
  {
  /* USER CODE BEGIN USART3_MspInit 0 */

  /* USER CODE END USART3_MspInit 0 */

  /** Initializes the peripherals clocks
  */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3;
    PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_USART3_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**USART3 GPIO Configuration
    PC10     ------> USART3_TX
    PC11     ------> USART3_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /* USART3 interrupt Init */
    HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART3_IRQn);
  /* USER CODE BEGIN USART3_MspInit 1 */

  /* USER CODE END USART3_MspInit 1 */

  }