Hello Element14 Community members,
I have been working on element14 roadtest for ST-WINBX1 and wanted to know about the problem that I am facing to get the sensor data.
I am using Zephyr OS. I am getting the data for one accelerometer but after that I could not get the data out of sensor. I do not know what could have gone wrong but the chances of sensors being damage are rare.
Here is a sample code that is not working as expected. For the first sensor, I am getting the correct data. But for the next one and many others, I get no data.?
if (sensor_sample_fetch(acc)) { printk("sensor_sample fetch failed\n"); } sensor_channel_get(acc, SENSOR_CHAN_ACCEL_X, &accx); sensor_channel_get(acc, SENSOR_CHAN_ACCEL_Y, &accy); sensor_channel_get(acc, SENSOR_CHAN_ACCEL_Z, &accz); printk("Acceleromer X: %f Acceleromer Y: %f Acceleromer Z: %f\n", sensor_value_to_double(&accx), sensor_value_to_double(&accy), sensor_value_to_double(&accz)); k_sleep(K_MSEC(50)); if (sensor_sample_fetch(acchp)) { printk("sensor_sample fetch failed\n"); } sensor_channel_get(acchp, SENSOR_CHAN_ACCEL_XYZ, &hpaccx); sensor_channel_get(acchp, SENSOR_CHAN_ACCEL_XYZ, &hpaccy); sensor_channel_get(acchp, SENSOR_CHAN_ACCEL_XYZ, &hpaccz); printk("Acceleromer X: %f Acceleromer Y: %f Acceleromer Z: %f\n", sensor_value_to_double(&hpaccx), sensor_value_to_double(&hpaccy), sensor_value_to_double(&hpaccz));
More Information
Here is the more information regarding the issue.
I am first getting the device using compatible, as used by Zephyr to get a device struct using compatible property.
static const struct device *acc = DEVICE_DT_GET_ANY(st_iis2dlpc); static const struct device *acchp = DEVICE_DT_GET_ANY(st_ism330dhcx); static const struct device *absprs = DEVICE_DT_GET_ANY(st_ilps22qs); static const struct device *tmps = DEVICE_DT_GET_ANY(st_stts22h); static const struct device *magt = DEVICE_DT_GET_ANY(st_iis2mdc);
if(!device_is_ready(acc)) { printk("Accelerometer is not ready"); return 0; } k_sleep(K_MSEC(50)); if(!device_is_ready(acchp)) { printk("High-p Accelerometer is not ready\n"); return 0; } k_sleep(K_MSEC(50));
struct sensor_value accx, accy, accz; struct sensor_value hpaccx, hpaccy, hpaccz; struct sensor_value abspress;
More Information
So, if you look at the following diagram, most of the sensors I am using, are connected on SPI2 and I2C2 bus.
After build I can check which bus is enabled or disabled in the final build and they are all enabled correctly. Even if they are not, I would get error to initialize the sensor, the device_is_ready() will let me know.
If say sensor/s is damaged, then also it will not init. I have verified from the code that the channels that I am using are correct channels. i.e. SENSOR_CHAN_AMBIENT_TEMP, etc.
Even the Bluetooth is working that is connected on SPI bus. Most other modules are working. Sensors are being initialized, and their address is printed. But the values are not.
Each sensor has real and fractional part. So I tried to print only real part if that could be an issue but seems that is also not a problem.
Sometimes we have to enable the CS line for SPI sensor, but that is handled by the driver itself in this case.
Here is the board.dts file
&spi2 { pinctrl-0 = <&spi2_sck_pi1 &spi2_miso_pd3 &spi2_mosi_pi3>; pinctrl-names = "default"; status = "okay"; cs-gpios = <&gpioh 6 GPIO_ACTIVE_LOW>, <&gpioh 15 GPIO_ACTIVE_LOW>, <&gpioi 7 GPIO_ACTIVE_LOW>; iis2dlpc: iis2dlpc@0 { compatible = "st,iis2dlpc"; spi-max-frequency = <DT_FREQ_M(10)>; reg = <0>; drdy-gpios = <&gpiof 1 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>; drdy-int = <1>; }; ism330dhcx: ism330dhcx@1 { compatible = "st,ism330dhcx"; spi-max-frequency = <DT_FREQ_M(10)>; reg = <1>; drdy-gpios = <&gpiob 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>; int-pin = <1>; }; iis2iclx: iis2iclx@2 { compatible = "st,iis2iclx"; spi-max-frequency = <DT_FREQ_M(10)>; reg = <2>; drdy-gpios = <&gpiof 11 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>; int-pin = <2>; }; }; &spi3 { pinctrl-0 = <&spi3_sck_pg9 &spi3_miso_pb4 &spi3_mosi_pb5>; pinctrl-names = "default"; status = "okay"; cs-gpios = <&gpioe 1 GPIO_ACTIVE_LOW>; hci_spi: bluenrg-2@0 { compatible = "st,hci-spi-v2"; reg = <0>; reset-gpios = <&gpiod 13 GPIO_ACTIVE_LOW>; irq-gpios = <&gpiof 14 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>; spi-cpha; /* CPHA=1 */ spi-hold-cs; spi-max-frequency = <DT_FREQ_M(1)>; reset-assert-duration-ms = <6>; }; }; &i2c2 { pinctrl-0 = <&i2c2_scl_ph4 &i2c2_sda_pf0>; pinctrl-names = "default"; status = "okay"; clock-frequency = <I2C_BITRATE_FAST>; stts22h@3f { compatible = "st,stts22h"; reg = <0x3f>; int-gpios = <&gpiof 5 GPIO_ACTIVE_HIGH>; status = "okay"; }; iis2mdc@1e { compatible = "st,iis2mdc"; reg = <0x1e>; drdy-gpios = <&gpiof 9 GPIO_ACTIVE_HIGH>; status = "okay"; }; ilps22qs@5c { compatible = "st,ilps22qs"; reg = <0x5c>; status = "okay"; }; };