SPI DRIVER FOR BME280 SENSOR

Hello everyone!

I am trying to figure out how SPI drivers for Bosch bme280 sensor work... I would like to somehow recycle them to write my own SPI drivers for another device (an adc from Texas Instruments).

 I am currently looking at "bme280_spi.c" (you can find it in /zephyr/drivers/sensor/bme280). I really don't get what is the purpose of the "buf" pointer passed as an input argument to the function bme280_reg_read_spi. Moreover, rx_buf[1].buf = &buf[1]: what does that mean?

Can you please me help me articulating a little bit more about the purpose of the "buf" pointer? And do you know why rx_buf has size 2?

Here's the code fragment I am struggling with:

static int bme280_reg_read_spi(const union bme280_bus *busuint8_t start, uint8_t *buf, int size{
   uint8_t addr;
   const struct spi_buf tx_buf = {
      .buf = &addr,
      .len = 1
   };
   const struct spi_buf_set tx = {
      .buffers = &tx_buf,
      .count = 1
   };
   struct spi_buf rx_buf[2];
   const struct spi_buf_set rx = {
      .buffers = rx_buf,
      .count = ARRAY_SIZE(rx_buf)
   };
   int i;

   rx_buf[0].buf = NULL;
   rx_buf[0].len = 1;

   rx_buf[1].len = 1;

   for (i = 0; i < size; i++) {
      int ret;

      addr = (start + i) | 0x80;
      rx_buf[1].buf = &buf[i];

      ret = spi_transceive_dt(&bus->spi, &tx, &rx);
      if (ret) {
         LOG_DBG("spi_transceive FAIL %d\n", ret);
         return ret;
      }
   }

   return 0;
}

Thanks to everyone in advance!

Parents Reply Children
No Data
Related