minimal communication with BMX055
Some checks failed
Build / build (macos-12) (push) Waiting to run
Build / build (macos-14) (push) Waiting to run
Build / build (windows-2022) (push) Waiting to run
Build / build (ubuntu-22.04) (push) Failing after 15s
Documentation / build (push) Successful in 5s

This commit is contained in:
2024-04-27 23:12:48 -06:00
parent 29be843ad0
commit d9acd6b2a8
8 changed files with 218 additions and 1 deletions

View File

@ -0,0 +1,171 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT bosch_bmx055
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>
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)