Page 1 of 1

chThdSleepMilliseconds question

Posted: Thu Dec 26, 2013 7:36 pm
by eribla
Hi

this is more a general question about threads I guess.

I finished my LCD driver code and can now write a number on the display from my main() program... ( :P celebrate small successes).

I am using the code from the CHIBIOS example, where the temp sensor and PC1 are constantly read out and PWM the LEDs.


My next step was to display the Temperature (the converted ADC value) on the display from inside the AD conversion callback function ( adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) ).

(Since the LCD output is slow, I am only doing that once).

The problem is that nothing is written to the LCD and the code never returns from WriteLCD(value, row, col). (Which again works perfect when called from main()).


I tracked down what happens and it seems to come down to chThdSleepMilliseconds(n) which is used to delay the signals to the LCD.

It meanders through:

chThdSleepMilliseconds(50); -> chThdSleep(systime_t time) -> chThdSleepS(time); -> msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time) -> chVTSetI(&vt, time, wakeup, currp);

That's where it stops.

The callback function "wakeup" is never invoked ( Breakpoint never hit).


Now - I do not know if I change certain conditions while single stepping through the code.


The whole ADC conversion is invoked in main() through

Code: Select all

  /*
   * Initializes the ADC driver 1 and enable the thermal sensor.
   * The pin PC1 on the port GPIOC is programmed as analog input.
   */
  adcStart(&ADCD1, NULL);
  adcSTM32EnableTSVREFE();
  palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_ANALOG);

  /*
   * Initializes the PWM driver 4, routes the TIM4 outputs to the board LEDs.
   */
  pwmStart(&PWMD4, &pwmcfg);
  palSetPadMode(GPIOD, GPIOD_LED4, PAL_MODE_ALTERNATE(2));  /* Green.   */
  palSetPadMode(GPIOD, GPIOD_LED6, PAL_MODE_ALTERNATE(2));  /* Blue.    */


Is this considered a different thread than the conversion call back function? (probably).

Why would the chThdSleepMilliseconds not work there? What is the solution/workaround?

----

I actually did another quick test: It IS the chThdSleepMilliseconds function!

Code: Select all

void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {
...
...
    /* Changes the channels pulse width, the change will be effective
       starting from the next cycle.*/
    pwmEnableChannelI(&PWMD4, 0, PWM_FRACTION_TO_WIDTH(&PWMD4, 4096, avg_ch1));
    pwmEnableChannelI(&PWMD4, 3, PWM_FRACTION_TO_WIDTH(&PWMD4, 4096, avg_ch2));
      
      chThdSleepMilliseconds(50);   [color=#FF0000] <- never returns[/color]

/*LCD code:  */
...
...
}
   


As always THANKS a LOT for any clues

























chThdSleepMilliseconds(50);

chThdSleep(systime_t time)

chThdSleepS(time);

msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time)

chVTSetI(&vt, time, wakeup, currp);

Re: chThdSleepMilliseconds question

Posted: Thu Dec 26, 2013 8:10 pm
by Giovanni
You should check for stack overflows I think. Something gets corrupted and the callback is not invoked.

Giovanni

Re: chThdSleepMilliseconds question

Posted: Thu Dec 26, 2013 10:59 pm
by alexblack
Hi.
I think You can't call Sleep functions from callback!

Re: chThdSleepMilliseconds question

Posted: Fri Dec 27, 2013 9:31 am
by Giovanni
alexblack wrote:Hi.
I think You can't call Sleep functions from callback!


Definitely, I missed that bit...

Callbacks are invoked from ISRs and you cannot suspend an ISR. I *recommend* to read *carefully* the chapter °Kernel Concepts° in the documentation. You need to understand what it is possible from the various contexts. In ChibiOS there are classes of functions: S-Class, I-Class and Normal, each one has its own requirements.

Also, please run with the debug options enabled, that error would have been caught by the state checker.

Giovanni

Re: chThdSleepMilliseconds question

Posted: Fri Dec 27, 2013 4:24 pm
by eribla
Thank you very much !!!!

I have "solved" the issue with a global variable that is loaded in the callback and used later.

Re: chThdSleepMilliseconds question

Posted: Sat Dec 28, 2013 1:03 am
by eribla
Giovanni

How do I enable more debug options?

Also, please run with the debug options enabled, that error would have been caught by the state checker.



Also,

I would like to create a periodic thread with low priority to refresh a display.. "do it whenever you get to it". Ho do I do this?
There does not seem to be a command like either ThreadPause("Tread") or ThreadDispose("Tread") so the same thread could be restarted
periodically.

What is the best practice there?

Thanks

Re: chThdSleepMilliseconds question

Posted: Sat Dec 28, 2013 8:56 am
by Giovanni
About timings: http://www.chibios.org/dokuwiki/doku.ph ... :kb:timing

All kernel settings are in the file chconf.h.

Giovanni