Skip to main content
AStar.4
Associate
May 27, 2021
Solved

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

  • May 27, 2021
  • 3 replies
  • 2537 views

I am a new member, I am learning about UART data transmission of MCU STM32F411RE on a nucleon board. I have read and understood the function HAL_UART_Transmit. However, in this function, there is a part I don't understand. I don't understand the meaning of the program below:

if (huart->Init.WordLength == UART_WORDLENGTH_9B)

     {

       if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)

       {

         return HAL_TIMEOUT;

       }

       tmp = (uint16_t *) pData;

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

       if (huart->Init.Parity == UART_PARITY_NONE)

       {

         pData += 2U;

       }

       else

       {

         pData += 1U;

       }

     }

In the 9-Bit frame mode, why after each assignment of data from the pData pointer to the DR register:

- pData += 2U if Parity = PARITY_NONE

- pData += 1U if Parity != PARITY_NONE

As I understand it, after: tmp = (uint16_t *) pData;

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

Then the pointer will be incremented by 1 to assign the next memory area to DR. So why is there a case where the pData pointer grows to 2U. If so, is it possible that the transmitted data will be wrong?

Hope everyone can answer. Thank you.

This topic has been closed for replies.
Best answer by TDK

If you are sending 9 bits of data, you must store each value in a uint16_t which takes 2 bytes. Since pData is of type uint8_t*, in order to increment it to the next value, the pointer must increase by 2.

If you are sending 9 bits, but only 8 of them are data and 1 is parity, the data fits in a uint8_t which is one byte.

3 replies

TDK
TDKBest answer
May 27, 2021

If you are sending 9 bits of data, you must store each value in a uint16_t which takes 2 bytes. Since pData is of type uint8_t*, in order to increment it to the next value, the pointer must increase by 2.

If you are sending 9 bits, but only 8 of them are data and 1 is parity, the data fits in a uint8_t which is one byte.

"If you feel a post has answered your question, please click ""Accept as Solution""."
AStar.4
AStar.4Author
Associate
May 28, 2021

Dear @TDK​ , thank you for your reply. Your answer helped me understand the essence of the problem.

I would like to review my understanding as below:

If I want to send 9 Bit data, I need to declare and transfer to the function a 2 Byte Data(uint16_t)

Since the pData pointer declared in the function is 8 Bit (uint8_t), it is necessary to increase pData by 2U to correctly point at the next location of the data area

For example, if I want to pass an Array: uint16_t Data_9Bit[2] = {0x1AB, 0x1FF};

0693W00000AQ7yPQAT.pngSo, the transmitted data will be guaranteed to be correct and accurate.

TDK
May 28, 2021

Correct. You would need to convert the pointer to uint8_t * to call the function.

HAL_UART_Transmit(huart, (uint8_t *) Data_9Bit, 2, timeout);

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