cancel
Showing results for 
Search instead for 
Did you mean: 

Using a defined constant inside HAL_UART_Transmit

skon.1
Senior

Hello,

I'm using STM32 with STM32CubeIDE

This is my code:

#define SOME_DATA 0x01
HAL_UART_Transmit ( & huart6 , SOME_DATA , 1 , 1000 ) ;

The compiler issues the following warning :

passing argument 2 of 'HAL_UART_Transmit' makes pointer from integer without a cast [-Wint-conversion]

What should I do to fix it ?

10 REPLIES 10
TDK
Guru

The value needs to exist in memory in order for you to pass a pointer to it.

const uint8_t SOME_DATA = 0x01;
HAL_UART_Transmit ( & huart6 , &SOME_DATA , 1 , 1000 ) ;

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

Where does it exist when we use:

#define SOME_DATA 0x01

Isn't "memory" the only option ?

skon.1
Senior

And I tried what you suggested...what I get now is this message :

passing argument 2 of 'HAL_UART_Transmit' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

The precompiler replaces every instance of the define with the value before it passes code to the compiler. It’s not a variable in memory. The actual compiler doesnt even know about the define.
If you feel a post has answered your question, please click "Accept as Solution".
Welcome to const correctness hell. Just make it non const.
If you feel a post has answered your question, please click "Accept as Solution".

> Isn't "memory" the only option ?

Options are many. Try this :

#define SOME_DATA 0x01
HAL_UART_Transmit ( & huart6 , (uint8_t[]){ SOME_DATA } , 1 , 1000 ) ;

Write a wrapper subroutine that takes a byte constant instead of a pointer.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I don't see any hell with const types generally. Mostly they safeguard the developer from unintended or unwanted writes. And in those few exceptions casting just ensures that the developer has checked the code logic. The problem here is only present because ST's brainless code monkeys define a data pointers for transmit functions as uint8_t *, while all sane libraries define such pointers as const void *.

Just another HAL stupidity...

Keita
Associate II

the solution to your issue is as simple as using a pointer like this:

 

uint8_t c = 'a';
uint8_t* pd = &c;

uint8_t SOME_DATA = 0x01;
uint8_t* pData = &SOME_DATA ;
HAL_UART_Transmit ( & huart6 , pData, 1 , 1000 ) ;

 

you should be just fine this way 🙂