Skip to content

Commit ba9ab9f

Browse files
committed
Revamp charger
1 parent d418cfa commit ba9ab9f

File tree

3 files changed

+118
-123
lines changed

3 files changed

+118
-123
lines changed

Diff for: examples/Charger/Charger.ino

+37-53
Original file line numberDiff line numberDiff line change
@@ -26,58 +26,49 @@ This sketch demonstrates how to write charging parameters,read charger state and
2626
- Set the baud rate to 115200.
2727
- You will see the sketch continuously printing battery status and charger state.
2828
*/
29-
#include "PowerManagement.h"
29+
#include "Arduino_PowerManagement.h"
3030

31-
PowerManagement manager;
3231
Battery battery;
3332
Charger charger;
3433

34+
// Charge current in mA, a safe value for most batteries is half the battery capacity
35+
constexpr int CHARGE_CURRENT_MA = 100; // mA
36+
constexpr int END_OF_CHARGE_CURRENT_MA = 5; // mA
37+
3538
void setup() {
3639
Serial.begin(115200);
3740
// Wait for Serial to be ready with a timeout of 5 seconds
3841
for (auto start = millis(); !Serial && millis() - start < 5000;);
42+
delay(1000); // Delay to give time to load the Serial Monitor
43+
44+
charger = Charger();
45+
if(!charger.begin()){
46+
Serial.println("Charger initialization failed.");
47+
while (true);
48+
}
49+
50+
Serial.print("* ✅ Charging is enabled: ");
51+
Serial.println(charger.isEnabled() ? "true" : "false");
3952

40-
manager = PowerManagement();
41-
manager.begin();
53+
auto chargeCurrent = charger.getChargeCurrent();
54+
auto chargeVoltage = charger.getChargeVoltage();
55+
auto endOfChargeCurrent = charger.getEndOfChargeCurrent();
56+
auto inputCurrentLimit = charger.getInputCurrentLimit();
4257

43-
battery = manager.getBattery();
44-
charger = manager.getCharger();
58+
Serial.println("* ⚡️ Charge current: " + String(chargeCurrent) + " mA");
59+
Serial.println("* ⚡️ Charge voltage: " + String(chargeVoltage) + " V");
60+
Serial.println("* ⚡️ End of charge current: " + String(endOfChargeCurrent) + " mA");
61+
Serial.println("* ⚡️ Input current limit: " + String(inputCurrentLimit) + " mA");
4562

46-
Serial.print("Charging is enabled: ");
47-
Serial.println(charger.isEnabled() ? "true" : "false");
63+
if (!charger.setChargeCurrent(CHARGE_CURRENT_MA)){
64+
Serial.println("Failed to set charge current");
65+
Serial.println("Please double check the supported values in the documentation");
66+
}
4867

49-
Serial.print("Charge current: ");
50-
Serial.println(charger.getChargeCurrent());
51-
52-
Serial.print("Charge voltage: ");
53-
Serial.println(charger.getChargeVoltage());
54-
55-
Serial.print("End of charge current: ");
56-
Serial.println(charger.getEndOfChargeCurrent());
57-
58-
Serial.print("Input current limit: ");
59-
Serial.println(charger.getInputCurrentLimit());
60-
61-
// The following charger settings are not supported on Nicla Vision
62-
#if !defined(ARDUINO_NICLA_VISION)
63-
if (!charger.setChargeCurrent(0.2))
64-
{
65-
Serial.println("Failed to set charge current");
66-
Serial.println("Please double check the supported values in the documentation");
67-
}
68-
69-
if (!charger.setChargeVoltage(3.8))
70-
{
71-
Serial.println("Failed to set charge voltage");
72-
Serial.println("Please double check the supported values in the documentation");
73-
}
74-
75-
if (!charger.setEndOfChargeCurrent(0.005))
76-
{
77-
Serial.println("Failed to set end of charge current");
78-
Serial.println("Please double check the supported values in the documentation");
79-
}
80-
#endif
68+
if (!charger.setEndOfChargeCurrent(END_OF_CHARGE_CURRENT_MA)){
69+
Serial.println("Failed to set end of charge current");
70+
Serial.println("Please double check the supported values in the documentation");
71+
}
8172
}
8273

