chprintf / DEBUG Macro

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Has thanked: 7 times
Been thanked: 1 time

Re: chprintf / DEBUG Macro

Post by Chudik »

Hi Giovanni,
So, chSysLock() and chSysUnlock() basically are not linked to any particular thread and could be used for short time access to any global variable. And this mechanism prevent simultaneous access to this variable from several threads. Is it correct?
I looked to the source code for these functions. For me it seemed like stopping any interrupts events. Does it mean, that even system clock will not interrupt for switching to another task?

Changed my macro M3_DEBUG

Code: Select all

#define M3_DEBUG( level, ... ) {                            \
     chSysLock();                                           \
    if(console_debug && level <= console_debug)             \
    { chSysUnlock();                                        \
      chprintf(console_cfg.sc_channel, ## __VA_ARGS__);     \
    }                                                       \
    else                                                    \
        chSysUnlock();                                      \
}


it works, but it is not as elegant as I would like it to be. Adding this macro will increase code space to significant number of bytes for each implementation. Probably will change it later by cloning chprintf itself with comparing level with console_debug inside the function.

However, my original idea for the console debug messages was different. I was planning to use a queue where my debug function/macro put the strings and debug messages thread would send these strings to the console stream. That should prevent corrupting messages from several tasks on the console. I used similar algorithm in my quasi-multitasking project about 7 years ago :-)

The currently used serial driver already has such queues, and the system has only I/O Queues. So, it should be some another mechanism. Besides, I would not like to transfer text data from one queue to another.
The algorithm I'm thinking about now is following (since i'm not fully familiar with the system yet, some or all statements will be wrong :)) :
  • Create debug messages task("DConsole", for example). Probably with priority of 1 level higher than NORMALPRIO.
  • Create EventSource for DConsole. Let's say DM_Event (Debug Message event).
  • Each task that requires to bring debug message to console, has it's own global buffer for such messages. So far they will be static without using malloc().
  • These tasks register their flags in DM_Event and somehow registers pointer to it's own message buffer. Total up to 32 flags/task. That should be enough so far.
  • When a task needs to send message, it forms the message in its buffer with chsprintf(...) or memcpy and set according flag in DM_Event.
  • DConsole uses its EventListener, transfer according buffer to the stream with chprintf(...) or another lighter function and clears according flag in DM_Event.

Could you comment, where I'm right and wrong for this mechanism? Is there a better way that is more elegant, requires less resources, more logical, etc...? May be everything is much easy and I'm inventing a bicycle? :-)

There are some notes:
1. Our system uses quite slow stream of input data - sample rates for the signals are lower than 1 Hz. There should be enough time to send message to console and, therefore, tasks probably will not need to check whether their previous requests were served or not. So, no need to check whether flag cleaned.
2. There will be faster task like ModBus slave, but I will try to minimize debug messages from it. Or maybe for this task I will check the flag before filling up the message buffer.
3. I'm aware that shell will be working in parallel to such task and when I type in a command it will corrupt the debug messages, but I don't care.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chprintf / DEBUG Macro

Post by Giovanni »

Hi,

Critical zones do not drop interrupts (common misconception) just delay them.

Anyway, you could give this article a look before taking decisions about your problem.

About your console thread idea, see synchronous messages for that, I usually use those for those kind of "server" entities.

Code: Select all

static thread_t *console;

/*
 * Console server thread, the argument is the stream where message
 * must be printed.
 */
static THD_WORKING_AREA(waConsoleServerThread, 512);
static THD_FUNCTION(ConsoleServerThread, arg) {
  BaseSequentialStream *stream = (BaseSequentialStream *)arg;

  while (true) {
    /* Waiting for a queued message then retrieving it.*/
    thread_t *tp = chMsgWait();
    const char *msg = (const char *msg)chMsgGet(tp);

    /* Printing message the message prefixing it with a time stamp.*/
    chprintf(stream, "%010d: %s\r\n", chVTGetSystemTime(), msg);

    /* Sending back an acknowledge.*/
    chMsgRelease(tp, MSG_OK);
  }
}

/*
 * Initialization.
 */
