Skip to main content
redmig
Associate III
April 11, 2019
Solved

BUG in HAL_UART_Transmit() and HAL_IRDA_Transmit()

  • April 11, 2019
  • 5 replies
  • 1487 views

This is the signature of the function:

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

where pData is the pointer to the data to be transmitted and Size the amount of data to be sent. In case of wordlength = 9bits and NO parity:

huart->TxXferSize = Size;
huart->TxXferCount = Size;
while(huart->TxXferCount > 0U)
 {
 huart->TxXferCount--;
 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
 {
 return HAL_TIMEOUT;
 }
 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
 {
 tmp = (uint16_t*) pData;
 huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
 pData += 2U;
 }
 else
 {
 huart->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
 }
 }

pData is incremented by 2 until it reaches 2 times the size of the array.. and BOOM!

Same problem with HAL_IRDA_Transmit(). With the receive the array has to be big enough (2xSize). Definetly confusing..

This topic has been closed for replies.
Best answer by Guenael Cadier

Hello,

@Bob S​ analysis is right : in case of 9bit data with no parity, each "item" sent/received by application is using 2 bytes in buffer provided as function parameter for HAL_UART_Transmit/Receive APIs.

So, sending/receiving a 5 items buffer requires for instance :

  • 10 bytes buffer in case of 9bit data/no parity
  • 5 bytes buffer in all other cases.

Agree this could be made clearer in function header (only following statement is present in code part for some series, as a comment : "/* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */). I took the point to request this to be improved in future releases.

Regards

Guenael

5 replies

ST Technical Moderator
April 11, 2019

Hello,

Which Cube firmware version used ?

Regards

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
redmig
redmigAuthor
Associate III
April 11, 2019

H7 v1.3.2

Bob S
Super User
April 11, 2019

According to the function header, the "size" parameter is "amount of data to be sent". Perhaps not perfectly clear, but I interpret this as meaning the number of data "items" to be sent, NOT necessarily the size of the buffer. For 9-bit data, each "item" is, well, 9 bits, which takes 2 bytes to store. So to send 4 each 9-bit data "items", your buffer needs to hold 8 bytes, and "size" passed to HAL_UART_Transmit() should be set to 4.

redmig
redmigAuthor
Associate III
April 15, 2019

mmm.. I see your point. Still not clear at all. Maybe "number of words to be sent" or "items", as you say, would be clearer. And maybe an additional check on the parameters wouldn't hurt...

Guenael Cadier
Guenael CadierBest answer
ST Employee
April 15, 2019

Hello,

@Bob S​ analysis is right : in case of 9bit data with no parity, each "item" sent/received by application is using 2 bytes in buffer provided as function parameter for HAL_UART_Transmit/Receive APIs.

So, sending/receiving a 5 items buffer requires for instance :

  • 10 bytes buffer in case of 9bit data/no parity
  • 5 bytes buffer in all other cases.

Agree this could be made clearer in function header (only following statement is present in code part for some series, as a comment : "/* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */). I took the point to request this to be improved in future releases.

Regards

Guenael