1 /*
2  * Copyright (c) 2019 STMicroelectronics
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <stdio.h>
11 #include <zephyr/sys/util.h>
12 
13 #ifdef CONFIG_IIS2DLPC_TRIGGER
14 static int iis2dlpc_trig_cnt;
15 
iis2dlpc_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)16 static void iis2dlpc_trigger_handler(const struct device *dev,
17 				     const struct sensor_trigger *trig)
18 {
19 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
20 	iis2dlpc_trig_cnt++;
21 }
22 #endif
23 
24 #ifdef CONFIG_ISM330DHCX_TRIGGER
25 static int ism330dhcx_acc_trig_cnt;
26 static int ism330dhcx_gyr_trig_cnt;
27 static int ism330dhcx_temp_trig_cnt;
28 
ism330dhcx_acc_trig_handler(const struct device * dev,const struct sensor_trigger * trig)29 static void ism330dhcx_acc_trig_handler(const struct device *dev,
30 					const struct sensor_trigger *trig)
31 {
32 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
33 	ism330dhcx_acc_trig_cnt++;
34 }
35 
ism330dhcx_gyr_trig_handler(const struct device * dev,const struct sensor_trigger * trig)36 static void ism330dhcx_gyr_trig_handler(const struct device *dev,
37 					const struct sensor_trigger *trig)
38 {
39 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ);
40 	ism330dhcx_gyr_trig_cnt++;
41 }
42 
ism330dhcx_temp_trig_handler(const struct device * dev,const struct sensor_trigger * trig)43 static void ism330dhcx_temp_trig_handler(const struct device *dev,
44 					 const struct sensor_trigger *trig)
45 {
46 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_DIE_TEMP);
47 	ism330dhcx_temp_trig_cnt++;
48 }
49 #endif
50 
iis2dlpc_config(const struct device * iis2dlpc)51 static void iis2dlpc_config(const struct device *iis2dlpc)
52 {
53 	struct sensor_value odr_attr, fs_attr;
54 
55 	/* set IIS2DLPC accel/gyro sampling frequency to 100 Hz */
56 	odr_attr.val1 = 100;
57 	odr_attr.val2 = 0;
58 
59 	if (sensor_attr_set(iis2dlpc, SENSOR_CHAN_ACCEL_XYZ,
60 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
61 		printk("Cannot set sampling frequency for IIS2DLPC accel\n");
62 		return;
63 	}
64 
65 	sensor_g_to_ms2(16, &fs_attr);
66 
67 	if (sensor_attr_set(iis2dlpc, SENSOR_CHAN_ACCEL_XYZ,
68 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
69 		printk("Cannot set sampling frequency for IIS2DLPC gyro\n");
70 		return;
71 	}
72 
73 #ifdef CONFIG_IIS2DLPC_TRIGGER
74 	struct sensor_trigger trig;
75 
76 	trig.type = SENSOR_TRIG_DATA_READY;
77 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
78 	sensor_trigger_set(iis2dlpc, &trig, iis2dlpc_trigger_handler);
79 #endif
80 }
81 
ism330dhcx_config(const struct device * ism330dhcx)82 static void ism330dhcx_config(const struct device *ism330dhcx)
83 {
84 	struct sensor_value odr_attr, fs_attr;
85 
86 	/* set ISM330DHCX accel sampling frequency to 208 Hz */
87 	odr_attr.val1 = 208;
88 	odr_attr.val2 = 0;
89 
90 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_ACCEL_XYZ,
91 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
92 		printk("Cannot set sampling frequency for ISM330DHCX accel\n");
93 		return;
94 	}
95 
96 	sensor_g_to_ms2(16, &fs_attr);
97 
98 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_ACCEL_XYZ,
99 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
100 		printk("Cannot set fs for ISM330DHCX accel\n");
101 		return;
102 	}
103 
104 	/* set ISM330DHCX gyro sampling frequency to 208 Hz */
105 	odr_attr.val1 = 208;
106 	odr_attr.val2 = 0;
107 
108 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_GYRO_XYZ,
109 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
110 		printk("Cannot set sampling frequency for ISM330DHCX gyro\n");
111 		return;
112 	}
113 
114 	sensor_degrees_to_rad(250, &fs_attr);
115 
116 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_GYRO_XYZ,
117 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
118 		printk("Cannot set fs for ISM330DHCX gyro\n");
119 		return;
120 	}
121 
122 	/* set ISM330DHCX external magn sampling frequency to 100 Hz */
123 	odr_attr.val1 = 100;
124 	odr_attr.val2 = 0;
125 
126 #ifdef CONFIG_ISM330DHCX_EXT_IIS2MDC
127 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_MAGN_XYZ,
128 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
129 		printk("Cannot set sampling frequency for ISM330DHCX ext magn\n");
130 		return;
131 	}
132 #endif
133 
134 #ifdef CONFIG_ISM330DHCX_TRIGGER
135 	struct sensor_trigger trig;
136 
137 	trig.type = SENSOR_TRIG_DATA_READY;
138 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
139 	sensor_trigger_set(ism330dhcx, &trig, ism330dhcx_acc_trig_handler);
140 
141 	trig.type = SENSOR_TRIG_DATA_READY;
142 	trig.chan = SENSOR_CHAN_GYRO_XYZ;
143 	sensor_trigger_set(ism330dhcx, &trig, ism330dhcx_gyr_trig_handler);
144 
145 	trig.type = SENSOR_TRIG_DATA_READY;
146 	trig.chan = SENSOR_CHAN_DIE_TEMP;
147 	sensor_trigger_set(ism330dhcx, &trig, ism330dhcx_temp_trig_handler);
148 #endif
149 }
150 
main(void)151 int main(void)
152 {
153 #ifdef CONFIG_ISM330DHCX_ENABLE_TEMP
154 	struct sensor_value die_temp;
155 #endif
156 	struct sensor_value accel1[3], accel2[3];
157 	struct sensor_value gyro[3];
158 	struct sensor_value magn[3];
159 	const struct device *const iis2dlpc = DEVICE_DT_GET_ONE(st_iis2dlpc);
160 	const struct device *const ism330dhcx = DEVICE_DT_GET_ONE(st_ism330dhcx);
161 	int cnt = 1;
162 
163 	if (!device_is_ready(iis2dlpc)) {
164 		printk("%s: device not ready.\n", iis2dlpc->name);
165 		return 0;
166 	}
167 	if (!device_is_ready(ism330dhcx)) {
168 		printk("%s: device not ready.\n", ism330dhcx->name);
169 		return 0;
170 	}
171 
172 	iis2dlpc_config(iis2dlpc);
173 	ism330dhcx_config(ism330dhcx);
174 
175 	while (1) {
176 		/* Get sensor samples */
177 
178 #ifndef CONFIG_IIS2DLPC_TRIGGER
179 		if (sensor_sample_fetch(iis2dlpc) < 0) {
180 			printf("IIS2DLPC Sensor sample update error\n");
181 			return 0;
182 		}
183 #endif
184 #ifndef CONFIG_ISM330DHCX_TRIGGER
185 		if (sensor_sample_fetch(ism330dhcx) < 0) {
186 			printf("ISM330DHCX Sensor sample update error\n");
187 			return 0;
188 		}
189 #endif
190 
191 		/* Get sensor data */
192 		sensor_channel_get(iis2dlpc, SENSOR_CHAN_ACCEL_XYZ, accel2);
193 		sensor_channel_get(ism330dhcx, SENSOR_CHAN_ACCEL_XYZ, accel1);
194 		sensor_channel_get(ism330dhcx, SENSOR_CHAN_GYRO_XYZ, gyro);
195 #ifdef CONFIG_ISM330DHCX_ENABLE_TEMP
196 		sensor_channel_get(ism330dhcx, SENSOR_CHAN_DIE_TEMP, &die_temp);
197 #endif
198 #ifdef CONFIG_ISM330DHCX_EXT_IIS2MDC
199 		sensor_channel_get(ism330dhcx, SENSOR_CHAN_MAGN_XYZ, magn);
200 #endif
201 
202 		/* Display sensor data */
203 
204 		/* Erase previous */
205 		printf("\0033\014");
206 
207 		printf("X-NUCLEO-IKS02A1 sensor Mode 2 dashboard\n\n");
208 
209 		printf("IIS2DLPC: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n",
210 			sensor_value_to_double(&accel2[0]),
211 			sensor_value_to_double(&accel2[1]),
212 			sensor_value_to_double(&accel2[2]));
213 
214 		printf("ISM330DHCX: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n",
215 			sensor_value_to_double(&accel1[0]),
216 			sensor_value_to_double(&accel1[1]),
217 			sensor_value_to_double(&accel1[2]));
218 
219 		printf("ISM330DHCX: GYro (dps): x: %.3f, y: %.3f, z: %.3f\n",
220 			sensor_value_to_double(&gyro[0]),
221 			sensor_value_to_double(&gyro[1]),
222 			sensor_value_to_double(&gyro[2]));
223 
224 #ifdef CONFIG_ISM330DHCX_ENABLE_TEMP
225 		/* temperature */
226 		printf("ISM330DHCX: Temperature: %.1f C\n",
227 		       sensor_value_to_double(&die_temp));
228 #endif
229 
230 #ifdef CONFIG_ISM330DHCX_EXT_IIS2MDC
231 		printf("ISM330DHCX: Magn (gauss): x: %.3f, y: %.3f, z: %.3f\n",
232 		       sensor_value_to_double(&magn[0]),
233 		       sensor_value_to_double(&magn[1]),
234 		       sensor_value_to_double(&magn[2]));
235 #endif
236 
237 #ifdef CONFIG_IIS2DLPC_TRIGGER
238 		printk("%d:: iis2dlpc trig %d\n", cnt, iis2dlpc_trig_cnt);
239 #endif
240 
241 #ifdef CONFIG_ISM330DHCX_TRIGGER
242 		printk("%d:: ism330dhcx acc trig %d\n", cnt, ism330dhcx_acc_trig_cnt);
243 		printk("%d:: ism330dhcx gyr trig %d\n", cnt, ism330dhcx_gyr_trig_cnt);
244 #ifdef CONFIG_ISM330DHCX_ENABLE_TEMP
245 		printk("%d:: ism330dhcx temp trig %d\n", cnt, ism330dhcx_temp_trig_cnt);
246 #endif
247 #endif
248 
249 		cnt++;
250 		k_sleep(K_MSEC(2000));
251 	}
252 }
253