drivers/sensor: iis2iclx: avoid using both ctx_i2c and ctx_spi
The STMEMSC HAL i/f requires every driver using it to define for each instance a stmdev_ctx_t structure, which is used to export the hardware specific APIs to handle i2c and spi busses operations. Since this structure is bus agnostic, there is no need to declare it twice for both i2c and spi. Instead, declare only one structure, which will be populated either with i2c APIs or with spi APIs according to where that particular instance is declared inside the DT. Signed-off-by: Armando Visconti <armando.visconti@st.com>
This commit is contained in:
parent
9e50268c96
commit
aae2161542
6 changed files with 38 additions and 46 deletions
|
@ -70,7 +70,7 @@ static inline int iis2iclx_reboot(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct iis2iclx_data *data = dev->data;
|
struct iis2iclx_data *data = dev->data;
|
||||||
|
|
||||||
if (iis2iclx_boot_set(data->ctx, 1) < 0) {
|
if (iis2iclx_boot_set(&data->ctx, 1) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ static int iis2iclx_accel_set_fs_raw(const struct device *dev, uint8_t fs)
|
||||||
{
|
{
|
||||||
struct iis2iclx_data *data = dev->data;
|
struct iis2iclx_data *data = dev->data;
|
||||||
|
|
||||||
if (iis2iclx_xl_full_scale_set(data->ctx, fs) < 0) {
|
if (iis2iclx_xl_full_scale_set(&data->ctx, fs) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +97,7 @@ static int iis2iclx_accel_set_odr_raw(const struct device *dev, uint8_t odr)
|
||||||
{
|
{
|
||||||
struct iis2iclx_data *data = dev->data;
|
struct iis2iclx_data *data = dev->data;
|
||||||
|
|
||||||
if (iis2iclx_xl_data_rate_set(data->ctx, odr) < 0) {
|
if (iis2iclx_xl_data_rate_set(&data->ctx, odr) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +196,7 @@ static int iis2iclx_sample_fetch_accel(const struct device *dev)
|
||||||
struct iis2iclx_data *data = dev->data;
|
struct iis2iclx_data *data = dev->data;
|
||||||
int16_t buf[2];
|
int16_t buf[2];
|
||||||
|
|
||||||
if (iis2iclx_acceleration_raw_get(data->ctx, buf) < 0) {
|
if (iis2iclx_acceleration_raw_get(&data->ctx, buf) < 0) {
|
||||||
LOG_ERR("Failed to read sample");
|
LOG_ERR("Failed to read sample");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -213,7 +213,7 @@ static int iis2iclx_sample_fetch_temp(const struct device *dev)
|
||||||
struct iis2iclx_data *data = dev->data;
|
struct iis2iclx_data *data = dev->data;
|
||||||
int16_t buf;
|
int16_t buf;
|
||||||
|
|
||||||
if (iis2iclx_temperature_raw_get(data->ctx, &buf) < 0) {
|
if (iis2iclx_temperature_raw_get(&data->ctx, &buf) < 0) {
|
||||||
LOG_ERR("Failed to read sample");
|
LOG_ERR("Failed to read sample");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -540,7 +540,7 @@ static int iis2iclx_init_chip(const struct device *dev)
|
||||||
|
|
||||||
iis2iclx->dev = dev;
|
iis2iclx->dev = dev;
|
||||||
|
|
||||||
if (iis2iclx_device_id_get(iis2iclx->ctx, &chip_id) < 0) {
|
if (iis2iclx_device_id_get(&iis2iclx->ctx, &chip_id) < 0) {
|
||||||
LOG_ERR("Failed reading chip id");
|
LOG_ERR("Failed reading chip id");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -553,7 +553,7 @@ static int iis2iclx_init_chip(const struct device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* reset device */
|
/* reset device */
|
||||||
if (iis2iclx_reset_set(iis2iclx->ctx, 1) < 0) {
|
if (iis2iclx_reset_set(&iis2iclx->ctx, 1) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,12 +573,12 @@ static int iis2iclx_init_chip(const struct device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set FIFO bypass mode */
|
/* Set FIFO bypass mode */
|
||||||
if (iis2iclx_fifo_mode_set(iis2iclx->ctx, IIS2ICLX_BYPASS_MODE) < 0) {
|
if (iis2iclx_fifo_mode_set(&iis2iclx->ctx, IIS2ICLX_BYPASS_MODE) < 0) {
|
||||||
LOG_ERR("failed to set FIFO mode");
|
LOG_ERR("failed to set FIFO mode");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iis2iclx_block_data_update_set(iis2iclx->ctx, 1) < 0) {
|
if (iis2iclx_block_data_update_set(&iis2iclx->ctx, 1) < 0) {
|
||||||
LOG_ERR("failed to set BDU mode");
|
LOG_ERR("failed to set BDU mode");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,14 +82,7 @@ struct iis2iclx_data {
|
||||||
bool shub_inited;
|
bool shub_inited;
|
||||||
#endif /* CONFIG_IIS2ICLX_SENSORHUB */
|
#endif /* CONFIG_IIS2ICLX_SENSORHUB */
|
||||||
|
|
||||||
stmdev_ctx_t *ctx;
|
stmdev_ctx_t ctx;
|
||||||
|
|
||||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
|
||||||
stmdev_ctx_t ctx_i2c;
|
|
||||||
#endif
|
|
||||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
|
||||||
stmdev_ctx_t ctx_spi;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint16_t accel_freq;
|
uint16_t accel_freq;
|
||||||
uint8_t accel_fs;
|
uint8_t accel_fs;
|
||||||
|
|
|
@ -23,11 +23,10 @@ int iis2iclx_i2c_init(const struct device *dev)
|
||||||
const struct iis2iclx_config *cfg = dev->config;
|
const struct iis2iclx_config *cfg = dev->config;
|
||||||
|
|
||||||
/* Use generic stmemsc routine for read/write I2C bus */
|
/* Use generic stmemsc routine for read/write I2C bus */
|
||||||
data->ctx_i2c.read_reg = (stmdev_read_ptr) stmemsc_i2c_read;
|
data->ctx.read_reg = (stmdev_read_ptr) stmemsc_i2c_read;
|
||||||
data->ctx_i2c.write_reg = (stmdev_write_ptr) stmemsc_i2c_write;
|
data->ctx.write_reg = (stmdev_write_ptr) stmemsc_i2c_write;
|
||||||
|
|
||||||
data->ctx = &data->ctx_i2c;
|
data->ctx.handle = (void *)&cfg->stmemsc_cfg.i2c;
|
||||||
data->ctx->handle = (void *)&cfg->stmemsc_cfg.i2c;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -423,16 +423,16 @@ static inline void iis2iclx_shub_wait_completed(struct iis2iclx_data *data)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
k_msleep(1);
|
k_msleep(1);
|
||||||
iis2iclx_sh_status_get(data->ctx, &status);
|
iis2iclx_sh_status_get(&data->ctx, &status);
|
||||||
} while (status.sens_hub_endop == 0);
|
} while (status.sens_hub_endop == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void iis2iclx_shub_embedded_en(struct iis2iclx_data *data, bool on)
|
static inline void iis2iclx_shub_embedded_en(struct iis2iclx_data *data, bool on)
|
||||||
{
|
{
|
||||||
if (on) {
|
if (on) {
|
||||||
(void) iis2iclx_mem_bank_set(data->ctx, IIS2ICLX_SENSOR_HUB_BANK);
|
(void) iis2iclx_mem_bank_set(&data->ctx, IIS2ICLX_SENSOR_HUB_BANK);
|
||||||
} else {
|
} else {
|
||||||
(void) iis2iclx_mem_bank_set(data->ctx, IIS2ICLX_USER_BANK);
|
(void) iis2iclx_mem_bank_set(&data->ctx, IIS2ICLX_USER_BANK);
|
||||||
}
|
}
|
||||||
|
|
||||||
k_busy_wait(150);
|
k_busy_wait(150);
|
||||||
|
@ -444,7 +444,7 @@ static int iis2iclx_shub_read_embedded_regs(struct iis2iclx_data *data,
|
||||||
{
|
{
|
||||||
iis2iclx_shub_embedded_en(data, true);
|
iis2iclx_shub_embedded_en(data, true);
|
||||||
|
|
||||||
if (iis2iclx_read_reg(data->ctx, reg_addr, value, len) < 0) {
|
if (iis2iclx_read_reg(&data->ctx, reg_addr, value, len) < 0) {
|
||||||
LOG_ERR("shub: failed to read external reg: %02x", reg_addr);
|
LOG_ERR("shub: failed to read external reg: %02x", reg_addr);
|
||||||
iis2iclx_shub_embedded_en(data, false);
|
iis2iclx_shub_embedded_en(data, false);
|
||||||
return -EIO;
|
return -EIO;
|
||||||
|
@ -461,7 +461,7 @@ static int iis2iclx_shub_write_embedded_regs(struct iis2iclx_data *data,
|
||||||
{
|
{
|
||||||
iis2iclx_shub_embedded_en(data, true);
|
iis2iclx_shub_embedded_en(data, true);
|
||||||
|
|
||||||
if (iis2iclx_write_reg(data->ctx, reg_addr, value, len) < 0) {
|
if (iis2iclx_write_reg(&data->ctx, reg_addr, value, len) < 0) {
|
||||||
LOG_ERR("shub: failed to write external reg: %02x", reg_addr);
|
LOG_ERR("shub: failed to write external reg: %02x", reg_addr);
|
||||||
iis2iclx_shub_embedded_en(data, false);
|
iis2iclx_shub_embedded_en(data, false);
|
||||||
return -EIO;
|
return -EIO;
|
||||||
|
@ -478,7 +478,7 @@ static void iis2iclx_shub_enable(struct iis2iclx_data *data, uint8_t enable)
|
||||||
if (!data->accel_freq) {
|
if (!data->accel_freq) {
|
||||||
uint8_t odr = (enable) ? 2 : 0;
|
uint8_t odr = (enable) ? 2 : 0;
|
||||||
|
|
||||||
if (iis2iclx_xl_data_rate_set(data->ctx, odr) < 0) {
|
if (iis2iclx_xl_data_rate_set(&data->ctx, odr) < 0) {
|
||||||
LOG_DBG("shub: failed to set XL sampling rate");
|
LOG_DBG("shub: failed to set XL sampling rate");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -486,7 +486,7 @@ static void iis2iclx_shub_enable(struct iis2iclx_data *data, uint8_t enable)
|
||||||
|
|
||||||
iis2iclx_shub_embedded_en(data, true);
|
iis2iclx_shub_embedded_en(data, true);
|
||||||
|
|
||||||
if (iis2iclx_sh_master_set(data->ctx, enable) < 0) {
|
if (iis2iclx_sh_master_set(&data->ctx, enable) < 0) {
|
||||||
LOG_DBG("shub: failed to set master on");
|
LOG_DBG("shub: failed to set master on");
|
||||||
iis2iclx_shub_embedded_en(data, false);
|
iis2iclx_shub_embedded_en(data, false);
|
||||||
return;
|
return;
|
||||||
|
@ -544,7 +544,7 @@ static int iis2iclx_shub_read_slave_reg(struct iis2iclx_data *data,
|
||||||
|
|
||||||
/* read data from external slave */
|
/* read data from external slave */
|
||||||
iis2iclx_shub_embedded_en(data, true);
|
iis2iclx_shub_embedded_en(data, true);
|
||||||
if (iis2iclx_read_reg(data->ctx, IIS2ICLX_SHUB_DATA_OUT,
|
if (iis2iclx_read_reg(&data->ctx, IIS2ICLX_SHUB_DATA_OUT,
|
||||||
value, len) < 0) {
|
value, len) < 0) {
|
||||||
LOG_ERR("shub: error reading sensor data");
|
LOG_ERR("shub: error reading sensor data");
|
||||||
iis2iclx_shub_embedded_en(data, false);
|
iis2iclx_shub_embedded_en(data, false);
|
||||||
|
@ -645,14 +645,14 @@ static int iis2iclx_shub_set_data_channel(struct iis2iclx_data *data)
|
||||||
/* Configure the master */
|
/* Configure the master */
|
||||||
iis2iclx_aux_sens_on_t aux = IIS2ICLX_SLV_0_1_2;
|
iis2iclx_aux_sens_on_t aux = IIS2ICLX_SLV_0_1_2;
|
||||||
|
|
||||||
if (iis2iclx_sh_slave_connected_set(data->ctx, aux) < 0) {
|
if (iis2iclx_sh_slave_connected_set(&data->ctx, aux) < 0) {
|
||||||
LOG_ERR("shub: error setting aux sensors");
|
LOG_ERR("shub: error setting aux sensors");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
iis2iclx_write_once_t wo = IIS2ICLX_ONLY_FIRST_CYCLE;
|
iis2iclx_write_once_t wo = IIS2ICLX_ONLY_FIRST_CYCLE;
|
||||||
|
|
||||||
if (iis2iclx_sh_write_mode_set(data->ctx, wo) < 0) {
|
if (iis2iclx_sh_write_mode_set(&data->ctx, wo) < 0) {
|
||||||
LOG_ERR("shub: error setting write once");
|
LOG_ERR("shub: error setting write once");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -692,7 +692,7 @@ int iis2iclx_shub_fetch_external_devs(const struct device *dev)
|
||||||
for (n = 0; n < num_ext_dev; n++) {
|
for (n = 0; n < num_ext_dev; n++) {
|
||||||
sp = &iis2iclx_shub_slist[shub_ext[n]];
|
sp = &iis2iclx_shub_slist[shub_ext[n]];
|
||||||
|
|
||||||
if (iis2iclx_read_reg(data->ctx, sp->sh_out_reg,
|
if (iis2iclx_read_reg(&data->ctx, sp->sh_out_reg,
|
||||||
data->ext_data[n], sp->out_data_len) < 0) {
|
data->ext_data[n], sp->out_data_len) < 0) {
|
||||||
LOG_ERR("shub: failed to read sample");
|
LOG_ERR("shub: failed to read sample");
|
||||||
iis2iclx_shub_embedded_en(data, false);
|
iis2iclx_shub_embedded_en(data, false);
|
||||||
|
|
|
@ -22,11 +22,11 @@ int iis2iclx_spi_init(const struct device *dev)
|
||||||
struct iis2iclx_data *data = dev->data;
|
struct iis2iclx_data *data = dev->data;
|
||||||
const struct iis2iclx_config *cfg = dev->config;
|
const struct iis2iclx_config *cfg = dev->config;
|
||||||
|
|
||||||
data->ctx_spi.read_reg = (stmdev_read_ptr) stmemsc_spi_read;
|
/* Use generic stmemsc routine for read/write SPI bus */
|
||||||
data->ctx_spi.write_reg = (stmdev_write_ptr) stmemsc_spi_write;
|
data->ctx.read_reg = (stmdev_read_ptr) stmemsc_spi_read;
|
||||||
|
data->ctx.write_reg = (stmdev_write_ptr) stmemsc_spi_write;
|
||||||
|
|
||||||
data->ctx = &data->ctx_spi;
|
data->ctx.handle = (void *)&cfg->stmemsc_cfg.spi;
|
||||||
data->ctx->handle = (void *)&cfg->stmemsc_cfg.spi;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,17 +33,17 @@ static int iis2iclx_enable_t_int(const struct device *dev, int enable)
|
||||||
int16_t buf;
|
int16_t buf;
|
||||||
|
|
||||||
/* dummy read: re-trigger interrupt */
|
/* dummy read: re-trigger interrupt */
|
||||||
iis2iclx_temperature_raw_get(iis2iclx->ctx, &buf);
|
iis2iclx_temperature_raw_get(&iis2iclx->ctx, &buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set interrupt (TEMP DRDY interrupt is only on INT2) */
|
/* set interrupt (TEMP DRDY interrupt is only on INT2) */
|
||||||
if (cfg->int_pin == 1)
|
if (cfg->int_pin == 1)
|
||||||
return -EIO;
|
return -EIO;
|
||||||
|
|
||||||
iis2iclx_read_reg(iis2iclx->ctx, IIS2ICLX_INT2_CTRL,
|
iis2iclx_read_reg(&iis2iclx->ctx, IIS2ICLX_INT2_CTRL,
|
||||||
(uint8_t *)&int2_route.int2_ctrl, 1);
|
(uint8_t *)&int2_route.int2_ctrl, 1);
|
||||||
int2_route.int2_ctrl.int2_drdy_temp = enable;
|
int2_route.int2_ctrl.int2_drdy_temp = enable;
|
||||||
return iis2iclx_write_reg(iis2iclx->ctx, IIS2ICLX_INT2_CTRL,
|
return iis2iclx_write_reg(&iis2iclx->ctx, IIS2ICLX_INT2_CTRL,
|
||||||
(uint8_t *)&int2_route.int2_ctrl, 1);
|
(uint8_t *)&int2_route.int2_ctrl, 1);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -60,26 +60,26 @@ static int iis2iclx_enable_xl_int(const struct device *dev, int enable)
|
||||||
int16_t buf[3];
|
int16_t buf[3];
|
||||||
|
|
||||||
/* dummy read: re-trigger interrupt */
|
/* dummy read: re-trigger interrupt */
|
||||||
iis2iclx_acceleration_raw_get(iis2iclx->ctx, buf);
|
iis2iclx_acceleration_raw_get(&iis2iclx->ctx, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set interrupt */
|
/* set interrupt */
|
||||||
if (cfg->int_pin == 1) {
|
if (cfg->int_pin == 1) {
|
||||||
iis2iclx_pin_int1_route_t int1_route;
|
iis2iclx_pin_int1_route_t int1_route;
|
||||||
|
|
||||||
iis2iclx_read_reg(iis2iclx->ctx, IIS2ICLX_INT1_CTRL,
|
iis2iclx_read_reg(&iis2iclx->ctx, IIS2ICLX_INT1_CTRL,
|
||||||
(uint8_t *)&int1_route.int1_ctrl, 1);
|
(uint8_t *)&int1_route.int1_ctrl, 1);
|
||||||
|
|
||||||
int1_route.int1_ctrl.int1_drdy_xl = enable;
|
int1_route.int1_ctrl.int1_drdy_xl = enable;
|
||||||
return iis2iclx_write_reg(iis2iclx->ctx, IIS2ICLX_INT1_CTRL,
|
return iis2iclx_write_reg(&iis2iclx->ctx, IIS2ICLX_INT1_CTRL,
|
||||||
(uint8_t *)&int1_route.int1_ctrl, 1);
|
(uint8_t *)&int1_route.int1_ctrl, 1);
|
||||||
} else {
|
} else {
|
||||||
iis2iclx_pin_int2_route_t int2_route;
|
iis2iclx_pin_int2_route_t int2_route;
|
||||||
|
|
||||||
iis2iclx_read_reg(iis2iclx->ctx, IIS2ICLX_INT2_CTRL,
|
iis2iclx_read_reg(&iis2iclx->ctx, IIS2ICLX_INT2_CTRL,
|
||||||
(uint8_t *)&int2_route.int2_ctrl, 1);
|
(uint8_t *)&int2_route.int2_ctrl, 1);
|
||||||
int2_route.int2_ctrl.int2_drdy_xl = enable;
|
int2_route.int2_ctrl.int2_drdy_xl = enable;
|
||||||
return iis2iclx_write_reg(iis2iclx->ctx, IIS2ICLX_INT2_CTRL,
|
return iis2iclx_write_reg(&iis2iclx->ctx, IIS2ICLX_INT2_CTRL,
|
||||||
(uint8_t *)&int2_route.int2_ctrl, 1);
|
(uint8_t *)&int2_route.int2_ctrl, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,7 +135,7 @@ static void iis2iclx_handle_interrupt(const struct device *dev)
|
||||||
iis2iclx_status_reg_t status;
|
iis2iclx_status_reg_t status;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (iis2iclx_status_reg_get(iis2iclx->ctx, &status) < 0) {
|
if (iis2iclx_status_reg_get(&iis2iclx->ctx, &status) < 0) {
|
||||||
LOG_DBG("failed reading status reg");
|
LOG_DBG("failed reading status reg");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -245,7 +245,7 @@ int iis2iclx_init_interrupt(const struct device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* enable interrupt on int1/int2 in pulse mode */
|
/* enable interrupt on int1/int2 in pulse mode */
|
||||||
if (iis2iclx_int_notification_set(iis2iclx->ctx,
|
if (iis2iclx_int_notification_set(&iis2iclx->ctx,
|
||||||
IIS2ICLX_ALL_INT_PULSED) < 0) {
|
IIS2ICLX_ALL_INT_PULSED) < 0) {
|
||||||
LOG_ERR("Could not set pulse mode");
|
LOG_ERR("Could not set pulse mode");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue