cancel
Showing results for 
Search instead for 
Did you mean: 

UART Interrupt Issues

KVeer.1
Associate II

Hello all,

I am very new to the STM32 environment and am just getting started with some of the basic things with the platform.

I am trying to simply read a byte of transfer from the computer on the STM32 using UART. I have enabled the interrupts and am trying to mess with the

void USART2_IRQHandler(void)

The issue is that I have no clue how to access a buffer after a receive event has completed. I suppose we are expected to pass the address of a buffer variable to let the pointer transfer the data based on what I found written in stm32f4xx_hal_uart.c file. I am using the HAL drivers for this. And therefore I am doing the following,

extern UART_HandleTypeDef huart2;
uint8_t buffer;
void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
 
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_6);
	huart2.pRxBuffPtr = &buffer; //passing the buffer's address as a parameter to the pointer Not sure if this is right :\
	if((buffer == 't')||(buffer == 'T'))
		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8);
 
 
	__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_TC);
	__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_RXNE);
  /* USER CODE END USART2_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */
 
  /* USER CODE END USART2_IRQn 1 */
}

The idea is to read the buffer and toggle a GPIO if character "t" is read.

Also, my UART initialization is as follows:

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 = 115200;
  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;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */
	__HAL_UART_ENABLE_IT(&huart2, UART_IT_TC);
	__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
  /* USER CODE END USART2_Init 2 */
 
}

Any help with the resolution of this will be greatly appreciated.

Thanks in advance!

2 REPLIES 2
AAl-H
Associate III

Hello,

If I understood correctly, you're receiving a byte from your computer and you want it to be stored in the variable 'buffer' right?

For that, you should do:

buffer = huart2.Instance->DR;

In general, your code in not optimal and might only work for 1 character

And I don't know why you're clearing the RXNE flag and TC flag

Also, I don't think you should enable UART_IT_TC flag in Init (not sure tho)

Tell me if it still doesn't work

Pavel A.
Evangelist III

Before tweaking the interrupt handler, read the HAL source and understand what it does to receive.

(Some say that reading the HAL sources is a pain .... maybe they have a point).

The HAL interrupt handler is too complex because it covers all possible cases that are not relevant or you, for example 8/9 bit units or FIFO modes.

Try to write your own small and efficient handler.

Consider the low level (LL) API instead of HAL, thru the USART2 structure - but then, maybe consider accessing the registers directly,

Decent knowledge of C is absolutely required.

-- pa