|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Sabrina Simkhovich <sabrinasimkhovich@gmail.com> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT maxbotix_mb7040 |
| 8 | + |
| 9 | +#include <zephyr/kernel.h> |
| 10 | +#include <zephyr/drivers/sensor.h> |
| 11 | +#include <zephyr/drivers/i2c.h> |
| 12 | +#include <zephyr/device.h> |
| 13 | +#include <zephyr/logging/log.h> |
| 14 | +#include <zephyr/drivers/gpio.h> |
| 15 | + |
| 16 | +#define RANGE_CMD 0x51 |
| 17 | + |
| 18 | +#define MB7040_HAS_STATUS_GPIO DT_ANY_INST_HAS_PROP_STATUS_OKAY(status_gpios) |
| 19 | + |
| 20 | +LOG_MODULE_REGISTER(mb7040, CONFIG_SENSOR_LOG_LEVEL); |
| 21 | + |
| 22 | +struct mb7040_data { |
| 23 | + uint16_t distance_cm; |
| 24 | + struct k_sem read_sem; |
| 25 | +#if MB7040_HAS_STATUS_GPIO |
| 26 | + struct gpio_callback gpio_cb; |
| 27 | +#endif |
| 28 | +}; |
| 29 | + |
| 30 | +struct mb7040_config { |
| 31 | + struct i2c_dt_spec i2c; |
| 32 | + uint8_t i2c_addr; |
| 33 | +#if MB7040_HAS_STATUS_GPIO |
| 34 | + struct gpio_dt_spec status_gpio; |
| 35 | +#endif |
| 36 | +}; |
| 37 | + |
| 38 | +#if MB7040_HAS_STATUS_GPIO |
| 39 | +static void status_gpio_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins) |
| 40 | +{ |
| 41 | + struct mb7040_data *data = CONTAINER_OF(cb, struct mb7040_data, gpio_cb); |
| 42 | + |
| 43 | + k_sem_give(&data->read_sem); |
| 44 | +} |
| 45 | +#endif |
| 46 | + |
| 47 | +static int mb7040_sample_fetch(const struct device *dev, enum sensor_channel chan) |
| 48 | +{ |
| 49 | + const struct mb7040_config *cfg = (struct mb7040_config *)dev->config; |
| 50 | + struct mb7040_data *data = (struct mb7040_data *)dev->data; |
| 51 | + uint8_t cmd = RANGE_CMD; |
| 52 | + int ret; |
| 53 | + uint8_t read_data[2]; |
| 54 | + |
| 55 | + if (chan != SENSOR_CHAN_DISTANCE && chan != SENSOR_CHAN_ALL) { |
| 56 | + LOG_ERR("Sensor only supports distance"); |
| 57 | + return -EINVAL; |
| 58 | + } |
| 59 | + |
| 60 | + k_sem_reset(&data->read_sem); |
| 61 | + |
| 62 | +#if MB7040_HAS_STATUS_GPIO |
| 63 | + /* Check if status_gpio port is present */ |
| 64 | + if (cfg->status_gpio.port != NULL) { |
| 65 | + /* Enable interrupt before writing */ |
| 66 | + ret = gpio_pin_interrupt_configure_dt(&cfg->status_gpio, GPIO_INT_EDGE_FALLING); |
| 67 | + if (ret != 0) { |
| 68 | + LOG_ERR("Failed to configure interrupt: %d", ret); |
| 69 | + return ret; |
| 70 | + } |
| 71 | + } |
| 72 | +#endif |
| 73 | + |
| 74 | + /* Write range command to sensor */ |
| 75 | + ret = i2c_reg_write_byte_dt(&cfg->i2c, cfg->i2c_addr, cmd); |
| 76 | + |
| 77 | + if (ret != 0) { |
| 78 | + LOG_ERR("I2C write failed with error %d", ret); |
| 79 | +#if MB7040_HAS_STATUS_GPIO |
| 80 | + /* Disable interrupt before returning error*/ |
| 81 | + if (cfg->status_gpio.port != NULL) { |
| 82 | + gpio_pin_interrupt_configure_dt(&cfg->status_gpio, GPIO_INT_DISABLE); |
| 83 | + } |
| 84 | +#endif |
| 85 | + return ret; |
| 86 | + } |
| 87 | + |
| 88 | + ret = k_sem_take(&data->read_sem, K_MSEC(CONFIG_MB7040_DELAY_MS)); |
| 89 | + |
| 90 | +#if MB7040_HAS_STATUS_GPIO |
| 91 | + /* Check if status_gpio port is present */ |
| 92 | + if (cfg->status_gpio.port != NULL) { |
| 93 | + gpio_pin_interrupt_configure_dt(&cfg->status_gpio, GPIO_INT_DISABLE); |
| 94 | + if (ret != 0) { |
| 95 | + /* Success: interrupt fired and semaphore taken */ |
| 96 | + return ret; |
| 97 | + } |
| 98 | + } else { |
| 99 | + if (ret != -EAGAIN) { |
| 100 | + /* Should not happen: semaphore only given by GPIO interrupt */ |
| 101 | + return ret; |
| 102 | + } |
| 103 | + } |
| 104 | +#else |
| 105 | + if (ret != -EAGAIN) { |
| 106 | + /* Should not happen: semaphore only given by GPIO interrupt */ |
| 107 | + return ret; |
| 108 | + } |
| 109 | +#endif |
| 110 | + /* |
| 111 | + * Small wait due to device specific internal i2c timings. 10ms is a common |
| 112 | + * wait to time to ensure ultrasonic sensors achieve stability and accuracy. |
| 113 | + */ |
| 114 | + k_msleep(10); |
| 115 | + |
| 116 | + ret = i2c_read_dt(&cfg->i2c, read_data, 2); |
| 117 | + |
| 118 | + if (ret != 0) { |
| 119 | + LOG_ERR("I2C read failed with error %d", ret); |
| 120 | +#if MB7040_HAS_STATUS_GPIO |
| 121 | + if (cfg->status_gpio.port != NULL) { |
| 122 | + gpio_pin_interrupt_configure_dt(&cfg->status_gpio, GPIO_INT_DISABLE); |
| 123 | + } |
| 124 | +#endif |
| 125 | + return ret; |
| 126 | + } |
| 127 | + |
| 128 | + /* Convert MSB/LSB to distance in cm */ |
| 129 | + data->distance_cm = (read_data[0] << 8) | read_data[1]; |
| 130 | + return 0; |
| 131 | +} |
| 132 | + |
| 133 | +static int mb7040_channel_get(const struct device *dev, enum sensor_channel chan, |
| 134 | + struct sensor_value *val) |
| 135 | +{ |
| 136 | + struct mb7040_data *data = (struct mb7040_data *)dev->data; |
| 137 | + |
| 138 | + if (chan != SENSOR_CHAN_DISTANCE) { |
| 139 | + LOG_ERR("Sensor only supports distance"); |
| 140 | + return -ENOTSUP; |
| 141 | + } |
| 142 | + |
| 143 | + /* Meters */ |
| 144 | + val->val1 = data->distance_cm / 100; |
| 145 | + /* Micrometers */ |
| 146 | + val->val2 = (data->distance_cm % 100) * 10000; |
| 147 | + |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +static DEVICE_API(sensor, mb7040_api) = { |
| 152 | + .sample_fetch = mb7040_sample_fetch, |
| 153 | + .channel_get = mb7040_channel_get, |
| 154 | +}; |
| 155 | + |
| 156 | +static int mb7040_init(const struct device *dev) |
| 157 | +{ |
| 158 | + const struct mb7040_config *cfg = (struct mb7040_config *)dev->config; |
| 159 | + struct mb7040_data *data = (struct mb7040_data *)dev->data; |
| 160 | + |
| 161 | + k_sem_init(&data->read_sem, 0, 1); |
| 162 | + |
| 163 | + if (!i2c_is_ready_dt(&cfg->i2c)) { |
| 164 | + LOG_ERR("I2C not ready!"); |
| 165 | + return -ENODEV; |
| 166 | + } |
| 167 | + /* Initialize status GPIO if present */ |
| 168 | +#if MB7040_HAS_STATUS_GPIO |
| 169 | + if (cfg->status_gpio.port != NULL) { |
| 170 | + if (!gpio_is_ready_dt(&cfg->status_gpio)) { |
| 171 | + LOG_ERR("Status GPIO not ready"); |
| 172 | + return -ENODEV; |
| 173 | + } |
| 174 | + |
| 175 | + int ret = gpio_pin_configure_dt(&cfg->status_gpio, GPIO_INPUT); |
| 176 | + |
| 177 | + if (ret < 0) { |
| 178 | + LOG_ERR("Failed to configure status GPIO: %d", ret); |
| 179 | + return ret; |
| 180 | + } |
| 181 | + |
| 182 | + gpio_init_callback(&data->gpio_cb, status_gpio_callback, BIT(cfg->status_gpio.pin)); |
| 183 | + ret = gpio_add_callback(cfg->status_gpio.port, &data->gpio_cb); |
| 184 | + if (ret < 0) { |
| 185 | + LOG_ERR("Failed to add GPIO callback: %d", ret); |
| 186 | + return ret; |
| 187 | + } |
| 188 | + LOG_INF("MB7040 initialized with status GPIO"); |
| 189 | + } |
| 190 | +#else |
| 191 | + LOG_INF("MB7040 initialized"); |
| 192 | +#endif |
| 193 | + return 0; |
| 194 | +} |
| 195 | + |
| 196 | +#define MB7040_DEFINE(inst) \ |
| 197 | + static struct mb7040_data mb7040_data_##inst = { \ |
| 198 | + .distance_cm = 0, \ |
| 199 | + }; \ |
| 200 | + static const struct mb7040_config mb7040_config_##inst = { \ |
| 201 | + .i2c = I2C_DT_SPEC_INST_GET(inst), \ |
| 202 | + .i2c_addr = DT_INST_REG_ADDR(inst), \ |
| 203 | + IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, status_gpios), \ |
| 204 | + (.status_gpio = GPIO_DT_SPEC_INST_GET(inst, status_gpios),)) }; \ |
| 205 | + SENSOR_DEVICE_DT_INST_DEFINE(inst, mb7040_init, NULL, &mb7040_data_##inst, \ |
| 206 | + &mb7040_config_##inst, POST_KERNEL, \ |
| 207 | + CONFIG_SENSOR_INIT_PRIORITY, &mb7040_api); |
| 208 | + |
| 209 | +DT_INST_FOREACH_STATUS_OKAY(MB7040_DEFINE) |
0 commit comments