drivers/sensor: stmemsc: add new sets of i2c/spi APIs

Add APIs to:

    1. read/write sensor regs on i2c/spi bus enabling adrress
       auto-increment in a stmemsc specific way.
    2. read/write sensor custom APIs.

Signed-off-by: Armando Visconti <armando.visconti@st.com>
This commit is contained in:
Armando Visconti 2024-01-16 12:07:54 +01:00 committed by Henrik Brix Andersen
commit dadac5d0ce
5 changed files with 126 additions and 18 deletions

View file

@ -9,3 +9,4 @@ zephyr_library()
zephyr_library_sources_ifdef(CONFIG_I2C stmemsc_i2c.c)
zephyr_library_sources_ifdef(CONFIG_I3C stmemsc_i3c.c)
zephyr_library_sources_ifdef(CONFIG_SPI stmemsc_spi.c)
zephyr_library_sources(stmemsc_mdelay.c)

View file

@ -15,22 +15,18 @@
#include <zephyr/drivers/i3c.h>
#include <zephyr/drivers/spi.h>
static inline void stmemsc_mdelay(uint32_t millisec)
{
k_msleep(millisec);
}
void stmemsc_mdelay(uint32_t millisec);
#ifdef CONFIG_I2C
/*
* Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
* stmemsc i2c APIs.
* standard stmemsc i2c APIs.
*/
#define STMEMSC_CTX_I2C(stmdev_ctx_ptr) \
.ctx = { \
.read_reg = (stmdev_read_ptr) stmemsc_i2c_read, \
.write_reg = (stmdev_write_ptr) stmemsc_i2c_write, \
.mdelay = (stmdev_mdelay_ptr) stmemsc_mdelay, \
.read_reg = (stmdev_read_ptr)stmemsc_i2c_read, \
.write_reg = (stmdev_write_ptr)stmemsc_i2c_write, \
.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay, \
.handle = (void *)stmdev_ctx_ptr \
}
@ -38,18 +34,49 @@ int stmemsc_i2c_read(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len);
int stmemsc_i2c_write(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len);
/*
* Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
* specific stmemsc i2c APIs that set reg_addr MSB to '1' in order to allow
* multiple read/write operations. This is common in some STMEMSC drivers
*/
#define STMEMSC_CTX_I2C_INCR(stmdev_ctx_ptr) \
.ctx = { \
.read_reg = (stmdev_read_ptr)stmemsc_i2c_read_incr, \
.write_reg = (stmdev_write_ptr)stmemsc_i2c_write_incr, \
.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay, \
.handle = (void *)stmdev_ctx_ptr \
}
int stmemsc_i2c_read_incr(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len);
int stmemsc_i2c_write_incr(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len);
/*
* Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
* custom stmemsc i2c APIs specified by driver.
*/
#define STMEMSC_CTX_I2C_CUSTOM(stmdev_ctx_ptr, i2c_rd_api, i2c_wr_api) \
.ctx = { \
.read_reg = (stmdev_read_ptr)i2c_rd_api, \
.write_reg = (stmdev_write_ptr)i2c_wr_api, \
.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay, \
.handle = (void *)stmdev_ctx_ptr \
}
#endif
#ifdef CONFIG_I3C
/*
* Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
* stmemsc i3c APIs.
* standard stmemsc i3c APIs.
*/
#define STMEMSC_CTX_I3C(stmdev_ctx_ptr) \
.ctx = { \
.read_reg = (stmdev_read_ptr) stmemsc_i3c_read, \
.write_reg = (stmdev_write_ptr) stmemsc_i3c_write, \
.mdelay = (stmdev_mdelay_ptr) stmemsc_mdelay, \
.read_reg = (stmdev_read_ptr)stmemsc_i3c_read, \
.write_reg = (stmdev_write_ptr)stmemsc_i3c_write, \
.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay, \
.handle = (void *)stmdev_ctx_ptr \
}
@ -66,9 +93,9 @@ int stmemsc_i3c_write(void *stmemsc,
*/
#define STMEMSC_CTX_SPI(stmdev_ctx_ptr) \
.ctx = { \
.read_reg = (stmdev_read_ptr) stmemsc_spi_read, \
.write_reg = (stmdev_write_ptr) stmemsc_spi_write, \
.mdelay = (stmdev_mdelay_ptr) stmemsc_mdelay, \
.read_reg = (stmdev_read_ptr)stmemsc_spi_read, \
.write_reg = (stmdev_write_ptr)stmemsc_spi_write, \
.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay, \
.handle = (void *)stmdev_ctx_ptr \
}
@ -76,5 +103,36 @@ int stmemsc_spi_read(const struct spi_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len);
int stmemsc_spi_write(const struct spi_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len);
/*
* Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
* specific stmemsc sp APIs that set reg_addr bit6 to '1' in order to allow
* multiple read/write operations. This is common in some STMEMSC drivers
*/
#define STMEMSC_CTX_SPI_INCR(stmdev_ctx_ptr) \
.ctx = { \
.read_reg = (stmdev_read_ptr)stmemsc_spi_read_incr, \
.write_reg = (stmdev_write_ptr)stmemsc_spi_write_incr, \
.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay, \
.handle = (void *)stmdev_ctx_ptr \
}
int stmemsc_spi_read_incr(const struct spi_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len);
int stmemsc_spi_write_incr(const struct spi_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len);
/*
* Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
* custom stmemsc spi APIs specified by driver.
*/
#define STMEMSC_CTX_SPI_CUSTOM(stmdev_ctx_ptr, spi_rd_api, spi_wr_api) \
.ctx = { \
.read_reg = (stmdev_read_ptr)spi_rd_api, \
.write_reg = (stmdev_write_ptr)spi_wr_api, \
.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay, \
.handle = (void *)stmdev_ctx_ptr \
}
#endif
#endif /* ZEPHYR_DRIVERS_SENSOR_STMEMSC_STMEMSC_H_ */

View file

@ -9,14 +9,31 @@
#include "stmemsc.h"
/* Enable address auto-increment on some stmemsc sensors */
#define STMEMSC_I2C_ADDR_AUTO_INCR BIT(7)
int stmemsc_i2c_read(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len)
uint8_t reg_addr, uint8_t *value, uint8_t len)
{
return i2c_burst_read_dt(stmemsc, reg_addr, value, len);
}
int stmemsc_i2c_write(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len)
uint8_t reg_addr, uint8_t *value, uint8_t len)
{
return i2c_burst_write_dt(stmemsc, reg_addr, value, len);
}
int stmemsc_i2c_read_incr(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len)
{
reg_addr |= STMEMSC_I2C_ADDR_AUTO_INCR;
return stmemsc_i2c_read(stmemsc, reg_addr, value, len);
}
int stmemsc_i2c_write_incr(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len)
{
reg_addr |= STMEMSC_I2C_ADDR_AUTO_INCR;
return stmemsc_i2c_write(stmemsc, reg_addr, value, len);
}

View file

@ -0,0 +1,15 @@
/* ST Microelectronics STMEMS hal i/f
*
* Copyright (c) 2024 STMicroelectronics
*
* SPDX-License-Identifier: Apache-2.0
*
* zephyrproject-rtos/modules/hal/st/sensor/stmemsc/
*/
#include "stmemsc.h"
void stmemsc_mdelay(uint32_t millisec)
{
k_msleep(millisec);
}

View file

@ -11,6 +11,9 @@
#define SPI_READ (1 << 7)
/* Enable address auto-increment on some stmemsc sensors */
#define STMEMSC_SPI_ADDR_AUTO_INCR BIT(6)
/*
* SPI read
*/
@ -56,3 +59,17 @@ int stmemsc_spi_write(const struct spi_dt_spec *stmemsc,
return spi_write_dt(stmemsc, &tx);
}
int stmemsc_spi_read_incr(const struct spi_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len)
{
reg_addr |= STMEMSC_SPI_ADDR_AUTO_INCR;
return stmemsc_spi_read(stmemsc, reg_addr, value, len);
}
int stmemsc_spi_write_incr(const struct spi_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len)
{
reg_addr |= STMEMSC_SPI_ADDR_AUTO_INCR;
return stmemsc_spi_write(stmemsc, reg_addr, value, len);
}