drivers/sensor: ms5607: Add multi-instance support
Add multi-instance support to the MS5607 driver. This is needed to easily add I2C support later. It also simplifies a bit the driver initialisation by using more static values. Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
This commit is contained in:
parent
098a5f51aa
commit
593ec46c0a
3 changed files with 103 additions and 104 deletions
|
@ -71,12 +71,12 @@ static void ms5607_compensate(struct ms5607_data *data,
|
|||
(1ll << 15);
|
||||
}
|
||||
|
||||
static int ms5607_read_prom(const struct ms5607_data *data, uint8_t cmd,
|
||||
static int ms5607_read_prom(const struct ms5607_config *config, uint8_t cmd,
|
||||
uint16_t *val)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = data->tf->read_prom(data, cmd, val);
|
||||
err = config->tf->read_prom(config, cmd, val);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Error reading prom");
|
||||
return err;
|
||||
|
@ -85,7 +85,7 @@ static int ms5607_read_prom(const struct ms5607_data *data, uint8_t cmd,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int ms5607_get_measurement(const struct ms5607_data *data,
|
||||
static int ms5607_get_measurement(const struct ms5607_config *config,
|
||||
uint32_t *val,
|
||||
uint8_t cmd,
|
||||
uint8_t delay)
|
||||
|
@ -94,14 +94,14 @@ static int ms5607_get_measurement(const struct ms5607_data *data,
|
|||
|
||||
*val = 0U;
|
||||
|
||||
err = data->tf->start_conversion(data, cmd);
|
||||
err = config->tf->start_conversion(config, cmd);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
k_msleep(delay);
|
||||
|
||||
err = data->tf->read_adc(data, val);
|
||||
err = config->tf->read_adc(config, val);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
@ -112,13 +112,14 @@ static int ms5607_get_measurement(const struct ms5607_data *data,
|
|||
static int ms5607_sample_fetch(const struct device *dev,
|
||||
enum sensor_channel channel)
|
||||
{
|
||||
const struct ms5607_config *config = dev->config;
|
||||
struct ms5607_data *data = dev->data;
|
||||
int err;
|
||||
uint32_t adc_pressure, adc_temperature;
|
||||
|
||||
__ASSERT_NO_MSG(channel == SENSOR_CHAN_ALL);
|
||||
|
||||
err = ms5607_get_measurement(data,
|
||||
err = ms5607_get_measurement(config,
|
||||
&adc_pressure,
|
||||
data->pressure_conv_cmd,
|
||||
data->pressure_conv_delay);
|
||||
|
@ -126,7 +127,7 @@ static int ms5607_sample_fetch(const struct device *dev,
|
|||
return err;
|
||||
}
|
||||
|
||||
err = ms5607_get_measurement(data,
|
||||
err = ms5607_get_measurement(config,
|
||||
&adc_temperature,
|
||||
data->temperature_conv_cmd,
|
||||
data->temperature_conv_delay);
|
||||
|
@ -224,10 +225,6 @@ static int ms5607_attr_set(const struct device *dev, enum sensor_channel chan,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static const struct ms5607_config ms5607_config = {
|
||||
.ms5607_device_name = DT_INST_BUS_LABEL(0),
|
||||
};
|
||||
|
||||
static int ms5607_init(const struct device *dev)
|
||||
{
|
||||
const struct ms5607_config *const config = dev->config;
|
||||
|
@ -235,18 +232,11 @@ static int ms5607_init(const struct device *dev)
|
|||
struct sensor_value val;
|
||||
int err;
|
||||
|
||||
data->ms5607_device = device_get_binding(config->ms5607_device_name);
|
||||
if (!data->ms5607_device) {
|
||||
LOG_ERR("master not found: %s", config->ms5607_device_name);
|
||||
return -EINVAL;
|
||||
err = config->tf->bus_check(config);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
ms5607_spi_init(dev);
|
||||
#else
|
||||
BUILD_ASSERT(1, "I2c interface not implemented yet");
|
||||
#endif
|
||||
|
||||
data->pressure = 0;
|
||||
data->temperature = 0;
|
||||
|
||||
|
@ -265,14 +255,14 @@ static int ms5607_init(const struct device *dev)
|
|||
return err;
|
||||
}
|
||||
|
||||
err = data->tf->reset(data);
|
||||
err = config->tf->reset(config);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
k_sleep(K_MSEC(2));
|
||||
|
||||
err = ms5607_read_prom(data, MS5607_CMD_CONV_READ_OFF_T1,
|
||||
err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_OFF_T1,
|
||||
&data->off_t1);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
|
@ -280,7 +270,7 @@ static int ms5607_init(const struct device *dev)
|
|||
|
||||
LOG_DBG("OFF_T1: %d", data->off_t1);
|
||||
|
||||
err = ms5607_read_prom(data, MS5607_CMD_CONV_READ_SENSE_T1,
|
||||
err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_SENSE_T1,
|
||||
&data->sens_t1);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
|
@ -288,28 +278,28 @@ static int ms5607_init(const struct device *dev)
|
|||
|
||||
LOG_DBG("SENSE_T1: %d", data->sens_t1);
|
||||
|
||||
err = ms5607_read_prom(data, MS5607_CMD_CONV_READ_T_REF, &data->t_ref);
|
||||
err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_T_REF, &data->t_ref);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
LOG_DBG("T_REF: %d", data->t_ref);
|
||||
|
||||
err = ms5607_read_prom(data, MS5607_CMD_CONV_READ_TCO, &data->tco);
|
||||
err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_TCO, &data->tco);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
LOG_DBG("TCO: %d", data->tco);
|
||||
|
||||
err = ms5607_read_prom(data, MS5607_CMD_CONV_READ_TCS, &data->tcs);
|
||||
err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_TCS, &data->tcs);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
LOG_DBG("TCS: %d", data->tcs);
|
||||
|
||||
err = ms5607_read_prom(data, MS5607_CMD_CONV_READ_TEMPSENS,
|
||||
err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_TEMPSENS,
|
||||
&data->tempsens);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
|
@ -326,13 +316,34 @@ static const struct sensor_driver_api ms5607_api_funcs = {
|
|||
.channel_get = ms5607_channel_get,
|
||||
};
|
||||
|
||||
static struct ms5607_data ms5607_data;
|
||||
#define MS5607_SPI_OPERATION (SPI_OP_MODE_MASTER | SPI_WORD_SET(8) | \
|
||||
SPI_MODE_CPOL | SPI_MODE_CPHA | \
|
||||
SPI_TRANSFER_MSB | SPI_LINES_SINGLE)
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0,
|
||||
ms5607_init,
|
||||
NULL,
|
||||
&ms5607_data,
|
||||
&ms5607_config,
|
||||
POST_KERNEL,
|
||||
CONFIG_SENSOR_INIT_PRIORITY,
|
||||
&ms5607_api_funcs);
|
||||
/* Initializes a struct ms5607_config for an instance on a SPI bus. */
|
||||
#define MS5607_CONFIG_SPI(inst) \
|
||||
{ \
|
||||
.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
|
||||
.tf = &ms5607_spi_transfer_function, \
|
||||
.spi_cfg = \
|
||||
SPI_CONFIG_DT_INST(inst, \
|
||||
MS5607_SPI_OPERATION, \
|
||||
0), \
|
||||
}
|
||||
|
||||
/* Main instantiation macro */
|
||||
#define MS5607_DEFINE(inst) \
|
||||
static struct ms5607_data ms5607_data_##inst; \
|
||||
static const struct ms5607_config ms5607_config_##inst = \
|
||||
MS5607_CONFIG_SPI(inst); \
|
||||
DEVICE_DT_INST_DEFINE(inst, \
|
||||
ms5607_init, \
|
||||
NULL, \
|
||||
&ms5607_data_##inst, \
|
||||
&ms5607_config_##inst, \
|
||||
POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, \
|
||||
&ms5607_api_funcs);
|
||||
|
||||
/* Create the struct device for every status "okay" node in the devicetree. */
|
||||
DT_INST_FOREACH_STATUS_OKAY(MS5607_DEFINE)
|
||||
|
|
|
@ -10,6 +10,10 @@
|
|||
#include <zephyr/types.h>
|
||||
#include <device.h>
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
#include <drivers/spi.h>
|
||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
||||
|
||||
#define MS5607_CMD_RESET 0x1E
|
||||
#define MS5607_CMD_CONV_P_256 0x40
|
||||
#define MS5607_CMD_CONV_P_512 0x42
|
||||
|
@ -61,20 +65,33 @@
|
|||
#define MS5607_TEMP_OVER_DEFAULT 2048
|
||||
#endif
|
||||
|
||||
/* Forward declaration */
|
||||
struct ms5607_config;
|
||||
|
||||
struct ms5607_transfer_function {
|
||||
int (*bus_check)(const struct ms5607_config *cfg);
|
||||
int (*reset)(const struct ms5607_config *cfg);
|
||||
int (*read_prom)(const struct ms5607_config *cfg, uint8_t cmd, uint16_t *val);
|
||||
int (*start_conversion)(const struct ms5607_config *cfg, uint8_t cmd);
|
||||
int (*read_adc)(const struct ms5607_config *cfg, uint32_t *val);
|
||||
};
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
int ms5607_spi_init(const struct device *dev);
|
||||
extern const struct ms5607_transfer_function ms5607_spi_transfer_function;
|
||||
#else
|
||||
/* I2c Interface not implemented yet */
|
||||
BUILD_ASSERT(1, "I2c interface not implemented yet");
|
||||
#endif
|
||||
|
||||
struct ms5607_config {
|
||||
char *ms5607_device_name;
|
||||
const struct device *bus;
|
||||
const struct ms5607_transfer_function *tf;
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
struct spi_config spi_cfg;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct ms5607_data {
|
||||
const struct device *ms5607_device;
|
||||
const struct ms5607_transfer_function *tf;
|
||||
/* Calibration values */
|
||||
uint16_t sens_t1;
|
||||
uint16_t off_t1;
|
||||
|
@ -95,11 +112,4 @@ struct ms5607_data {
|
|||
uint8_t temperature_conv_delay;
|
||||
};
|
||||
|
||||
struct ms5607_transfer_function {
|
||||
int (*reset)(const struct ms5607_data *data);
|
||||
int (*read_prom)(const struct ms5607_data *data, uint8_t cmd, uint16_t *val);
|
||||
int (*start_conversion)(const struct ms5607_data *data, uint8_t cmd);
|
||||
int (*read_adc)(const struct ms5607_data *data, uint32_t *val);
|
||||
};
|
||||
|
||||
#endif /* __SENSOR_MS607_H__*/
|
||||
|
|
|
@ -17,23 +17,7 @@ LOG_MODULE_DECLARE(ms5607);
|
|||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
|
||||
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
|
||||
static struct spi_cs_control ms5607_cs_ctrl;
|
||||
#endif
|
||||
|
||||
#define SPI_CS NULL
|
||||
|
||||
static struct spi_config ms5607_spi_conf = {
|
||||
.frequency = DT_INST_PROP(0, spi_max_frequency),
|
||||
.operation = (SPI_OP_MODE_MASTER | SPI_WORD_SET(8) |
|
||||
SPI_MODE_CPOL | SPI_MODE_CPHA |
|
||||
SPI_TRANSFER_MSB |
|
||||
SPI_LINES_SINGLE),
|
||||
.slave = DT_INST_REG_ADDR(0),
|
||||
.cs = SPI_CS,
|
||||
};
|
||||
|
||||
static int ms5607_spi_raw_cmd(const struct ms5607_data *data, uint8_t cmd)
|
||||
static int ms5607_spi_raw_cmd(const struct ms5607_config *config, uint8_t cmd)
|
||||
{
|
||||
const struct spi_buf buf = {
|
||||
.buf = &cmd,
|
||||
|
@ -45,12 +29,12 @@ static int ms5607_spi_raw_cmd(const struct ms5607_data *data, uint8_t cmd)
|
|||
.count = 1,
|
||||
};
|
||||
|
||||
return spi_write(data->ms5607_device, &ms5607_spi_conf, &buf_set);
|
||||
return spi_write(config->bus, &config->spi_cfg, &buf_set);
|
||||
}
|
||||
|
||||
static int ms5607_spi_reset(const struct ms5607_data *data)
|
||||
static int ms5607_spi_reset(const struct ms5607_config *config)
|
||||
{
|
||||
int err = ms5607_spi_raw_cmd(data, MS5607_CMD_RESET);
|
||||
int err = ms5607_spi_raw_cmd(config, MS5607_CMD_RESET);
|
||||
|
||||
if (err < 0) {
|
||||
return err;
|
||||
|
@ -60,7 +44,7 @@ static int ms5607_spi_reset(const struct ms5607_data *data)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int ms5607_spi_read_prom(const struct ms5607_data *data, uint8_t cmd,
|
||||
static int ms5607_spi_read_prom(const struct ms5607_config *config, uint8_t cmd,
|
||||
uint16_t *val)
|
||||
{
|
||||
int err;
|
||||
|
@ -95,8 +79,8 @@ static int ms5607_spi_read_prom(const struct ms5607_data *data, uint8_t cmd,
|
|||
.count = 1,
|
||||
};
|
||||
|
||||
err = spi_transceive(data->ms5607_device,
|
||||
&ms5607_spi_conf,
|
||||
err = spi_transceive(config->bus,
|
||||
&config->spi_cfg,
|
||||
&tx_buf_set,
|
||||
&rx_buf_set);
|
||||
if (err < 0) {
|
||||
|
@ -109,12 +93,12 @@ static int ms5607_spi_read_prom(const struct ms5607_data *data, uint8_t cmd,
|
|||
}
|
||||
|
||||
|
||||
static int ms5607_spi_start_conversion(const struct ms5607_data *data, uint8_t cmd)
|
||||
static int ms5607_spi_start_conversion(const struct ms5607_config *config, uint8_t cmd)
|
||||
{
|
||||
return ms5607_spi_raw_cmd(data, cmd);
|
||||
return ms5607_spi_raw_cmd(config, cmd);
|
||||
}
|
||||
|
||||
static int ms5607_spi_read_adc(const struct ms5607_data *data, uint32_t *val)
|
||||
static int ms5607_spi_read_adc(const struct ms5607_config *config, uint32_t *val)
|
||||
{
|
||||
int err;
|
||||
|
||||
|
@ -146,8 +130,8 @@ static int ms5607_spi_read_adc(const struct ms5607_data *data, uint32_t *val)
|
|||
.count = 1,
|
||||
};
|
||||
|
||||
err = spi_transceive(data->ms5607_device,
|
||||
&ms5607_spi_conf,
|
||||
err = spi_transceive(config->bus,
|
||||
&config->spi_cfg,
|
||||
&tx_buf_set,
|
||||
&rx_buf_set);
|
||||
if (err < 0) {
|
||||
|
@ -159,38 +143,32 @@ static int ms5607_spi_read_adc(const struct ms5607_data *data, uint32_t *val)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static const struct ms5607_transfer_function ms5607_spi_transfer_function = {
|
||||
static int ms5607_spi_check(const struct ms5607_config *config)
|
||||
{
|
||||
const struct spi_cs_control *cs = config->spi_cfg.cs;
|
||||
|
||||
if (!device_is_ready(config->bus)) {
|
||||
LOG_DBG("SPI bus %s not ready", config->bus->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (cs) {
|
||||
if (!device_is_ready(cs->gpio_dev)) {
|
||||
LOG_DBG("SPI CS GPIO controller %s not ready", cs->gpio_dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
LOG_DBG("SPI GPIO CS configured on %s:%u", cs->gpio_dev->name, cs->gpio_pin);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct ms5607_transfer_function ms5607_spi_transfer_function = {
|
||||
.bus_check = ms5607_spi_check,
|
||||
.reset = ms5607_spi_reset,
|
||||
.read_prom = ms5607_spi_read_prom,
|
||||
.start_conversion = ms5607_spi_start_conversion,
|
||||
.read_adc = ms5607_spi_read_adc,
|
||||
};
|
||||
|
||||
int ms5607_spi_init(const struct device *dev)
|
||||
{
|
||||
struct ms5607_data *data = dev->data;
|
||||
|
||||
data->tf = &ms5607_spi_transfer_function;
|
||||
|
||||
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
|
||||
ms5607_cs_ctrl.gpio_dev = device_get_binding(
|
||||
DT_INST_SPI_DEV_CS_GPIOS_LABEL(0));
|
||||
if (!ms5607_cs_ctrl.gpio_dev) {
|
||||
LOG_ERR("Unable to get GPIO SPI CS device");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ms5607_cs_ctrl.gpio_pin = DT_INST_SPI_DEV_CS_GPIOS_PIN(0);
|
||||
ms5607_cs_ctrl.gpio_dt_flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0);
|
||||
ms5607_cs_ctrl.delay = 0U;
|
||||
|
||||
ms5607_spi_conf.cs = &ms5607_cs_ctrl;
|
||||
|
||||
LOG_DBG("SPI GPIO CS configured on %s:%u",
|
||||
DT_INST_SPI_DEV_CS_GPIOS_LABEL(0),
|
||||
DT_INST_SPI_DEV_CS_GPIOS_PIN(0));
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue