Page 1 of 1

Debug best practice

Posted: Mon Oct 02, 2017 8:48 am
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();

}

Re: Debug best practice

Posted: Mon Oct 02, 2017 9:48 am
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

Re: Debug best practice

Posted: Mon Oct 02, 2017 10:28 am
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?

Re: Debug best practice

Posted: Mon Oct 02, 2017 10:41 am
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

Re: Debug best practice

Posted: Tue Oct 03, 2017 9:06 am
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.