Firmware hangs when idle thread is disabled

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
nullbert
Posts: 19
Joined: Tue Aug 30, 2016 7:12 pm
Been thanked: 1 time

Firmware hangs when idle thread is disabled

Post by nullbert »

Hello,

For our application (STM32f042) we want to disable the idle thread to save flash space.

When we disable it via chconf.h, our firmware hangs (never starts up).

Our main() implements an infinite loop, like so:

[font=Courier New]while (true) {
chThdSleepMilliseconds(MAIN_THREAD_CHECK_INTERVAL_MS); //1000 ms
stats_check += MAIN_THREAD_CHECK_INTERVAL_MS;
if (stats_check > MAIN_THREAD_SLEEP_NORMAL_MS) {
/* TODO re-enable this when we have safe I2C / CAN simultaneous operation */
broadcast_stats();
stats_check = 0;
}
check_system_state();
}
return 0;[/font]

What else can we check / set?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Firmware hangs when idle thread is disabled

Post by Giovanni »

Hi,

If you disable the idle thread then the main() becomes the idle thread, you cannot call any function that changes the thread state from there like that chThdSleepMilliseconds().

If you need delays there then you can implement a polled delay on the system time counter.

Giovanni
nullbert
Posts: 19
Joined: Tue Aug 30, 2016 7:12 pm
Been thanked: 1 time

Re: Firmware hangs when idle thread is disabled

Post by nullbert »

Thank you. I assume you mean allow the thread to run at maximum speed, since it is now the idle thread and will run at a lower priority than other threads.

Will the idle thread get starved if other threads never sleep or block?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Firmware hangs when idle thread is disabled

Post by Giovanni »

Hi,

Threads not releasing CPU (not sleeping) block lower priority threads.

The general rule is: the thread in execution is always the ready one with highest priority.

Giovanni
nullbert
Posts: 19
Joined: Tue Aug 30, 2016 7:12 pm
Been thanked: 1 time

Re: Firmware hangs when idle thread is disabled

Post by nullbert »

Thanks.

So, I changed the main() loop to a simple infinite loop and the firmware still hangs.

What should the main() implement besides a simple infinite loop?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Firmware hangs when idle thread is disabled

Post by Giovanni »

Just a loop, I just tried this:

Code: Select all

int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Activates the serial driver 3 using the driver default configuration.
   */
  sdStart(&LPSD1, NULL);

  /*
   * Creates the example thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);

  /*
   * Idle loop.
   */
  while (true) {
  }
}


Enable assertions, checks and state checker in chconf.h and see if something is caught. Make sure you don't have some other state-changing code in the main.

Giovanni
Post Reply