Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BMA456 Fails after initialization

    BMA456 Fails after initialization

    nafihahmd
    Member

    Hi, First of all let me admit that I am a newbie in embedded firmware. I am trying to write a code for BMA456 using ESP32 and Arduino platform(PlatformIO). I am using latest version of BMA456 Sensor API from the official repo. I am able to read the chip ID (0x16) correctly. But when I try to execute the further steps it's failing. The BMA456 seems to get hang at 

     

     

     while (Wire.available() < len) 
        ;

     

     
    Also I tried to read the status register at 0x2A  and got a value 0b11111 which is not even defined in the datasheet.
    I will attach my code below.
     
    P.S: I also tried arduino portred library from Seed studio. The link is here. I tried their step counter example and the steps never incremented. 
     
     Main.cpp

     

     

    #include <Arduino.h>
    #include "bma456.h"
    #include "bma4_common.h"
    
    
    //  Function Declaration
    uint16_t init_BMA();
    uint16_t config_acc();
    
    // Global Variables
    struct bma4_dev bma_global; // Declare an instance of the BMA456 device
    uint16_t rslt = BMA4_OK;
    uint8_t init_seq_status;
    uint16_t int_status;         // Variable to store accel data ready interrupt status
    struct bma4_accel_config accel_conf = {0};
    
    void setup()
    {
      Serial.begin(115200);
      Serial.println("\n BMA456");
      
      init_BMA();
      //config_acc();
    
      delay(150);
    }
    
    void loop()
    {
      // read_acc();
    }
    
    uint16_t init_BMA()
    {
      Wire.begin();
    
      // Modify Params
      // bma.dev_addr = BMA4_I2C_ADDR_PRIMARY;
      struct bma4_dev bma; // Declare an instance of the BMA456 device
      bma.intf = BMA4_I2C_INTF;
      bma.bus_read = usr_i2c_r;
      bma.bus_write = usr_i2c_w;
      bma.delay_us = usr_delay;
      bma.read_write_len = 8;
      bma.resolution = 12;
      bma.feature_len = BMA456_FEATURE_SIZE;
    
      /* a. Reading the chip id. */
      uint16_t rslt = BMA4_OK;
      rslt |= bma456_init(&bma);
      Serial.println("tst_pnt1");
      Serial.println(rslt, HEX);
      bma4_error_codes_print_result("bma456_init status", rslt);
      bma4_set_command_register(0xB6, &bma); // reset device  
      Serial.println("tst_pnt2");
      delay(500);
    
      /* b. Performing initialization sequence. 
           c. Checking the correct status of the initialization sequence.
        */
    
      rslt |= bma456_write_config_file(&bma);
      Serial.println(rslt);
      bma4_error_codes_print_result("bma456_write_config status", rslt);
      Serial.println("tst_pnt3");
    
      bma_global = bma;
      return rslt;
    }
    
    uint16_t config_acc()
    {
      struct bma4_dev bma; // Declare an instance of the BMA456 device
      bma = bma_global;
      bma4_set_accel_enable(BMA4_ENABLE, &bma);
      bma4_error_codes_print_result("bma4_set_accel_enable status", rslt);
    
      /* Assign the desired settings */
      accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
      accel_conf.range = BMA4_ACCEL_RANGE_2G;
      accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
    
      /* Set the configuration */
      rslt |= bma4_set_accel_config(&accel_conf, &bma);
      bma_global = bma;
      return rslt;
    }

     

     

     

    i2c_read functipon

     

     

    int8_t usr_i2c_r(uint8_t reg_addr, uint8_t *read_data, uint32_t len, void *intf_ptr)
    {
      Wire.beginTransmission(dev_addr);
      Wire.write(reg_addr);
      Wire.endTransmission(false);
    
      Wire.requestFrom(dev_addr, len);
      Serial.println("i2cR/tst_pnt1");
      while (Wire.available() < len) 
        ;
      Serial.println("i2cR/tst_pnt2");
      for (int i = 0; i < len; i++)
      {
        read_data[i++] = Wire.read();
      }
    
      Wire.endTransmission();
      Serial.println("i2cR/tst_pnt3");
    
      return 0;
    }

     

     

     

    P.S 2: I am using the Serial.print() for debugging

    8 REPLIES 8

    BSTRobin
    Community Moderator
    Community Moderator

    Hello nafihahmd,

    1.The address should be shifted like this in I2C read or write function.
    uint8_t dev_addr = *(uint8_t*)intf_ptr;
    uint8_t DevAddress = dev_addr << 1;

    BMA456 I2C read.png

    2. Could you find any error return init_BMA()?

    3.  You should enable step counter before you read step value.

    rslt = bma456_feature_enable(BMA456_STEP_CNTR, BMA4_ENABLE, &bma);
    bma4_error_codes_print_result("bma456_feature_enable status", rslt);

    https://github.com/BoschSensortec/BMA456-Sensor-API/blob/master/examples/bma456/generic/step_counter...

     

    BSTRobin, Thank you for the fast response.

    1. I am able to read the chip ID correctly using the aforementioned i2c read function. Doesn't that mean the communication is carried out properly ? I have successfully operated other i2c sensors with this same programming logic, that's why I am asking.

    2. The code is not executing to the point where it will return a value (It is getting stuck in between). So I am unable to get the return value init_BMA().

     

    3. Regarding step counter I was using Seeed-Studio/Seeed_BMA456 library example. So everything is done automatically by the library including enabling the step counter. But stil there was no output

     

    Also I carried out some more investigations into the issue. The Seeed_BMA456 working fine with AtMega328 uC (Arduino Uno). I looked into the Status registrer at 0x2A and the value is '1' meaning successfully initialized. So why is it not working on an ESP32 and working fine on an AVR ATMega328.

     

    Thanks again for the reply.

    Now I am getting the following error:

     

     

     

    bma456_init status      Error [65535] : Null pointer
    
    bma456_write_config status      Error [65535] : Null pointer

     

     

     

    What does this mean? 

     

    Edit:

    I solved the above error by locally defining  instance of the BMA456 device (struct bma4_dev bma;). But now the Init_BMA() is returning 0xFFFC

    BSTRobin
    Community Moderator
    Community Moderator

    Hello nafihahmd,

    In init_BMA(), your code called bma456_write_config_file() function. You could add printf log to see which code fail in bma456_write_config_file() function.

    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