Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    How to use 3-wire SPI on BMP3 sensor

    How to use 3-wire SPI on BMP3 sensor

    ArDes
    New Poster

    Hi everyone,

    For my master's thesis, I designed a PCB with Bosch BMP384 sensors. In my previous version, I utilized 4-wire SPI for communication. However, in the newest version, I decided to switch to 3-wire SPI. I am using an Arduino to communicate with the sensors, and I found a GitHub repository from Bosch that explains how to write code for sensor communication (https://github.com/boschsensortec/BMP3_SensorAPI). The GitHub repository only provides code examples for 4-wire SPI and does not cover 3-wire SPI.

    The sensors uses 4-wire SPI by default,  to use 3-wire SPI, bit 0 of register 0x1A needs to be changed from 0 to 1. I have tried multiple approaches, but I am unable to make it work. Does someone have an example of how to set up 3-wire SPI communication with a Bosch sensor?

    Thank you in advance.

    3 REPLIES 3

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Christoforos,

    In addition to setting IF-CONF to 3-wire SPI, you need to ensure that the schematic is connected according to the 3-wire SPI.

    BMP384 3 or 4 wire SPI diagram.png
    The remaining work requires the host driver to access BMP384 through a 3-wire SPI.

    Hi, 

    The schematic of my PCB is indeed the same as in the picture. However, my struggle lies in the code. I don't know how to program the code to set up the 3-wire communication.

    ArDes
    New Poster

    This is the code I made to write on register 0x1A and to read on register 0x00 to see what the chip_id is. The chip id should be 0x50.When I read the register, I'm getting either 255 or 0 as the output, indicating a mistake in my code. Does someone know what I did wrong? 

    #include <SPI.h>

    const int ssPin = 6; // Set the SS (Slave Select) pin
    #define BMP3_REG_IF_CONF UINT8_C(0x1A)
    #define BMP3_REG_CHIP_ID UINT8_C(0x00)
    uint32_t clockFrequency = 100000;


    void get_reg(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, uint8_t dummy_byte);
    void read(uint8_t regAddress, uint8_t* dataBuffer, uint32_t numBytes, int pin);
    void write(uint8_t regAddress, uint8_t* dataBuffer, uint32_t numBytes, int pin);
    void set_reg(uint8_t *reg_addr, const uint8_t *reg_data, uint32_t len);


    void setup() {
    Serial.println("SPI SETUP BEGIN");
    pinMode(ssPin, OUTPUT); // Set the SS pin as an output
    SPI.begin(); // Initialize the SPI library
    Serial.begin(9600); // Initialize serial communication for debugging
    Serial.println("SPI SETUP DONE");
    }

    void loop() {
    Serial.println("Start writing");
    int8_t rslt;
    uint8_t reg_addr1 = BMP3_REG_IF_CONF;
    /* bit i want to change*/
    uint8_t op_mode_reg_val = 1;

    /* Write the power mode in the register */
    set_reg(&reg_addr1, &op_mode_reg_val, 1);
    Serial.println("end writing");

    Serial.println("wait 5 sec");
    delay(5000);



    Serial.println("start reading");
    uint8_t reg_addr2;
    uint8_t reg = 0;
    uint8_t dummy_byte = 1;
    uint32_t len = 1;
    reg_addr2 = BMP3_REG_CHIP_ID;
    get_reg(reg_addr2, &reg, 1, dummy_byte);

    // Print the value of reg
    Serial.print("Register value: ");
    Serial.println(reg);
    Serial.println("end reading");

     

    delay(5000);
    }



    void get_reg(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, uint8_t dummy_byte){
    uint32_t idx;
    uint32_t temp_len = len + dummy_byte;
    uint8_t temp_buff[len + dummy_byte];
    reg_addr = reg_addr | 0x80;
    read(reg_addr, temp_buff, temp_len, ssPin);
    for (idx = 0; idx < len; idx++)
    {
    reg_data[idx] = temp_buff[idx + dummy_byte];
    }
    }

    void read(uint8_t regAddress, uint8_t* dataBuffer, uint32_t numBytes, int pin){
    SPI.beginTransaction(SPISettings(clockFrequency, MSBFIRST, SPI_MODE0));
    digitalWrite(pin, LOW);
    SPI.transfer(regAddress | 0x80);

    // Read all requested bytes
    for(uint32_t i = 0; i < numBytes; i++)
    {
    dataBuffer[i] = SPI.transfer(0);
    }

    // End transmission
    digitalWrite(pin, HIGH);
    SPI.endTransaction();
    }



    void set_reg(uint8_t *reg_addr, const uint8_t *reg_data, uint32_t len)
    {
    int8_t rslt;
    uint8_t temp_buff[len * 2];
    uint32_t temp_len;
    uint8_t reg_addr_cnt;

    temp_buff[0] = reg_data[0];


    for (reg_addr_cnt = 0; reg_addr_cnt < len; reg_addr_cnt++)
    {
    reg_addr[reg_addr_cnt] = reg_addr[reg_addr_cnt] & 0x7F;
    }
    temp_len = len;
     

    write(reg_addr[0], temp_buff, temp_len, ssPin);
    }

    void write(uint8_t regAddress, uint8_t* dataBuffer, uint32_t numBytes, int pin){
    SPI.beginTransaction(SPISettings(clockFrequency, MSBFIRST, SPI_MODE0));
    digitalWrite(pin, LOW);
     
    SPI.transfer(regAddress);
     
    /* Write all the data */
    for(uint32_t i = 0; i < numBytes; i++)
    {
    SPI.transfer(dataBuffer[i]);
    }
    // End transmission
    digitalWrite(pin, HIGH);
    SPI.endTransaction();
    }
    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist