Skip to content

Commit 47f3026

Browse files
committed
nicla-system: Add API to set regulated battery voltage.
1 parent 36333e8 commit 47f3026

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,38 @@ float nicla::getRegulatedBatteryVoltage(){
181181

182182
}
183183

184+
void nicla::setRegulatedBatteryVoltage(float voltage){
185+
if (voltage < 3.6f){
186+
voltage = 3.6f;
187+
} else if (voltage > 4.2f) {
188+
voltage = 4.2f;
189+
}
190+
191+
// The formula is: VBATREG = 3.6 V + Sum(VBREG[Bit 7:1])
192+
193+
/*
194+
+---------+--------------------+
195+
| Bit | Regulation Voltage |
196+
+---------+--------------------+
197+
| 7 (MSB) | 640 mV |
198+
| 6 | 320 mV |
199+
| 5 | 160 mV |
200+
| 4 | 80 mV |
201+
| 3 | 40 mV |
202+
| 2 | 20 mV |
203+
| 1 | 10 mV |
204+
| 0 (LSB) | – |
205+
+---------+--------------------+
206+
*/
207+
208+
// Shift one bit to the left because the LSB is not used.
209+
uint16_t additionalMilliVolts = (voltage - 3.6f) * 100;
210+
uint8_t value = additionalMilliVolts << 1;
211+
// e.g. 4.2V - 3.6V = 0.6V * 100 = 60. 60 << 1 = 120 = 01111000
212+
213+
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATTERY_CTRL, value);
214+
}
215+
184216
float nicla::getCurrentBatteryVoltage(){
185217
return getRegulatedBatteryVoltage() / 100 * getBatteryPercentage();
186218
}
@@ -284,12 +316,9 @@ BatteryChargeLevel nicla::getBatteryChargeLevel() {
284316
}
285317
}
286318

287-
uint8_t nicla::getBatteryTemperature() {
288-
if(!_ntcEnabled) return BATTERY_TEMPERATURE_NORMAL;
319+
BatteryTemperature nicla::getBatteryTemperature() {
320+
if(!_ntcEnabled) return BatteryTemperature::Normal;
289321

290-
// Extract bits 5 and 6 (TS_FAULT0 and TS_FAULT1)
291-
uint8_t temperatureSenseFault = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL) >> 5 & 0b11;
292-
293322
/*
294323
+------+-------------------------------------------------------------+
295324
| Bits | Description |
@@ -300,18 +329,9 @@ uint8_t nicla::getBatteryTemperature() {
300329
| 11 | TWARM < TS temp < THOT (Charging voltage reduced by 140 mV) |
301330
+------+-------------------------------------------------------------+
302331
*/
303-
304-
if(temperatureSenseFault == 0){
305-
return BATTERY_TEMPERATURE_NORMAL;
306-
} else if (temperatureSenseFault == 1) {
307-
return BATTERY_TEMPERATURE_EXTREME;
308-
} else if (temperatureSenseFault == 2) {
309-
return BATTERY_TEMPERTURE_COOL;
310-
} else if (temperatureSenseFault == 3) {
311-
return BATTERY_TEMPERTURE_WARM;
312-
}
313-
314-
return BATTERY_TEMPERATURE_NORMAL;
332+
// Extract bits 5 and 6 (TS_FAULT0 and TS_FAULT1)
333+
uint8_t temperatureSenseFault = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL) >> 5 & 0b11;
334+
return static_cast<BatteryTemperature>(temperatureSenseFault);
315335
}
316336

317337

libraries/Nicla_System/src/Nicla_System.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ class nicla {
7070
*/
7171
static float getRegulatedBatteryVoltage();
7272

73+
/**
74+
* @brief Set the Regulated Battery Voltage.
75+
*
76+
* @param voltage The voltage in the range of 3.6V to 4.65V.
77+
*/
78+
static void setRegulatedBatteryVoltage(float voltage);
79+
7380
/**
7481
* @brief Get the Current Battery Voltage in Volts. This value is calculated by multiplying
7582
* the regulated voltage by the battery percentage.

0 commit comments

Comments
 (0)