Skip to content

Upon charging completion, battery is in ChargingState::fastChargeConstantVoltage state. #6

Open
@aliphys

Description

@aliphys

According to the docs describing the charging state, the final charging state should be END_OF_CHARGE or DONE.

#### Get charger status
You can find out what stage the charger is in by calling the `getChargeStatus()` method.
It will return a value of *ChargeStatus* which can be one of the above:
* `PRECHARGE` - First stage of the charging process
* `FAST_CHARGE_CC` - Second stage of the charging process
* `FAST_CHARGE_CV` - Last stage of the charging process
* `END_OF_CHARGE` - If the battery is still connected, the charger will ensure it's kept at 4.2V by topping up the voltage to avoid self discharge.
* `DONE` - Battery is fully charged
* `TIMER_FAULT` - The timer that is monitoring the charge status has encountered an error.
* `THERMISTOR_SUSPEND` - Charging was suspended due to overheating
* `OFF` - Charger is disabled
* `BATTERY_OVERVOLTAGE` - Charging was suspended due to an overvoltage fault
* `LINEAR_ONLY` - in this state, the charger is bypassed completely and the USB voltage is powering the board

However, when charging is completed to the set voltage, getState() returns ChargingState::fastChargeConstantVoltage.

fast-charge constant voltage- Battery Voltage: 4.22 V
fast-charge constant voltage- Battery Voltage: 4.23 V
Charging complete.
Final Charger Status: fast-charge constant voltage
Final Battery Voltage: 4.22 V

This occurs on both the Portenta C33 as well as the Portenta H7.

Portenta H7 Sketch

/**
 * Test Scenario ID : TPL-ARD-PWRM-001-H7-ChargingState02
 * 
 * This sketch demonstrates the three charging states that can be applied to a battery. It does this by reading the voltage, 
 * and then setting the charging voltage to be 2 steps (2*0.02V) above the battery voltage. 
 * 
 * Test Case IDs :
 * TPL-ARD-PWRM-001-H7-ChargingState02-Initialize          -> Initialize the PMIC
 * TPL-ARD-PWRM-001-H7-ChargingState02-ReadBattery         -> Read battery voltage and set charging voltage 
 * TPL-ARD-PWRM-001-H7-ChargingState02-SetChargeParameters -> Set charging parameters
 * TPL-ARD-PWRM-001-H7-ChargingState02-ChargeStatus        -> Switch status to done when charging complete
 * 
 * NOTE: The battery voltage must be under 4.1V for the charging to start
 * 
 * 
 * Initial author: Ali Jahangiri @aliphys
 */

#include "Arduino_PowerManagement.h"
/*
#include <vector>
#include <string>
#include <array>
*/

const char testScenarioID[] = "TPL-ARD-PWRM-001-H7-ChargingState02";

Battery battery; 
Charger charger;

/*
std::array<std::string, 48> VoltListArray = {
    "V_3_50", "V_3_52", "V_3_54", "V_3_56", "V_3_58", "V_3_60", "V_3_62", "V_3_64", "V_3_66", "V_3_68",
    "V_3_70", "V_3_72", "V_3_74", "V_3_76", "V_3_78", "V_3_80", "V_3_82", "V_3_84", "V_3_86", "V_3_88",
    "V_3_90", "V_3_92", "V_3_94", "V_3_96", "V_3_98", "V_4_00", "V_4_02", "V_4_04", "V_4_06", "V_4_08",
    "V_4_10", "V_4_12", "V_4_14", "V_4_16", "V_4_18", "V_4_20", "V_4_22", "V_4_24", "V_4_26", "V_4_28",
    "V_4_30", "V_4_32", "V_4_34", "V_4_36", "V_4_38", "V_4_40", "V_4_42", "V_4_44"
};

enum class VoltList {
    V_3_50, V_3_52, V_3_54, V_3_56, V_3_58, V_3_60, V_3_62, V_3_64, V_3_66, V_3_68,
    V_3_70, V_3_72, V_3_74, V_3_76, V_3_78, V_3_80, V_3_82, V_3_84, V_3_86, V_3_88,
    V_3_90, V_3_92, V_3_94, V_3_96, V_3_98, V_4_00, V_4_02, V_4_04, V_4_06, V_4_08,
    V_4_10, V_4_12, V_4_14, V_4_16, V_4_18, V_4_20, V_4_22, V_4_24, V_4_26, V_4_28,
    V_4_30, V_4_32, V_4_34, V_4_36, V_4_38, V_4_40, V_4_42, V_4_44
};
*/

