Hi all,
I already learned a lot about Zephyr from posts on this blog, thank you! I now have a question to which I could not find an answer to. It is about using the sensor API in sensor.h and having multiple sensors on the same i2c bus.
I started with one LIS2DH12 acceleremeter sensor and used Zephyr’s library for the LIS2DH group. Following different examples, I did this:
add a lis2dh12 node to the i2c devicethree
&i2c0 {
compatible = "nordic,nrf-twi";
status = "okay";
pinctrl-0 = <&i2c0_default>;
pinctrl-1 = <&i2c0_sleep>;
pinctrl-names = "default", "sleep";
lis2dh12: lis2dh12@19 {
compatible = "st,lis2dh12", "st,lis2dh";
reg = <0x19>;
irq-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
};
};
Include only sensor.h into the code (not lis2dh.h) and do the rest via the sensor API (initiate device, get data, …), like this:
#include <sensor.h>
...
const struct device *const sensor = DEVICE_DT_GET_ANY(st_lis2dh);
....
struct sensor_value accel[3];
int rc = sensor_sample_fetch(sensor);
rc = sensor_channel_get(sensor, SENSOR_CHAN_ACCEL_XYZ, accel);
...
Now, how do I add another sensor e.g. for temperature measurement with a different address to the same i2c bus?
My understanding is that I would need to add another subnode to my i2c node in the devicetree like this:
&i2c0 {
compatible = "nordic,nrf-twi";
status = "okay";
pinctrl-0 = <&i2c0_default>;
pinctrl-1 = <&i2c0_sleep>;
pinctrl-names = "default", "sleep";
lis2dh12: lis2dh12@19 {
compatible = "st,lis2dh12", "st,lis2dh";
reg = <0x19>;
irq-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
};
someTempSens: someTempSens@20 {
...
};
};
And then also use the sensor API to access it.
But if I call the different sensors from different threads, how do I make sure there is no conflict on the bus? Is there a mutex integrated somewhere? If so, how is this done or if not, then where do I add it?
Until now, without the sensor-API, I had an extra i2c-driver layer that took care of this. But with the solution above, I do not acces i2c directly anymore, so I do not know how to handle this.
I would be very happy to get an answer and explanation on how the sensor API is meant to be used! I am sure it is cool, but I must be missing some point :-)
Best regards,
Joanna