From d9acd6b2a89edbfe4a2b50d45dca8eb5ca5bec17 Mon Sep 17 00:00:00 2001 From: Brendan Haines Date: Sat, 27 Apr 2024 23:12:48 -0600 Subject: [PATCH] minimal communication with BMX055 --- app/src/main.c | 10 +- boards/bh/mellifera_rev1/mellifera_rev1.dts | 5 + drivers/sensor/CMakeLists.txt | 1 + drivers/sensor/Kconfig | 1 + drivers/sensor/bmx055/CMakeLists.txt | 2 + drivers/sensor/bmx055/Kconfig | 17 ++ drivers/sensor/bmx055/bmx055.c | 171 ++++++++++++++++++++ dts/bindings/sensor/bosch,bmx055.yaml | 12 ++ 8 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 drivers/sensor/bmx055/CMakeLists.txt create mode 100644 drivers/sensor/bmx055/Kconfig create mode 100644 drivers/sensor/bmx055/bmx055.c create mode 100644 dts/bindings/sensor/bosch,bmx055.yaml diff --git a/app/src/main.c b/app/src/main.c index f042d19..3cd4c7e 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -84,7 +84,7 @@ int main(void) cfb_set_kerning(dev, 3); - const struct device *ina, *bmp, *hdc; + const struct device *ina, *bmp, *bmx, *hdc; ina = DEVICE_DT_GET(DT_NODELABEL(ina231)); if (!device_is_ready(ina)) @@ -100,6 +100,14 @@ int main(void) return 0; } + bmx = DEVICE_DT_GET(DT_NODELABEL(bmx055)); + if (!device_is_ready(bmx)) + { + LOG_ERR("Device %s not ready\n", bmx->name); + return 0; + } + printf("Initialized %s\n", bmx->name); + // hdc = DEVICE_DT_GET(DT_NODELABEL(hdc1080)); // if (!device_is_ready(dev)) // { diff --git a/boards/bh/mellifera_rev1/mellifera_rev1.dts b/boards/bh/mellifera_rev1/mellifera_rev1.dts index 9ae1da8..9b967f0 100644 --- a/boards/bh/mellifera_rev1/mellifera_rev1.dts +++ b/boards/bh/mellifera_rev1/mellifera_rev1.dts @@ -168,6 +168,11 @@ compatible = "ti,hdc20xx"; // FIXME: add proper support for this part. Hopefully these parts are close enough reg = <0x40>; }; + + bmx055: bmx055@18 { + compatible = "bosch,bmx055"; + reg = <0x18>; + }; }; &spi1 { diff --git a/drivers/sensor/CMakeLists.txt b/drivers/sensor/CMakeLists.txt index 72ed549..1b12c15 100644 --- a/drivers/sensor/CMakeLists.txt +++ b/drivers/sensor/CMakeLists.txt @@ -2,3 +2,4 @@ # SPDX-License-Identifier: Apache-2.0 add_subdirectory_ifdef(CONFIG_EXAMPLE_SENSOR example_sensor) +add_subdirectory_ifdef(CONFIG_BMX055 bmx055) diff --git a/drivers/sensor/Kconfig b/drivers/sensor/Kconfig index 07b6c17..52e2f51 100644 --- a/drivers/sensor/Kconfig +++ b/drivers/sensor/Kconfig @@ -3,4 +3,5 @@ if SENSOR rsource "example_sensor/Kconfig" +rsource "bmx055/Kconfig" endif # SENSOR diff --git a/drivers/sensor/bmx055/CMakeLists.txt b/drivers/sensor/bmx055/CMakeLists.txt new file mode 100644 index 0000000..ad9d838 --- /dev/null +++ b/drivers/sensor/bmx055/CMakeLists.txt @@ -0,0 +1,2 @@ +zephyr_library() +zephyr_library_sources(bmx055.c) diff --git a/drivers/sensor/bmx055/Kconfig b/drivers/sensor/bmx055/Kconfig new file mode 100644 index 0000000..f384dad --- /dev/null +++ b/drivers/sensor/bmx055/Kconfig @@ -0,0 +1,17 @@ +config BMX055 + bool "BMX055" + default y + depends on DT_HAS_BOSCH_BMX055_ENABLED + help + Enable driver for BMX055. + +if BMX055 + +config BMX055_TRIGGER + bool "BMX055 trigger mode" + depends on BMX055 + help + Set to enable trigger mode using gpio interrupt, where + interrupts are configured to line ALERT PIN. + +endif # BMX055 diff --git a/drivers/sensor/bmx055/bmx055.c b/drivers/sensor/bmx055/bmx055.c new file mode 100644 index 0000000..c2f7141 --- /dev/null +++ b/drivers/sensor/bmx055/bmx055.c @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2021 Nordic Semiconductor ASA + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT bosch_bmx055 + +#include +#include +#include +#include + +#include +LOG_MODULE_REGISTER(bmx055, CONFIG_SENSOR_LOG_LEVEL); + +struct bmx055_data +{ + int state; +}; + +struct bmx055_config +{ + struct i2c_dt_spec bus; +#ifdef CONFIG_INA230_TRIGGER + bool trig_enabled; + uint16_t mask; + const struct gpio_dt_spec alert_gpio; + uint16_t alert_limit; +#endif /* CONFIG_INA230_TRIGGER */ +}; + +// int bmx055_reg_read(const struct i2c_dt_spec *bus, uint8_t reg, uint8_t *val) +// { +// uint8_t data[2]; +// int ret; + +// ret = i2c_burst_read_dt(bus, reg, data, sizeof(data)); +// if (ret < 0) +// { +// return ret; +// } + +// *val = sys_get_be16(data); + +// return ret; +// } + +// int bmx055_reg_write(const struct i2c_dt_spec *bus, uint8_t reg, uint8_t val) +// { +// uint8_t tx_buf[2]; + +// tx_buf[0] = reg; +// tx_buf[1] = val; + +// return i2c_write_dt(bus, tx_buf, sizeof(tx_buf)); +// } + +static int bmx055_sample_fetch(const struct device *dev, + enum sensor_channel chan) +{ + // const struct bmx055_config *config = dev->config; + struct bmx055_data *data = dev->data; + + // data->state = gpio_pin_get_dt(&config->input); + data->state = 0; + + return 0; +} + +static int bmx055_channel_get(const struct device *dev, + enum sensor_channel chan, + struct sensor_value *val) +{ + struct bmx055_data *data = dev->data; + + if (chan != SENSOR_CHAN_PROX) + { + return -ENOTSUP; + } + + val->val1 = data->state; + + return 0; +} + +static const struct sensor_driver_api bmx055_api = { + .sample_fetch = &bmx055_sample_fetch, + .channel_get = &bmx055_channel_get, +}; + +static int bmx055_init(const struct device *dev) +{ + const struct bmx055_config *const config = dev->config; + int ret; + + if (!device_is_ready(config->bus.bus)) + { + LOG_ERR("I2C bus %s is not ready", config->bus.bus->name); + return -ENODEV; + } + + const uint8_t REG_BWG_CHIPID = 0x00; + uint8_t chip_id; + ret = i2c_burst_read_dt(&config->bus, REG_BWG_CHIPID, &chip_id, sizeof(chip_id)); + if (ret < 0) + { + LOG_ERR("Failed to read chip ID register!"); + return ret; + } + const uint8_t CHIP_ID = 0xfa; + if (chip_id != CHIP_ID) + { + LOG_ERR("Chip ID read from %s incorrect. Read 0x%02X, expected 0x%02X", dev->name, chip_id, CHIP_ID); + } + + // ret = ina23x_reg_write(&config->bus, INA230_REG_CONFIG, config->config); + // if (ret < 0) + // { + // LOG_ERR("Failed to write configuration register!"); + // return ret; + // } + + // ret = ina230_calibrate(dev); + // if (ret < 0) + // { + // LOG_ERR("Failed to write calibration register!"); + // return ret; + // } + + // #ifdef CONFIG_INA230_TRIGGER + // if (config->trig_enabled) + // { + // ret = ina230_trigger_mode_init(dev); + // if (ret < 0) + // { + // LOG_ERR("Failed to init trigger mode\n"); + // return ret; + // } + + // ret = ina23x_reg_write(&config->bus, INA230_REG_ALERT, config->alert_limit); + // if (ret < 0) + // { + // LOG_ERR("Failed to write alert register!"); + // return ret; + // } + + // ret = ina23x_reg_write(&config->bus, INA230_REG_MASK, config->mask); + // if (ret < 0) + // { + // LOG_ERR("Failed to write mask register!"); + // return ret; + // } + // } + // #endif /* CONFIG_INA230_TRIGGER */ + + return 0; +} + +#define BMX055_INIT(i) \ + static struct bmx055_data bmx055_data_##i; \ + \ + static const struct bmx055_config bmx055_config_##i = { \ + .bus = I2C_DT_SPEC_INST_GET(i), \ + }; \ + \ + SENSOR_DEVICE_DT_INST_DEFINE(i, &bmx055_init, NULL, \ + &bmx055_data_##i, \ + &bmx055_config_##i, POST_KERNEL, \ + CONFIG_SENSOR_INIT_PRIORITY, &bmx055_api); + +DT_INST_FOREACH_STATUS_OKAY(BMX055_INIT) diff --git a/dts/bindings/sensor/bosch,bmx055.yaml b/dts/bindings/sensor/bosch,bmx055.yaml new file mode 100644 index 0000000..37a2cfb --- /dev/null +++ b/dts/bindings/sensor/bosch,bmx055.yaml @@ -0,0 +1,12 @@ +description: | + TODO: add a description + +compatible: "bosch,bmx055" + +include: [sensor-device.yaml, i2c-device.yaml] + +# properties: +# input-gpios: +# type: phandle-array +# required: true +# description: Input GPIO to be sensed.