Suspending a thread also blocks I2C

Hello,

I have a thread that is reading sensor data over I2C. When I suspend it I cant access I2C from another thread that's reading another I2C device. 

I tried isolating the I2C read function with mutex so only one device can access it at a time and when thread goes to sleep to release the mutex but it did not help.

Any ideas why?

Regards, 

Parents Reply Children
  • code is part of huge project. 

    I have two I2C devices connected. 

    One is Accelerometar

      const struct i2c_dt_spec spec = {
        .bus  = dev_I2C,
        .addr = 0x6A
      };
    other is RTC
    const struct i2c_dt_spec spec_rtc = {
       .bus  = dev_I2C,
       .addr = 0x68
    };
    Accelerometar is beeing read in its thread by calling 
    static inline int readSensorData(int16_t* acceleration, int16_t* angular_rate) {
      // unsigned int key;
    
      // k_mutex_lock(&i2c_access_mutex, K_FOREVER);
      // key = irq_lock();
    
      int err = lsm6dso_acceleration_raw_get(gdev, acceleration);
      if (err) {
        printf("Error reading acceleration: %d\n", err);
        return err;
      }
    
      err = lsm6dso_angular_rate_raw_get(gdev, angular_rate);
      if (err) {
        printf("Error reading gyroscope: %d\n", err);
        return err;
      }
    
      // irq_unlock(key);
      // k_mutex_unlock(&i2c_access_mutex);
      return 0;
    }
    

    without success. When I suspent that thread and try to read RTC the entire device hangs without any errors. 
  • IvanR said:
    without success.

    What does that mean? Does readSensorData() return anything other than 0?

    IvanR said:
    When I suspent that thread and try to read RTC the entire device hangs without any errors

    Where does it hang? Do you have any other threads? Are they still running?

    From where are you calling readSensorData? From main()? From an interrupt (timer, perhaps)?

    Best regards,

    Edvin

  • readSensorData  works fine, I can suspend it and resume it and it works, that part is not an issue. But when I suspend the thread thats reading it. 

    readSensorData is read from thread get_Gyro within while(1). I need that data as fast as LSM can provide. 

    if I suspend that thread I cant access I2C from main thread that (with issued command) reads RTC. If I unsuspend getGyro thread I can read the RTC. 

     

  • What exactly do you mean by suspending the thread? How do you do it? Can you please share some snippets?

    Best regards,

    Edvin

  • void suspendThreads(struct k_thread* notifThread, struct k_thread* gyroThread, struct k_thread* adcThread) {
      k_thread_suspend(notifThread);
      k_thread_suspend(gyroThread);
      adc_thread_pause = true;
      // Wait a short period to ensure the ADC thread has paused corectly
      k_sleep(K_MSEC(200));
      k_thread_suspend(adcThread);
      k_timer_stop(&notification_timer);
    }

Related