1 /* Bosch BMI160 inertial measurement unit driver, trigger implementation
2 *
3 * Copyright (c) 2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/kernel.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/drivers/gpio.h>
11
12 #include "bmi160.h"
13
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_DECLARE(BMI160, CONFIG_SENSOR_LOG_LEVEL);
16
bmi160_handle_anymotion(const struct device * dev)17 static void bmi160_handle_anymotion(const struct device *dev)
18 {
19 struct bmi160_data *data = dev->data;
20
21 if (data->handler_anymotion) {
22 data->handler_anymotion(dev, data->trig_anymotion);
23 }
24 }
25
bmi160_handle_drdy(const struct device * dev,uint8_t status)26 static void bmi160_handle_drdy(const struct device *dev, uint8_t status)
27 {
28 struct bmi160_data *data = dev->data;
29
30 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
31 if (data->handler_drdy_acc && (status & BMI160_STATUS_ACC_DRDY)) {
32 data->handler_drdy_acc(dev, data->trig_drdy_acc);
33 }
34 #endif
35
36 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
37 if (data->handler_drdy_gyr && (status & BMI160_STATUS_GYR_DRDY)) {
38 data->handler_drdy_gyr(dev, data->trig_drdy_gyr);
39 }
40 #endif
41 }
42
bmi160_handle_interrupts(const struct device * dev)43 static void bmi160_handle_interrupts(const struct device *dev)
44 {
45 union {
46 uint8_t raw[5];
47 struct {
48 uint8_t status;
49 uint8_t int_status[4];
50 };
51 } buf;
52
53 if (bmi160_read(dev, BMI160_REG_STATUS, buf.raw, sizeof(buf)) < 0) {
54 return;
55 }
56
57 if ((buf.int_status[0] & BMI160_INT_STATUS0_ANYM) &&
58 (buf.int_status[2] & (BMI160_INT_STATUS2_ANYM_FIRST_X |
59 BMI160_INT_STATUS2_ANYM_FIRST_Y |
60 BMI160_INT_STATUS2_ANYM_FIRST_Z))) {
61 bmi160_handle_anymotion(dev);
62 }
63
64 if (buf.int_status[1] & BMI160_INT_STATUS1_DRDY) {
65 bmi160_handle_drdy(dev, buf.status);
66 }
67
68 }
69
70 #ifdef CONFIG_BMI160_TRIGGER_OWN_THREAD
71 static K_KERNEL_STACK_DEFINE(bmi160_thread_stack, CONFIG_BMI160_THREAD_STACK_SIZE);
72 static struct k_thread bmi160_thread;
73
bmi160_thread_main(struct bmi160_data * data)74 static void bmi160_thread_main(struct bmi160_data *data)
75 {
76 while (1) {
77 k_sem_take(&data->sem, K_FOREVER);
78 bmi160_handle_interrupts(data->dev);
79 }
80 }
81 #endif
82
83 #ifdef CONFIG_BMI160_TRIGGER_GLOBAL_THREAD
bmi160_work_handler(struct k_work * work)84 static void bmi160_work_handler(struct k_work *work)
85 {
86 struct bmi160_data *data = CONTAINER_OF(work, struct bmi160_data, work);
87
88 bmi160_handle_interrupts(data->dev);
89 }
90 #endif
91
92 extern struct bmi160_data bmi160_data;
93
bmi160_gpio_callback(const struct device * port,struct gpio_callback * cb,uint32_t pin)94 static void bmi160_gpio_callback(const struct device *port,
95 struct gpio_callback *cb, uint32_t pin)
96 {
97 struct bmi160_data *data =
98 CONTAINER_OF(cb, struct bmi160_data, gpio_cb);
99
100 ARG_UNUSED(port);
101 ARG_UNUSED(pin);
102
103 #if defined(CONFIG_BMI160_TRIGGER_OWN_THREAD)
104 k_sem_give(&data->sem);
105 #elif defined(CONFIG_BMI160_TRIGGER_GLOBAL_THREAD)
106 k_work_submit(&data->work);
107 #endif
108 }
109
bmi160_trigger_drdy_set(const struct device * dev,enum sensor_channel chan,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)110 static int bmi160_trigger_drdy_set(const struct device *dev,
111 enum sensor_channel chan,
112 const struct sensor_trigger *trig,
113 sensor_trigger_handler_t handler)
114 {
115 struct bmi160_data *data = dev->data;
116 uint8_t drdy_en = 0U;
117
118 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
119 if (chan == SENSOR_CHAN_ACCEL_XYZ) {
120 data->handler_drdy_acc = handler;
121 data->trig_drdy_acc = trig;
122 }
123
124 if (data->handler_drdy_acc) {
125 drdy_en = BMI160_INT_DRDY_EN;
126 }
127 #endif
128
129 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
130 if (chan == SENSOR_CHAN_GYRO_XYZ) {
131 data->handler_drdy_gyr = handler;
132 data->trig_drdy_gyr = trig;
133 }
134
135 if (data->handler_drdy_gyr) {
136 drdy_en = BMI160_INT_DRDY_EN;
137 }
138 #endif
139
140 if (bmi160_reg_update(dev, BMI160_REG_INT_EN1,
141 BMI160_INT_DRDY_EN, drdy_en) < 0) {
142 return -EIO;
143 }
144
145 return 0;
146 }
147
148 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
bmi160_trigger_anym_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)149 static int bmi160_trigger_anym_set(const struct device *dev,
150 const struct sensor_trigger *trig,
151 sensor_trigger_handler_t handler)
152 {
153 struct bmi160_data *data = dev->data;
154 uint8_t anym_en = 0U;
155
156 data->handler_anymotion = handler;
157 data->trig_anymotion = trig;
158
159 if (handler) {
160 anym_en = BMI160_INT_ANYM_X_EN |
161 BMI160_INT_ANYM_Y_EN |
162 BMI160_INT_ANYM_Z_EN;
163 }
164
165 if (bmi160_reg_update(dev, BMI160_REG_INT_EN0,
166 BMI160_INT_ANYM_MASK, anym_en) < 0) {
167 return -EIO;
168 }
169
170 return 0;
171 }
172
bmi160_trigger_set_acc(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)173 static int bmi160_trigger_set_acc(const struct device *dev,
174 const struct sensor_trigger *trig,
175 sensor_trigger_handler_t handler)
176 {
177 if (trig->type == SENSOR_TRIG_DATA_READY) {
178 return bmi160_trigger_drdy_set(dev, trig->chan, trig, handler);
179 } else if (trig->type == SENSOR_TRIG_DELTA) {
180 return bmi160_trigger_anym_set(dev, trig, handler);
181 }
182
183 return -ENOTSUP;
184 }
185
bmi160_acc_slope_config(const struct device * dev,enum sensor_attribute attr,const struct sensor_value * val)186 int bmi160_acc_slope_config(const struct device *dev,
187 enum sensor_attribute attr,
188 const struct sensor_value *val)
189 {
190 uint8_t acc_range_g, reg_val;
191 uint32_t slope_th_ums2;
192
193 if (attr == SENSOR_ATTR_SLOPE_TH) {
194 if (bmi160_byte_read(dev, BMI160_REG_ACC_RANGE, ®_val) < 0) {
195 return -EIO;
196 }
197
198 acc_range_g = bmi160_acc_reg_val_to_range(reg_val);
199
200 slope_th_ums2 = val->val1 * 1000000 + val->val2;
201
202 /* make sure the provided threshold does not exceed range / 2 */
203 if (slope_th_ums2 > (acc_range_g / 2 * SENSOR_G)) {
204 return -EINVAL;
205 }
206
207 reg_val = (slope_th_ums2 - 1) * 512U / (acc_range_g * SENSOR_G);
208
209 if (bmi160_byte_write(dev, BMI160_REG_INT_MOTION1,
210 reg_val) < 0) {
211 return -EIO;
212 }
213 } else { /* SENSOR_ATTR_SLOPE_DUR */
214 /* slope duration is measured in number of samples */
215 if (val->val1 < 1 || val->val1 > 4) {
216 return -ENOTSUP;
217 }
218
219 if (bmi160_reg_field_update(dev, BMI160_REG_INT_MOTION0,
220 BMI160_ANYM_DUR_POS,
221 BMI160_ANYM_DUR_MASK,
222 val->val1) < 0) {
223 return -EIO;
224 }
225 }
226
227 return 0;
228 }
229 #endif
230
231 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
bmi160_trigger_set_gyr(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)232 static int bmi160_trigger_set_gyr(const struct device *dev,
233 const struct sensor_trigger *trig,
234 sensor_trigger_handler_t handler)
235 {
236 if (trig->type == SENSOR_TRIG_DATA_READY) {
237 return bmi160_trigger_drdy_set(dev, trig->chan, trig, handler);
238 }
239
240 return -ENOTSUP;
241 }
242 #endif
243
bmi160_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)244 int bmi160_trigger_set(const struct device *dev,
245 const struct sensor_trigger *trig,
246 sensor_trigger_handler_t handler)
247 {
248 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
249 if (trig->chan == SENSOR_CHAN_ACCEL_XYZ) {
250 return bmi160_trigger_set_acc(dev, trig, handler);
251 }
252 #endif
253 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
254 if (trig->chan == SENSOR_CHAN_GYRO_XYZ) {
255 return bmi160_trigger_set_gyr(dev, trig, handler);
256 }
257 #endif
258 return -ENOTSUP;
259 }
260
bmi160_trigger_mode_init(const struct device * dev)261 int bmi160_trigger_mode_init(const struct device *dev)
262 {
263 struct bmi160_data *data = dev->data;
264 const struct bmi160_cfg *cfg = dev->config;
265 int ret;
266
267 if (!device_is_ready(cfg->interrupt.port)) {
268 LOG_DBG("GPIO port %s not ready", cfg->interrupt.port->name);
269 return -EINVAL;
270 }
271
272 data->dev = dev;
273
274 #if defined(CONFIG_BMI160_TRIGGER_OWN_THREAD)
275 k_sem_init(&data->sem, 0, K_SEM_MAX_LIMIT);
276
277 k_thread_create(&bmi160_thread, bmi160_thread_stack,
278 CONFIG_BMI160_THREAD_STACK_SIZE,
279 (k_thread_entry_t)bmi160_thread_main,
280 data, NULL, NULL,
281 K_PRIO_COOP(CONFIG_BMI160_THREAD_PRIORITY),
282 0, K_NO_WAIT);
283 #elif defined(CONFIG_BMI160_TRIGGER_GLOBAL_THREAD)
284 data->work.handler = bmi160_work_handler;
285 #endif
286
287 /* map all interrupts to INT1 pin */
288 if (bmi160_word_write(dev, BMI160_REG_INT_MAP0, 0xf0ff) < 0) {
289 LOG_DBG("Failed to map interrupts.");
290 return -EIO;
291 }
292
293 ret = gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
294 if (ret < 0) {
295 return ret;
296 }
297
298 gpio_init_callback(&data->gpio_cb,
299 bmi160_gpio_callback,
300 BIT(cfg->interrupt.pin));
301
302 ret = gpio_add_callback(cfg->interrupt.port, &data->gpio_cb);
303 if (ret < 0) {
304 return ret;
305 }
306
307 ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
308 GPIO_INT_EDGE_TO_ACTIVE);
309 if (ret < 0) {
310 return ret;
311 }
312
313 return bmi160_byte_write(dev, BMI160_REG_INT_OUT_CTRL,
314 BMI160_INT1_OUT_EN | BMI160_INT1_EDGE_CTRL);
315 }
316