Electronic – STM32 UART How to receive 9 bit

stm32

I am trying to understand how I can setup the UART peripheral to receive 9 bit data blocks.

I am using the ST IDE (STM32 cube IDE) and the Nucleo-F103RB board. I can setup the peripheral in 9bit mode using the STM32 Cube. But the HAL function to transmit/receive data still uses an 8bit pointer to a data buffer.

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

How can I receive the 9th bit?

Best Answer

If you are trying to stuff 9 bits of data into an 8 bit integer, that simply wont work. The compiler would likely cut off the 9th bit, or complain you didn't pass it an array.

You will need to pass it an array of 8 bit integers large enough to capture your received data. In this case, you would need 2 8 bit integers, giving you 16 bits of bit-space for the API to stuff its 9 bits into. The HAL function will write to both integers in the array you passed it. You will need to apply some fancy bit-masking and typecasting to massage the buffer into a native type, or decompose it further into whatever schema you have devised.