Debug best practice

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
nonameletters
Posts: 7
Joined: Fri Aug 25, 2017 1:07 pm

Debug best practice

Post by nonameletters »

Hello. I am using a thread to handle button press interrupt EXTI on STM MCU.
The thread wakes up when button pressed and goes sleep after it finishes. But some times it enters in undefined state. What is the best practice to do this and debug.
This is thread

Code: Select all

static THD_WORKING_AREA(waButtonPowerThread, 256);
static __attribute__((noreturn)) THD_FUNCTION(ButtonPowerThread, arg)
{
    (void)arg;
    chRegSetThreadName("ButtonPowerThread");

   BaseSequentialStream *outChannel2 = shell_cfg1.sc_channel;

   chSysLock();
   for(;;)
   {
         chThdSuspendTimeoutS(&powerButtonThread, TIME_INFINITE);
      chSysUnlock();
         chprintf(outChannel2, " Power on was pressed \r\n");
         idtOnOff();
         powerToggle();
      chSysLock();
   }
}


thread_reference_t powerButtonThread;

Button press handler

Code: Select all

static void btn_do(void *arg)
{
  (void)arg;

  chSysLockFromISR();
     if (powerButtonThread != NULL)
     {
        chThdResumeI(&powerButtonThread, MSG_OK);
     }
     else
     {
        powerButtonThreadNullCount++;
     }

  chSysUnlockFromISR();

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

Re: Debug best practice

Post by Giovanni »

Hi,

You should define this undefined state :) is the thread suspended, crashed or something else?

Try to look at the threads state and the trace buffer using the ChibiStudio debug plugin plugin.

Also make sure to enable the usual debug options: assertions, checks, state checker.

Giovanni
nonameletters
Posts: 7
Joined: Fri Aug 25, 2017 1:07 pm

Re: Debug best practice

Post by nonameletters »

I noticed that powerButtonThread becomes NULL. Interrupt handler works fine. Pressing button I enter into handler, and when I check powerButtonThread it is NULL. This is global variable. It is used to control thread. I check its usage. Only 3 place where it used:
1. Defenition
2. Thread to enter suspend mode
3. Button handler to wake up thread

How can I guaranty the atomicity of thread?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Debug best practice

Post by Giovanni »

If that variable is not NULL then the thread is not suspended but stuck/looping/waiting in some other place, you need to focus on that.

Giovanni
nonameletters
Posts: 7
Joined: Fri Aug 25, 2017 1:07 pm

Re: Debug best practice

Post by nonameletters »

I have a strange scheduler behavior.
2 threads. first blinker with NORMALPRIO. Second Message Box listener with NORMALPRIO+1. Second listens button presses. After some period second thread doesn't get execution. When I pressing button interrupt happens. In debugger I see that I don't enter thread. But if I send some thing in CLI thread starts execute and I see all messages.
Post Reply