BMP280_SPI

abd alrahman
Posts: 1
Joined: Mon Jul 22, 2024 3:42 pm

BMP280_SPI

Postby abd alrahman » Mon Jul 22, 2024 4:02 pm

Hi,
can anyone check if this code is functional.
This code should send the ID register address (0xD0) of BMP280 and return the value (0x58) by using SPI.
knowing that the used board is ESP32.

another question: what is queue_size ??

Code: Select all

 
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "driver/gpio.h"

#define GPIO_MOSI 23
#define GPIO_MISO 19
#define GPIO_SCLK 18
#define GPIO_CS 5

void app_main(void)
{
    spi_device_handle_t handle;

    spi_bus_config_t buscfg = {
        .mosi_io_num = GPIO_MOSI,
        .miso_io_num = GPIO_MISO,
        .sclk_io_num = GPIO_SCLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1
        };

    spi_device_interface_config_t devcfg = {
        .command_bits = 0,
        .address_bits = 0,
        .dummy_bits = 0,
        .clock_speed_hz = 400000,
        .duty_cycle_pos = 128,
        .mode = 0,
        .spics_io_num = GPIO_CS,
        .queue_size = 7,
        };

    spi_bus_initialize(VSPI_HOST, &buscfg, SPI_DMA_CH_AUTO);
    spi_bus_add_device(VSPI_HOST, &devcfg, &handle);

    uint8_t recvbuf[1];
    uint8_t sendbuf[1] = {0xD0};
    spi_transaction_t t;
    memset(&t, 0, sizeof(t));

    t.length = 16;
    t.rxlength = 8;
    t.tx_buffer = sendbuf;
    t.rx_buffer = recvbuf;
    spi_device_transmit(handle, &t);
    printf("ID: %x\n", recvbuf[0]);
} 
Thank you for your help.

Who is online

Users browsing this forum: Baidu [Spider] and 91 guests