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
/** * @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.
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 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 ?
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.
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.