STM32 UART Interrupt Handling with Ending Character

In this post, we will explain an effective way of the interrupt based UART communication. It is easier to deal with the constant buffer size on the interrupt based communications. The interrupt can be triggered by initiating the communication with receiving a determined number of characters. In STM32 Architecture, using the HAL libraries shortens the development process significantly and the community helps the developers on the process. In the case of the interrupt based uart communication, the high level HAL_UART_Receive_IT function can be used with its callback HAL_UART_RxCpltCallback.

If there is no rule on the buffer size of each transfer block, the conventional method is to take advantage of ending character, so the buffer will be filled until the ending character is received, then it is ready to process. The handling can be developed by the HAL_UART_Receive_IT function with the size of 1, then the same initiation can be done at the end of the HAL_UART_RxCpltCallback, but it is problematic to operate in this way. There is an option for the development of the same operation in LL Libraries. However, it is possible to develop the same operation with the lower level functions and macros of the HAL library.

void USART1_IRQHandler(void)
{

	__HAL_UART_DISABLE_IT(&huart1, UART_IT_RXNE);

	if(index == 0)
		memset(Buffer, 0, BUFFER_LENGTH);

	if((&huart1)->Instance->DR != 'q'){

		Buffer[index++] = (&huart1)->Instance->DR;

	}
	else{

		index = 0;
		rxTransferCpltFlag = 1;

	}

	__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);

}

In the proposed method, the initiation will be activated by __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE), then each character is processed at interrupt handler until the ‘q’ which is the ending character is received. The simple STM32F4 project with the interrupt handling with the ending character can be seen at this repository.  

Leave a Reply

Your email address will not be published. Required fields are marked *