How to access multiple SPI sensors

Dear all,

It's my first project with a Nordic chip and with Zepyhr as well, so a little overwhelmed.

I made a PCB based on a nRF52840 and the reference design.
The custom board has been added to my project and tested with various samples.  Everything works on the hardware side.

Now, the tricky part:
The board has 11 similar sensors connected on the same SPI.
The sensors are the ST LPS27HH, but I use the LPS22HH drivers as they are similar.

I can easily access a single sensor on the SPI with:

const struct device *const sensor = DEVICE_DT_GET_ONE(st_lps22hh);



My issue arises when I need to access my sensors from the source code.
I tried multiple different version, but cannot find a loop or a macro equivalent to:

static const struct device *sensors[NUM_SENSORS];
sensors[0] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_0));
sensors[1] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_1));
sensors[2] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_2));
sensors[3] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_3));
sensors[4] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_4));
sensors[5] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_5));
sensors[6] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_6));
sensors[7] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_7));
sensors[8] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_8));
sensors[9] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_9));
sensors[10] = DEVICE_DT_GET(DT_NODELABEL(lps27hh_10));

I tried multiple version, like:

static const struct device *sensors[NUM_SENSORS];
for (int i=0; i<NUM_SENSORS; i++) {
sensors[i] = DEVICE_DT_DEFINE(DT_NODELABEL(lps27hh_ ## i))
}

(In that case, the index i of the loop is not compute by the macro)

Or something like that, with the same issue:

#define MYDEV(idx) DT_NODELABEL(lps27hh_ ## idx)
static const struct device *sensors[NUM_SENSORS];
for (int i=0; i<NUM_SENSORS; i++) {
sensors[i] = DEVICE_DT_DEFINE(MYDEV(i));
}

If you have any idea how to access all my sensor in a better way, I would really appreciate.

Thanks a lot!


PS: Here is my overlay file:

spi0: &spi0 {
status = "okay";
compatible = "nordic,nrf-spim";
pinctrl-0 = <&spi0_default>;
pinctrl-1 = <&spi0_sleep>;
pinctrl-names = "default", "sleep";
max-frequency = <DT_FREQ_M(8)>;

// chip select GPIOs selected by the "reg" property of the slave node.
cs-gpios = <&gpio0 20 GPIO_ACTIVE_LOW>,
<&
gpio0 22 GPIO_ACTIVE_LOW>,
<&
gpio0 24 GPIO_ACTIVE_LOW>,
<&
gpio1 0 GPIO_ACTIVE_LOW>,
<&
gpio1 1 GPIO_ACTIVE_LOW>,
<&
gpio1 2 GPIO_ACTIVE_LOW>,
<&
gpio1 4 GPIO_ACTIVE_LOW>,
<&
gpio1 6 GPIO_ACTIVE_LOW>,
<&
gpio1 7 GPIO_ACTIVE_LOW>,
<&
gpio0 9 GPIO_ACTIVE_LOW>,
<&
gpio0 10 GPIO_ACTIVE_LOW>;

lps27hh_0: lps27hh@0 {
compatible = "st,lps22hh";
reg = <0x00>; // address is a chip select line number;
spi-max-frequency = <4000000>;
odr = <7>;
};

lps27hh_1: lps27hh@1 {
compatible = "st,lps22hh";
reg = <0x01>;
spi-max-frequency = <8000000>;
odr = <7>;
};

// More sensors here

lps27hh_10: lps27hh@A {
compatible = "st,lps22hh";
reg = <0x0A>;
spi-max-frequency = <8000000>;
odr = <7>;
};

};
Parents Reply Children
No Data
Related