2020-04-28 08:46 PM
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).
2020-04-28 11:18 PM
>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);
2020-05-02 05:46 AM
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.