Hi. I am trying to read the device type of BQ27426 fuel gauge EVM.
static int16_t BQ27426_i2cReadBytes(uint8_t reg, uint8_t *data, uint8_t len)
{
ret_code_t err_code;
m_xfer_done = false;
err_code = nrf_drv_twi_tx(&twi0,BQ72426_I2C_ADDRESS, ®, 1, true);
APP_ERROR_CHECK(err_code);
while(m_xfer_done == false){}
if(NRF_SUCCESS != err_code)
{
return false;
}
m_xfer_done = false;
err_code = nrf_drv_twi_rx(&twi0,BQ72426_I2C_ADDRESS, data, len );
APP_ERROR_CHECK(err_code);
while(m_xfer_done == false){}
if(NRF_SUCCESS != err_code)
{
return false;
}
return true;
}
static int16_t BQ27426_i2cWriteBytes(uint8_t reg, uint8_t *data, uint8_t len)
{
ret_code_t err_code = nrf_drv_twi_tx(&twi0,BQ72426_I2C_ADDRESS ,data, len , false);
APP_ERROR_CHECK(err_code);
while (m_xfer_done == false);
if (err_code != NRF_SUCCESS)
{
NRF_LOG_INFO("Failed while calling twi tx, err_code = %d", err_code);
return err_code;
}
nrf_delay_ms( BQ27426_DELAY );
return NRF_SUCCESS;
}
uint16_t BQ27426_deviceType(void)
{
NRF_LOG_INFO("Reading device type...");
uint16_t DeviceType = BQ27426_readControlWord(BQ27426_CONTROL_DEVICE_TYPE);
NRF_LOG_INFO("Device type read :0x%x",DeviceType);
return DeviceType;
}
static uint16_t BQ27426_readControlWord(uint16_t function) {
uint8_t command[2] = {function & 0x00FF, function >> 8};
uint8_t data[2] = {0, 0};
NRF_LOG_INFO("Control word send: ");
NRF_LOG_HEXDUMP_INFO(command,sizeof(command));
BQ27426_i2cWriteBytes((uint8_t) 0, command,2);
if (BQ27426_i2cReadBytes((uint8_t) 0, data, 2)) {
NRF_LOG_INFO("Control word read: ");
NRF_LOG_HEXDUMP_INFO(data,sizeof(data));
return ((uint16_t) data[1] << 8) | data[0]; //Return 1
}
else{
NRF_LOG_INFO("Failed to read Control word ");
return false;
}
}
I was getting the correct device type of 0x426, but now I am getting the wrong values sometimes it is 0x8C or 0x9c. Can somebody help me understand what's the problem.
I have attached the code for reference.