View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Debug Printfs |
Posted: Tue Dec 01, 2015 10:33 pm |
|
|
Hi All,
I have a very long and very verbose code with plenty printfs via software uart.
The final circuit does not have any way for me to see these printfs unless I physically plug an FTDI chip or Bluetooth radio.
I would like to be able to turn these on/off on command as to not waste any power and especially TIME executing these software printfs while I'm not playing big brother on my circuit.
ive considered using the #ifdef way but that is more of a All or Nothing route.
I'd like to en/dis-able them during run time.
Am i supposed to place an "if Flag then print" on all of them?
Suggestions appreciated.
thanks,
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Wed Dec 02, 2015 3:45 am |
|
|
The standard way is to change the actual definition of the debug printf.
So instead of using 'printf', use 'debug_printf', and #define this to be the printf you want to use (either a standard printf to the required stream, or nothing). |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Dec 02, 2015 5:56 am |
|
|
Hi Ttelmah,
Thanks for your input.
Could i enable / disable the printfs during run time WITHOUT recompiling?
Thanks,
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Wed Dec 02, 2015 6:09 am |
|
|
Only by encapsulating them, and this then will cost code.
So you have the debug_printf, print to your own output routine (debug_putc), and then have this coded something like:
Code: |
int1 debug_on=TRUE;
void debug_putc(int8 chr)
{
if (debug_on)
fputc(chr, DEBUG_STREAM; //send the character to the debug stream
}
|
If you set 'debug_on' to false, the debug_putc, will just return immediately, without printing anything. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1357
|
|
Posted: Wed Dec 02, 2015 7:16 am |
|
|
I do something similar to that, but move the check to outside the printf. This is only practical with the version 5 compiler as it supports variadic macros
Code: |
BOOLEAN g_debugEnabled = TRUE;
#define dbg_printf(...) if(g_debugEnabled){ fprintf(DEBUG_STREAM,__VA_ARGS__);}
|
Sometimes I wrap it in a #ifdef checking for the existence of DEBUG_STREAM so I also have the option of completely removing the code via the preprocessor.
something like:
Code: |
#ifdef DEBUG_STREAM
BOOLEAN g_debugEnabled = TRUE;
#define ENABLE_DEBUG() g_debugEnabled=TRUE
#define DISABLE_DEBUG() g_debugEnabled=FALSE
#define dbg_printf(...) if(g_debugEnabled){ fprintf(DEBUG_STREAM,__VA_ARGS__);}
#else
#define ENABLE_DEBUG()
#define DISABLE_DEBUG()
#define dbg_printf(...)
#endif
|
Then I just have to remember to use the DEBUG_STREAM as a #define:
Code: |
#define DEBUG_STREAM STREAM_UART1
#use rs232(UART1,baud=9600,errors,stream=DEBUG_STREAM)
#include "debugging.h"
|
where debugging.h has all that stuff above.
EDIT: those are all typed out by hand, so it is possible I made a typo or two. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Dec 02, 2015 8:40 am |
|
|
Idea:
what are the consequences of a "Setup_Uart(0);" on a software Uart Printf?
Would this be another possible way to do this?
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Wed Dec 02, 2015 9:59 am |
|
|
"This function is only available on devices with a built in UART." |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Fri Dec 04, 2015 5:23 am |
|
|
If I'm using the debugger monitor window with
Code: | #use rs232(DEBUGGER, stream=STREAM_MONITOR, xmit=PIN_B2, rcv=PIN_B2) |
Is this enabled only in debug mode? i.e. is the Code: | fprintf(STREAM_MONITOR, .. etc | code called in normal build? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Fri Dec 04, 2015 9:35 am |
|
|
You have to test for being in debug mode 'round' your print, or the print code will be generated in all build modes. |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Mon Dec 07, 2015 3:52 am |
|
|
Ttelmah wrote: | You have to test for being in debug mode 'round' your print, or the print code will be generated in all build modes. |
Sadly manually I think, because the compiler doesn't automatically make a _DEBUG_ symbol when in debug mode. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Mon Dec 07, 2015 4:51 am |
|
|
Don't get me wrong, but it does seem as if a 'complex' solution is being sought for a simple thing.
#DEFINE DEBUG
#DEFINE DEBUG_PRINT
Then have all your debug code (printouts, the actual selection of the debugger etc.), all enabled by these defines.
Just selecting the DEBUGGER printout stream, does not automatically imply you are actually running an ICD. I'll often print to the DEBUG hardware port, while actually executing the code (other than the prints) in 'run' mode. Means you don't have to have a MAX232, and saves pins. So you don't want this controlled or linked to the ICD options.
Sometimes trying to tie things together too much is more complex/restrictive than allowing people just to select what they want....
However key is that when you are writing code, you must think about the debugging as you write. I usually set up macros like 'DEBUG_MESSAGE', which contains the tests, and outputs a message as needed. This has become much easier (used to have several supporting different message types, but the last few dozen CCS releases support variable length argument lists in macros - makes this much easier/nicer...).
The original poster seems to be looking for a complex way to do a simple thing.
Just use search and replace, to replace all the printfs that are 'debug only', with something like 'debug_printf'. Assuming they all will talk to a particular stream, a search for printf(STREAMNAME will find them all.
Then just have a single #define, that makes debug_printf into a blank statement or into the required output, depending on whether you are debugging or not. Job done. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Dec 07, 2015 11:45 am |
|
|
My original intention was to be able to switch the printfs on and off DURING RUNTIME without having to recompile in the simplest way.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Mon Dec 07, 2015 11:56 am |
|
|
Which the 'debug_on' flag I showed earlier does. Just route all your debug printf's through this. So
printf(debug_putc,"what you want to print");
This can then be disabled by setting 'debug_on' to FALSE; |
|
|
nuclear__
Joined: 24 Jan 2015 Posts: 63
|
adapt this on usb output |
Posted: Tue Jan 19, 2016 10:26 am |
|
|
Ttelmah, how can i modify my code to use your solution and subtitute the line below.
Printf(usb_cdc_putc,"debug test ");
?
What should be the definition on debug_stream ? |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Tue Jan 19, 2016 10:42 am |
|
|
Another option you might want to consider (it still involves a flag test around each printf) is to use an available pin with a jumper. If the jumper is in place (say to a pull-up or your choice), print else not print. Sort of a "bench mode" if you will where the board behaves differently (prints debug info) if the debug jumper is in place.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
|