Skip to main content
SLuit.1
Associate III
June 21, 2021
Question

Having problem with UART communication in Interrupt mode

  • June 21, 2021
  • 2 replies
  • 792 views

I am doing a project on STM32H747 DISCO Discovery board involving UART. I am sending one string continuoulsy with 5 seconds delay from esp32 to stm32. I am able to receive the string in polling mode but I am not able to receive the string in Interrupt mode.

I am using UART 8 and I have configure GPIO for the pins as follows:

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOJ_CLK_ENABLE();

 /*Configure GPIO pin: PJ8   For D1 pin for TX */

 GPIO_InitStruct.Pin = GPIO_PIN_8;

 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

 //GPIO_InitStruct.Pull = GPIO_NOPULL; //pullup

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 GPIO_InitStruct.Alternate = GPIO_AF8_UART8;

 HAL_GPIO_Init(GPIOJ, &GPIO_InitStruct);

 /*Configure GPIO pin: PJ9   For D0 pin for RX */

 GPIO_InitStruct.Pin = GPIO_PIN_9;

 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; //MODE_ALTERNATE?

 //GPIO_InitStruct.Pull = GPIO_NOPULL; //or NOPULL?

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 GPIO_InitStruct.Alternate = GPIO_AF8_UART8;

 HAL_GPIO_Init(GPIOJ, &GPIO_InitStruct);

}

I have initialised the UART as follow:

void MX_UART8_Init(void){

//Peripheral clock enable

__HAL_RCC_UART8_CLK_ENABLE();

__HAL_UART_CLEAR_IDLEFLAG(&huart8);

__HAL_UART_ENABLE_IT(&huart8, UART_IT_IDLE); //Enable serial port idle interrupt

huart8.Instance = UART8;

huart8.Init.BaudRate = 115200;

huart8.Init.WordLength = UART_WORDLENGTH_8B;

huart8.Init.StopBits = UART_STOPBITS_1;

huart8.Init.Parity = UART_PARITY_NONE;

huart8.Init.Mode = UART_MODE_TX_RX;

huart8.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart8.Init.OverSampling = UART_OVERSAMPLING_16;

huart8.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart8.Init.ClockPrescaler = UART_PRESCALER_DIV1;

huart8.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

HAL_UART_Init(&huart8);

if (HAL_UART_Init(&huart8) != HAL_OK)

 {

  Error_Handler();

 }

//UART8 Idle Interrupt Configuration

HAL_NVIC_SetPriority(UART8_IRQn, 1, 1);

HAL_NVIC_EnableIRQ(UART8_IRQn);

}

I called the function below before while(1) loop inside main:

 HAL_UART_Receive_IT (&huart8, (uint8_t*) &Rx_data, sizeof(Rx_data));

I called this complete interrupt function and tried to print some string to terminal once it is triggered.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

if(huart->Instance == UART8 ){

  HAL_UART_Receive_IT(&huart8, (uint8_t*) &Rx_data, sizeof(Rx_data));

  HAL_UART_Transmit(&huart3,(void*)"Data Received\r\n",15, 100);

}

}

Do I need to enable or configure something somewhere else in the library as well to make the interrupt work. I could not found any document regarding it . The only thing that I have found while surfing on the internet and example projects are the things that I have explained above.

Could you please help me out with it and let me know the problem as I am having problem with such a simple thing.

This topic has been closed for replies.

2 replies

Technical Moderator
June 29, 2021

Hello @SLuit.1​ ,

Have a look at this FAQ STM32 UART DMA RX/TX, it may be helpful for you.

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
waclawek.jan
Super User
June 29, 2021

> I am not able to receive the string in Interrupt mode.

What are the symptoms? Is the ISR called at all?

> HAL_UART_Transmit(&huart3,(void*)"Data Received\r\n",15, 100);

It's not a good idea to call a lengthy blocking function in the ISR.

JW