1 /*
2 * Copyright 2022 Grinn
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "ina23x_trigger.h"
8
9 #include <zephyr/logging/log.h>
10
11 LOG_MODULE_REGISTER(INA23X_TRIGGER, CONFIG_SENSOR_LOG_LEVEL);
12
ina23x_gpio_callback(const struct device * port,struct gpio_callback * cb,uint32_t pin)13 static void ina23x_gpio_callback(const struct device *port,
14 struct gpio_callback *cb, uint32_t pin)
15 {
16 ARG_UNUSED(pin);
17
18 struct ina23x_trigger *trigg = CONTAINER_OF(cb, struct ina23x_trigger, gpio_cb);
19
20 k_work_submit(&trigg->conversion_work);
21 }
22
ina23x_trigger_mode_init(struct ina23x_trigger * trigg,const struct gpio_dt_spec * alert_gpio)23 int ina23x_trigger_mode_init(struct ina23x_trigger *trigg, const struct gpio_dt_spec *alert_gpio)
24 {
25 int ret;
26
27 if (!device_is_ready(alert_gpio->port)) {
28 LOG_ERR("Alert GPIO device not ready");
29 return -ENODEV;
30 }
31
32 ret = gpio_pin_configure_dt(alert_gpio, GPIO_INPUT);
33 if (ret < 0) {
34 LOG_ERR("Could not configure gpio");
35 return ret;
36 }
37
38 gpio_init_callback(&trigg->gpio_cb,
39 ina23x_gpio_callback,
40 BIT(alert_gpio->pin));
41
42 ret = gpio_add_callback(alert_gpio->port, &trigg->gpio_cb);
43 if (ret < 0) {
44 LOG_ERR("Could not set gpio callback");
45 return ret;
46 }
47
48 return gpio_pin_interrupt_configure_dt(alert_gpio,
49 GPIO_INT_EDGE_FALLING);
50 }
51