Fairly new to nRF Connect SDK & Zephyr although I have worked a lot with NRF5 SDK
I'm at a bit of a dead end here. I trying to setup a way to repeatedly read out data from an I2C device. I have a custom driver that I can sucessfully use to read out the data wrom my main context.
It goes something like this:
main(){
while(1){
rc = sensor_sample_fetch(imu_dev);
if(rc){
LOG_ERR("sensor_sample_fetch failed with %d", rc);
}
sensor_channel_get(imu_dev, SENSOR_CHAN_ACCEL_XYZ, hoots_accel);
LOG_INF("AX=%10.2f AY=%10.2f AZ=%10.2f (m/s^2)",
sensor_value_to_double(&hoots_accel[0]),
sensor_value_to_double(&hoots_accel[1]),
sensor_value_to_double(&hoots_accel[2]));
}
}
This works as expected:
[00:00:00.002,593] <inf> uart_mgr: UART Module INIT 0 [00:00:00.002,655] <inf> adc_mgr: ADC Module INIT 0 [00:00:00.002,685] <inf> imu_mgr: IMU Module INIT 0 [00:00:00.038,360] <inf> imu_mgr: AX= -156.00 AY= -312.00 AZ= -937.00 (m/s^2)
The problems start when I try to make this call repeatedly:
k_timer_init(&my_timer, imu_sample_event, NULL);
void imu_sample_event(struct k_timer *timer_id){
int rc = -ENOENT;
rc = sensor_sample_fetch(imu_dev);
if(rc){
LOG_ERR("sensor_sample_fetch failed with %d", rc);
}
sensor_channel_get(imu_dev, SENSOR_CHAN_ACCEL_XYZ, hoots_accel);
LOG_INF("AX=%10.2f AY=%10.2f AZ=%10.2f (m/s^2)",
sensor_value_to_double(&hoots_accel[0]),
sensor_value_to_double(&hoots_accel[1]),
sensor_value_to_double(&hoots_accel[2]));
}
k_timer_start(&my_timer, K_MSEC(IMU_TIMER_INTERVAL_MSEC), K_MSEC(IMU_TIMER_INTERVAL_MSEC));
this way of doing it leads to the following error:
[00:00:00.002,593] <inf> uart_mgr: UART Module INIT 0 [00:00:00.002,655] <inf> adc_mgr: ADC Module INIT 0 [00:00:00.002,685] <inf> imu_mgr: IMU Module INIT 0 [00:00:00.002,716] <inf> app: Hello World! nrf52_hoots [00:00:00.002,716] <inf> app: addnums: 3 [00:00:00.502,868] <err> i2c_nrfx_twi: Error on I2C line occurred for message 0 [00:00:00.502,899] <err> ADXL343: Failed to read FIFO status rc = -5 [00:00:00.502,929] <err> imu_mgr: sensor_sample_fetch failed with -5 [00:00:00.502,929] <inf> imu_mgr: AX= 0.00 AY= 0.00 AZ= 0.00 (m/s^2)
Searching online lead my to the conclusion that in Zephyr it's in fact not possible to perofrm I2C transactions from an interrupt context.
https://github.com/zephyrproject-rtos/zephyr/issues/21538
I'm fairly certain I'm not the first person to need repeated I2C transfers from outside of main function & I'm guessing there's a better way to do it from what I'm trying to do.
How do I get around this?