Skip to content

Commit e51a021

Browse files
committed
nicla-system: Add option to configure safety timer.
1 parent 21a7c56 commit e51a021

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,22 @@ bool nicla::enableCharging(uint16_t mA)
150150
// Also sets the input current limit to 350mA.
151151
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_ILIM_UVLO_CTRL, 0x3F);
152152

153-
// Set safety timer to 9 hours (Bit 1+2 0b10) to give the battery enough of time to charge.
154-
// Set it to 0b11 to disable safety timers. See: Table 24 in the datasheet.
155-
uint8_t dpmTimerRegisterData = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_VIN_DPM);
156-
dpmTimerRegisterData |= 0b00000100; // Set Bit 2 to 1
157-
dpmTimerRegisterData &= 0b11111101; // Set Bit 1 to 0
153+
configureChargingSafetyTimer(ChargingSafetyTimerOption::NineHours);
154+
155+
return _pmic.getFastChargeControlRegister() == _fastChargeRegisterData;
156+
}
157+
158+
bool nicla::configureChargingSafetyTimer(ChargingSafetyTimerOption option){
159+
// See: Table 24 in the datasheet.
160+
// The two bits need to be shifted to skip the unused LSB.
161+
uint8_t timerValue = static_cast<uint8_t>(option) << 1;
162+
uint8_t dpmTimerRegisterData = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_VIN_DPM);
163+
164+
dpmTimerRegisterData &= 0b11111001; // Clear bits 1 and 2
165+
dpmTimerRegisterData |= timerValue; // Update bits 1 and 2
158166
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_VIN_DPM, dpmTimerRegisterData);
159167

160-
return _pmic.getFastChargeControlRegister() == _fastChargeRegisterData;
168+
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_VIN_DPM) == dpmTimerRegisterData;
161169
}
162170

163171
bool nicla::disableCharging()

libraries/Nicla_System/src/Nicla_System.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ enum class BatteryTemperature {
3737
Warm = 0b11
3838
};
3939

40+
enum class ChargingSafetyTimerOption {
41+
ThirtyMinutes = 0b00,
42+
ThreeHours = 0b01,
43+
NineHours = 0b10,
44+
Disabled = 0b11
45+
};
46+
47+
4048
class nicla {
4149

4250
public:
@@ -93,12 +101,23 @@ class nicla {
93101
* For example, a 200mAh battery could be charged at 100mA (0.1A).
94102
* This charging rate is generally safe for most LiPo batteries and provides a good balance between charging speed and battery longevity.
95103
* @note If your battery doesn't have an NTC thermistor, the charging speed will be limited to ~16mA.
96-
* @note There is a saftey timer that will stop the charging after 9 hours.
104+
* @note There is a saftey timer that will stop the charging after 9 hours by default.
105+
* This can be configured by calling configureChargingSafetyTimer().
97106
* @return true If the fast charging is enabled successfully. False, otherwise.
98107
* @see disableCharging()
99108
*/
100109
static bool enableCharging(uint16_t mA = 20);
101110

111+
/**
112+
* @brief Configures the charging safety timer after which the charging is stopped.
113+
* This is useful to prevent overcharging the battery. The timer can have one of the following options:
114+
* 30 minutes, 3 hours, 9 hours or disabled.
115+
*
116+
* @param option One of the following options: ThirtyMinutes, ThreeHours, NineHours or Disabled.
117+
* @return true if the charging safety timer is configured successfully, false otherwise.
118+
*/
119+
static bool configureChargingSafetyTimer(ChargingSafetyTimerOption option);
120+
102121
/**
103122
* @brief Disables charging of the battery. It can be resumed by calling enableCharging().
104123
*

0 commit comments

Comments
 (0)