8374
String getChargerState(){
@@ -121,20 +112,13 @@ String getChargerState(){
121112
}
122113

123114
void loop(){
124-
ChargingState status = charger.getState();
125-
126-
Serial.print("* Voltage: ");
127-
Serial.println(String(battery.voltage()) + "mV");
115+
static ChargingState status = ChargingState::None;
128116

129-
Serial.print("* Current: ");
130-
Serial.println(String(battery.current()) + " mA");
131-
132-
Serial.print("* Percentage: ");
133-
Serial.println(String(battery.percentage()) + "%");
134-
135-
Serial.print("* Charger state: ");
136-
Serial.println(getChargerState());
117+
if (status != charger.getState()) {
118+
status = charger.getState();
119+
Serial.print("* 👀 Charger state: ");
120+
Serial.println(getChargerState());
121+
}
137122

138-
Serial.println();
139123
delay(1000);
140124
}

Diff for: src/Charger.cpp

+61-54
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#include "Charger.h"
22
#include <map>
33

4-
std::map<float, ChargeCurrent> chargeCurrentMap = {
5-
{0.1, ChargeCurrent::I_100_mA},
6-
{0.15, ChargeCurrent::I_150_mA},
7-
{0.2, ChargeCurrent::I_200_mA},
8-
{0.25, ChargeCurrent::I_250_mA},
9-
{0.3, ChargeCurrent::I_300_mA},
10-
{0.35, ChargeCurrent::I_350_mA},
11-
{0.4, ChargeCurrent::I_400_mA},
12-
{0.45, ChargeCurrent::I_450_mA},
13-
{0.5, ChargeCurrent::I_500_mA},
14-
{0.55, ChargeCurrent::I_550_mA},
15-
{0.6, ChargeCurrent::I_600_mA},
16-
{0.65, ChargeCurrent::I_650_mA},
17-
{0.7, ChargeCurrent::I_700_mA},
18-
{0.75, ChargeCurrent::I_750_mA},
19-
{0.8, ChargeCurrent::I_800_mA},
20-
{0.85, ChargeCurrent::I_850_mA},
21-
{0.9, ChargeCurrent::I_900_mA},
22-
{0.95, ChargeCurrent::I_950_mA},
23-
{1.0, ChargeCurrent::I_1000_mA}
4+
std::map<uint16_t, ChargeCurrent> chargeCurrentMap = {
5+
{100, ChargeCurrent::I_100_mA},
6+
{150, ChargeCurrent::I_150_mA},
7+
{200, ChargeCurrent::I_200_mA},
8+
{250, ChargeCurrent::I_250_mA},
9+
{300, ChargeCurrent::I_300_mA},
10+
{350, ChargeCurrent::I_350_mA},
11+
{400, ChargeCurrent::I_400_mA},
12+
{450, ChargeCurrent::I_450_mA},
13+
{500, ChargeCurrent::I_500_mA},
14+
{550, ChargeCurrent::I_550_mA},
15+
{600, ChargeCurrent::I_600_mA},
16+
{650, ChargeCurrent::I_650_mA},
17+
{700, ChargeCurrent::I_700_mA},
18+
{750, ChargeCurrent::I_750_mA},
19+
{800, ChargeCurrent::I_800_mA},
20+
{850, ChargeCurrent::I_850_mA},
21+
{900, ChargeCurrent::I_900_mA},
22+
{950, ChargeCurrent::I_950_mA},
23+
{1000, ChargeCurrent::I_1000_mA}
2424
};
2525

2626
std::map<float, ChargeVoltage> chargeVoltageMap = {
@@ -74,36 +74,36 @@ std::map<float, ChargeVoltage> chargeVoltageMap = {
7474
{4.44, ChargeVoltage::V_4_44}
7575
};
7676

77-
std::map<float, EndOfChargeCurrent> endOfChargeCurrentMap = {
78-
{0.005, EndOfChargeCurrent::I_5_mA},
79-
{0.01, EndOfChargeCurrent::I_10_mA},
80-
{0.02, EndOfChargeCurrent::I_20_mA},
81-
{0.03, EndOfChargeCurrent::I_30_mA},
82-
{0.05, EndOfChargeCurrent::I_50_mA}
77+
std::map<uint16_t, EndOfChargeCurrent> endOfChargeCurrentMap = {
78+
{5, EndOfChargeCurrent::I_5_mA},
79+
{10, EndOfChargeCurrent::I_10_mA},
80+
{20, EndOfChargeCurrent::I_20_mA},
81+
{30, EndOfChargeCurrent::I_30_mA},
82+
{50, EndOfChargeCurrent::I_50_mA}
8383
};
8484

85-
std::map<float, InputCurrentLimit> inputCurrentLimitMap = {
86-
{0.01, InputCurrentLimit::I_10_mA},
87-
{0.015, InputCurrentLimit::I_15_mA},
88-
{0.02, InputCurrentLimit::I_20_mA},
89-
{0.025, InputCurrentLimit::I_25_mA},
90-
{0.03, InputCurrentLimit::I_30_mA},
91-
{0.035, InputCurrentLimit::I_35_mA},
92-
{0.04, InputCurrentLimit::I_40_mA},
93-
{0.045, InputCurrentLimit::I_45_mA},
94-
{0.05, InputCurrentLimit::I_50_mA},
95-
{0.1, InputCurrentLimit::I_100_mA},
96-
{0.15, InputCurrentLimit::I_150_mA},
97-
{0.2, InputCurrentLimit::I_200_mA},
98-
{0.3, InputCurrentLimit::I_300_mA},
99-
{0.4, InputCurrentLimit::I_400_mA},
100-
{0.5, InputCurrentLimit::I_500_mA},
101-
{0.6, InputCurrentLimit::I_600_mA},
102-
{0.7, InputCurrentLimit::I_700_mA},
103-
{0.8, InputCurrentLimit::I_800_mA},
104-
{0.9, InputCurrentLimit::I_900_mA},
105-
{1.0, InputCurrentLimit::I_1000_mA},
106-
{1.5, InputCurrentLimit::I_1500_mA}
85+
std::map<uint16_t, InputCurrentLimit> inputCurrentLimitMap = {
86+
{10, InputCurrentLimit::I_10_mA},
87+
{15, InputCurrentLimit::I_15_mA},
88+
{20, InputCurrentLimit::I_20_mA},
89+
{25, InputCurrentLimit::I_25_mA},
90+
{30, InputCurrentLimit::I_30_mA},
91+
{35, InputCurrentLimit::I_35_mA},
92+
{40, InputCurrentLimit::I_40_mA},
93+
{45, InputCurrentLimit::I_45_mA},
94+
{50, InputCurrentLimit::I_50_mA},
95+
{100, InputCurrentLimit::I_100_mA},
96+
{150, InputCurrentLimit::I_150_mA},
97+
{200, InputCurrentLimit::I_200_mA},
98+
{300, InputCurrentLimit::I_300_mA},
99+
{400, InputCurrentLimit::I_400_mA},
100+
{500, InputCurrentLimit::I_500_mA},
101+
{600, InputCurrentLimit::I_600_mA},
102+
{700, InputCurrentLimit::I_700_mA},
103+
{800, InputCurrentLimit::I_800_mA},
104+
{900, InputCurrentLimit::I_900_mA},
105+
{1000, InputCurrentLimit::I_1000_mA},
106+
{1500, InputCurrentLimit::I_1500_mA}
107107
};
108108

109109

@@ -113,7 +113,11 @@ bool Charger::begin(){
113113
return PMIC.begin() == 0;
114114
}
115115

116-
bool Charger::setChargeCurrent(float current) {
116+
bool Charger::setChargeCurrent(uint16_t current) {
117+
#if defined(ARDUINO_NICLA_VISION)
118+
return false; // Not supported on Nicla Vision
119+
#endif
120+
117121
if (chargeCurrentMap.find(current) != chargeCurrentMap.end()) {
118122
ChargeCurrent convertedCurrent = chargeCurrentMap[current];
119123
PMIC.getControl() -> setFastChargeCurrent(convertedCurrent);
@@ -122,7 +126,7 @@ bool Charger::setChargeCurrent(float current) {
122126
return false;
123127
}
124128

125-
float Charger::getChargeCurrent() {
129+
uint16_t Charger::getChargeCurrent() {
126130
auto currentValue = PMIC.readPMICreg(Register::CHARGER_CHG_CURR_CFG);
127131
currentValue = (currentValue & REG_CHG_CURR_CFG_CHG_CC_mask);
128132
ChargeCurrent current = static_cast<ChargeCurrent>(currentValue);
@@ -157,7 +161,10 @@ bool Charger::setChargeVoltage(float voltage) {
157161
return false;
158162
}
159163

160-
bool Charger::setEndOfChargeCurrent(float current) {
164+
bool Charger::setEndOfChargeCurrent(uint16_t current) {
165+
#if defined(ARDUINO_NICLA_VISION)
166+
return false; // Not supported on Nicla Vision
167+
#endif
161168
if(endOfChargeCurrentMap.find(current) != endOfChargeCurrentMap.end()) {
162169
EndOfChargeCurrent convertedCurrent = endOfChargeCurrentMap[current];
163170
PMIC.getControl() -> setEndOfChargeCurrent(convertedCurrent);
@@ -166,7 +173,7 @@ bool Charger::setEndOfChargeCurrent(float current) {
166173
return false;
167174
}
168175

169-
float Charger::getEndOfChargeCurrent() {
176+
uint16_t Charger::getEndOfChargeCurrent() {
170177
uint8_t currentValue = PMIC.readPMICreg(Register::CHARGER_CHG_EOC_CNFG);
171178
currentValue = (currentValue & REG_CHG_EOC_CNFG_IEOC_mask);
172179
EndOfChargeCurrent current = static_cast<EndOfChargeCurrent>(currentValue);
@@ -178,7 +185,7 @@ float Charger::getEndOfChargeCurrent() {
178185
return -1;
179186
}
180187

181-
bool Charger::setInputCurrentLimit(float current) {
188+
bool Charger::setInputCurrentLimit(uint16_t current) {
182189
if(inputCurrentLimitMap.find(current) != inputCurrentLimitMap.end()) {
183190
InputCurrentLimit convertedCurrent = inputCurrentLimitMap[current];
184191
PMIC.getControl() -> setInputCurrentLimit(convertedCurrent);
@@ -187,7 +194,7 @@ bool Charger::setInputCurrentLimit(float current) {
187194
return false;
188195
}
189196

190-
float Charger::getInputCurrentLimit() {
197+
uint16_t Charger::getInputCurrentLimit() {
191198
uint8_t currentValue = PMIC.readPMICreg(Register::CHARGER_VBUS_INLIM_CNFG);
192199
currentValue = (currentValue & REG_VBUS_INLIM_CNFG_VBUS_LIN_INLIM_mask);
193200
InputCurrentLimit current = static_cast<InputCurrentLimit>(currentValue);

Diff for: src/Charger.h

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#ifndef CHARGER_H
2+
13
#include <Arduino_PF1550.h>
2-
#include "wireUtils.h"
4+
#include "WireUtils.h"
35

46
typedef VFastCharge ChargeVoltage;
57
typedef IFastCharge ChargeCurrent;
@@ -97,18 +99,18 @@ class Charger {
9799

98100
/**
99101
* @brief Set the charging current.
100-
* The default charging current is set to 0.1A.
101-
* @param current Charging current in amperes (A).
102-
* Supported values: 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0
102+
* The default charging current is set to 100mA.
103+
* @param current Charging current in milli amperes (mA).
104+
* Supported values: 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000
103105
* @return True if successful, false if an invalid value was provided or if the PMIC communication failed.
104106
*/
105-
bool setChargeCurrent(float current);
107+
bool setChargeCurrent(uint16_t current);
106108

107109
/**
108-
* @brief Get the charge current in amperes (A).
110+
* @brief Get the charge current in milli amperes (mA).
109111
* @return The charge current in float.
110112
*/
111-
float getChargeCurrent();
113+
uint16_t getChargeCurrent();
112114

113115
/**
114116
* @brief Set the charging voltage in volts (V).
@@ -130,11 +132,11 @@ class Charger {
130132
/**
131133
* @brief Set the end-of-charge current.
132134
* The default end-of-charge current is set to 0.05A.
133-
* @param current End-of-charge current in amperes (A).
134-
* Supported values: 0.005, 0.01, 0.02, 0.03, 0.05
135+
* @param current End-of-charge current in milli amperes (mA).
136+
* Supported values: 5, 10, 20, 30, 50
135137
* @return True if successful, false if an invalid value was provided or if the PMIC communication failed.
136138
*/
137-
bool setEndOfChargeCurrent(float current);
139+
bool setEndOfChargeCurrent(uint16_t current);
138140

139141
/**
140142
* @brief Get the end of charge current.
@@ -144,19 +146,19 @@ class Charger {
144146
*
145147
* @return The end of charge current.
146148
*/
147-
float getEndOfChargeCurrent();
149+
uint16_t getEndOfChargeCurrent();
148150

149151
/**
150152
* @brief The input current limit (ILIM) safeguards the device by preventing overcurrent,
151153
* ensuring the charging current is within safe levels for the battery, and adapting to the maximum
152154
* current the power source can provide, allowing you to charge and use the system at the same time.
153155
* The default input current limit is set to 1.5A.
154-
* @param current Maximum input current in amperes (A).
155-
* Supported values: 0.01, 0.015, 0.02, 0.025, 0.03, 0.035, 0.04, 0.045, 0.05, 0.1, 0.15, 0.2, 0.3,
156-
* 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.5
156+
* @param current Maximum input current in milli amperes (mA).
157+
* Supported values: 10, 15, 20, 25, 30, 35, 40, 45, 50, 100, 150, 200, 300,
158+
* 400, 500, 600, 700, 800, 900, 1000, 1500
157159
* @return True if successful, false if an invalid value was provided or if the PMIC communication failed.
158160
*/
159-
bool setInputCurrentLimit(float current);
161+
bool setInputCurrentLimit(uint16_t current);
160162

161163
/**
162164
* @brief Get the input current limit. It is a safeguard to prevent overcurrent when charging
@@ -166,7 +168,7 @@ class Charger {
166168
*
167169
* @return The input current limit in amps.
168170
*/
169-
float getInputCurrentLimit();
171+
uint16_t getInputCurrentLimit();
170172

171173
/**
172174
* @brief Get the current charging status.
@@ -189,3 +191,5 @@ class Charger {
189191
*/
190192
bool setEnabled(bool enabled);
191193
};
194+
195+
#endif // CHARGER_H

0 commit comments

Comments
 (0)