cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F401 - HAL_UART_Receive() - Buggy?

mwp
Senior
Posted on January 16, 2017 at 19:30

Hi all,

I believe HAL_UART_Receive() is buggy.

Test case:

uint8_t data[2] = {0xAA, 0xBB};

HAL_UART_Receive

(&huart1, &data[0], 1, 100);

You would expect data[0] to be the data received from the USART, and data[1] to be left alone as 1 is the specified size in bytes.

This is not the case. 

HAL_UART_Receive() whacks data[1] as well (sets it to a value of 0).

Its quite clear why this happens looking at the source for 

HAL_UART_Receive()...

HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)

{

uint16_t* tmp;

......

tmp = (uint16_t*) pData;

......

*tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FFU);

......

}

Seems to be a pretty basic mistake?

1 REPLY 1
Posted on January 16, 2017 at 19:42

>>

Seems to be a pretty basic mistake?

Does seem to be the result of a lot of confused thinking, and inappropriate casting. I can see this causing all kinds of unexplained behaviour, on a Cortex-M0 it could lead to alignment faults.

The DR can be 9-bit wide (16-bit Read) to account for the bit/parity options.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..