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.

BQ27210 SOC Problem.

Other Parts Discussed in Thread: BQ27210, CC2650, BQ27010, BQEVSW

Hi,

Am using BQ27210 battery sensor with DTP 602525 Li-Polymer cell (340mah) for battery charge measurement. Am trying to get battery charge percentage from RSOC register but it is giving abnormal values. while the battery charging it seems like working fine until it reaches 17 or 18% suddenly the percentage jumps to 100% (takes around 90 min to charge 17%), and when it is discharging from 100% it seems like working fine until 86% then suddenly the percentage falls down to 0%.

Any help would be appreciated.

Regards,

Yashavanth K S

  • Hello Yashavanth,

    Can you provide your memory map configuration for the bq27210? Also, how did you pick your EDV1 threshold?

    Thank you.
  • Hi Fernando,

     As per the source code I got from TI in msp430 for bq27210 and datasheet provided by TI,  Register memory mapping as below.

    /* BQ27210 register addresses */
    #define BQ27210_REG_ADDR_CNTRL                             0x00 /* Control */
    #define BQ27210_REG_ADDR_MODE                              0x01 /* Mode */
    #define BQ27210_REG_ADDR_AR_LOW                         0x02 /* At-Rate low byte */
    #define BQ27210_REG_ADDR_AR_HIGH                        0x03 /* At-Rate High byte */
    #define BQ27210_REG_ADDR_TEMP                               0x06 /* Temperature */
    #define BQ27210_REG_ADDR_VOLT_LOW                    0x08 /* Voltage Low byte */
    #define BQ27210_REG_ADDR_VOLT_HIGH                   0x09 /* Voltage High byte*/
    #define BQ27210_REG_ADDR_AVG_CURR_LOW        0x14 /* Average Current Low byte */
    #define BQ27210_REG_ADDR_AVG_CURR_HIGH       0x15 /* Average Current High byte */
    #define BQ27210_REG_ADDR_FLAGS                             0X0a /* Flags */
    #define BQ27210_REG_ADDR_TTE                                   0X16 /* Time-to-Empty */
    #define BQ27210_REG_ADDR_TTF                                   0X18 /* Time-to-Full */
    #define BQ27210_REG_ADDR_TTES                                0X1c /* Time-to-Empty Standby */
    #define BQ27210_REG_ADDR_TTECP                             0X26 /* Time-to-Empty at Constant Power */
    #define BQ27210_REG_ADDR_NAC_LOW                      0X0C /* Nominal Available Capacity Low byte */
    #define BQ27210_REG_ADDR_NAC_HIGH                     0X0D /* Nominal Available Capacity High byte */
    #define BQ27210_REG_ADDR_LMD_LOW                      0X0E /* Full Charge Capacity Low byte */
    #define BQ27210_REG_ADDR_LMD_HIGH                     0X0F /* Full Charge Capacity High byte */
    #define BQ27210_REG_ADDR_CYCT                               0X2A /* Cycle Count */
    #define BQ27210_REG_ADDR_SOC                                  0X0b /* State-of-Charge */
    #define BQ27210_REG_ADDR_DCAP                               0X76 /* Design Capacity */
    #define BQ27210_REG_ADDR_CAC_LOW                       0x10 /* Compensated Available Capacity Low */
    #define BQ27210_REG_ADDR_CAC_HIGH                      0x11 /* Compensated Available Capacity High */
    #define BQ27210_REG_ADDR_FCAC                                0x12 /* Full Compensated Available Capacity High */
    #define BQ27210_REG_ADDR_CSOC                               0x2C /* Compensated State-of-Charge */

    and EDV1 threshold is 0xA9. Am not doing any initialization before reading RSOC register, If needed please share the information.


    PS: using CC2650 platform

  • Hi Fernando,

    I have collected some details with my device, its giving ILMD value as 0x2C(0x2C for 2000mah cell) but am using 340mah cell. So, ILMD value should be 0x07, i tried to write 0x07 to ILMD ram register(0x46) but it's not changing.

    Rs=20m ohm
    DesignCapaciaty = 340mah
    LMD=2C00

    PS: I have written 0xDD to 0x6E for write access to RAM locations.

  • Hello Yashavanth,

    Yes, a value of 0x07 is correct for a 340mAh cell with a 20mOhm sense resistor. Did you issue the full reset command to force the gauge to use the new EEPROM values?

    If not, please refer to the following app note that describes the entire process to program the bq27210.

  • Hi Fernando,

    Yeah I issued full reset command to force the guage to use the new EEPROM values (written 0x02 into mode register for guage reset) still no changes in ILMD,

    Am doing the process as below.

    1. am writing 0xdd to 0x6e of ram location.

    2. written 0x07 to 0x46(ILMD) of ram location.

    3. written 0x00 to 0x6e of ram location to stop access to RAM location.

    4. written 0x02 to 0x00 (mode register) for full reset of guage.

    PS: am writing into ram register(0x46) which is mirror address of EEPROM(0x76). 

  • Hi Fernando,

    I need to write directly into EEPROM address or writing into mirrored ram location of EEPROM is enough for ILMD changes??
  • Hi Fernando,

    In www.ti.com/.../slua414.pdf datasheet they mentioned we need to programmed into EEPROM for desired changes in ILMD. But i made changes in RAM location which is mirrored location of EEPROM. Is that correct what am doing??
  • Hello Yashavanth,

    Did you apply the programming pulse of 21-V amplitude for 10-to-100 ms to the PGM pin on the bq27210?

    Thank you.

  • Hi Fernando,

    Is possible to program EEPROM location by writing into RAM location(0x46 - 0x4F) directly, without programming into EEPROM (0x76 - 0x7F)??

    In http://www.ti.com/lit/an/slua414/slua414.pdf datasheet, page no 2, 2nd paragraph said that we can programmed into RAM location(0x46 - 0x4F), the new co-efficient in ram location will be copied to the EEPROM (0x76 - 0x7F). 

  • Hi Fernando,

    Without programming into EEPROM Can we change RAM address 0x46 to 0x4F. If possible please tell me how. 

    Am waiting for your answer for my Previous question, Please answer ASAP. 

  • Hello Yashavanth,

    It is possible to write to RAM without programming the EEPROM.

    Page 13-14 of the bq27010 datasheet lists the commands you can use to indirectly update RAM registers without programming the EEPROM.

    In order to update ILMD in RAM location 0x46, the value can't be written directly to register 0x46. You can first write the desired value to the AR register, then use WRTLMD command to transfer the value to LMD.

    You can also use WNACCI to initialize NAC to some initial value. Otherwise, NAC will always initialize to 0 and stay there until a learning cycle has completed.

    Thank you.

  • Hi Fernando,

    Thank you for your reply, LMD is updated now but NAC is not varying.

    As you told i write desired value to AR reg, and apply the WRTLMD, WRTCYC, WNACCI command. 

    RSOC is always showing 100% and NAC = 0x07, LMD =0x07(No changes in this register).

    regards,

    Yashavanth K S

  • Hi fernando,

    I need your support very badly to solve my above issue, please do the reply as soon as possible.

    thanks in advance

    Yashavanth 

  • Hello Yashavanth,

    RSOC is reporting 100% because at the moment NAC=LMD. Do you mean it reports 100% during the entire discharge? Have you ran a learning cycle yet?

    LMD is updated after every valid learning cycle during lifetime.

    NAC is updated as follows:

    •Updated to full (NAC = LMD) on charge termination
    •Updated by coulomb counting during discharge or charge
    •Updated to “almost empty” (6.25% of full) when EDV1 is reached
    •Updated to empty (0% of full) when EDVF is reached
    Ensure your EDV1 and EDVF thresholds are also correctly set.
    Thank you.
  • Hi Fernando,

    Yes RSOC 100% through out the entire discharge. Yeah i'm running learning cycle by applying command WRTCYC only during initialization. 

    Are you asking about Scaled EDV1 and Scaled EDVF? How to check EDV1 and EDVF thresholds?? How much it should be? If i select correct EDV1 and EDVF all the updates you mentioned above about NAC will be achieve?

    Thanks in Advance

    Yashavanth

  • Hello Yashavanth,

    Applying WRTCYC during initialization only updates the cycle count in the device.

    EDV1 and EDVF are selected based on the cell specs. EDVF is a fixed value and should be set to a voltage that the system sees as the zero-capacity battery voltage. EDV1 should be programmed to a voltage determined from battery characterization that affords 6.25% of the design capacity remaining before the battery reaches zero capacity.

    Some clarification would be helpful, on a previous message on this thread you mentioned that "RSOC is always showing 100% and NAC = 0x07, LMD =0x07(No changes in this register)". If the Nominal Available Capacity is initialized to equal the Last Measured Discharge, RSOC should initialized to 100%. Are you able to update NAC or is this the issue? After initializing the gauge, have you performed any cycles? If so, do you have a log you can share?

    Thank you.
  • Hi Fernando,

    (write values to control and mode register during initialization)

    initial Program:

    Control register = 0x56

    Mode register = 0x03

    Control register = 0xA9

    Mode register = 0x20

    Main program:

    (reading RSOC, LMD, NAC)

    RSOC giving the output like as follows

    100% -> 85% -> 71% -> 57% -> 43% -> 29% -> 15% -> 0% (no changes when reaches to zero).

    Similarly NAC decreasing from 0x07.  

    If i connect charger to charge battery, RSOC goes suddenly to 100%. I'm using PMIC which indicated charging, not charging and full charge. Once it reaches full charge if i remove the charger also RSOC doesn't change from 100 until it empty.

  • Hi Yashavanth,

    Do you have a complete log file showing all registers over a period of time you can share? You can log with our evaluation software.

    Thank you.
  • Hi Fernando,

    Log of bq27210

    (Init)
    mode_56 unsigned char 0x43 (Hex) 0x20000B81
    mode_A9 unsigned char 0x63 (Hex) 0x20000B80

    (Main Prog)

    (continuous loop) 

    1.
    RSOC unsigned char 71 (Decimal) 0x200035E5
    CSOC unsigned char 71 (Decimal) 0x200035E4
    NAC[0] unsigned char 0x05 (Hex) 0x200035E6
    NAC[1] unsigned char 0x00 (Hex) 0x200035E7
    LMD[0] unsigned char 0x07 (Hex) 0x200035E8
    LMD[1] unsigned char 0x00 (Hex) 0x200035E9

    2.

    RSOC unsigned char 57 (Decimal) 0x200035E5
    CSOC unsigned char 57 (Decimal) 0x200035E4
    NAC[0] unsigned char 0x04 (Hex) 0x200035E6
    NAC[1] unsigned char 0x00 (Hex) 0x200035E7
    LMD[0] unsigned char 0x07 (Hex) 0x200035E8
    LMD[1] unsigned char 0x00 (Hex) 0x200035E9

    3.

    RSOC unsigned char 43 (Decimal) 0x200035E5
    CSOC unsigned char 43 (Decimal) 0x200035E4
    NAC[0] unsigned char 0x03 (Hex) 0x200035E6
    NAC[1] unsigned char 0x00 (Hex) 0x200035E7
    LMD[0] unsigned char 0x07 (Hex) 0x200035E8
    LMD[1] unsigned char 0x00 (Hex) 0x200035E9

    4.

    RSOC unsigned char 29 (Decimal) 0x200035E5
    CSOC unsigned char 29 (Decimal) 0x200035E4
    NAC[0] unsigned char 0x02 (Hex) 0x200035E6
    NAC[1] unsigned char 0x00 (Hex) 0x200035E7
    LMD[0] unsigned char 0x07 (Hex) 0x200035E8
    LMD[1] unsigned char 0x00 (Hex) 0x200035E9

    5.
    RSOC unsigned char 14 (Decimal) 0x200035E5
    CSOC unsigned char 14 (Decimal) 0x200035E4
    NAC[0] unsigned char 0x01 (Hex) 0x200035E6
    NAC[1] unsigned char 0x00 (Hex) 0x200035E7
    LMD[0] unsigned char 0x07 (Hex) 0x200035E8
    LMD[1] unsigned char 0x00 (Hex) 0x200035E9

    6.

    RSOC unsigned char 0 (Decimal) 0x200035E5
    CSOC unsigned char 0 (Decimal) 0x200035E4
    NAC[0] unsigned char 0x00 (Hex) 0x200035E6
    NAC[1] unsigned char 0x00 (Hex) 0x200035E7
    LMD[0] unsigned char 0x07 (Hex) 0x200035E8
    LMD[1] unsigned char 0x00 (Hex) 0x200035E9


    (After putting the charger)

    7.

    RSOC unsigned char 100 (Decimal) 0x200035E5
    CSOC unsigned char 100 (Decimal) 0x200035E4
    NAC[0] unsigned char 0x07 (Hex) 0x200035E6
    NAC[1] unsigned char 0x00 (Hex) 0x200035E7
    LMD[0] unsigned char 0x07 (Hex) 0x200035E8
    LMD[1] unsigned char 0x00 (Hex) 0x200035E9

  • Hi Fernando,

    Below piece of code is my initialization code for bq27210

    if (!SENSOR_SELECT())
    return false;

    //Write to AR reg
    mode_val1 = 0x07;
    sensorWriteReg(BQ27210_REG_ADDR_AR_LOW, &mode_val1, REG_DATA_SIZE);
    mode_val1 = 0x00;
    sensorWriteReg(BQ27210_REG_ADDR_AR_HIGH, &mode_val1, REG_DATA_SIZE);

    // Apply commands to made reg, WRTLMD, WRTCYC
    cmd_key1 = 0x56;
    sensorWriteReg(BQ27210_REG_ADDR_CNTRL,&cmd_key1,REG_DATA_SIZE);
    sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val3, REG_DATA_SIZE);
    mode_val4 = mode_val3;
    mode_val4 |= 0X03;

    sensorWriteReg(BQ27210_REG_ADDR_MODE, &mode_val4, REG_DATA_SIZE);
    sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_56, REG_DATA_SIZE);

    // Clear INIT bit
    cmd_key1 = 0x56;
    sensorWriteReg(BQ27210_REG_ADDR_CNTRL,&cmd_key1,REG_DATA_SIZE);
    sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val3, REG_DATA_SIZE);
    mode_val4 = mode_val3;
    mode_val4 &= ~(0X04);

    // Apply commands to made reg, WRTNAC.
    sensorWriteReg(BQ27210_REG_ADDR_MODE, &mode_val4, REG_DATA_SIZE);
    sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_56, REG_DATA_SIZE);

    cmd_key1 = 0xA9;
    sensorWriteReg(BQ27210_REG_ADDR_CNTRL,&cmd_key1,REG_DATA_SIZE);
    sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val3, REG_DATA_SIZE);
    mode_val4 = mode_val3;
    mode_val4 |= 0X20;

    sensorWriteReg(BQ27210_REG_ADDR_MODE, &mode_val4, REG_DATA_SIZE);
    sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_A9, REG_DATA_SIZE);

    // Clear INIT bit
    cmd_key1 = 0xA9;
    sensorWriteReg(BQ27210_REG_ADDR_CNTRL,&cmd_key1,REG_DATA_SIZE);
    sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val3, REG_DATA_SIZE);
    mode_val4 = mode_val3;
    mode_val4 &= ~(0X04);

    sensorWriteReg(BQ27210_REG_ADDR_MODE, &mode_val4, REG_DATA_SIZE);
    sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_A9, REG_DATA_SIZE);


    SENSOR_DESELECT();

    return true;
  • Hello Yashavanth,

    What are the time intervals between RSOC reads in your main loop? What is your discharge rate?

    Thanks.

  • Hi Fernando,

    Normally Time Interval between RSOC read is 1 sec in our main loop and also i tested with 2.5 sec interval. I don't know anything about discharge rate. Is that SEDV1??

  • Hello Yashavanth,

    From your previous comment, if the intervals between RSOC readings is 1 sec in the main loop, you are seeing your cell is being discharged completely in ~6 seconds?

    By discharge rate, I mean what is the current you are discharging the cell? SEDV1 refers to the scaled EDV1 threshold, which corresponds to the voltage threshold where the battery is supposed to still have at least 6.25% capacity remaining at light loads. Since your design capacity is 340mAh, it may be too small and the gauge may be rounding the RSOC values.

    Are you able to use bqEVSW to log the entire discharge and charge cycle? Only monitoring SOC doesn't provide enough information to continue debugging.

    Thank you.
  • Hi Fernando,
    Doesnt the discharge rate is taken automatically by the learning cycle.
    Is there any way to reconfigure the setting, by changing the ILMD value to 0x7 in the EEPROM - RAM mirror register and EDVF and EDVF1 to 119 and 139 respectively in the RAM mirror register. Without Resetting.

    I think i cannot use the AR register to update the those RAM mirror registers. I tried direct write but its not working. Is my assumption of re configuring is right?
  • Hi yashvanth

    It is not possible to write those RAM register.If you want to modify the value you have only one option AR register only.

    Your Design capacity value is 0x07

    First write the value in IMLD Register.The IMLD is updated by UPDC bit in the Mode register.

    As per the configuring EEPROM datasheet your LMD value is 0x700.

    so write LMD value using WRTLMD

    and then write NAC value also equal to 0x700

    For EDVF  update  use the  UPDC bit

    and for EDV1 use the bit UPEDV1......

    Joseph

  • Hi Joseph,

    I have tested all the scenarios you have mentioned above still facing the same problem.

    LMD is updated properly at the initialization but

    NAC is not updating with battery discharging, Cycle count also updated using command. Can you please provide some sample code for 340mah cell. It helps me lot.

    regards,

    Yashavanth K S 

  • 2046.data.LOG

    Hi Joseph Christober,

    Please see the log and help me to solve this issue. Am struggling about this since from 3 months, this log may help you to solve my issue. 

    thanks in advance

    Regards,

    Yashavanth

  • Hello Yashavanth,

    Is the log file you attached supposed to correspond to a full cycle? I'm a bit confused, I see the voltage remain constant for ~1hour with no current. Your RSOC is reported as 100% when your voltage is ~2.4V, at this voltage a Li-ion cell shouldn't have any capacity left, unless this is a NiMH or NiCd cell in a multi-cell configuration. Please clarify.

    EDIT: Forgot to mention, the bq27210 can also measure voltage down to 2.6V, below this threshold is the POR threshold. Can you elaborate how you have set up your gauge?


    Thanks.

  • /*******************************************************************************
    *  Filename:       sensor_opt3001.c
    *  Revised:        $Date: 2014-02-05 10:47:02 +0100 (on, 05 feb 2014) $
    *  Revision:       $Revision: 12066 $
    *
    *  Description:    Driver for the Texas Instruments OP3001 Optical Sensor
    *
    *  Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
    *
    *  Redistribution and use in source and binary forms, with or without
    *  modification, are permitted provided that the following conditions
    *  are met:
    *
    *    Redistributions of source code must retain the above copyright
    *    notice, this list of conditions and the following disclaimer.
    *
    *    Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in the
    *    documentation and/or other materials provided with the distribution.
    *
    *    Neither the name of Texas Instruments Incorporated nor the names of
    *    its contributors may be used to endorse or promote products derived
    *    from this software without specific prior written permission.
    *
    *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    *******************************************************************************/
    
    /* -----------------------------------------------------------------------------
    *                                          Includes
    * ------------------------------------------------------------------------------
    */
    #include "bsp_i2c.h"
    #include "sensor.h"
    #include "sensor_opt3001.h"
    #include "math.h"
    #include "sensortag.h"
    /* -----------------------------------------------------------------------------
    *                                           Constants
    * ------------------------------------------------------------------------------
    */
    
    /* Battery I2C Slave address */
    
    #define SENSOR_I2C_ADDRESS              	0x55
    
    /* BQ27210 register addresses */
    #define BQ27210_REG_ADDR_CNTRL				0x00 /* Control */
    #define BQ27210_REG_ADDR_MODE				0x01 /* Mode */
    #define BQ27210_REG_ADDR_AR_LOW				0x02 /* At-Rate low byte */
    #define BQ27210_REG_ADDR_AR_HIGH			0x03 /* At-Rate High byte */
    #define BQ27210_REG_ADDR_TEMP				0x06 /* Temperature */
    #define BQ27210_REG_ADDR_VOLT_LOW			0x08 /* Voltage Low byte */
    #define BQ27210_REG_ADDR_VOLT_HIGH			0x09 /* Voltage High byte*/
    #define BQ27210_REG_ADDR_AVG_CURR_LOW		0x14 /* Average Current Low byte */
    #define BQ27210_REG_ADDR_AVG_CURR_HIGH		0x15 /* Average Current High byte */
    #define BQ27210_REG_ADDR_FLAGS				0X0a /* Flags */
    #define BQ27210_REG_ADDR_TTE				0X16 /* Time-to-Empty */
    #define BQ27210_REG_ADDR_TTF				0X18 /* Time-to-Full */
    #define BQ27210_REG_ADDR_TTES				0X1c /* Time-to-Empty Standby */
    #define BQ27210_REG_ADDR_TTECP				0X26 /* Time-to-Empty at Constant Power */
    #define BQ27210_REG_ADDR_NAC_LOW			0X0C /* Nominal Available Capacity Low byte */
    #define BQ27210_REG_ADDR_NAC_HIGH			0X0D /* Nominal Available Capacity High byte */
    #define BQ27210_REG_ADDR_LMD_LOW			0X0E /* Full Charge Capacity Low byte */
    #define BQ27210_REG_ADDR_LMD_HIGH			0X0F /* Full Charge Capacity High byte */
    #define BQ27210_REG_ADDR_CYCT				0X2A /* Cycle Count */
    #define BQ27210_REG_ADDR_SOC				0X0b /* State-of-Charge */
    #define BQ27210_REG_ADDR_DCAP				0X76 /* Design Capacity */
    #define BQ27210_REG_ADDR_CAC_LOW			0x10 /* Compensated Available Capacity Low */
    #define BQ27210_REG_ADDR_CAC_HIGH			0x11 /* Compensated Available Capacity High */
    #define BQ27210_REG_ADDR_FCAC				0x12 /* Full Compensated Available Capacity High */
    #define BQ27210_REG_ADDR_FCAC_HIGH			0x13 /* Full compensated charge capacity */
    #define BQ27210_REG_ADDR_CSOC				0x2C /* Compensated State-of-Charge */
    
    //EEPROM mirrored RAM location
    #define BQ27210_RAM_REG_ILMD				0X46
    #define BQ27210_RAM_REG2_SEDVF				0X47
    #define BQ27210_RAM_REG3_SEDV1				0X48
    
    //EEPROM Register
    #define BQ27210_EEPROM_ACCESS				0x6E
    #define BQ27210_EEPROM_ILMD					0X76
    #define BQ27210_EEPROM_SEDVF				0X77
    #define BQ27210_EEPROM_SEDV1				0X78
    #define BQ27210_EEPROM_EDVT					0X79
    #define BQ27210_EEPROM_DMFSD				0X7A
    #define BQ27210_EEPROM_TAPER				0X7B
    #define BQ27210_EEPROM_PKCFG				0X7C
    #define BQ27210_EEPROM_DEDV					0X7D
    #define BQ27210_EEPROM_DCOMP				0X7E
    #define BQ27210_EEPROM_TCOMP				0X7F
    
    
    
    //Status Register Bits
    #define BQ27210_FLAG_CHGS					0x80
    #define BQ27210_FLAG_EDV1					0x02
    #define BQ27210_FLAG_EDVF					0x01
    #define BQ27210_FLAG_CI						0x10
    
    // Fixed values
    #define REG_DATA_SIZE						1
    #define BATT_DATA_SIZE						2
    
    // Sensor selection/deselection
    #define SENSOR_SELECT()     bspI2cSelect(BSP_I2C_INTERFACE_0,SENSOR_I2C_ADDRESS)
    #define SENSOR_DESELECT()   bspI2cDeselect()
    
    
    /* -----------------------------------------------------------------------------
    *                                           Local Functions
    * ------------------------------------------------------------------------------
    */
    
    
    /* -----------------------------------------------------------------------------
    *                                           Local Variables
    * ------------------------------------------------------------------------------
    */
    
    /* -----------------------------------------------------------------------------
    *                                           Public functions
    * ------------------------------------------------------------------------------
    */
    
    
    /*******************************************************************************
     * @fn          sensorBq27210Init
     *
     * @brief       Initialise the temperature sensor driver
     *
     * @return      none
     ******************************************************************************/
    bool sensorBq27210Init(void)
    {
    
    
    
    	uint8_t mode_val3, mode_val4, mode_val5, mode_val6;
    	uint8_t cmd_key1;
    
    	if (!SENSOR_SELECT())
    	    return false;
    
    	uint8_t mode_val1 = 0x00;
    	sensorWriteReg(BQ27210_REG_ADDR_AR_LOW, &mode_val1, REG_DATA_SIZE);
    	mode_val1 = 0x07;
    	sensorWriteReg(BQ27210_REG_ADDR_AR_HIGH, &mode_val1, REG_DATA_SIZE);
    
    	cmd_key1 = 0x56;
    	sensorWriteReg(BQ27210_REG_ADDR_CNTRL,&cmd_key1,REG_DATA_SIZE);
    	sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val3, REG_DATA_SIZE);
    	mode_val4 = mode_val3;
    	mode_val4 |= 0X03;
    
    	sensorWriteReg(BQ27210_REG_ADDR_MODE, &mode_val4, REG_DATA_SIZE);
    	sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val5, REG_DATA_SIZE);
    
    	cmd_key1 = 0xA9;
    	sensorWriteReg(BQ27210_REG_ADDR_CNTRL,&cmd_key1,REG_DATA_SIZE);
    	sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val3, REG_DATA_SIZE);
    	mode_val4 = mode_val3;
    	mode_val4 |= 0X20;
    
    	sensorWriteReg(BQ27210_REG_ADDR_MODE, &mode_val4, REG_DATA_SIZE);
    	sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val6, REG_DATA_SIZE);
    
    	cmd_key1 = 0xC5;
    	sensorWriteReg(BQ27210_REG_ADDR_CNTRL,&cmd_key1,REG_DATA_SIZE);
    	sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val3, REG_DATA_SIZE);
    	mode_val4 = mode_val3;
    	mode_val4 |= 0X50;
    
    	sensorWriteReg(BQ27210_REG_ADDR_MODE, &mode_val4, REG_DATA_SIZE);
    	sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val6, REG_DATA_SIZE);
    
    
    	SENSOR_DESELECT();
    
    
      return true;
    }
    
    
    /*******************************************************************************
     * @fn          sensorBq27210Enable
     *
     * @brief       Turn the sensor on/off
     *
     * @return      true/false
     ******************************************************************************/
    bool sensorBq27210Enable(bool enable)
    {
    
    	  if (!SENSOR_SELECT())
    	    return false;
    
    	  SENSOR_DESELECT();
    
      return true;
    }
    
    
    /*******************************************************************************
     * @fn          sensorRead_BattLevel
     *
     * @brief       Read the result register
     *
     * @param       Buffer to store data in
     *
     * @return      TRUE if valid data
     ******************************************************************************/
    bool sensorRead_BattLevel(uint8_t *rawData)
    {
      bool success;
      static uint8_t val;
      static uint8_t val1;
      static uint8_t buf[2];
      static uint8_t buf1[2];
      static uint8_t buf2[2];
      static uint8_t buf3[2];
      static uint8_t debug[2];
      if (!SENSOR_SELECT())
        return false;
    
    // Read Battery Charge Percentage
      success = sensorReadReg(BQ27210_REG_ADDR_SOC, (uint8_t *)&val1, REG_DATA_SIZE);		// RSOC register for Battery Charge percentage.
      sensorReadReg(BQ27210_REG_ADDR_CSOC, (uint8_t *)&val, REG_DATA_SIZE);					// CSOC register
      sensorReadReg(BQ27210_REG_ADDR_NAC_LOW, (uint8_t *)&buf[0], REG_DATA_SIZE);			// NAC low byte
      sensorReadReg(BQ27210_REG_ADDR_NAC_HIGH, (uint8_t *)&buf[1], REG_DATA_SIZE);			// NAC high byte
      sensorReadReg(BQ27210_REG_ADDR_LMD_LOW, (uint8_t *)&buf1[0], REG_DATA_SIZE);			// LMD low byte
      sensorReadReg(BQ27210_REG_ADDR_LMD_HIGH, (uint8_t *)&buf1[1], REG_DATA_SIZE);			// LMD high byte
      sensorReadReg(BQ27210_REG_ADDR_AVG_CURR_LOW, (uint8_t *)&buf2[0], REG_DATA_SIZE);		// AI low byte
      sensorReadReg(BQ27210_REG_ADDR_AVG_CURR_HIGH, (uint8_t *)&buf2[1], REG_DATA_SIZE);	// AI high byte
      sensorReadReg(BQ27210_REG_ADDR_CAC_LOW, (uint8_t *)&buf3[0], REG_DATA_SIZE);			// CAC low byte
      sensorReadReg(BQ27210_REG_ADDR_CAC_HIGH, (uint8_t *)&buf3[1], REG_DATA_SIZE);			// CAC high byte
    
      if (success)
      {
        // Swap bytes
       *rawData = val1 ;
      }
      else
      {
        sensorSetErrorData((uint8_t*)rawData, REG_DATA_SIZE);
      }
    
      SENSOR_DESELECT();
    
      return success;
    }
    
    /*******************************************************************************
     * @fn          sensorRead_BattStatus
     *
     * @brief       Read the result register
     *
     * @param       Buffer to store data in
     *
     * @return      TRUE if valid data
     ******************************************************************************/
    bool sensorRead_BattStatus(uint8_t *rawData)
    {
      bool success;
      uint8_t val;
    
      if (!SENSOR_SELECT())
        return false;
    
    // Read Status flag to check Charging bit.
      success = sensorReadReg(BQ27210_REG_ADDR_FLAGS, &val, REG_DATA_SIZE);
    
    
      if (success)
      {
    	  if(val & 0x80){
    		    // Swap bytes
    		    *rawData = 1 ;
    	  }
    	  else{
    		    *rawData = 0 ;
    	  }
      }
      else
      {
        sensorSetErrorData((uint8_t*)rawData, REG_DATA_SIZE);
      }
    
      SENSOR_DESELECT();
    
      return success;
    }
    
    
    /*******************************************************************************
     * @fn          sensorRead_BattIndic
     *
     * @brief       Read the result register
     *
     * @param       Buffer to store data in
     *
     * @return      TRUE if valid data
     ******************************************************************************/
    bool sensorRead_BattIndic(uint8_t *rawData)
    {
      bool success;
      uint8_t val;
    
      if (!SENSOR_SELECT())
        return false;
    
    // RSOC register read for charge percentage
      success = sensorReadReg(BQ27210_REG_ADDR_SOC, (uint8_t *)&val, REG_DATA_SIZE);
    
    
      if (success)
      {
    	  if(val < 10){
    		    // Swap bytes
    		    *rawData = 0 ;
    		    if(val > 5)
    		    	SensorTag_blinkLed(Board_LED1,1);
    		    else
    		    	SensorTag_blinkLed(Board_LED1,5);
    	  }
    	  else{
    		    *rawData = 1 ;
    	  }
      }
      else
      {
        sensorSetErrorData((uint8_t*)rawData, REG_DATA_SIZE);
      }
    
      SENSOR_DESELECT();
    
      return success;
    }
    
    
    /*******************************************************************************
     * @fn          sensorBq27210Test
     *
     * @brief       Run a sensor self-test
     *
     * @return      TRUE if passed, FALSE if failed
     ******************************************************************************/
    bool sensorBq27210Test(void)
    {
    
      return true;
    }
    
    /*******************************************************************************
     * @fn          sensorBq27210Convert
     *
     * @brief       Convert raw data to object and ambience tempertaure
     *
     * @param       data - raw data from sensor
     *
     * @param       lux - converted value (lux)
     *
     * @return      none
     ******************************************************************************/
    bool sensorBq27210Convert(void)
    {
    	 bool success;
    	  uint8_t val;
    	  uint8_t val1;
    	  if (!SENSOR_SELECT())
    	    return false;
    
    	  success = sensorReadReg(BQ27210_REG_ADDR_SOC, (uint8_t *)&val, REG_DATA_SIZE);
    	  sensorReadReg(BQ27210_REG_ADDR_FLAGS, &val1, REG_DATA_SIZE);
    	  if(val1 & 0x80){
    	  if (success)
    	  {
    		  if(val < 10){
    			    // Swap bytes
    			    PIN_setOutputValue(hGpioPin,Board_LED1, Board_LED_OFF);
    		  }
    		  else{
    			    PIN_setOutputValue(hGpioPin,Board_LED1, Board_LED_ON);
    		  }
    	  }
    	  }
    
    	  SENSOR_DESELECT();
    
    	  return success;
    }
    
    /*******************************************************************************
    *  Filename:       sensor_opt3001.c
    *  Revised:        $Date: 2014-02-05 10:47:02 +0100 (on, 05 feb 2014) $
    *  Revision:       $Revision: 12066 $
    *
    *  Description:    Driver for the Texas Instruments OP3001 Optical Sensor
    *
    *  Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
    *
    *  Redistribution and use in source and binary forms, with or without
    *  modification, are permitted provided that the following conditions
    *  are met:
    *
    *    Redistributions of source code must retain the above copyright
    *    notice, this list of conditions and the following disclaimer.
    *
    *    Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in the
    *    documentation and/or other materials provided with the distribution.
    *
    *    Neither the name of Texas Instruments Incorporated nor the names of
    *    its contributors may be used to endorse or promote products derived
    *    from this software without specific prior written permission.
    *
    *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    *******************************************************************************/
    
    /* -----------------------------------------------------------------------------
    *                                          Includes
    * ------------------------------------------------------------------------------
    */
    #include "bsp_i2c.h"
    #include "sensor.h"
    #include "sensor_opt3001.h"
    #include "math.h"
    #include "sensortag.h"
    /* -----------------------------------------------------------------------------
    *                                           Constants
    * ------------------------------------------------------------------------------
    */
    
    /* Battery I2C Slave address */
    
    #define SENSOR_I2C_ADDRESS              	0x55
    
    /* BQ27210 register addresses */
    #define BQ27210_REG_ADDR_CNTRL				0x00 /* Control */
    #define BQ27210_REG_ADDR_MODE				0x01 /* Mode */
    #define BQ27210_REG_ADDR_AR_LOW				0x02 /* At-Rate low byte */
    #define BQ27210_REG_ADDR_AR_HIGH			0x03 /* At-Rate High byte */
    #define BQ27210_REG_ADDR_TEMP				0x06 /* Temperature */
    #define BQ27210_REG_ADDR_VOLT_LOW			0x08 /* Voltage Low byte */
    #define BQ27210_REG_ADDR_VOLT_HIGH			0x09 /* Voltage High byte*/
    #define BQ27210_REG_ADDR_AVG_CURR_LOW		0x14 /* Average Current Low byte */
    #define BQ27210_REG_ADDR_AVG_CURR_HIGH		0x15 /* Average Current High byte */
    #define BQ27210_REG_ADDR_FLAGS				0X0a /* Flags */
    #define BQ27210_REG_ADDR_TTE				0X16 /* Time-to-Empty */
    #define BQ27210_REG_ADDR_TTF				0X18 /* Time-to-Full */
    #define BQ27210_REG_ADDR_TTES				0X1c /* Time-to-Empty Standby */
    #define BQ27210_REG_ADDR_TTECP				0X26 /* Time-to-Empty at Constant Power */
    #define BQ27210_REG_ADDR_NAC_LOW			0X0C /* Nominal Available Capacity Low byte */
    #define BQ27210_REG_ADDR_NAC_HIGH			0X0D /* Nominal Available Capacity High byte */
    #define BQ27210_REG_ADDR_LMD_LOW			0X0E /* Full Charge Capacity Low byte */
    #define BQ27210_REG_ADDR_LMD_HIGH			0X0F /* Full Charge Capacity High byte */
    #define BQ27210_REG_ADDR_CYCT				0X2A /* Cycle Count */
    #define BQ27210_REG_ADDR_SOC				0X0b /* State-of-Charge */
    #define BQ27210_REG_ADDR_DCAP				0X76 /* Design Capacity */
    #define BQ27210_REG_ADDR_CAC_LOW			0x10 /* Compensated Available Capacity Low */
    #define BQ27210_REG_ADDR_CAC_HIGH			0x11 /* Compensated Available Capacity High */
    #define BQ27210_REG_ADDR_FCAC				0x12 /* Full Compensated Available Capacity High */
    #define BQ27210_REG_ADDR_FCAC_HIGH			0x13 /* Full compensated charge capacity */
    #define BQ27210_REG_ADDR_CSOC				0x2C /* Compensated State-of-Charge */
    
    //EEPROM mirrored RAM location
    #define BQ27210_RAM_REG_ILMD				0X46
    #define BQ27210_RAM_REG2_SEDVF				0X47
    #define BQ27210_RAM_REG3_SEDV1				0X48
    
    //EEPROM Register
    #define BQ27210_EEPROM_ACCESS				0x6E
    #define BQ27210_EEPROM_ILMD					0X76
    #define BQ27210_EEPROM_SEDVF				0X77
    #define BQ27210_EEPROM_SEDV1				0X78
    #define BQ27210_EEPROM_EDVT					0X79
    #define BQ27210_EEPROM_DMFSD				0X7A
    #define BQ27210_EEPROM_TAPER				0X7B
    #define BQ27210_EEPROM_PKCFG				0X7C
    #define BQ27210_EEPROM_DEDV					0X7D
    #define BQ27210_EEPROM_DCOMP				0X7E
    #define BQ27210_EEPROM_TCOMP				0X7F
    
    
    
    //Status Register Bits
    #define BQ27210_FLAG_CHGS					0x80
    #define BQ27210_FLAG_EDV1					0x02
    #define BQ27210_FLAG_EDVF					0x01
    #define BQ27210_FLAG_CI						0x10
    
    // Fixed values
    #define REG_DATA_SIZE						1
    #define BATT_DATA_SIZE						2
    
    // Sensor selection/deselection
    #define SENSOR_SELECT()     bspI2cSelect(BSP_I2C_INTERFACE_0,SENSOR_I2C_ADDRESS)
    #define SENSOR_DESELECT()   bspI2cDeselect()
    
    
    /* -----------------------------------------------------------------------------
    *                                           Local Functions
    * ------------------------------------------------------------------------------
    */
    
    
    /* -----------------------------------------------------------------------------
    *                                           Local Variables
    * ------------------------------------------------------------------------------
    */
    
    /* -----------------------------------------------------------------------------
    *                                           Public functions
    * ------------------------------------------------------------------------------
    */
    
    
    /*******************************************************************************
     * @fn          sensorBq27210Init
     *
     * @brief       Initialise the temperature sensor driver
     *
     * @return      none
     ******************************************************************************/
    bool sensorBq27210Init(void)
    {
    
    	if (!SENSOR_SELECT())
    	    return false;
    
    	uint8_t mode_val1, mode_val2;
    
    	mode_val1 = 0xdd;
    	sensorWriteReg(BQ27210_EEPROM_ACCESS, &mode_val1, REG_DATA_SIZE);
    	mode_val1 = 0x07;
    	sensorWriteReg(BQ27210_EEPROM_ILMD, &mode_val1, REG_DATA_SIZE);
    	mode_val1 = 0x77;
    	sensorWriteReg(BQ27210_EEPROM_SEDVF, &mode_val1, REG_DATA_SIZE);
    	mode_val1 = 0xa9;
    	sensorWriteReg(BQ27210_EEPROM_SEDV1, &mode_val1, REG_DATA_SIZE);
    	mode_val1 = 0xa9;
    	sensorWriteReg(BQ27210_REG_ADDR_CNTRL, &mode_val1, REG_DATA_SIZE);
    	sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val1, REG_DATA_SIZE);
    	mode_val1 |= 0X02;
    	sensorWriteReg(BQ27210_REG_ADDR_MODE, &mode_val1, REG_DATA_SIZE);
    	sensorReadReg(BQ27210_RAM_REG_ILMD, &mode_val2, REG_DATA_SIZE);
    	sensorReadReg(BQ27210_RAM_REG2_SEDVF, &mode_val2, REG_DATA_SIZE);
    	sensorReadReg(BQ27210_RAM_REG3_SEDV1, &mode_val2, REG_DATA_SIZE);
    	sensorReadReg(BQ27210_REG_ADDR_MODE, &mode_val2, REG_DATA_SIZE);
    	mode_val1 = 0x00;
    	sensorWriteReg(BQ27210_EEPROM_ACCESS,&mode_val1,REG_DATA_SIZE);
    
    
    	SENSOR_DESELECT();
    
    
      return true;
    }
    
    
    /*******************************************************************************
     * @fn          sensorBq27210Enable
     *
     * @brief       Turn the sensor on/off
     *
     * @return      true/false
     ******************************************************************************/
    bool sensorBq27210Enable(bool enable)
    {
    
    	  if (!SENSOR_SELECT())
    	    return false;
    
    	  SENSOR_DESELECT();
    
      return true;
    }
    
    
    /*******************************************************************************
     * @fn          sensorRead_BattLevel
     *
     * @brief       Read the result register
     *
     * @param       Buffer to store data in
     *
     * @return      TRUE if valid data
     ******************************************************************************/
    bool sensorRead_BattLevel(uint8_t *rawData)
    {
      bool success;
      static uint8_t val;
      static uint8_t val1;
      static uint8_t buf[2];
      static uint8_t buf1[2];
      static uint8_t buf2[2];
      static uint8_t buf3[2];
      static uint8_t debug[2];
      if (!SENSOR_SELECT())
        return false;
    
    // Read Battery Charge Percentage
      success = sensorReadReg(BQ27210_REG_ADDR_SOC, (uint8_t *)&val1, REG_DATA_SIZE);		// RSOC register for Battery Charge percentage.
      sensorReadReg(BQ27210_REG_ADDR_CSOC, (uint8_t *)&val, REG_DATA_SIZE);					// CSOC register
      sensorReadReg(BQ27210_REG_ADDR_NAC_LOW, (uint8_t *)&buf[0], REG_DATA_SIZE);			// NAC low byte
      sensorReadReg(BQ27210_REG_ADDR_NAC_HIGH, (uint8_t *)&buf[1], REG_DATA_SIZE);			// NAC high byte
      sensorReadReg(BQ27210_REG_ADDR_LMD_LOW, (uint8_t *)&buf1[0], REG_DATA_SIZE);			// LMD low byte
      sensorReadReg(BQ27210_REG_ADDR_LMD_HIGH, (uint8_t *)&buf1[1], REG_DATA_SIZE);			// LMD high byte
      sensorReadReg(BQ27210_REG_ADDR_AVG_CURR_LOW, (uint8_t *)&buf2[0], REG_DATA_SIZE);		// AI low byte
      sensorReadReg(BQ27210_REG_ADDR_AVG_CURR_HIGH, (uint8_t *)&buf2[1], REG_DATA_SIZE);	// AI high byte
      sensorReadReg(BQ27210_REG_ADDR_CAC_LOW, (uint8_t *)&buf3[0], REG_DATA_SIZE);			// CAC low byte
      sensorReadReg(BQ27210_REG_ADDR_CAC_HIGH, (uint8_t *)&buf3[1], REG_DATA_SIZE);			// CAC high byte
    
      if (success)
      {
        // Swap bytes
       *rawData = val1 ;
      }
      else
      {
        sensorSetErrorData((uint8_t*)rawData, REG_DATA_SIZE);
      }
    
      SENSOR_DESELECT();
    
      return success;
    }
    
    /*******************************************************************************
     * @fn          sensorRead_BattStatus
     *
     * @brief       Read the result register
     *
     * @param       Buffer to store data in
     *
     * @return      TRUE if valid data
     ******************************************************************************/
    bool sensorRead_BattStatus(uint8_t *rawData)
    {
      bool success;
      uint8_t val;
    
      if (!SENSOR_SELECT())
        return false;
    
    // Read Status flag to check Charging bit.
      success = sensorReadReg(BQ27210_REG_ADDR_FLAGS, &val, REG_DATA_SIZE);
    
    
      if (success)
      {
    	  if(val & 0x80){
    		    // Swap bytes
    		    *rawData = 1 ;
    	  }
    	  else{
    		    *rawData = 0 ;
    	  }
      }
      else
      {
        sensorSetErrorData((uint8_t*)rawData, REG_DATA_SIZE);
      }
    
      SENSOR_DESELECT();
    
      return success;
    }
    
    
    /*******************************************************************************
     * @fn          sensorRead_BattIndic
     *
     * @brief       Read the result register
     *
     * @param       Buffer to store data in
     *
     * @return      TRUE if valid data
     ******************************************************************************/
    bool sensorRead_BattIndic(uint8_t *rawData)
    {
      bool success;
      uint8_t val;
    
      if (!SENSOR_SELECT())
        return false;
    
    // RSOC register read for charge percentage
      success = sensorReadReg(BQ27210_REG_ADDR_SOC, (uint8_t *)&val, REG_DATA_SIZE);
    
    
      if (success)
      {
    	  if(val < 10){
    		    // Swap bytes
    		    *rawData = 0 ;
    		    if(val > 5)
    		    	SensorTag_blinkLed(Board_LED1,1);
    		    else
    		    	SensorTag_blinkLed(Board_LED1,5);
    	  }
    	  else{
    		    *rawData = 1 ;
    	  }
      }
      else
      {
        sensorSetErrorData((uint8_t*)rawData, REG_DATA_SIZE);
      }
    
      SENSOR_DESELECT();
    
      return success;
    }
    
    
    /*******************************************************************************
     * @fn          sensorBq27210Test
     *
     * @brief       Run a sensor self-test
     *
     * @return      TRUE if passed, FALSE if failed
     ******************************************************************************/
    bool sensorBq27210Test(void)
    {
    
      return true;
    }
    
    /*******************************************************************************
     * @fn          sensorBq27210Convert
     *
     * @brief       Convert raw data to object and ambience tempertaure
     *
     * @param       data - raw data from sensor
     *
     * @param       lux - converted value (lux)
     *
     * @return      none
     ******************************************************************************/
    bool sensorBq27210Convert(void)
    {
    	 bool success;
    	  uint8_t val;
    	  uint8_t val1;
    	  if (!SENSOR_SELECT())
    	    return false;
    
    	  success = sensorReadReg(BQ27210_REG_ADDR_SOC, (uint8_t *)&val, REG_DATA_SIZE);
    	  sensorReadReg(BQ27210_REG_ADDR_FLAGS, &val1, REG_DATA_SIZE);
    	  if(val1 & 0x80){
    	  if (success)
    	  {
    		  if(val < 10){
    			    // Swap bytes
    			    PIN_setOutputValue(hGpioPin,Board_LED1, Board_LED_OFF);
    		  }
    		  else{
    			    PIN_setOutputValue(hGpioPin,Board_LED1, Board_LED_ON);
    		  }
    	  }
    	  }
    
    	  SENSOR_DESELECT();
    
    	  return success;
    }
    
    Thanks Fernando for your quick response.  Yes,The log I have attached is for full cycle.  We are facing issue in RSOC % and that is the reason we are seeking your help fixing this issue.  These are inputs from our end:

    1> We are using 340mah battery and default bq27210 is not supporting this capacity in our board.

    2> We updated the values of ILMD ,EDv1 and EDVF to RAM locations 0x76,0x77 and 0x78 respectively with no 21v power supply connection.

    3> On Full reset/Partial reset as mentioned in Datasheet, the same values were not updated to 0x46-0x4f locations.

    4> We also tried updating NAC,LMD values using AR registers but NAC did not change (neither decrease nor increase) with respect to battery charge.

    So we planned to share part of the source code performing the same actions,can you please review on the same and let me know ?

  • Hello,

    Your NAC and LMD values are initialized to 319.87mAh, which are fine for a 340mAh cell.

    Do you have a log showing a cycle discharging the cell? In other words, with a discharge current flowing through the sense resistor from fully charged to completely discharged?

    Also, why is your battery voltage 2.4V? A Li-ion cell should never be discharged this low, it will force an internal chemical reaction that can permanently damaged the cell and cause capacity to be lost. Are you initializing the NAC value to the full capacity when the voltage is 2.4V?
  • FYI we updating NAC at 2.4v is that problem? if so what's your suggestion for it?

    Regards,

    Yashavanth.

  • Hello,

    Any particular reason why your cell is discharged to 2.4V? Li-ion and polymer cells shouldn't be discharged this low? Have you tried cycling the cell from full to empty? If so, do you have a log showing this? The log provided earlier didn't have any current information.