STM32CubdeIDE printing messages to UART only when in DEBUG mode
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-24 9:54 PM
Hello. I would like to print certain messages to UART only in DEBUG mode. These prints will not be necessary in release mode.
I have tried the following:
#ifdef DEBUG
printf("hello from debug \n");
#endif
And then I have built the project for both release and debug but the message is printed in both cases. How can I only get message to print in debug mode and not in release mode?
Solved! Go to Solution.
- Labels:
-
DEBUG
-
STM32CubeIDE
-
UART-USART
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:16 AM
Now I am getting more and more confused. It is very strange that such a simple task is so complex to achieve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:23 AM
I have also tried the following:
#define DEBUG 0
#ifdef DEBUG
#define DEBUG_PRINT(X) printf X
#else
#define DEBUG_PRINT(X)
#endif
I have manually set the DEBUG variable to 0 and so the debug printf should not work if I understand this correctly. However, it is still printing
I have an RTOS task which is printing "test" periodically
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
DEBUG_PRINT(("test\n"));
osDelay(1000);
}
/* USER CODE END 5 */
}
st
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:26 AM
It wont let me go to definition. Clicking go to declaration doesnt go anywhere.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:26 AM
It should be simple - DEBUG should just be defined for the 'Debug' configuration in the Project settings.
But that fact that it's not working shows that something's gone wrong ...
If those prints are still appearing, DEBUG must be getting defined somewhere.
Or you're not loading the code you think you are.
have you checked that?
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:29 AM
I am definately loading the right code because each time I change printf message and I can see it update.
I cant find this property page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:32 AM
"I have manually set the DEBUG variable to 0 and so the debug printf should not work if I understand this correctly"
You misunderstand.
I explained this earlier:
#define DEBUG 0
that is a definition of DEBUG; you have defined it to be zero - so it is defined!
Therefore
#ifdef DEBUG
will be true - so all the prints will be enabled.
"'ifdef" means "if defined"
If you want to test the value that has been defined, you need:
#if DEBUG == 1
#define DEBUG_PRINT(X) printf X
#else
#define DEBUG_PRINT(X)
#endif
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:38 AM
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:40 AM
Ok I understand now. I did the following:
For release I define debug 0
For debug I define debug 1
Then in my main.c:
#if(DEBUG==1)
#define DEBUG_PRINT(X) printf X
#else
#define DEBUG_PRINT(X)
#endif
Now the DEBUG_PRINT only works when I am in debug mode exactly how I want to!
Thank you very much for your help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 4:47 AM
Note that it is more conventional for DEBUG to be either defined or not, and to test it with #ifdef.
Anyhow, if the issue is now resolved, please mark the solution:
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-05-25 6:30 AM
Instead of ==1 one of these should be used:
#if DEBUG != 0
#if DEBUG
Anyway, #ifdef is, of course, the most appropriate here.
