Skip to main content
skon.1
Associate III
August 20, 2020
Question

Using a defined constant inside HAL_UART_Transmit

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 ?

This topic has been closed for replies.

10 replies

TDK
August 20, 2020

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
skon.1Author
Associate III
August 20, 2020

Where does it exist when we use:

#define SOME_DATA 0x01

Isn't "memory" the only option ?

TDK
August 20, 2020
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""."
skon.1
skon.1Author
Associate III
August 20, 2020

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]

TDK
August 21, 2020
Welcome to const correctness hell. Just make it non const.
"If you feel a post has answered your question, please click ""Accept as Solution""."
Piranha
Principal III
August 22, 2020

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

Tesla DeLorean
Guru
August 22, 2020

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

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Associate
November 13, 2023

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 :)

Associate
November 13, 2023

you can pass the address directly too like this

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

Or you can also force it a bit with on the fly casting like this

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