[solved] how to bme280 spi read?
Posted: Tue Jan 03, 2023 1:17 am
Hello,
i am trying to use the bosh bme280 driver to interface the sensor trought spi.
this is how i init the spi line:
and this is the function required from driver to read from spi
there is for sure a problem in how i do the transaction, because this is what i read:
i don't know how to proceed, i don't understand spi transaction well, any help is appirciated!
i am trying to use the bosh bme280 driver to interface the sensor trought spi.
this is how i init the spi line:
Code: Select all
#define CLK_PIN GPIO_NUM_18
#define MISO_PIN GPIO_NUM_19
#define MOSI_PIN GPIO_NUM_23
#define CS_PIN GPIO_NUM_5
spi_device_handle_t spi2;
void spi_master_init()
{
esp_err_t ret;
spi_bus_config_t buscfg={
.miso_io_num = MISO_PIN,
.mosi_io_num = MOSI_PIN,
.sclk_io_num = CLK_PIN,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 32,
};
ret = spi_bus_initialize(VSPI_HOST, &buscfg, SPI_DMA_CH_AUTO);
ESP_ERROR_CHECK(ret);
spi_device_interface_config_t devcfg={
.clock_speed_hz = 1000000, // 1 MHz
.mode = 0, //SPI mode 0
.spics_io_num = CS_PIN,
.queue_size = 1,
.pre_cb = NULL,
.post_cb = NULL
};
ESP_ERROR_CHECK(spi_bus_add_device(VSPI_HOST, &devcfg, &spi2));
}
Code: Select all
int8_t user_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
/*
* The parameter intf_ptr can be used as a variable to select which Chip Select pin has
* to be set low to activate the relevant device on the SPI bus
*/
/*
* Data on the bus should be like
* |----------------+---------------------+-------------|
* | MOSI | MISO | Chip Select |
* |----------------+---------------------|-------------|
* | (don't care) | (don't care) | HIGH |
* | (reg_addr) | (don't care) | LOW |
* | (don't care) | (reg_data[0]) | LOW |
* | (....) | (....) | LOW |
* | (don't care) | (reg_data[len - 1]) | LOW |
* | (don't care) | (don't care) | HIGH |
* |----------------+---------------------|-------------|
*/
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
t.length = 8;
t.rxlength = 8*len;
t.tx_buffer = NULL;
t.rx_buffer = reg_data;
t.addr = reg_addr;
rslt = spi_device_polling_transmit(spi2, &t);
ESP_LOGI(TAG, "spi read rslt %s addr is %i data is %s len is %lu", esp_err_to_name(rslt), reg_addr,reg_data, len);
if (rslt == ESP_OK)
return 0;
return rslt;
}
Code: Select all
I (1829) example: spi read rslt ESP_OK addr is 208 data is � len is 1
I (2829) example: spi read rslt ESP_OK addr is 208 data is � len is 1
I (3829) example: spi read rslt ESP_OK addr is 208 data is � len is 1