drivers/sensor: iis2iclx: Make use of stmemsc common routines
Make use of the stmemsc i2c/spi read/write routine that has been introduced to simplify the ST sensor drivers code. Signed-off-by: Armando Visconti <armando.visconti@st.com>
This commit is contained in:
parent
253cbfd0aa
commit
9e50268c96
5 changed files with 25 additions and 121 deletions
|
@ -11,3 +11,5 @@ zephyr_library_sources_ifdef(CONFIG_IIS2ICLX iis2iclx_i2c.c)
|
|||
zephyr_library_sources_ifdef(CONFIG_IIS2ICLX iis2iclx_spi.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_IIS2ICLX_SENSORHUB iis2iclx_shub.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_IIS2ICLX_TRIGGER iis2iclx_trigger.c)
|
||||
|
||||
zephyr_library_include_directories(../stmemsc)
|
||||
|
|
|
@ -658,12 +658,12 @@ static int iis2iclx_init(const struct device *dev)
|
|||
|
||||
#define IIS2ICLX_CONFIG_SPI(inst) \
|
||||
{ \
|
||||
.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
|
||||
.bus_init = iis2iclx_spi_init, \
|
||||
.bus_cfg.spi_cfg = \
|
||||
.stmemsc_cfg.spi.bus = DEVICE_DT_GET(DT_INST_BUS(inst)),\
|
||||
.stmemsc_cfg.spi.spi_cfg = \
|
||||
SPI_CONFIG_DT_INST(inst, \
|
||||
IIS2ICLX_SPI_OPERATION, \
|
||||
0), \
|
||||
.bus_init = iis2iclx_spi_init, \
|
||||
.odr = DT_INST_PROP(inst, odr), \
|
||||
.range = DT_INST_PROP(inst, range), \
|
||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
|
||||
|
@ -676,9 +676,9 @@ static int iis2iclx_init(const struct device *dev)
|
|||
|
||||
#define IIS2ICLX_CONFIG_I2C(inst) \
|
||||
{ \
|
||||
.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
|
||||
.stmemsc_cfg.i2c.bus = DEVICE_DT_GET(DT_INST_BUS(inst)),\
|
||||
.stmemsc_cfg.i2c.i2c_slv_addr = DT_INST_REG_ADDR(inst), \
|
||||
.bus_init = iis2iclx_i2c_init, \
|
||||
.bus_cfg.i2c_slv_addr = DT_INST_REG_ADDR(inst), \
|
||||
.odr = DT_INST_PROP(inst, odr), \
|
||||
.range = DT_INST_PROP(inst, range), \
|
||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include <zephyr/types.h>
|
||||
#include <drivers/gpio.h>
|
||||
#include <sys/util.h>
|
||||
#include <stmemsc.h>
|
||||
#include "iis2iclx_reg.h"
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
|
@ -35,20 +36,16 @@
|
|||
#define SENSOR_DEG2RAD_DOUBLE (SENSOR_PI_DOUBLE / 180)
|
||||
#define SENSOR_G_DOUBLE (SENSOR_G / 1000000.0)
|
||||
|
||||
union iis2iclx_bus_cfg {
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
uint16_t i2c_slv_addr;
|
||||
#endif
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
struct spi_config spi_cfg;
|
||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
||||
};
|
||||
|
||||
struct iis2iclx_config {
|
||||
const struct device *bus;
|
||||
union {
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
const struct stmemsc_cfg_i2c i2c;
|
||||
#endif
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
const struct stmemsc_cfg_spi spi;
|
||||
#endif
|
||||
} stmemsc_cfg;
|
||||
int (*bus_init)(const struct device *dev);
|
||||
const union iis2iclx_bus_cfg bus_cfg;
|
||||
uint8_t odr;
|
||||
uint8_t range;
|
||||
#ifdef CONFIG_IIS2ICLX_TRIGGER
|
||||
|
@ -66,7 +63,6 @@ struct iis2iclx_data;
|
|||
|
||||
struct iis2iclx_data {
|
||||
const struct device *dev;
|
||||
const struct device *bus;
|
||||
int16_t acc[2];
|
||||
uint32_t acc_gain;
|
||||
#if defined(CONFIG_IIS2ICLX_ENABLE_TEMP)
|
||||
|
|
|
@ -10,42 +10,24 @@
|
|||
|
||||
#define DT_DRV_COMPAT st_iis2iclx
|
||||
|
||||
#include <string.h>
|
||||
#include <logging/log.h>
|
||||
|
||||
#include "iis2iclx.h"
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
|
||||
LOG_MODULE_DECLARE(IIS2ICLX, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static int iis2iclx_i2c_read(const struct device *dev, uint8_t reg_addr,
|
||||
uint8_t *value, uint8_t len)
|
||||
{
|
||||
const struct iis2iclx_config *cfg = dev->config;
|
||||
|
||||
return i2c_burst_read(cfg->bus, cfg->bus_cfg.i2c_slv_addr,
|
||||
reg_addr, value, len);
|
||||
}
|
||||
|
||||
static int iis2iclx_i2c_write(const struct device *dev, uint8_t reg_addr,
|
||||
uint8_t *value, uint8_t len)
|
||||
{
|
||||
const struct iis2iclx_config *cfg = dev->config;
|
||||
|
||||
return i2c_burst_write(cfg->bus, cfg->bus_cfg.i2c_slv_addr,
|
||||
reg_addr, value, len);
|
||||
}
|
||||
|
||||
int iis2iclx_i2c_init(const struct device *dev)
|
||||
{
|
||||
struct iis2iclx_data *data = dev->data;
|
||||
const struct iis2iclx_config *cfg = dev->config;
|
||||
|
||||
data->ctx_i2c.read_reg = (stmdev_read_ptr) iis2iclx_i2c_read;
|
||||
data->ctx_i2c.write_reg = (stmdev_write_ptr) iis2iclx_i2c_write;
|
||||
/* Use generic stmemsc routine for read/write I2C bus */
|
||||
data->ctx_i2c.read_reg = (stmdev_read_ptr) stmemsc_i2c_read;
|
||||
data->ctx_i2c.write_reg = (stmdev_write_ptr) stmemsc_i2c_write;
|
||||
|
||||
data->ctx = &data->ctx_i2c;
|
||||
data->ctx->handle = (void *)dev;
|
||||
data->ctx->handle = (void *)&cfg->stmemsc_cfg.i2c;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -10,99 +10,23 @@
|
|||
|
||||
#define DT_DRV_COMPAT st_iis2iclx
|
||||
|
||||
#include <string.h>
|
||||
#include "iis2iclx.h"
|
||||
#include <logging/log.h>
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
|
||||
#define IIS2ICLX_SPI_READ (1 << 7)
|
||||
|
||||
LOG_MODULE_DECLARE(IIS2ICLX, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static int iis2iclx_spi_read(const struct device *dev, uint8_t reg_addr,
|
||||
uint8_t *value, uint8_t len)
|
||||
{
|
||||
const struct iis2iclx_config *cfg = dev->config;
|
||||
const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg;
|
||||
uint8_t buffer_tx[2] = { reg_addr | IIS2ICLX_SPI_READ, 0 };
|
||||
const struct spi_buf tx_buf = {
|
||||
.buf = buffer_tx,
|
||||
.len = 2,
|
||||
};
|
||||
const struct spi_buf_set tx = {
|
||||
.buffers = &tx_buf,
|
||||
.count = 1
|
||||
};
|
||||
const struct spi_buf rx_buf[2] = {
|
||||
{
|
||||
.buf = NULL,
|
||||
.len = 1,
|
||||
},
|
||||
{
|
||||
.buf = value,
|
||||
.len = len,
|
||||
}
|
||||
};
|
||||
const struct spi_buf_set rx = {
|
||||
.buffers = rx_buf,
|
||||
.count = 2
|
||||
};
|
||||
|
||||
|
||||
if (len > 64) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (spi_transceive(cfg->bus, spi_cfg, &tx, &rx)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int iis2iclx_spi_write(const struct device *dev, uint8_t reg_addr,
|
||||
uint8_t *value, uint8_t len)
|
||||
{
|
||||
const struct iis2iclx_config *cfg = dev->config;
|
||||
const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg;
|
||||
uint8_t buffer_tx[1] = { reg_addr & ~IIS2ICLX_SPI_READ };
|
||||
const struct spi_buf tx_buf[2] = {
|
||||
{
|
||||
.buf = buffer_tx,
|
||||
.len = 1,
|
||||
},
|
||||
{
|
||||
.buf = value,
|
||||
.len = len,
|
||||
}
|
||||
};
|
||||
const struct spi_buf_set tx = {
|
||||
.buffers = tx_buf,
|
||||
.count = 2
|
||||
};
|
||||
|
||||
|
||||
if (len > 64) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (spi_write(cfg->bus, spi_cfg, &tx)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int iis2iclx_spi_init(const struct device *dev)
|
||||
{
|
||||
struct iis2iclx_data *data = dev->data;
|
||||
const struct iis2iclx_config *cfg = dev->config;
|
||||
|
||||
data->ctx_spi.read_reg = (stmdev_read_ptr) iis2iclx_spi_read;
|
||||
data->ctx_spi.write_reg = (stmdev_write_ptr) iis2iclx_spi_write;
|
||||
data->ctx_spi.read_reg = (stmdev_read_ptr) stmemsc_spi_read;
|
||||
data->ctx_spi.write_reg = (stmdev_write_ptr) stmemsc_spi_write;
|
||||
|
||||
data->ctx = &data->ctx_spi;
|
||||
data->ctx->handle = (void *)dev;
|
||||
data->ctx->handle = (void *)&cfg->stmemsc_cfg.spi;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue