Skip to content

Commit

Permalink
drivers: sensor: st: replace i2/3c_burst_write
Browse files Browse the repository at this point in the history
i2/3c_burst_write comes with a warning where this combined write
synthesized by thsi API may not be supported by all I2/3C
devices. Replace with i2c_write instead with a buffer combining
the address and data.

The Kconfig STMEMSC_I2C_WRITE_BUFFER_SIZE was added to set the
size of the buffer pushed to the stack.

Signed-off-by: Ryan McClelland <[email protected]>
  • Loading branch information
XenuIsWatching authored and kartben committed Jan 29, 2025
1 parent 8f83ffc commit 8f0ffbb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions drivers/sensor/st/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ source "drivers/sensor/st/stm32_digi_temp/Kconfig"
source "drivers/sensor/st/stm32_temp/Kconfig"
source "drivers/sensor/st/stm32_vbat/Kconfig"
source "drivers/sensor/st/stm32_vref/Kconfig"
source "drivers/sensor/st/stmemsc/Kconfig"
source "drivers/sensor/st/stts22h/Kconfig"
source "drivers/sensor/st/stts751/Kconfig"
source "drivers/sensor/st/vl53l0x/Kconfig"
Expand Down
13 changes: 13 additions & 0 deletions drivers/sensor/st/stmemsc/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ST MEMS sensor configuration options

# Copyright (c) 2025 Meta Platforms
# SPDX-License-Identifier: Apache-2.0

config STMEMSC_I3C_I2C_WRITE_BUFFER_SIZE
int "ST memsc I2C write buffer size"
default 16
depends on I2C || I3C
depends on HAS_STMEMSC
help
The size of the buffer pushed on the stack used to copy
the data to be written along with the register address.
19 changes: 16 additions & 3 deletions drivers/sensor/st/stmemsc/stmemsc_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ int stmemsc_i2c_read(const struct i2c_dt_spec *stmemsc,
int stmemsc_i2c_write(const struct i2c_dt_spec *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len)
{
return i2c_burst_write_dt(stmemsc, reg_addr, value, len);
uint8_t buf[CONFIG_STMEMSC_I3C_I2C_WRITE_BUFFER_SIZE];

__ASSERT_NO_MSG(len <= sizeof(buf) - 1);

buf[0] = reg_addr;
memcpy(&buf[1], value, len);

return i2c_write_dt(stmemsc, buf, len + 1);
}

int stmemsc_i2c_read_incr(const struct i2c_dt_spec *stmemsc,
Expand All @@ -34,6 +41,12 @@ int stmemsc_i2c_read_incr(const struct i2c_dt_spec *stmemsc,
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);
uint8_t buf[17];

__ASSERT_NO_MSG(len <= sizeof(buf) - 1);

buf[0] = reg_addr | STMEMSC_I2C_ADDR_AUTO_INCR;
memcpy(&buf[1], value, len);

return i2c_write_dt(stmemsc, buf, len + 1);
}
8 changes: 7 additions & 1 deletion drivers/sensor/st/stmemsc/stmemsc_i3c.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ int stmemsc_i3c_write(void *stmemsc,
uint8_t reg_addr, uint8_t *value, uint8_t len)
{
struct i3c_device_desc *target = **(struct i3c_device_desc ***)stmemsc;
uint8_t buf[CONFIG_STMEMSC_I3C_I2C_WRITE_BUFFER_SIZE];

return i3c_burst_write(target, reg_addr, value, len);
__ASSERT_NO_MSG(len <= sizeof(buf) - 1);

buf[0] = reg_addr;
memcpy(&buf[1], value, len);

return i3c_write(target, buf, len + 1);
}

0 comments on commit 8f0ffbb

Please sign in to comment.