chThdSleepMilliseconds( 1000 ) Weird ?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

chThdSleepMilliseconds( 1000 ) Weird ?

Post by Tabulous »

ive got a gpt timer running @ 100hz

the callback for the timer processes some data this take about 1ms to do.


i have then a thread that toggles a led i.e.

Code: Select all

static THD_FUNCTION( ThreadDBH, arg )
{

    (void)arg;
    chRegSetThreadName("dbh");

    while (true)
    {
        /*
         * logic for driver events
         */
        chThdSleepMilliseconds( 1000 );
        LedToggle();
}


This led does not toggle at 1 second ? it runs at 1.1 seconds.

To get 1 second i have to use chThdSleepMilliseconds( 900 );

Is this due to the gpt timer callback starving the scheduler ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by Giovanni »

Hi,

100hz is not enough to starve anything.

Do you have other threads at the same priority? are your clocks what you expect?

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by steved »

What happens with:
chThdSleepMilliseconds( 999 );
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by Tabulous »

right found what causes the issue, the gpt timer callback reads data from the i2 bus, i use my own bit banged version here, as the chibios version caused endless issues.

The net result is i don't yield to the scheduler whilst doing the read from the i2c. The delays in the the i2c routines are just simple loops, it would have been nice to use chThdSleepMicroseconds( x ) to implement the delays but as the scheduler it setup as follows

Code: Select all

/*===========================================================================*/
/**
 * @name System timers settings
 * @{
 */
/*===========================================================================*/

#define CORTEX_VTOR_INIT                    0x8400

/**
 * @brief   System time counter resolution.
 * @note    Allowed values are 16 or 32 bits.
 */
#define CH_CFG_ST_RESOLUTION                32//16

/**
 * @brief   System tick frequency.
 * @details Frequency of the system timer that drives the system ticks. This
 *          setting also defines the system tick time unit.
 */
#define CH_CFG_ST_FREQUENCY                 1000//10000//50000

/**
 * @brief   Time delta constant for the tick-less mode.
 * @note    If this value is zero then the system uses the classic
 *          periodic tick. This value represents the minimum number
 *          of ticks that is safe to specify in a timeout directive.
 *          The value one is not valid, timeouts are rounded up to
 *          this value.
 */
#define CH_CFG_ST_TIMEDELTA                 0//10//2


any chThdSleepMicroseconds( x ) would been rounded to the nearest and thus take a 1ms.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by Giovanni »

You should put threads doing CPU-intensive operations, like a bit banged I2C, to a lower priority level or threads will be served in round robin, which is what you see. Alternatively you could call chThdYield() after each banged bit or byte.

HAL drivers do not require this because implementations suspend the calling thread during transfers.

giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by Tabulous »

Giovanni wrote:You should put threads doing CPU-intensive operations, like a bit banged I2C, to a lower priority level or threads will be served in round robin, which is what you see. Alternatively you could call chThdYield() after each banged bit or byte.

HAL drivers do not require this because implementations suspend the calling thread during transfers.

giovanni


The i2c bit banging is in the gpt callback, it was done like this as it was impossible to get accurate 100hz timing from a thread.

So is it safe to call chThdYield() in callback context ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by Giovanni »

I misunderstood, it is not safe to use that function in ISR context.

Why not bitbang using PAL and gptPolledDelay()? without callbacks.

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by Tabulous »

Giovanni wrote:I misunderstood, it is not safe to use that function in ISR context.

Why not bitbang using PAL and gptPolledDelay()? without callbacks.

Giovanni


i use bit banging already, and this uses the PAL .....

gptPolledDelay() is blocking, i.e it waits for the delay to expire ? what difference will that make to my i2c driver ?

From what i can see nothing, you'll still starve the scheduler for the time it takes to execute the i2c code.
Marco
Posts: 128
Joined: Tue Apr 16, 2013 8:22 pm
Has thanked: 4 times
Been thanked: 11 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by Marco »

The timer callback is called in interrupt context. While in that timer callback the scheduler is blocked.
You could use gptPolledDelay() from within a thread like Giovanni suggested. This way the scheduler is able to preempt the i2c operation to do other stuff, like toggling your led.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdSleepMilliseconds( 1000 ) Weird ?

Post by Giovanni »

Marco wrote:The timer callback is called in interrupt context. While in that timer callback the scheduler is blocked.
You could use gptPolledDelay() from within a thread like Giovanni suggested. This way the scheduler is able to preempt the i2c operation to do other stuff, like toggling your led.


Correct, in addition, not having to serve IRQs would make the process more efficient. Consider that an IRQ can take 1..5 uS in addition to the callback time.

Giovanni
Post Reply