This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How can i write a SPI read/write function to use with my BME680 sensor?

Other Parts Discussed in Thread: CC2652RB

Hi, I want to use a BME680 sensor and the drivers provided by Bosch, but i have to write two read and write function prototypes to communicate via SPI with the sensor. I started writing a wrapper for this driver, but i'm doing something wrong with my communication functions.

This is the function pointer prototype 

 * Generic communication function pointer
 * @param[in] dev_id: Place holder to store the id of the device structure
 *                    Can be used to store the index of the Chip select or
 *                    I2C address of the device.
 * @param[in] reg_addr:	Used to select the register the where data needs to
 *                      be read from or written to.
 * @param[in/out] reg_data: Data array to read/write
 * @param[in] len: Length of the data array
 */
typedef int8_t (*bme680_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len);

The function that i wrote and does not communicate with the sensor:

static struct bme680_dev gBME680Struct = {};
static SPI_Handle gSpi;
static SPI_Transaction gSpiTransaction;
static uint8_t gRegAddr = 0;

int8_t userTransfer(uint8_t dev_id, uint8_t reg_addr, uint8_t *data,
                    uint16_t len)
{
    if (dev_id != gBME680Struct.dev_id)
    {
        return __LINE__;
    }

    gRegAddr = reg_addr;

    gSpiTransaction.txBuf = (void*) &gRegAddr;
    gSpiTransaction.rxBuf = (void*)data;
    gSpiTransaction.count = len;

    bool success = SPI_transfer(gSpi, &gSpiTransaction);

    if (!success)
    {
        return __LINE__;
    }

    return 0;
}

It is unclear to me what ought to be different between a write and a read function, as there is only one SPI_Transfer function. How do i send one byte and receive more? Or how do i send multiple bytes and receive no response? Would appreciate some clarifications on this issue. Thanks.

  • Hello AV,

    Please provide more information - What processor are you using? 

    Thank you,

    ~Leonard  

  • Hello.

    I am using a CC2652RB launch-pad. I've configured the spi from the syscfg file and SPI init and open are successful. Spi mode is 4 wire SS active low 

  • Hello A V,

    Here is the SPI TI Drivers Runtime APIs, and also the spimaster example.  

    "SPI_transfer() always performs full-duplex SPI transactions. This means the SPI simultaneously receives data as it transmits data. The application is responsible for formatting the data to be transmitted as well as determining whether the data received is meaningful. Specifics about SPI frame formatting and data sizes are provided in device-specific data sheets and technical reference manuals."

    You will also need to make sure the BME680 supports the SPI_POL0_PHA0 SPI_Params.frameFormat or modify accordingly.  Make sure the LaunchPad disables the on-board MX25R8035F SPI Flash with Board_shutDownExtFlash and that BOARD_EXT_FLASH_SPI_CS is kept high to keep the device disabled.  You should also monitor the SPI lines with an oscilloscope or logic analyzer to further determine what actually happens during the SPI transaction.

    Regards,
    Ryan