1 /*
2  * Copyright (c) 2018 STMicroelectronics
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/drivers/led.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/drivers/spi.h>
14 #include <zephyr/drivers/sensor.h>
15 
16 #include <stdio.h>
17 
18 /* #define ARGONKEY_TEST_LOG 1 */
19 
20 #define WHOAMI_REG      0x0F
21 #define WHOAMI_ALT_REG  0x4F
22 
23 #ifdef CONFIG_LP3943
24 static const struct device *const ledc = DEVICE_DT_GET_ONE(ti_lp3943);
25 #endif
26 
out_ev(struct sensor_value * val)27 static inline float out_ev(struct sensor_value *val)
28 {
29 	return (val->val1 + (float)val->val2 / 1000000);
30 }
31 
32 static int lsm6dsl_trig_cnt;
33 #ifdef CONFIG_LSM6DSL_TRIGGER
lsm6dsl_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)34 static void lsm6dsl_trigger_handler(const struct device *dev,
35 				    const struct sensor_trigger *trig)
36 {
37 #ifdef ARGONKEY_TEST_LOG
38 	char out_str[64];
39 #endif
40 	struct sensor_value accel_x, accel_y, accel_z;
41 	struct sensor_value gyro_x, gyro_y, gyro_z;
42 
43 #if defined(CONFIG_LSM6DSL_EXT0_LIS2MDL)
44 	struct sensor_value magn_x, magn_y, magn_z;
45 #endif
46 #if defined(CONFIG_LSM6DSL_EXT0_LPS22HB)
47 	struct sensor_value press, temp;
48 #endif
49 
50 	lsm6dsl_trig_cnt++;
51 
52 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
53 	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_X, &accel_x);
54 	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Y, &accel_y);
55 	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Z, &accel_z);
56 #ifdef ARGONKEY_TEST_LOG
57 	sprintf(out_str, "accel (%f %f %f) m/s2", (double)out_ev(&accel_x),
58 						(double)out_ev(&accel_y),
59 						(double)out_ev(&accel_z));
60 	printk("TRIG %s\n", out_str);
61 #endif
62 
63 	/* lsm6dsl gyro */
64 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ);
65 	sensor_channel_get(dev, SENSOR_CHAN_GYRO_X, &gyro_x);
66 	sensor_channel_get(dev, SENSOR_CHAN_GYRO_Y, &gyro_y);
67 	sensor_channel_get(dev, SENSOR_CHAN_GYRO_Z, &gyro_z);
68 #ifdef ARGONKEY_TEST_LOG
69 	sprintf(out_str, "gyro (%f %f %f) dps", (double)out_ev(&gyro_x),
70 						(double)out_ev(&gyro_y),
71 						(double)out_ev(&gyro_z));
72 	printk("TRIG %s\n", out_str);
73 #endif
74 
75 #if defined(CONFIG_LSM6DSL_EXT0_LIS2MDL)
76 	/* lsm6dsl magn */
77 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_MAGN_XYZ);
78 	sensor_channel_get(dev, SENSOR_CHAN_MAGN_X, &magn_x);
79 	sensor_channel_get(dev, SENSOR_CHAN_MAGN_Y, &magn_y);
80 	sensor_channel_get(dev, SENSOR_CHAN_MAGN_Z, &magn_z);
81 #ifdef ARGONKEY_TEST_LOG
82 	sprintf(out_str, "magn (%f %f %f) gauss", (double)out_ev(&magn_x),
83 						 (double)out_ev(&magn_y),
84 						 (double)out_ev(&magn_z));
85 	printk("TRIG %s\n", out_str);
86 #endif
87 
88 #endif
89 #if defined(CONFIG_LSM6DSL_EXT0_LPS22HB)
90 	/* lsm6dsl press/temp */
91 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_PRESS);
92 	sensor_channel_get(dev, SENSOR_CHAN_PRESS, &press);
93 
94 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_AMBIENT_TEMP);
95 	sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
96 
97 #ifdef ARGONKEY_TEST_LOG
98 	sprintf(out_str, "press (%f) kPa - temp (%f) deg", (double)out_ev(&press),
99 							   (double)out_ev(&temp));
100 	printk("%s\n", out_str);
101 #endif
102 
103 #endif
104 }
105 #endif
106 
107 #define NUM_LEDS 12
108 #define DELAY_TIME K_MSEC(50)
109 
main(void)110 int main(void)
111 {
112 	int cnt = 0;
113 	char out_str[64];
114 	static const struct gpio_dt_spec led0_gpio = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
115 	static const struct gpio_dt_spec led1_gpio = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
116 	int i, on = 1;
117 
118 #ifdef CONFIG_LP3943
119 	if (!device_is_ready(ledc)) {
120 		printk("%s: device not ready.\n", ledc->name);
121 		return 0;
122 	}
123 
124 	/* turn all leds on */
125 	for (i = 0; i < NUM_LEDS; i++) {
126 		led_on(ledc, i);
127 		k_sleep(DELAY_TIME);
128 	}
129 
130 	/* turn all leds off */
131 	for (i = 0; i < NUM_LEDS; i++) {
132 		led_off(ledc, i);
133 		k_sleep(DELAY_TIME);
134 	}
135 #endif
136 
137 	if (!gpio_is_ready_dt(&led0_gpio)) {
138 		printk("%s: device not ready.\n", led0_gpio.port->name);
139 		return 0;
140 	}
141 	gpio_pin_configure_dt(&led0_gpio, GPIO_OUTPUT_ACTIVE);
142 
143 	if (!gpio_is_ready_dt(&led1_gpio)) {
144 		printk("%s: device not ready.\n", led1_gpio.port->name);
145 		return 0;
146 	}
147 	gpio_pin_configure_dt(&led1_gpio, GPIO_OUTPUT_INACTIVE);
148 
149 	for (i = 0; i < 5; i++) {
150 		gpio_pin_set_dt(&led1_gpio, on);
151 		k_sleep(K_MSEC(200));
152 		on = (on == 1) ? 0 : 1;
153 	}
154 
155 	printk("ArgonKey test!!\n");
156 
157 #ifdef CONFIG_LPS22HB
158 	const struct device *const baro_dev = DEVICE_DT_GET_ONE(st_lps22hb_press);
159 
160 	if (!device_is_ready(baro_dev)) {
161 		printk("%s: device not ready.\n", baro_dev->name);
162 		return 0;
163 	}
164 #endif
165 
166 #ifdef CONFIG_HTS221
167 	const struct device *const hum_dev = DEVICE_DT_GET_ONE(st_hts221);
168 
169 	if (!device_is_ready(hum_dev)) {
170 		printk("%s: device not ready.\n", hum_dev->name);
171 		return 0;
172 	}
173 #endif
174 
175 #ifdef CONFIG_LSM6DSL
176 	const struct device *const accel_dev = DEVICE_DT_GET_ONE(st_lsm6dsl);
177 
178 	if (!device_is_ready(accel_dev)) {
179 		printk("%s: device not ready.\n", accel_dev->name);
180 		return 0;
181 	}
182 
183 #if defined(CONFIG_LSM6DSL_ACCEL_ODR) && (CONFIG_LSM6DSL_ACCEL_ODR == 0)
184 	struct sensor_value a_odr_attr;
185 
186 	/* set sampling frequency to 104Hz for accel */
187 	a_odr_attr.val1 = 104;
188 	a_odr_attr.val2 = 0;
189 
190 	if (sensor_attr_set(accel_dev, SENSOR_CHAN_ACCEL_XYZ,
191 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &a_odr_attr) < 0) {
192 		printk("Cannot set sampling frequency for accelerometer.\n");
193 		return 0;
194 	}
195 #endif
196 
197 #if defined(CONFIG_LSM6DSL_ACCEL_FS) && (CONFIG_LSM6DSL_ACCEL_FS == 0)
198 	struct sensor_value a_fs_attr;
199 
200 	/* set full scale to 16g for accel */
201 	sensor_g_to_ms2(16, &a_fs_attr);
202 
203 	if (sensor_attr_set(accel_dev, SENSOR_CHAN_ACCEL_XYZ,
204 			    SENSOR_ATTR_FULL_SCALE, &a_fs_attr) < 0) {
205 		printk("Cannot set fs for accelerometer.\n");
206 		return 0;
207 	}
208 #endif
209 
210 #if defined(CONFIG_LSM6DSL_GYRO_ODR) && (CONFIG_LSM6DSL_GYRO_ODR == 0)
211 	struct sensor_value g_odr_attr;
212 
213 	/* set sampling frequency to 104Hz for accel */
214 	g_odr_attr.val1 = 104;
215 	g_odr_attr.val2 = 0;
216 
217 	if (sensor_attr_set(accel_dev, SENSOR_CHAN_GYRO_XYZ,
218 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &g_odr_attr) < 0) {
219 		printk("Cannot set sampling frequency for gyro.\n");
220 		return 0;
221 	}
222 #endif
223 
224 #if defined(CONFIG_LSM6DSL_GYRO_FS) && (CONFIG_LSM6DSL_GYRO_FS == 0)
225 	struct sensor_value g_fs_attr;
226 
227 	/* set full scale to 245dps for accel */
228 	sensor_degrees_to_rad(245, &g_fs_attr);
229 
230 	if (sensor_attr_set(accel_dev, SENSOR_CHAN_GYRO_XYZ,
231 			    SENSOR_ATTR_FULL_SCALE, &g_fs_attr) < 0) {
232 		printk("Cannot set fs for gyroscope.\n");
233 		return 0;
234 	}
235 #endif
236 
237 #endif
238 
239 #ifdef CONFIG_VL53L0X
240 	const struct device *const tof_dev = DEVICE_DT_GET_ONE(st_vl53l0x);
241 
242 	if (!device_is_ready(tof_dev)) {
243 		printk("%s: device not ready.\n", tof_dev->name);
244 		return 0;
245 	}
246 #endif
247 
248 #ifdef CONFIG_LSM6DSL_TRIGGER
249 	struct sensor_trigger trig;
250 
251 	trig.type = SENSOR_TRIG_DATA_READY;
252 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
253 	if (sensor_trigger_set(accel_dev, &trig,
254 			       lsm6dsl_trigger_handler) != 0) {
255 		printk("Could not set sensor type and channel\n");
256 		return 0;
257 	}
258 #endif
259 
260 	while (1) {
261 #ifdef CONFIG_LPS22HB
262 		struct sensor_value temp, press;
263 #endif
264 #ifdef CONFIG_HTS221
265 		struct sensor_value humidity;
266 #endif
267 #ifdef CONFIG_LSM6DSL
268 		struct sensor_value accel_x, accel_y, accel_z;
269 		struct sensor_value gyro_x, gyro_y, gyro_z;
270 #if defined(CONFIG_LSM6DSL_EXT0_LIS2MDL)
271 		struct sensor_value magn_x, magn_y, magn_z;
272 #endif
273 #if defined(CONFIG_LSM6DSL_EXT0_LPS22HB)
274 		struct sensor_value press, temp;
275 #endif
276 #endif
277 #ifdef CONFIG_VL53L0X
278 		struct sensor_value prox;
279 #endif
280 
281 #ifdef CONFIG_VL53L0X
282 		sensor_sample_fetch(tof_dev);
283 		sensor_channel_get(tof_dev, SENSOR_CHAN_PROX, &prox);
284 		printk("proxy: %d  ;\n", prox.val1);
285 		sensor_channel_get(tof_dev, SENSOR_CHAN_DISTANCE, &prox);
286 		printk("distance: %d m -- %02d cm;\n", prox.val1,
287 						       prox.val2/10000);
288 #endif
289 
290 #ifdef CONFIG_LPS22HB
291 		sensor_sample_fetch(baro_dev);
292 		sensor_channel_get(baro_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
293 		sensor_channel_get(baro_dev, SENSOR_CHAN_PRESS, &press);
294 
295 		printk("temp: %d.%02d C; press: %d.%06d\n",
296 		       temp.val1, temp.val2, press.val1, press.val2);
297 #endif
298 
299 #ifdef CONFIG_HTS221
300 		sensor_sample_fetch(hum_dev);
301 		sensor_channel_get(hum_dev, SENSOR_CHAN_HUMIDITY, &humidity);
302 
303 		printk("humidity: %d.%06d\n",
304 		       humidity.val1, humidity.val2);
305 #endif
306 
307 #ifdef CONFIG_LSM6DSL
308 		/* lsm6dsl accel */
309 		sensor_sample_fetch_chan(accel_dev, SENSOR_CHAN_ACCEL_XYZ);
310 		sensor_channel_get(accel_dev, SENSOR_CHAN_ACCEL_X, &accel_x);
311 		sensor_channel_get(accel_dev, SENSOR_CHAN_ACCEL_Y, &accel_y);
312 		sensor_channel_get(accel_dev, SENSOR_CHAN_ACCEL_Z, &accel_z);
313 		sprintf(out_str, "accel (%f %f %f) m/s2", (double)out_ev(&accel_x),
314 							(double)out_ev(&accel_y),
315 							(double)out_ev(&accel_z));
316 		printk("%s\n", out_str);
317 
318 		/* lsm6dsl gyro */
319 		sensor_sample_fetch_chan(accel_dev, SENSOR_CHAN_GYRO_XYZ);
320 		sensor_channel_get(accel_dev, SENSOR_CHAN_GYRO_X, &gyro_x);
321 		sensor_channel_get(accel_dev, SENSOR_CHAN_GYRO_Y, &gyro_y);
322 		sensor_channel_get(accel_dev, SENSOR_CHAN_GYRO_Z, &gyro_z);
323 		sprintf(out_str, "gyro (%f %f %f) dps", (double)out_ev(&gyro_x),
324 							(double)out_ev(&gyro_y),
325 							(double)out_ev(&gyro_z));
326 		printk("%s\n", out_str);
327 #if defined(CONFIG_LSM6DSL_EXT0_LIS2MDL)
328 		/* lsm6dsl magn */
329 		sensor_sample_fetch_chan(accel_dev, SENSOR_CHAN_MAGN_XYZ);
330 		sensor_channel_get(accel_dev, SENSOR_CHAN_MAGN_X, &magn_x);
331 		sensor_channel_get(accel_dev, SENSOR_CHAN_MAGN_Y, &magn_y);
332 		sensor_channel_get(accel_dev, SENSOR_CHAN_MAGN_Z, &magn_z);
333 		sprintf(out_str, "magn (%f %f %f) gauss", (double)out_ev(&magn_x),
334 							 (double)out_ev(&magn_y),
335 							 (double)out_ev(&magn_z));
336 		printk("%s\n", out_str);
337 #endif
338 #if defined(CONFIG_LSM6DSL_EXT0_LPS22HB)
339 		/* lsm6dsl press/temp */
340 		sensor_sample_fetch_chan(accel_dev, SENSOR_CHAN_PRESS);
341 		sensor_channel_get(accel_dev, SENSOR_CHAN_PRESS, &press);
342 
343 		sensor_sample_fetch_chan(accel_dev, SENSOR_CHAN_AMBIENT_TEMP);
344 		sensor_channel_get(accel_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
345 
346 		sprintf(out_str, "press (%f) kPa - temp (%f) deg",
347 			(double)out_ev(&press), (double)out_ev(&temp));
348 		printk("%s\n", out_str);
349 #endif
350 
351 #endif /* CONFIG_LSM6DSL */
352 
353 		printk("- (%d) (trig_cnt: %d)\n\n", ++cnt, lsm6dsl_trig_cnt);
354 		k_sleep(K_MSEC(2000));
355 	}
356 }
357