01-05-2021 05:54 AM
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)
;
#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
01-05-2021 06:41 AM
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;
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);
01-05-2021 07:58 AM
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.
01-05-2021 09:57 AM - edited 01-05-2021 11:25 AM
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
01-07-2021 07:23 AM
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.