float floorVoltage(float voltage, int stepSize = 0) {
    // Check voltage is within the correct range
    if (voltage < 3.50 || voltage > (4.4 - (0.02 * stepSize))) {
        throw std::out_of_range("Voltage/step size is out of range");
    }
    // Get the rounded number (to 0.02) of the voltage
    float roundedVoltage = floor(voltage / 0.02) * 0.02 + (0.02 * stepSize);
    return roundedVoltage;
}

String getChargerState(){
    ChargingState status = charger.getState();

    switch (status) {
        case ChargingState::preCharge:
            return "precharge";
            break;
        case ChargingState::fastChargeConstantCurrent:
            return "fast-charge constant current";
            break;
        case ChargingState::fastChargeConstantVoltage:
            return "fast-charge constant voltage";
            break;
        case ChargingState::endOfCharge:
            return "end-of-charge";
            break;
        case ChargingState::done:
            return "done";
            break;
        case ChargingState::timerFaultError:
            return "timer fault";
            break;
        case ChargingState::thermistorSuspendError:
            return "thermistor suspend";
            break;
        case ChargingState::chargerDisabled:
            return "off";
            break;
        case ChargingState::batteryOvervoltageError:
            return "overvoltage condition";
            break;
        case ChargingState::chargerBypassed:
            return "disabled";
            break;
        default:
            return "unknown";
            break;
    }
}

void setup(){
    Serial.begin(115200);
    while (!Serial);

    Serial.println();
    Serial.println("---------");
    Serial.print("Test Scenario ID: ");
    Serial.println(testScenarioID);
    Serial.println("---------");
    Serial.println();

    Serial.println("---");
    Serial.print("Test Case ID: ");
    Serial.println("TPL-ARD-PWRM-001-H7-ChargingState02-Initialize");

    if (charger.begin()) {
        Serial.println("PMIC initialized.");
    } else {
        Serial.println("PMIC failed to initialize.");
    }

    Serial.println("---");
    Serial.print("Test Case ID: ");
    Serial.println("TPL-ARD-PWRM-001-H7-ChargingState02-ReadBattery");
    
    Serial.println("Reading battery parameters...");
    
    float batteryVoltage = battery.voltage();
    Serial.println("Battery Voltage: " + String(batteryVoltage) + " V");
    
    float batteryVoltageFloor = floorVoltage(batteryVoltage, 0);
    Serial.println("Floor Battery Voltage: " + String(batteryVoltageFloor) + " V");
    
    float batterySetVoltage = floorVoltage(batteryVoltage, 2);
    Serial.println("Set Battery Voltage: " + String(batterySetVoltage) + " V");

    Serial.println("---");
    Serial.print("Test Case ID: ");
    Serial.println("TPL-ARD-PWRM-001-H7-ChargingState02-SetChargeParameters");
    
    Serial.println("Setting charger parameters...");
    charger.setChargeVoltage(batterySetVoltage);
    auto chargeVoltage = charger.getChargeVoltage();
    Serial.println("Final charge voltage: " + String(chargeVoltage) + " V");

    charger.setChargeCurrent(100);
    auto chargeCurrent = charger.getChargeCurrent();
    Serial.println("Charging current: " + String(chargeCurrent) + " mA");
    
    charger.setEndOfChargeCurrent(5);
    auto endOfChargeCurrent = charger.getEndOfChargeCurrent();
    Serial.println("End charge current: " + String(endOfChargeCurrent) + " mA");


    Serial.println("---");
    Serial.print("Test Case ID: ");
    Serial.println("TPL-ARD-PWRM-001-H7-ChargingState02-ChargeStatus");
    
    static ChargingState status = ChargingState::none;
    status = charger.getState();
    batteryVoltage = battery.voltage();

    while (batteryVoltage < chargeVoltage) {
        status = charger.getState();
        batteryVoltage = battery.voltage();
        Serial.println(getChargerState() + "- Battery Voltage: " + String(batteryVoltage) + " V");
        delay(5000);
    }

    if (batteryVoltage >= chargeVoltage) {
        Serial.println("Charging complete.");
        status = charger.getState();
        Serial.println("Final Charger Status: " + getChargerState());
        batteryVoltage = battery.voltage();
        Serial.println("Final Battery Voltage: " + String(batteryVoltage) + " V");
    }
    Serial.println("---");
    Serial.println("---");


    Serial.println();
    Serial.println("---------");
    Serial.print("End of Test Scenario ID: ");
    Serial.println(testScenarioID);
    Serial.println("---------");
    Serial.println();
}

