Page 1 of 4

chprintf / DEBUG Macro

Posted: Mon Jan 12, 2015 3:36 pm
by Tabulous
I'm try to create my own DEBUG routines by using chprintf.

chprintf works fine when called like this
chprintf((BaseSequentialStream*)&SD6, "my string");


But i want to add some conditions around this so i tried this

in my debug.h file i have a macro
#define EDC_DEBUG( x, y, ... ) chprintf(&SD6, y)

now if i call macro EDC_DEBUG like this
EDC_DEBUG( DBG_MASK_ATC, "my string" );


It outputs rubbish, am i doing something wrong here, sure i'm missing something.

Re: chprintf / DEBUG Macro

Posted: Mon Jan 12, 2015 5:11 pm
by utzig
The snippets of code you copied look perfectly fine to me. There must be something else causing the corruption.

Re: chprintf / DEBUG Macro

Posted: Mon Jan 12, 2015 5:21 pm
by Tabulous
its something more sinister than than, like i said the chprintf((BaseSequentialStream*)&SD6, "my string"); works fine.

its is something to do with the macro not been defined correctly, this can be tricky where variadic is concerned.

Re: chprintf / DEBUG Macro

Posted: Mon Jan 12, 2015 5:25 pm
by utzig
So, if you remove the variadic parameter it all works?

Re: chprintf / DEBUG Macro

Posted: Mon Jan 12, 2015 5:42 pm
by utzig
Btw, just did a little test here and your macro definition should probably look like this:

Code: Select all

#define EDC_DEBUG( x, y, ... ) chprintf(&SD6, y, ## __VA_ARGS__)


Some code I used to test (not on ChibiOS):

Code: Select all

#include <stdio.h>

#define DEBUG(x, y, ...) printf(y, ## __VA_ARGS__)

int main(void) {
        DEBUG(0, "test\n");
        DEBUG(1, "test value=%d\n", 1);
        DEBUG(2, "test values=(%d, %d)\n", 1, 2);
        return 0;
}


The output looks correct:

Code: Select all

test
test value=1
test values=(1, 2)


More info: https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

Re: chprintf / DEBUG Macro

Posted: Mon Jan 12, 2015 6:31 pm
by Tabulous
Thanks utzig :-)

Re: chprintf / DEBUG Macro

Posted: Mon Feb 02, 2015 8:14 am
by Chudik
Based on this thread and Beginners - Command Shell I created a base version for my Debug messages processing.
With my shell I can set 3 levels of debug messages (could be any, but I think 3 will be enough). Level set in global variable console_debug.

Here are the fragments of my code:

console.h

Code: Select all

...
#define M3_DEBUG( level, ... ) if(console_debug && level <= console_debug) chprintf(console_cfg.sc_channel, ## __VA_ARGS__)
...
extern uint8_t console_debug;
...



console.c

Code: Select all

...
uint8_t console_debug = 0;

void cmd_debug(BaseSequentialStream *chp, int argc, char *argv[])
{
  uint8_t dlevel;


  if (argc != 1)
  {
    chprintf(chp, "Usage: debug [0..3]\r\n");
    chprintf(chp, "\t0 - no debug info\r\n");
    chprintf(chp, "\t1 - debug level 1\r\n");
    chprintf(chp, "\t2 - debug level 2\r\n");
    chprintf(chp, "\t3 - debug level 3\r\n");
    chprintf(chp, "Debug level %d\r\n", console_debug);
    return;
  }

    dlevel = argv[0][0];

    if (isdigit(dlevel))
    {
      switch(dlevel)
      {
        case '0': console_debug = 0;
                  chprintf(chp, "Debug off\r\n");
                  return;
        case '1': console_debug = 1; break;
        case '2': console_debug = 2; break;
        case '3': console_debug = 3; break;
        default:  chprintf(chp, "No debug level change\r\n"); return;
      }

      chprintf(chp, "Debug level %c\r\n", dlevel);
    }
    else
    {
        chprintf(chp, "No debug level change\r\n");
    }
}
...

I know it could be written more effective :-)



And in the main (of course it is Blinky, what else to use for beginning :-))
main.c

Code: Select all

....

 static msg_t Blinky(void *arg)
 {
     (void)arg;

     chRegSetThreadName("Blinky");

     while(1)
     {
        palSetPad(GPIOA, GPIOA_LED_GREEN);
        chThdSleepMilliseconds(500);
        M3_DEBUG(2,"LED ON\r\n");

        palClearPad(GPIOA, GPIOA_LED_GREEN);
        chThdSleepMilliseconds(500);
        M3_DEBUG(3,"LED OFF\r\n");
     }

         return 0;
 }
...

When I set debug level 0 (off) or 1, there are no messages on terminal. When I set 2, only "LED ON" messages appear, and when I set 3, both messages shown in terminal. So, it works as expected.
However, I'm not sure that I correctly accessing the console_debug variable. What is the correct way of accessing the global variable in multitasking environment? May I use such global variable or I should use some other way?

Re: chprintf / DEBUG Macro

Posted: Mon Feb 02, 2015 9:12 am
by Giovanni
Hi,

Global variables are perfectly normal variables unless accessed by more than one thread, if this is the case then you should evaluate implementing a mutual exclusion on the variable, it depends how it is used, usually if there are read-modify-write operations on the variable then you could need mutual exclusion.

Giovanni

Re: chprintf / DEBUG Macro

Posted: Mon Feb 02, 2015 10:10 am
by Chudik
Yeah, that what I was expecting... Definitely I will need to use it from several threads. Need to read about the mutexes.

Is it OK to use chprintf with one channel from several threads? Does this function resolves conflicts internally?

Re: chprintf / DEBUG Macro

Posted: Mon Feb 02, 2015 12:01 pm
by Giovanni
Hi,

chprintf() writes to a stream, in itself it can be used in multiple threads but written data can get corrupted because concurrent writes to the same stream.
For example writing concurrently "ciao" and "mamma" could result in something like "cimamaoma".

For mutual exclusion over a simple variable you don't need mutexes (heavy mechanism), you may use chSysLock() and chSysUnlock() for a lightweight critical zone.

Giovanni