-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmonitor_battery.c
517 lines (510 loc) · 13.8 KB
/
monitor_battery.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* Copyright 2018 Frank Adams
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Release History:
// Rev 1.0 - Feb 14, 2018 - Original Release
// Rev 1.1 - March 7, 2018 - Added old_soc to keep previous value for testing
// Rev 1.2 - Nov 30 2018 - Added Apache License header
//
// Execute this program at startup so that it can monitor
// the battery state of charge every minute.
// At 10% SoC, the disk LED turns on to indicate a low battery warning.
// At 7% SoC, the LCD blinks off and on to get the users attention.
// At 5% SoC, a safe shutdown is executed.
//
// This program reads the laptop battery status registers over a bit-
// bang SMBus created with two of the Pi's GPIO pins and wiringPi.
// SMBus Data and clock are pulled to 3.3 volts with 3K resistors.
// Data is wired from Pi GPIO 19 to battery pin 3.
// Clock is wired from Pi GPIO 26 to battery pin 4.
//
// Add -l wiringPi to the Compile & Build and sudo to the execute per:
// https://learn.sparkfun.com/tutorials/raspberry-gpio/using-an-ide
//
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
// Pin number declarations
const int clock = 26; // SMBus clock on Pin 37, GPIO26
const int data = 19; // SMBus data on Pin 35, GPIO19
const int quarter = 10; // quarter period time in usec
// Global variables
int error = 0; // set to 1 when battery gives a NACK
// Functions
void go_z(int pin) // float the pin and let pullup or battery set level
{
pinMode(pin, INPUT); // set pin as input to tri-state the driver
}
//
void go_0(int pin) // drive the pin low
{
pinMode(pin, OUTPUT); // set pin as output
digitalWrite(pin, LOW); // drive pin low
}
//
int read_pin(int pin) // read the pin and return logic level
{
pinMode(pin, INPUT); // set pin as input
return (digitalRead(pin)); // return the logic level
}
//
void setupbus(void)
{
wiringPiSetupGpio(); //Init wiringPi using the Broadcom GPIO numbers
piHiPri(99); //Make program the highest priority (doesn't help)
go_z(clock); // set clock and data to inactive state
go_z(data);
delayMicroseconds(200); // wait before sending data
}
//
void startbus(void)
{
delayMicroseconds(1000); // needed when doing multiple reads
go_0(data); // start condition - data low when clock goes low
delayMicroseconds(quarter);
go_0(clock);
delayMicroseconds(4 * quarter); // wait 1 period before proceeding
}
//
void send8(int sendbits)
{
// send bit 7
if ((sendbits & 0x80) == 0x80) // check if bit 7 set
{
go_z(data); // send high
}
else
{
go_0(data); // send low
}
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// send bit 6
if ((sendbits & 0x40) == 0x40) // check if bit 6 set
{
go_z(data); // send high
}
else
{
go_0(data); // send low
}
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// send bit 5
if ((sendbits & 0x20) == 0x20) // check if bit 5 set
{
go_z(data); // send high
}
else
{
go_0(data); // send low
}
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// send bit 4
if ((sendbits & 0x10) == 0x10) // check if bit 4 set
{
go_z(data); // send high
}
else
{
go_0(data); // send low
}
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// send bit 3
if ((sendbits & 0x08) == 0x08) // check if bit 3 set
{
go_z(data); // send high
}
else
{
go_0(data); // send low
}
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// send bit 2
if ((sendbits & 0x04) == 0x04) // check if bit 2 set
{
go_z(data); // send high
}
else
{
go_0(data); // send low
}
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// send bit 1
if ((sendbits & 0x02) == 0x02) // check if bit 1 set
{
go_z(data); // send high
}
else
{
go_0(data); // send low
}
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// send bit 0
if ((sendbits & 0x01) == 0x01) // check if bit 0 set
{
go_z(data); // send high
}
else
{
go_0(data); // send low
}
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// ack/nack
delayMicroseconds(quarter * 4);
go_z(data); // float data to see ack
delayMicroseconds(quarter);
go_z(clock); // clock high
// read data to see if battery sends a low (acknowledge transfer)
if (read_pin(data))
{
error = 1; // battery did not acknowledge the transfer
}
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
go_0(data); // data low
delayMicroseconds(quarter * 90);
}
//
void sendrptstart(void) // send repeated start condition
{
go_z(data); // data high
delayMicroseconds(quarter * 8);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(data); // data low
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter * 16);
}
//
int read16(void) // read low and high bytes
{
int readval = 0x00; // initialize read word to zero
// bit 7 of low byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0080;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 6 of low byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0040;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 5 of low byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0020;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 4 of low byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0010;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 3 of low byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0008;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 2 of low byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0004;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 1 of low byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0002;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 0 of low byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0001;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// ack/nack of low byte
delayMicroseconds(quarter * 2);
go_0(data); // send ack back to battery
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
go_0(data); // data low
delayMicroseconds(quarter * 40);
// bit 7 of high byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x8000;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 6 of high byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x4000;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 5 of high byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x2000;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 4 of high byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x1000;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 3 of high byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0800;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 2 of high byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0400;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 1 of high byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0200;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// bit 0 of high byte
go_z(data);
delayMicroseconds(quarter);
if (read_pin(data))
{
readval = readval | 0x0100;
}
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
delayMicroseconds(quarter);
// ack/nack of high byte
delayMicroseconds(quarter * 2);
go_z(data); // send nack back to battery
delayMicroseconds(quarter);
go_z(clock); // clock high
delayMicroseconds(quarter * 2);
go_0(clock); // clock low
go_0(data); // data low
delayMicroseconds(quarter * 8);
return readval;
}
//
void stopbus(void) // stop condition, data low when clock goes high
{
go_z(clock); // clock high
delayMicroseconds(quarter);
go_z(data); // data high
delayMicroseconds(quarter * 30);
}
// Main program
int main(void)
{
delay(1000); // wait a second before starting
setupbus(); // setup the GPIO SMBus
int led_on = 0; // variable to keep track of when warning led is on
int soc; // variable to store the state of charge
int old_soc = 50; // soc from last time battery was checked
unsigned short bat_stat; // variable to store the battery status
while(1) // infinite loop
{
// Read Battery status to see if charger is plugged in
error = 0; // initialize to no error
startbus(); // send start condition
send8(0x16); // send battery address 0x16 (0x0b w/ write)
send8(0x16); // load register pointer 0x16
sendrptstart(); // send repeated start codition
send8(0x17); // send battery address 0x17 (0x0b w/ read)
bat_stat = read16();
stopbus(); // send stop condition
if ((bat_stat == 0xffff) | (error))// read again if all 1's or nack
{
error = 0; // initialize to no error
startbus(); // send start condition
send8(0x16); // send battery address 0x16 (0x0b w/ write)
send8(0x16); // load register pointer 0x16
sendrptstart(); // send repeated start codition
send8(0x17); // send battery address 0x17 (0x0b w/ read)
bat_stat = read16();
stopbus(); // send stop condition
}
// Only proceed with reading the SoC if discharge bit is set
if ((bat_stat & 0x0040) == 0x0040)
{
// Read Battery Relative State of Charge
error = 0; // initialize to no error
startbus(); // send start condition
send8(0x16); // send battery address 0x16 (0x0b w/ write)
send8(0x0d); // load soc register pointer 0x0d
sendrptstart(); // send repeated start codition
send8(0x17); // send battery address 0x17 (0x0b w/ read)
soc = read16(); // read soc low & high bytes
stopbus(); // send stop condition
if ((soc >= 150) | (error))//check if out of range or any nack's
{ // try again
startbus(); // send start condition
send8(0x16); // send battery address 0x16 (0x0b w/ write)
send8(0x0d); // load register pointer 0x0d
sendrptstart(); // send repeated start codition
send8(0x17); // send battery address 0x17 (0x0b w/ read)
soc = read16(); //read low & high bytes
stopbus(); // send stop condition
}
// Check the battery State of Charge for the following:
// <= 5% causes a safe shutdown (must have been <= 8% on last check).
// <= 7% causes the display to blink (must have been <= 10% on last check).
// <= 10% turns on the disk LED as a warning (must have been <= 13% on last check).
// Keeping track of the old soc is done in case there is a bad smbus read
if ((soc <= 5) & (old_soc <= 8)) // check for shutdown condition
{
system("sudo shutdown -h now"); // safe shutdown of Pi
// note that a systemd unit file sends
// i2cset -y 1 0x08 0x00 0x5a
// which commands the Teensy to turn off the power
}
else if ((soc <= 7) & (old_soc <= 10)) // check for blink display condition
{
system("i2cset -y 1 0x08 0x00 0xe2"); // blink the display
}
else if ((soc <= 10) & (old_soc <= 13) & (!led_on)) // soc at 10% and LED is off
{
system("i2cset -y 1 0x08 0x00 0x10"); // turn on disk LED
led_on = 0x01; // variable shows led is turned on
}
old_soc = soc; // save soc for next time
}
else if (led_on) // charger plugged in and LED is on
{
system("i2cset -y 1 0x08 0x00 0x11"); // turn off disk LED
led_on = 0x00; // variable shows led is turned off
}
delay(60000); // wait 60 seconds before repeating the loop
}
return 0;
}