void loop(){
    // Empty loop
}

Portenta H7 Output

---------
Test Scenario ID: TPL-ARD-PWRM-001-H7-ChargingState
---------

---
Test Case ID: TPL-ARD-PWRM-001-H7-ChargingState02-Initialize
PMIC initialized.
---
Test Case ID: TPL-ARD-PWRM-001-H7-ChargingState02-ReadBattery
Reading battery parameters...
Battery Voltage: 3.72 V
Floor Battery Voltage: 3.72 V
Set Battery Voltage: 3.76 V
---
Test Case ID: TPL-ARD-PWRM-001-H7-ChargingState02-SetChargeParameters
Setting charger parameters...
Final charge voltage: 3.76 V
Charging current: 100 mA
End charge current: 5 mA
---
Test Case ID: TPL-ARD-PWRM-001-H7-ChargingState02-ChargeStatus
fast-charge constant voltage- Battery Voltage: 3.72 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.74 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.75 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
fast-charge constant voltage- Battery Voltage: 3.76 V
Charging complete.
Final Charger Status: fast-charge constant voltage
Final Battery Voltage: 3.76 V
---
---

---------
End of Test Scenario ID: TPL-ARD-PWRM-001-H7-ChargingState
---------

Portenta C33 Sketch
/**
 * Test Scenario ID : TPL-ARD-PWRM-001-C33-ChargingState02
 * 
 * This sketch demonstrates the three charging states that can be applied to a battery. It does this by reading the voltage, 
 * and then setting the charging voltage to be 2 steps (2*0.02V) above the battery voltage. 
 * 
 * Test Case IDs :
 * TPL-ARD-PWRM-001-C33-ChargingState02-Initialize          -> Initialize the PMIC
 * TPL-ARD-PWRM-001-C33-ChargingState02-ReadBattery         -> Read battery voltage and set charging voltage 
 * TPL-ARD-PWRM-001-C33-ChargingState02-SetChargeParameters -> Set charging parameters
 * TPL-ARD-PWRM-001-C33-ChargingState02-ChargeStatus        -> Switch status to done when charging complete
 * 
 * NOTE: The battery voltage must be under 4.1V for the charging to start
 * 
 * 
 * Initial author: Ali Jahangiri @aliphys
 */

#include "Arduino_PowerManagement.h"
/*
#include <vector>
#include <string>
#include <array>
*/

const char testScenarioID[] = "TPL-ARD-PWRM-001-C33-ChargingState02";

Battery battery; 
Charger charger;

