Problem with chThdSleepSeconds() STM32F407

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
MySOL
Posts: 8
Joined: Wed Mar 04, 2015 10:40 pm

Problem with chThdSleepSeconds() STM32F407

Post by MySOL »

Hello gentelmens! Really need your help. I studied CHIBIOS and now there is a problem with the function call chThdSleepMilliseconds(). Actually, it works fine, as in the main thread, and in the simple regular threads (like led blinking). But now there was a need revival thread on the external interrupt. I used the example that comes with the OS in the test folder and examples of site. There is my code

Code: Select all

static void extcb1(EXTDriver *extp, expchannel_t channel) {


  (void)extp;
  (void)channel;
   
  chSysLockFromIsr();
  palSetPad(GPIOD, GPIOD_LED5);    // red led
   chSchReadyI(tp);
   tp=NULL;
  chSysUnlockFromIsr();
}

static const EXTConfig extcfg = {
  {
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOA, extcb1},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL}
  }
};



static WORKING_AREA(waThread1, 256);
static msg_t Thread1(void *arg) {

  (void)arg;
   
  chRegSetThreadName("blinker");
   tp=chThdSelf();
   chSysLock();
   chSchGoSleepS(THD_STATE_SUSPENDED);
   chSysUnlock();
  while (TRUE) {
    palSetPad(GPIOD, GPIOD_LED3);       /* Orange.  */
    chThdSleepSeconds(1);
    palClearPad(GPIOD, GPIOD_LED3);     /* Orange.  */
    chThdSleepSeconds(1);
  }
   
   
}


/*
 * Application entry point.
 */
int main(void) {
  uint8_t i;
  halInit();
  chSysInit();
   
   palSetPadMode(GPIOD, GPIOD_LED4, PAL_MODE_OUTPUT_PUSHPULL);
   palSetPadMode(GPIOD, GPIOD_LED3, PAL_MODE_OUTPUT_PUSHPULL);
   

  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  extStart(&EXTD1, &extcfg);
   
   while (TRUE)
   {
   palSetPad(GPIOD, GPIOD_LED4);       /* Green.  */
    chThdSleepSeconds(1);
    palClearPad(GPIOD, GPIOD_LED4);     /* Green.  */
    chThdSleepSeconds(1);
   }
}


After reset "Green led" is blinking. After external interrupting green flashing stops and the orange LED lights up , but does not blink. From this I concluded that the system is hung on the process chThdSleepSeconds(1) from the tread. Rly dont know what to do. Please help. Thank you!
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Problem with chThdSleepSeconds() STM32F407

Post by Giovanni »

Hi,

Several problems there, try this:

Code: Select all

static void extcb1(EXTDriver *extp, expchannel_t channel) {

  (void)extp;
  (void)channel;
   
  chSysLockFromIsr();
  palSetPad(GPIOD, GPIOD_LED5);    // red led
  if (tp != NULL) {  // You need to check if the thread entered the suspended state
   chSchReadyI(tp);
   tp=NULL;
  }
  chSysUnlockFromIsr();
}


Code: Select all

static WORKING_AREA(waThread1, 256);
static msg_t Thread1(void *arg) {

  (void)arg;
   
  chRegSetThreadName("blinker");

  chSysLock();
  tp=chThdSelf(); // This must go inside the critical zone
  chSchGoSleepS(THD_STATE_SUSPENDED);
  chSysUnlock();
  while (TRUE) {
    palSetPad(GPIOD, GPIOD_LED3);       /* Orange.  */
    chThdSleepSeconds(1);
    palClearPad(GPIOD, GPIOD_LED3);     /* Orange.  */
    chThdSleepSeconds(1);
  }
}


For this kind of things it is better to use binary semaphores anyway: chBSemSignalI() from the callback, chBSemWait() from the thread, the semaphore is initialized as "taken".

Giovanni
MySOL
Posts: 8
Joined: Wed Mar 04, 2015 10:40 pm

Re: Problem with chThdSleepSeconds() STM32F407

Post by MySOL »

Perfectly working! Thank you for your help and please forgive me for concern=)
Post Reply