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 <jerzy.kasenberg@codecoup.pl>
  • Loading branch information
kasjer committed Sep 9, 2024
1 parent 23b6eeb commit 32d7b75
Show file tree
Hide file tree
Showing 2 changed files with 21 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
27 changes: 20 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 @@ -894,23 +902,28 @@ bme280_writelen(struct sensor_itf *itf, uint8_t addr, uint8_t *payload,
int rc;

#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
uint8_t buf[4];

if (len > 3) {
return SYS_EINVAL;
}

struct os_dev *dev = itf->si_dev;

rc = bus_node_lock(dev, OS_TIMEOUT_NEVER);
if (rc) {
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 32d7b75

Please sign in to comment.