Flying Silicon Sensor
pwrSupply.cpp
Go to the documentation of this file.
1
23#include <Arduino.h>
24
25#include "pwrSupply.h"
26#include "pindefs.h"
27#include "debug.h"
28
30#define MinBattV 3.6
31
33#define FAST_CHARGE 1
34
35
36namespace PwrSupply {
37
48
50static int pwrStat = PwrUsbOnly;
52static float batVolt = 0.0;
54static float batPct = 0.0;
56static int charging = 0;
57
62int isCharging(void) {
63 return charging;
64}
69float getBatPct(void) {
70 return batPct;
71}
72
77float getBatVolt(void) {
78 return batVolt;
79}
80
85int isUsbConnected(void) {
86 return (pwrStat == PwrConnected) ? 1 : 0;
87}
88
93int isUsbOnly(void) {
94 return (pwrStat == PwrUsbOnly) ? 1 : 0;
95}
96
101int isBatPowered(void) {
102 return (pwrStat == PwrBattOnly) ? 1 : 0;
103}
104
109int isBatDrained(void) {
110 return (pwrStat == PwrBattDrained) ? 1 : 0;
111}
112
116void reset(void) {
118}
119
123void updateState(void) {
124
125 // Battery sense pin
126 // BQ25100 circuit behaviour is not quite clear without battery:
127 // at 100mA: voltage toggles 3.7 - 4.1
128 // at 50mA: voltage low
129
130 // CHG pin logic
131 // Bat Usb Chg
132 // off on goes on / 1-3x when plugged in, then off
133 // on off off
134 // on on goes on when plugged in, then off if batt is full
135
136 // USB voltage Low <= 0.99V, High >= 2.31V
137 const int usbOn = digitalRead(PIN_USB);
138
139 // Charging indicator
140 charging = digitalRead(PIN_CHG) ? 0 : 1;
141
142 // Enable fast charge
143 // only if the chg pin indicates that charging is active == battery connected
144 if (FAST_CHARGE && usbOn && charging)
145 digitalWrite(PIN_HICHG, LOW); // on
146 else
147 digitalWrite(PIN_HICHG, HIGH); // off
148
149 // Battery level
150 // at 100mA this varies 3.7 - 4.1 when battery disconnected !
151 const int adcRead = analogRead(PIN_BATTV);
152 const float adcV = 2.4 * adcRead / 4096.0 * 1510.0 / 510.0;
153
154 if (usbOn) {
155 // USB connected
156 // no reliable percentage known
157 if (adcV < 3.0 && ! charging) {
158 // running without battery
159 batPct = 100.0;
160 batVolt = 0;
162 }
163 else if (adcV < 3.6) {
164 // battery voltage low
165 batPct = 0.0;
166 batVolt = adcV;
168 }
169 else {
170 // with / without battery, power connnected
171 batPct = 100;
172 batVolt = adcV;
174 }
175 }
176 else {
177 // USB not connected
178 if (adcV < MinBattV) {
179 // for sure empty
180 batPct = 0.0;
181 batVolt = adcV;
183 }
184 else if (adcV <= 4.2) {
185 // formula applicable
186 float levelPct = -4370.0 + (2105.0 * adcV) - (247.5144 * adcV * adcV);
187 // limit range to 0,100 !
188 levelPct = max(0.0, min(100.0, levelPct));
189
190 if (pwrStat != PwrBattOnly) {
191 // initialise percentage when starting to discharge
192 batPct = levelPct;
193 }
194 else if (batPct <= 0 || abs(batPct - levelPct) > 20.0) {
195 // big step (should not happen)
196 batPct = levelPct;
197 }
198 else {
199 // IIR filter to hide spurious small changes
200 batPct = batPct + (levelPct - batPct) * 0.05;
201 }
202 batVolt = adcV;
204 }
205 else {
206 // full
207 batPct = 100.0;
208 batVolt = adcV;
210 }
211 }
212
213 if (DEBUG) {
214 LOG("Power: ")
215 switch (pwrStat) {
216 case PwrUsbOnly: LOGL("USB Only")
217 break;
218 case PwrBattOnly: LOGL("Battery Only")
219 break;
220 case PwrConnected: LOGL("USB Connected")
221 break;
222 case PwrBattDrained: LOGL("Battery Drained")
223 break;
224 default: LOGL("?")
225 break;
226 }
227 LOG("Adc: ")
228 LOG(adcRead)
229 LOG(" ")
230 LOG(batVolt)
231 LOG(" V")
232 LOG(" ")
233 LOG(batPct)
234 LOGL(" %")
235 }
236}
237
238
239}
Debug log macros.
#define LOG(s)
Debug log inactive.
Definition debug.h:37
#define DEBUG
Enable/disable debugging.
Definition debug.h:27
#define LOGL(s)
Debug log inactive.
Definition debug.h:39
float getBatPct(void)
Definition pwrSupply.cpp:69
int isCharging(void)
Definition pwrSupply.cpp:62
static float batVolt
Battery Voltage.
Definition pwrSupply.cpp:52
static float batPct
Battery charge %.
Definition pwrSupply.cpp:54
int isBatPowered(void)
void updateState(void)
int isUsbConnected(void)
Definition pwrSupply.cpp:85
void reset(void)
@ PwrConnected
Powered by battery and connected to USB.
Definition pwrSupply.cpp:44
@ PwrBattDrained
Battery drained.
Definition pwrSupply.cpp:46
@ PwrUsbOnly
Powered by USB only.
Definition pwrSupply.cpp:40
@ PwrBattOnly
Powered by battery only.
Definition pwrSupply.cpp:42
float getBatVolt(void)
Definition pwrSupply.cpp:77
int isBatDrained(void)
int isUsbOnly(void)
Definition pwrSupply.cpp:93
static int pwrStat
Power supply status.
Definition pwrSupply.cpp:50
static int charging
Charging state.
Definition pwrSupply.cpp:56
Pin definitions for Seeed XIAO nRF52840 2.9.2 with Adafruit bluefruit library.
#define PIN_HICHG
Definition pindefs.h:31
#define PIN_USB
Definition pindefs.h:29
#define PIN_CHG
Definition pindefs.h:37
#define PIN_BATTV
Definition pindefs.h:35
#define MinBattV
Min voltage limit to protect battery.
Definition pwrSupply.cpp:30
#define FAST_CHARGE
Enable fast charging (100mA)
Definition pwrSupply.cpp:33
Manage power supply status of the device.