void main(void) {
 
  /*
   * System initializations.
   * - The Hardware Abstraction Layer is initialized.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /* Opening the serial port 1.*/
  sdStart(&SD1, NULL);

  /* Starting the console thread making it print over the serial
     port.*/
  console = chThdCreateStatic(waConsoleServerThread, sizeof(waConsoleServerThread),
                              NORMALPRIO + 1, ConsoleServerThread, (void *)&SD1);

  (void)chMsgSend(console, (msg_t)"System started.");

  /* Continuing.*/
  ...;
}


Giovanni
User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Has thanked: 7 times
Been thanked: 1 time

Re: chprintf / DEBUG Macro

Post by Chudik »

Giovanni,
Seems like thread_t, THD_WORKING_AREA, THD_FUNCTION are parts of ChibiOS 3.0, not 2.6.6 that I'm using now.

Is the version 3.0 stable enough that could be recommended to use?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chprintf / DEBUG Macro

Post by Giovanni »

There are equivalents in 2.6.6. 3.0 is perfectly stable already.

Giovanni
User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Has thanked: 7 times
Been thanked: 1 time

Re: chprintf / DEBUG Macro

Post by Chudik »

I can't find where download v.3.0 from :?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chprintf / DEBUG Macro

Post by Giovanni »

Repository trunk or by downloading ChibiStudio 8.

Giovanni
User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Has thanked: 7 times
Been thanked: 1 time

Re: chprintf / DEBUG Macro

Post by Chudik »

Giovanni,
I'm trying to transfer my project to Ver. 3.0. I think it's better to do now, while my project is small.
During compiling get error message
.\ChibiOS_3.0\os\rt\ports\ARMCMx\chcore_v7m.h|388|error: 'SVCall_IRQn' undeclared (first use in this function)|


makefile in EM::Blocks saved in xml form. So, probably graphical representation might be easy
ChibiOS 3.0.PNG
ChibiOS 3.0.PNG (32.97 KiB) Viewed 4180 times


SVCall_IRQn defined in stm32l1xx.h (in typedef enum IRQn), but chcore_v7m.h does not see it. What need to be done? Did I miss something?

Tried to find a discussion with such question, looks like nobody asked that.

Igor
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chprintf / DEBUG Macro

Post by Giovanni »

Hi,

The file is included by cmparams.h, make sure your project paths point to the correct cmparam.h.

Giovanni
User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Has thanked: 7 times
Been thanked: 1 time

Re: chprintf / DEBUG Macro

Post by Chudik »

Hi Giovanni,
Yes, it is included

ChibiOS 3.0 error in IDE.PNG
ChibiOS 3.0 error in IDE.PNG (93.44 KiB) Viewed 4175 times



Just noticed: in @brief and @description text says about M4 core instead of M3. Not too important, though.
Igor
User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Has thanked: 7 times
Been thanked: 1 time

Re: chprintf / DEBUG Macro

Post by Chudik »

Seems like during transferring from version 2.6.6 to 3.0 some drivers were kept from the old version.
Downloaded Chibistudio again (to office computer), changed all STM32 LLD my project from this new downloaded rev, deleted some wrong versions of drivers and all became a little bit easy.
However, not everything.
Got error message:
ChibiOS_3.0\os\hal\ports\STM32\LLD\TIMv1\st_lld.c:43:2: error: #error "TIM2 is not a 32bits timer"


Looked on the code where it come from.
st_lld.h

Code: Select all

#if !defined(STM32_ST_USE_TIMER) || defined(__DOXYGEN__)
#define STM32_ST_USE_TIMER                  2
#endif
....
#if STM32_ST_USE_TIMER == 2
#if (OSAL_ST_RESOLUTION == 32) && !STM32_TIM2_IS_32BITS
#error "TIM2 is not a 32bits timer"
#endif


After some research I added line to the mcuconf.h

Code: Select all

/*
 * ChibiOS timer system settings
 */
#define STM32_ST_USE_TIMER                  5


After that my starting project compiled without errors. It does not mean it will work :mrgreen: , but coming closer :)
May be makes sense to add that line to mcuconf.h to the system distribution for STM32L1xx part?

BTW, what this timer used for? Is it not for the same purpose as SysTick timer?

And another question. The system has 2 versions of several HAL drivers. Is there a document, where's written which version for which MCU to use?
Post Reply