cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle Variable length receive data via UART?

TMuka.1
Associate II
uint8_t rx_buffer[32], rx_flag, rx_index, rx_data, RxData[32];
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if (huart->Instance==USART1)
	{
		//if the data is not being received, clear the buffer
		if(rx_index ==0)
		{
			for (int i=0; i<20; i++)
			{
				rx_buffer[i]=0;
			}
		}
		//if the character received is other than 'enter' ASCII13, save the data in buffer
		if(rx_data!=13)
		{
			rx_buffer[rx_index++]=rx_data;
		}
		else
		{
			rx_index=0;
			rx_flag=1;  // turn the rx_flag 'ON'
			HAL_UART_Transmit(&huart1, rx_buffer, sizeof(rx_buffer), 100); // transmit the data via UART
		}
		HAL_UART_Receive_IT(&huart1, &rx_data, 1);  // restart the interrupt reception mode & receive only 1 char at a time!
 
	}
 
}

I declared a 32byte rx_buffer[], when I send more than 32 Bytes, the board couldn't receive any more serial data through interrupts! Attached my sample callback function and code inside that portion!

Regards,

Thangz

15 REPLIES 15

stm32l1xx_hal_msp_template.c file is not expected to be compiled. Maybe you added it the the list of objects in your projects ?

However, if you are using CubeMx, the generated stm32l1xx_hal_msp.c file should contain a valid HAL_MspInit() body (for example all peripheral clocks are to be enabled here). Without this function body, it can't work.

Could you use the previous content of HAL_MspInit() function when you were able to receive some chars ?

@Guenael Cadier , @TMuka.1 , @TDK 

I find the solution the Guenael Cadier suggested (UART DMA circular buffer ReceptionToIdle) exactly what I am leaning to with my application (variable length commands) and I will be moving now towards the implementation.

I cannot see in the Examples\UART\ folder the UART_ReceptionToIdle_CircularDMA example for STM32L05x (CubeIDE 1.12.1)

Actually there is no implementation of HAL_UARTEx_ReceiveToIdle_DMA for STM32L0 family in stm32l0xx_hal_uart.

Can you please indicate where I could find/download examples? Or does L0 family not support this function?

Regards

Guenael Cadier
ST Employee

Hi @_alaBaster 

I think ReceiveToIdle API should be available in latest STM32L0 HAL drivers version (please check in stm32l0xx_uart_ex.c). If not, maybe you could have a look in latest  version of STM32L0 HAL drivers available on github here .
API prototypes are present in stm32l0xx_hal_driver/Src/stm32l0xx_hal_uart_ex.c

Regards

There aren't any examples in the L0, but you can adapt the examples for another family.

The function definition is here:

stm32l0xx_hal_driver/Src/stm32l0xx_hal_uart_ex.c at 0eac588d92ca6ab730621131c71614ce240bab8a · STMicroelectronics/stm32l0xx_hal_driver (github.com)

If you feel a post has answered your question, please click "Accept as Solution".

Hi @Guenael Cadier got it, thanks. I was looking only at stm32l0xx_hal_uart.c

Kind regards

ok great, thanks. Now it is time to dive into it! 🙂