This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Zephyr Timer + I2C

I'm building a simple application using I2C to read sensor data (BNO055). I confirmed I2C and my sensor are all configured appropriately and working smoothly, but when I added in zephyr timers to my application to read the sensors, the application repeatedly crashes.  I've confirmed that both I2C and the timers work as expected independently, but not when used together.  Please see some sudo-code below:

static void repeating_timer_callback(struct k_timer *timer_id){

        i2c_read_write(...);

        ...

}

K_TIMER_DEFINE(my_timer, repeating_timer_callback, NULL);

void main(){

             ...

             ...

            k_timer_start(&my_timer, K_MSEC(100), K_MSEC(100));

Parents
  • Hi.

    I2C can take a lot of time, thats why you shoud use K_WORK_DEFINE for your timer

    Try this

    static void repeating_timer_callback(struct k_timer *timer_id){
    
            i2c_read_write(...);
    
            ...
    
    }
    
    K_WORK_DEFINE(repeating_timer_work, repeating_timer_callback);
    
    void repeating_timer_handler(struct k_timer *dummy)
    {
            k_work_submit(&repeating_timer_work);
    }
    
    K_TIMER_DEFINE(my_timer, repeating_timer_handler, NULL);
    

  • Thanks, but doesn’t that submit solution not force the sensor to read every 100ms as intended?  In other words, once submitted via k_work_submit, the i2c instructions may await execution for a variable amount of time, and therefore I may not get 100ms intervals on the sensor reads. 

Reply
  • Thanks, but doesn’t that submit solution not force the sensor to read every 100ms as intended?  In other words, once submitted via k_work_submit, the i2c instructions may await execution for a variable amount of time, and therefore I may not get 100ms intervals on the sensor reads. 

Children
Related