samples: accel_polling: set sampling frequency when necessary

The accel_polling sample uses various sensor, but doesn't set a sampling
rate. But some sensors (like st,lsm6dso) have a default sampling
frequency of 0. So, depending on the sensor, the sample may not always
work.

There are two ways to fix this: either all drivers must set a valid
sampling rate, or the sample shall at least try to set a value if there
is none.

We propose here the second approach, wich should allow the sample to
work on more sensors out of the box.

Signed-off-by: Miguel Gazquez <miguel.gazquez@bootlin.com>
This commit is contained in:
Miguel Gazquez 2024-04-25 17:39:20 +02:00 committed by Alberto Escolar
parent c44c567780
commit f6f03867d8

View file

@ -51,6 +51,29 @@ static int print_accels(const struct device *dev)
return 0;
}
static int set_sampling_freq(const struct device *dev)
{
int ret;
struct sensor_value odr;
ret = sensor_attr_get(dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY, &odr);
/* If we don't get a frequency > 0, we set one */
if (ret != 0 || (odr.val1 == 0 && odr.val2 == 0)) {
odr.val1 = 100;
odr.val2 = 0;
ret = sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY,
&odr);
if (ret != 0) {
printk("%s : failed to set sampling frequency\n", dev->name);
}
}
return 0;
}
int main(void)
{
int ret;
@ -60,6 +83,7 @@ int main(void)
printk("sensor: device %s not ready.\n", sensors[i]->name);
return 0;
}
set_sampling_freq(sensors[i]);
}
#ifndef CONFIG_COVERAGE