/*
std::array<std::string, 48> VoltListArray = {
    "V_3_50", "V_3_52", "V_3_54", "V_3_56", "V_3_58", "V_3_60", "V_3_62", "V_3_64", "V_3_66", "V_3_68",
    "V_3_70", "V_3_72", "V_3_74", "V_3_76", "V_3_78", "V_3_80", "V_3_82", "V_3_84", "V_3_86", "V_3_88",
    "V_3_90", "V_3_92", "V_3_94", "V_3_96", "V_3_98", "V_4_00", "V_4_02", "V_4_04", "V_4_06", "V_4_08",
    "V_4_10", "V_4_12", "V_4_14", "V_4_16", "V_4_18", "V_4_20", "V_4_22", "V_4_24", "V_4_26", "V_4_28",
    "V_4_30", "V_4_32", "V_4_34", "V_4_36", "V_4_38", "V_4_40", "V_4_42", "V_4_44"
};

enum class VoltList {
    V_3_50, V_3_52, V_3_54, V_3_56, V_3_58, V_3_60, V_3_62, V_3_64, V_3_66, V_3_68,
    V_3_70, V_3_72, V_3_74, V_3_76, V_3_78, V_3_80, V_3_82, V_3_84, V_3_86, V_3_88,
    V_3_90, V_3_92, V_3_94, V_3_96, V_3_98, V_4_00, V_4_02, V_4_04, V_4_06, V_4_08,
    V_4_10, V_4_12, V_4_14, V_4_16, V_4_18, V_4_20, V_4_22, V_4_24, V_4_26, V_4_28,
    V_4_30, V_4_32, V_4_34, V_4_36, V_4_38, V_4_40, V_4_42, V_4_44
};
*/

float floorVoltage(float voltage, int stepSize = 0) {
    // Check voltage is within the correct range
    if (voltage < 3.50 || voltage > (4.4 - (0.02 * stepSize))) {
        //throw std::out_of_range("Voltage/step size is out of range");
        Serial.println("Voltage/step size is out of range");
    }
    // Get the rounded number (to 0.02) of the voltage
    float roundedVoltage = floor(voltage / 0.02) * 0.02 + (0.02 * stepSize);
    return roundedVoltage;
}

String getChargerState(){
    ChargingState status = charger.getState();

    switch (status) {
        case ChargingState::preCharge:
            return "precharge";
            break;
        case ChargingState::fastChargeConstantCurrent:
            return "fast-charge constant current";
            break;
        case ChargingState::fastChargeConstantVoltage:
            return "fast-charge constant voltage";
            break;
        case ChargingState::endOfCharge:
            return "end-of-charge";
            break;
        case ChargingState::done:
            return "done";
            break;
        case ChargingState::timerFaultError:
            return "timer fault";
            break;
        case ChargingState::thermistorSuspendError:
            return "thermistor suspend";
            break;
        case ChargingState::chargerDisabled:
            return "off";
            break;
        case ChargingState::batteryOvervoltageError:
            return "overvoltage condition";
            break;
        case ChargingState::chargerBypassed:
            return "disabled";
            break;
        default:
            return "unknown";
            break;
    }
}

void setup(){
    Serial.begin(115200);
    while (!Serial);

    Serial.println();
    Serial.println("---------");
    Serial.print("Test Scenario ID: ");
    Serial.println(testScenarioID);
    Serial.println("---------");
    Serial.println();

    Serial.println("---");
    Serial.print("Test Case ID: ");
    Serial.println("TPL-ARD-PWRM-001-C33-ChargingState02-Initialize");

    if (charger.begin()) {
        Serial.println("PMIC initialized.");
    } else {
        Serial.println("PMIC failed to initialize.");
    }

    Serial.println("---");
    Serial.print("Test Case ID: ");
    Serial.println("TPL-ARD-PWRM-001-C33-ChargingState02-ReadBattery");
    
    Serial.println("Reading battery parameters...");
    
    float batteryVoltage = battery.voltage();
    Serial.println("Battery Voltage: " + String(batteryVoltage) + " V");
    
    float batteryVoltageFloor = floorVoltage(batteryVoltage, 0);
    Serial.println("Floor Battery Voltage: " + String(batteryVoltageFloor) + " V");
    
    float batterySetVoltage = floorVoltage(batteryVoltage, 2);
    Serial.println("Set Battery Voltage: " + String(batterySetVoltage) + " V");

    Serial.println("---");
    Serial.print("Test Case ID: ");
    Serial.println("TPL-ARD-PWRM-001-C33-ChargingState02-SetChargeParameters");
    
    Serial.println("Setting charger parameters...");
    charger.setChargeVoltage(batterySetVoltage);
    auto chargeVoltage = charger.getChargeVoltage();
    Serial.println("Final charge voltage: " + String(chargeVoltage) + " V");

    charger.setChargeCurrent(100);
    auto chargeCurrent = charger.getChargeCurrent();
    Serial.println("Charging current: " + String(chargeCurrent) + " mA");
    
    charger.setEndOfChargeCurrent(5);
    auto endOfChargeCurrent = charger.getEndOfChargeCurrent();
    Serial.println("End charge current: " + String(endOfChargeCurrent) + " mA");


    Serial.println("---");
    Serial.print("Test Case ID: ");
    Serial.println("TPL-ARD-PWRM-001-C33-ChargingState02-ChargeStatus");
    
    static ChargingState status = ChargingState::none;
    status = charger.getState();
    batteryVoltage = battery.voltage();

    while (batteryVoltage < chargeVoltage) {
        status = charger.getState();
        batteryVoltage = battery.voltage();
        Serial.println(getChargerState() + "- Battery Voltage: " + String(batteryVoltage) + " V");
        delay(5000);
    }

    if (batteryVoltage >= chargeVoltage) {
        Serial.println("Charging complete.");
        status = charger.getState();
        Serial.println("Final Charger Status: " + getChargerState());
        batteryVoltage = battery.voltage();
        Serial.println("Final Battery Voltage: " + String(batteryVoltage) + " V");
    }
    Serial.println("---");
    Serial.println("---");


    Serial.println();
    Serial.println("---------");
    Serial.print("End of Test Scenario ID: ");
    Serial.println(testScenarioID);
    Serial.println("---------");
    Serial.println();
}

