cancel
Showing results for 
Search instead for 
Did you mean: 

BUG in HAL_UART_Transmit() and HAL_IRDA_Transmit()

redmig
Associate III

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..

1 ACCEPTED SOLUTION

Accepted Solutions
Guenael Cadier
ST Employee

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

View solution in original post

5 REPLIES 5
Imen.D
ST Employee

Hello,

Which Cube firmware version used ?

Regards

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
redmig
Associate III

H7 v1.3.2

Bob S
Principal

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
Associate III

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
ST Employee

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