Skip to content

Commit

Permalink
hw/drivers/bme280: Fix writes to I2C
Browse files Browse the repository at this point in the history
I2C writes when driver was enabled clear MSB of register
address.

This prevented any write to control registers.

Now registers MSB is cleared on write only for SPI.

Writing registers also requires single start condition.
Code was doing write with restart condtion that does not
seem to work correctly

Signed-off-by: Jerzy Kasenberg <[email protected]>
  • Loading branch information
kasjer committed Sep 9, 2024
1 parent 23b6eeb commit f1b4d88
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions hw/drivers/sensors/bme280/include/bme280/bme280.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct bme280 {
struct bus_i2c_node i2c_node;
struct bus_spi_node spi_node;
};
bool node_is_spi;
#else
struct os_dev dev;
#endif
Expand Down
22 changes: 15 additions & 7 deletions hw/drivers/sensors/bme280/src/bme280.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ static const struct sensor_driver g_bme280_sensor_driver = {
bme280_sensor_get_config
};

#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
static bool
bme280_uses_spi(struct sensor_itf *itf)
{
return ((struct bme280 *)(itf->si_dev))->node_is_spi;
}
#endif

static int
bme280_default_cfg(struct bme280_cfg *cfg)
{
Expand Down Expand Up @@ -892,6 +900,7 @@ bme280_writelen(struct sensor_itf *itf, uint8_t addr, uint8_t *payload,
uint8_t len)
{
int rc;
uint8_t buf[len + 1];

#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
struct os_dev *dev = itf->si_dev;
Expand All @@ -901,16 +910,15 @@ bme280_writelen(struct sensor_itf *itf, uint8_t addr, uint8_t *payload,
return SYS_EINVAL;
}

addr &= ~BME280_SPI_READ_CMD_BIT;

rc = bus_node_write(dev, &addr, 1, OS_TIMEOUT_NEVER, BUS_F_NOSTOP);
if (rc) {
goto done;
if (bme280_uses_spi(itf)) {
addr &= ~BME280_SPI_READ_CMD_BIT;
}

rc = bus_node_simple_write(dev, payload, len);
buf[0] = addr;
memcpy(buf + 1, payload, len);

rc = bus_node_simple_write(dev, buf, len + 1);

done:
(void)bus_node_unlock(dev);
#else
int i;
Expand Down

0 comments on commit f1b4d88

Please sign in to comment.