void loop(){
    // Empty loop
}
Portenta C33 Output

---------
Test Scenario ID: TPL-ARD-PWRM-001-C33-ChargingState02
---------

---
Test Case ID: TPL-ARD-PWRM-001-C33-ChargingState02-Initialize
PMIC initialized.
---
Test Case ID: TPL-ARD-PWRM-001-C33-ChargingState02-ReadBattery
Reading battery parameters...
Battery Voltage: 4.20 V
Floor Battery Voltage: 4.18 V
Set Battery Voltage: 4.22 V
---
Test Case ID: TPL-ARD-PWRM-001-C33-ChargingState02-SetChargeParameters
Setting charger parameters...
Final charge voltage: 4.22 V
Charging current: 100 mA
End charge current: 5 mA
---
Test Case ID: TPL-ARD-PWRM-001-C33-ChargingState02-ChargeStatus
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.20 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.21 V
fast-charge constant voltage- Battery Voltage: 4.22 V
fast-charge constant voltage- Battery Voltage: 4.22 V
fast-charge constant voltage- Battery Voltage: 4.23 V
Charging complete.
Final Charger Status: fast-charge constant voltage
Final Battery Voltage: 4.22 V
---
---

---------
End of Test Scenario ID: TPL-ARD-PWRM-001-C33-ChargingState02
---------


Next steps:

  • I could not find a getChargeStatus() method inside the library. Should this be getState() instead?
  • Battery has final status of ChargingState::fastChargeConstantVoltage which should not be the case when it has reached terminal voltage. This should report that the charging has finished
  • The states mentioned in the docs should match the getState() method
    ChargingState Charger::getState(){
    uint8_t reg_val = PMIC.readPMICreg(Register::CHARGER_CHG_SNS);
    switch (extractBits(reg_val, 0, 3)) {
    case 0:
    return ChargingState::preCharge;
    case 1:
    return ChargingState::fastChargeConstantCurrent;
    case 2:
    return ChargingState::fastChargeConstantVoltage;
    case 3:
    return ChargingState::endOfCharge;
    case 4:
    return ChargingState::done;
    case 6:
    return ChargingState::timerFaultError;
    case 7:
    return ChargingState::thermistorSuspendError;
    case 8:
    return ChargingState::chargerDisabled;
    case 9:
    return ChargingState::batteryOvervoltageError;
    case 12:
    return ChargingState::chargerBypassed;
    default:
    return ChargingState::none;
    }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: documentationRelated to documentation for the projecttype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions