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
36
namespace
PwrSupply
{
37
38
typedef
enum
{
40
PwrUsbOnly
= 0,
42
PwrBattOnly
= 1,
44
PwrConnected
= 2,
46
PwrBattDrained
= 3,
47
}
Supply
;
48
50
static
int
pwrStat
=
PwrUsbOnly
;
52
static
float
batVolt
= 0.0;
54
static
float
batPct
= 0.0;
56
static
int
charging
= 0;
57
62
int
isCharging
(
void
) {
63
return
charging
;
64
}
69
float
getBatPct
(
void
) {
70
return
batPct
;
71
}
72
77
float
getBatVolt
(
void
) {
78
return
batVolt
;
79
}
80
85
int
isUsbConnected
(
void
) {
86
return
(
pwrStat
==
PwrConnected
) ? 1 : 0;
87
}
88
93
int
isUsbOnly
(
void
) {
94
return
(
pwrStat
==
PwrUsbOnly
) ? 1 : 0;
95
}
96
101
int
isBatPowered
(
void
) {
102
return
(
pwrStat
==
PwrBattOnly
) ? 1 : 0;
103
}
104
109
int
isBatDrained
(
void
) {
110
return
(
pwrStat
==
PwrBattDrained
) ? 1 : 0;
111
}
112
116
void
reset
(
void
) {
117
pwrStat
=
PwrUsbOnly
;
118
}
119
123
void
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;
161
pwrStat
=
PwrUsbOnly
;
162
}
163
else
if
(adcV < 3.6) {
164
// battery voltage low
165
batPct
= 0.0;
166
batVolt
= adcV;
167
pwrStat
=
PwrConnected
;
168
}
169
else
{
170
// with / without battery, power connnected
171
batPct
= 100;
172
batVolt
= adcV;
173
pwrStat
=
PwrConnected
;
174
}
175
}
176
else
{
177
// USB not connected
178
if
(adcV <
MinBattV
) {
179
// for sure empty
180
batPct
= 0.0;
181
batVolt
= adcV;
182
pwrStat
=
PwrBattDrained
;
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;
203
pwrStat
=
PwrBattOnly
;
204
}
205
else
{
206
// full
207
batPct
= 100.0;
208
batVolt
= adcV;
209
pwrStat
=
PwrBattOnly
;
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.h
Debug log macros.
LOG
#define LOG(s)
Debug log inactive.
Definition
debug.h:37
DEBUG
#define DEBUG
Enable/disable debugging.
Definition
debug.h:27
LOGL
#define LOGL(s)
Debug log inactive.
Definition
debug.h:39
PwrSupply
Definition
pwrSupply.cpp:36
PwrSupply::getBatPct
float getBatPct(void)
Definition
pwrSupply.cpp:69
PwrSupply::isCharging
int isCharging(void)
Definition
pwrSupply.cpp:62
PwrSupply::batVolt
static float batVolt
Battery Voltage.
Definition
pwrSupply.cpp:52
PwrSupply::batPct
static float batPct
Battery charge %.
Definition
pwrSupply.cpp:54
PwrSupply::isBatPowered
int isBatPowered(void)
Definition
pwrSupply.cpp:101
PwrSupply::updateState
void updateState(void)
Definition
pwrSupply.cpp:123
PwrSupply::isUsbConnected
int isUsbConnected(void)
Definition
pwrSupply.cpp:85
PwrSupply::reset
void reset(void)
Definition
pwrSupply.cpp:116
PwrSupply::Supply
Supply
Definition
pwrSupply.cpp:38
PwrSupply::PwrConnected
@ PwrConnected
Powered by battery and connected to USB.
Definition
pwrSupply.cpp:44
PwrSupply::PwrBattDrained
@ PwrBattDrained
Battery drained.
Definition
pwrSupply.cpp:46
PwrSupply::PwrUsbOnly
@ PwrUsbOnly
Powered by USB only.
Definition
pwrSupply.cpp:40
PwrSupply::PwrBattOnly
@ PwrBattOnly
Powered by battery only.
Definition
pwrSupply.cpp:42
PwrSupply::getBatVolt
float getBatVolt(void)
Definition
pwrSupply.cpp:77
PwrSupply::isBatDrained
int isBatDrained(void)
Definition
pwrSupply.cpp:109
PwrSupply::isUsbOnly
int isUsbOnly(void)
Definition
pwrSupply.cpp:93
PwrSupply::pwrStat
static int pwrStat
Power supply status.
Definition
pwrSupply.cpp:50
PwrSupply::charging
static int charging
Charging state.
Definition
pwrSupply.cpp:56
pindefs.h
Pin definitions for Seeed XIAO nRF52840 2.9.2 with Adafruit bluefruit library.
PIN_HICHG
#define PIN_HICHG
Definition
pindefs.h:31
PIN_USB
#define PIN_USB
Definition
pindefs.h:29
PIN_CHG
#define PIN_CHG
Definition
pindefs.h:37
PIN_BATTV
#define PIN_BATTV
Definition
pindefs.h:35
MinBattV
#define MinBattV
Min voltage limit to protect battery.
Definition
pwrSupply.cpp:30
FAST_CHARGE
#define FAST_CHARGE
Enable fast charging (100mA)
Definition
pwrSupply.cpp:33
pwrSupply.h
Manage power supply status of the device.
pwrSupply.cpp
Generated by
1.9.8