Skip to main content
Robin Kuzmin
Associate
April 29, 2020
Question

Signature issue in HAL function HAL_UART_Transmit(). How to file a bug/{change request}?

  • April 29, 2020
  • 2 replies
  • 1290 views

Function:

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

The param pData should be `const uint8_t *pData`, because the data pointed to by that pointer are read only.

If I pass a `const char *` argument to to this param then the compiler complains:

error: invalid conversion from 'const char*' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

(i.e. the function signature tries to remove const, which is bad).

This topic has been closed for replies.

2 replies

alister
Senior III
April 29, 2020

>The param pData should be `const uint8_t *pData`

Yes it's low quality and it's a bug in my book too. Better if it were declared like this:

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

>because the data pointed to by that pointer are read only.

The "const" in the prototype doesn't mean the data pointed to by pData must be read-only. It means only the function won't modify what pData points at.

>error: invalid conversion from 'const char*' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

The compiler knows the data is read-only and doesn't know the function won't try to modify it.

Obviously the function shouldn't modify the data. But you can confirm by reading it. The work-around is to cast the pData argument as (uint8_t *) in the function call like this:

static const uint8_t myData[] = {1,2,3};
myStatus = HAL_UART_Transmit(&huart, (uint8_t *)myData, sizeof(myData), MY_TIMEOUT);

Piranha
Principal III
May 2, 2020

In all sane APIs such a universal data input parameters are const void *pData so that one doesn't have to cast it everywhere the API functions are called.