Skip to content

Commit 7b0d298

Browse files
alexskpMaureenHelm
authored andcommitted
drivers: sensor: bmi270: fix I2C write to use single transaction
Combine register address and data in one buffer and use i2c_write_dt() instead of i2c_burst_write_dt() so the transfer is a single I2C transaction. Some I2C controllers (e.g. Infineon) split burst write into two transactions with a STOP in between, which causes failure. Signed-off-by: Oleksandr Skopets <skopets.sasha@gmail.com>
1 parent 705703a commit 7b0d298

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

drivers/sensor/bosch/bmi270/bmi270.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919

2020
LOG_MODULE_REGISTER(bmi270, CONFIG_SENSOR_LOG_LEVEL);
2121

22-
#define BMI270_WR_LEN 256
23-
#define BMI270_CONFIG_FILE_RETRIES 15
24-
#define BMI270_CONFIG_FILE_POLL_PERIOD_US 10000
25-
#define BMI270_INTER_WRITE_DELAY_US 1000
26-
2722
static inline int bmi270_bus_check(const struct device *dev)
2823
{
2924
const struct bmi270_config *cfg = dev->config;

drivers/sensor/bosch/bmi270/bmi270.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include <zephyr/devicetree.h>
2020
#include <zephyr/drivers/gpio.h>
2121

22+
#define BMI270_WR_LEN 32
23+
#define BMI270_CONFIG_FILE_RETRIES 15
24+
#define BMI270_CONFIG_FILE_POLL_PERIOD_US 10000
25+
#define BMI270_INTER_WRITE_DELAY_US 1000
26+
2227
#define BMI270_REG_CHIP_ID 0x00
2328
#define BMI270_REG_ERROR 0x02
2429
#define BMI270_REG_STATUS 0x03

drivers/sensor/bosch/bmi270/bmi270_i2c.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Bus-specific functionality for BMI270s accessed via I2C.
99
*/
1010

11+
#include <string.h>
1112
#include "bmi270.h"
1213

1314
static int bmi270_bus_check_i2c(const union bmi270_bus *bus)
@@ -24,7 +25,23 @@ static int bmi270_reg_read_i2c(const union bmi270_bus *bus,
2425
static int bmi270_reg_write_i2c(const union bmi270_bus *bus, uint8_t start,
2526
const uint8_t *data, uint16_t len)
2627
{
27-
return i2c_burst_write_dt(&bus->i2c, start, data, len);
28+
/* Combine register address and data into a single buffer and
29+
* use i2c_write_dt() instead of i2c_burst_write_dt() which
30+
* may not be supported on all I2C devices.
31+
* Maximum write length is BMI270_WR_LEN + 1 byte for address.
32+
*/
33+
uint8_t buf[1 + BMI270_WR_LEN];
34+
35+
if (len > BMI270_WR_LEN) {
36+
return -EINVAL;
37+
}
38+
39+
buf[0] = start;
40+
if (len > 0) {
41+
memcpy(&buf[1], data, len);
42+
}
43+
44+
return i2c_write_dt(&bus->i2c, buf, 1 + len);
2845
}
2946

3047
static int bmi270_bus_init_i2c(const union bmi270_bus *bus)

0 commit comments

Comments
 (0)