Page 1 of 1

STM32F3 hangs when using event listener

Posted: Mon Apr 15, 2013 9:48 pm
by Vik
Hi all,

I've ported my project from the simulator to the STM32F3 discovery board. Since the simulator uses serial drivers, I used those rather than USARTs. I found that using event listeners on the serial ports causes Chibios to hang (but only if they are actually called). If I comment out the listener, the code runs happily. I stripped out all code from my project and finally ended up with this relatively simple test case:

Code: Select all

#include "ch.h"
#include "hal.h"

#define cputs(msg) chnWrite(&SD1, (unsigned char*) msg, sizeof(msg));

static EventListener sd1fel, sd2fel;
static VirtualTimer vt1;
int counter = 0;

/* this may be called from SYSTEM_TICK_EVENT_HOOK */
void ledblink(void)
{
    static bool s = true;

    if(s) {
      palSetPad(GPIOE, GPIOE_LED8_ORANGE);
      s = false;
    } else {
      palClearPad(GPIOE, GPIOE_LED8_ORANGE);
      s = true;
    }
}

static void ledoff(void *p) {
  (void)p;
  palClearPad(GPIOE, GPIOE_LED3_RED);
}

static void gpt2cb(GPTDriver *gptp) {
    (void) gptp;
    static bool s = true;
    counter++;

    if(s) {
      palSetPad(GPIOE, GPIOE_LED5_ORANGE);
      s = false;
    } else {
      palClearPad(GPIOE, GPIOE_LED5_ORANGE);
      s = true;
    }
}

static const GPTConfig gpt2cfg = {
  1000,  /* 1kHz timer clock.*/
  gpt2cb    /* Timer callback.*/
};

static void sd1_handler(eventid_t id)
{
  flagsmask_t flags;

  (void)id;
  flags = chEvtGetAndClearFlags(&sd1fel);
  cputs("1\r\n");
}

static void sd2_handler(eventid_t id) {
  flagsmask_t flags;

  (void)id;
  cputs("2\r\n");
  flags = chEvtGetAndClearFlags(&sd2fel);
}

static evhandler_t fhandlers[] = {
  sd1_handler,
  sd2_handler
};

int main(void) {
  halInit();
  chSysInit();

  /* Init pins for serial ports 1 and 2 */
  palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7));       /* USART1 TX.       */
  palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7));      /* USART1 RX.       */
  palSetPadMode(GPIOB, 3, PAL_MODE_ALTERNATE(7));       /* USART1 TX.       */
  palSetPadMode(GPIOB, 4, PAL_MODE_ALTERNATE(7));      /* USART1 RX.       */

  sdStart(&SD1, NULL);
  sdStart(&SD2, NULL);

  gptStart(&GPTD2, &gpt2cfg);
  gptStartContinuous(&GPTD2, S2ST(1));

  cputs("Adding handler for SD1\r\n");
  // HERE: comment out the following line and the code will work
  chEvtRegister(chnGetEventSource(&SD1), &sd1fel, 0);
  cputs("Adding handler for SD2\r\n");
  chEvtRegister(chnGetEventSource(&SD2), &sd2fel, 2);


  while(!chThdShouldTerminate()) {

    palSetPad(GPIOE, GPIOE_LED3_RED);
    chVTSet(&vt1, MS2ST(200), ledoff, NULL);
    cputs("mainloop\r\n");
    chEvtDispatch(fhandlers, chEvtWaitOneTimeout(ALL_EVENTS, MS2ST(1000)));
  }

  chEvtUnregister(chnGetEventSource(&SD1), &sd1fel);
  chEvtUnregister(chnGetEventSource(&SD2), &sd2fel);

  return 0;
}


This code will hang until you comment out the chEvtRegister() call under the comment starting with "HERE".
Also note that I'm calling the "ledblink" function from SYSTEM_TICK_EVENT_HOOK and when Chibios hangs, it also stops flashing.

Output on the serial port from above is this, after the second '1' Chibios hangs:

Code: Select all

Adding handler for SD1
Adding handler for SD2
mainloop
1
mainloop
1

Any help would be much appreciated. Thanks in advance.

Cheers,
Vik

Re: STM32F3 hangs when using event listener

Posted: Tue Apr 16, 2013 5:17 am
by Giovanni
Hi,

You are registering an even with number 2, your array is only two elements long.

Giovanni

Re: STM32F3 hangs when using event listener

Posted: Tue Apr 16, 2013 8:46 am
by Vik
Hi Giovanni,

Thanks for that, I didn't really understand what those numbers were.

Unfortunately fixing this doesn't make the problem go away though.

Cheers,
Vik

Re: STM32F3 hangs when using event listener

Posted: Tue Apr 16, 2013 9:43 am
by Vik
Hi,

I think I've found the problem. Looking at it with the debugger, it appears as if it's infinite looping in chvt.c here:

Code: Select all

  while (p->vt_time < time) {
    time -= p->vt_time;
    p = p->vt_next;
  }


Where time = 1000 and p->vt_time is perpetually 0. Call stack here:

Code: Select all

#0  chVTSetI (vtp=0x200007bc, time=1000, vtfunc=0x8000611 <wakeup>, par=0x20000810) at /home/efti/Projects/electronics/chibios/chibios/trunk/os/kernel/src/chvt.c:81
#1  0x08000744 in chSchGoSleepTimeoutS (newstate=<optimized out>, time=<optimized out>) at /home/efti/Projects/electronics/chibios/chibios/trunk/os/kernel/src/chschd.c:189
#2  0x08000b80 in chEvtWaitOneTimeout (mask=4294967295, time=<optimized out>) at /home/efti/Projects/electronics/chibios/chibios/trunk/os/kernel/src/chevents.c:451
#3  0x0800192e in main () at main.c:98


And when I comment out the VirtualTimer, everything works. Hope this helps in finding what's going on. Let me know if there is anything I can try to get to the root cause.

Cheers,
Vik

Re: STM32F3 hangs when using event listener

Posted: Tue Apr 16, 2013 11:55 am
by Giovanni
Hi,

It is because you call chVTSet(&vt1, MS2ST(200), ledoff, NULL) on a timer that is potentially already running.

if you want to re-start a timer then you should do something like this:

chVTReset(&vt1);
chVTSet(&vt1, MS2ST(200), ledoff, NULL);

Probably this could be better documented.

Giovanni

Re: STM32F3 hangs when using event listener

Posted: Tue Apr 16, 2013 2:13 pm
by Vik
Hi Giovanni,

I can confirm that adding chVTReset(&vt1) to makes the problem go away. Thanks very much for your help (and for ChibiOS/RT in general).

Giovanni wrote:Probably this could be better documented.

You mean like this: :)

Code: Select all

--- os/kernel/include/chvt.h   (revision 5591)
+++ os/kernel/include/chvt.h   (working copy)
@@ -155,6 +155,8 @@
 /**
  * @brief   Enables a virtual timer.
  * @note    The associated function is invoked from interrupt context.
+ *          The timer MUST NOT be already set when this function is invoked,
+ *          call chVTReset() before if there is a risk of this.
  *
  * @param[out] vtp      the @p VirtualTimer structure pointer
  * @param[in] time      the number of ticks before the operation timeouts, the


By the way, do you see any value in expanding this code and adding it to testhal/STM32F3 or the demos as a timer or serial port example?

Cheers,
Vik

Re: STM32F3 hangs when using event listener

Posted: Tue Apr 16, 2013 5:38 pm
by Giovanni
Of course, posting examples into the contributions forum is going to help users in general.

Giovanni