1 /*
2 * Copyright (c) 2020 Linumiz
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8
9 #include <zephyr/sys/printk.h>
10 #include <zephyr/sys_clock.h>
11 #include <stdio.h>
12
13 #include <zephyr/device.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/drivers/i2c.h>
16
17 #define MAX_TEST_TIME 5000
18 #define SLEEPTIME 300
19
print_accl_data(const struct device * itds)20 static void print_accl_data(const struct device *itds)
21 {
22 struct sensor_value val[3];
23
24 if (sensor_channel_get(itds, SENSOR_CHAN_ACCEL_XYZ, val) < 0) {
25 printf("Cannot read itds accl channels.\n");
26 return;
27 }
28
29 printf("Accl (m/s): X=%f, Y=%f, Z=%f\n",
30 sensor_value_to_double(&val[0]),
31 sensor_value_to_double(&val[1]),
32 sensor_value_to_double(&val[2]));
33 }
34
print_temp_data(const struct device * itds)35 static void print_temp_data(const struct device *itds)
36 {
37 struct sensor_value val;
38
39 if (sensor_channel_get(itds, SENSOR_CHAN_DIE_TEMP, &val) < 0) {
40 printf("Temperature channel read error.\n");
41 return;
42 }
43
44 printf("Temperature (Celsius): %f\n",
45 sensor_value_to_double(&val));
46 }
47
test_polling_mode(const struct device * itds)48 static void test_polling_mode(const struct device *itds)
49 {
50 int32_t remaining_test_time = MAX_TEST_TIME;
51
52 do {
53 if (sensor_sample_fetch(itds) < 0) {
54 printf("accl sample update error.\n");
55 } else {
56 print_accl_data(itds);
57
58 print_temp_data(itds);
59 }
60
61 /* wait a while */
62 k_sleep(K_MSEC(SLEEPTIME));
63
64 remaining_test_time -= SLEEPTIME;
65 } while (remaining_test_time > 0);
66 }
67
68 #if defined(CONFIG_ITDS_TRIGGER)
trigger_handler(const struct device * itds,const struct sensor_trigger * trigger)69 static void trigger_handler(const struct device *itds,
70 const struct sensor_trigger *trigger)
71 {
72 switch (trigger->type) {
73 case SENSOR_TRIG_DATA_READY:
74 printf("Data ready.\n");
75 if (sensor_sample_fetch(itds) < 0) {
76 printf("sample update error.\n");
77 } else {
78 print_accl_data(itds);
79 }
80 return;
81
82 default:
83 printf("trigger handler: unknown trigger type.\n");
84 return;
85 }
86 }
87 #endif
88
test_trigger_mode(const struct device * itds)89 static void test_trigger_mode(const struct device *itds)
90 {
91 #if defined(CONFIG_ITDS_TRIGGER)
92 struct sensor_trigger trig;
93 struct sensor_value attr;
94
95 printf("Testing data ready trigger.\n");
96
97 attr.val1 = 400;
98 attr.val2 = 0;
99
100 if (sensor_attr_set(itds, SENSOR_CHAN_ACCEL_XYZ,
101 SENSOR_ATTR_SAMPLING_FREQUENCY, &attr) < 0) {
102 printf("cannot set sampling frequency.\n");
103 return;
104 }
105
106 trig.type = SENSOR_TRIG_DATA_READY;
107 trig.chan = SENSOR_CHAN_ACCEL_XYZ;
108
109 if (sensor_trigger_set(itds, &trig, trigger_handler) < 0) {
110 printf("cannot set trigger.\n");
111 return;
112 }
113
114 k_sleep(K_MSEC(MAX_TEST_TIME));
115
116 trig.type = SENSOR_TRIG_DATA_READY;
117 trig.chan = SENSOR_CHAN_ACCEL_XYZ;
118 if (sensor_trigger_set(itds, &trig, NULL) < 0) {
119 printf("cannot clear trigger.\n");
120 return;
121 }
122
123 printf("Data ready trigger test finished.\n");
124 #endif
125
126 }
127
main(void)128 int main(void)
129 {
130 const struct device *const itds = DEVICE_DT_GET_ONE(we_wsen_itds);
131 struct sensor_value attr;
132
133 printf("get device wsen-itds\n");
134 if (!device_is_ready(itds)) {
135 printk("sensor: device not ready.\n");
136 return 0;
137 }
138
139 /*
140 * Set accl range to +/- 16 G. Since the sensor API needs SI
141 * units, convert the range to rad/s.
142 */
143 sensor_g_to_ms2(4, &attr);
144
145 if (sensor_attr_set(itds, SENSOR_CHAN_ACCEL_XYZ,
146 SENSOR_ATTR_FULL_SCALE, &attr) < 0) {
147 printf("Cannot set accl range.\n");
148 return 0;
149 }
150
151 printf("Testing the polling mode.\n");
152 test_polling_mode(itds);
153 printf("Polling mode test finished.\n");
154
155 printf("Testing the trigger mode.\n");
156 test_trigger_mode(itds);
157 printf("Trigger mode test finished.\n");
158 return 0;
159 }
160