-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbattery
executable file
·126 lines (99 loc) · 4.2 KB
/
battery
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
#!/bin/sh
# This program gets the battery info from PMU
# Voltage and current charging/discharging
#
# Nota : temperature can be more than real because of self heating
#######################################################################
# Copyright (c) 2014 by RzBo, Bellesserre, France
#
# Permission is granted to use the source code within this
# file in whole or in part for any use, personal or commercial,
# without restriction or limitation.
#
# No warranties, either explicit or implied, are made as to the
# suitability of this code for any purpose. Use at your own risk.
#######################################################################
#
# Installation:
# Place in /usr/bin/battery
# chmod 755 /usr/bin/battery
#######################################################################
# force ADC enable for battery voltage and current
/usr/sbin/i2cset -y -f 0 0x34 0x82 0xC3
################################
#read Power status register @00h
POWER_STATUS=$(/usr/sbin/i2cget -y -f 0 0x34 0x00)
#echo $POWER_STATUS
BAT_STATUS=$(($(($POWER_STATUS&0x02))/2)) # divide by 2 is like shifting rigth 1 times
#echo $(($POWER_STATUS&0x02))
#echo "BAT_STATUS="$BAT_STATUS
# echo $BAT_STATUS
################################
#read Power OPERATING MODE register @01h
POWER_OP_MODE=$(/usr/sbin/i2cget -y -f 0 0x34 0x01)
#echo $POWER_OP_MODE
CHARG_IND=$(($(($POWER_OP_MODE&0x40))/64)) # divide by 64 is like shifting rigth 6 times
#echo $(($POWER_OP_MODE&0x40))
#echo "CHARG_IND="$CHARG_IND
# echo $CHARG_IND
BAT_EXIST=$(($(($POWER_OP_MODE&0x20))/32)) # divide by 32 is like shifting rigth 5 times
#echo $(($POWER_OP_MODE&0x20))
#echo "BAT_EXIST="$BAT_EXIST
# echo $BAT_EXIST
################################
#read Charge control register @33h
CHARGE_CTL=$(/usr/sbin/i2cget -y -f 0 0x34 0x33)
#echo "CHARGE_CTL="$CHARGE_CTL
# echo $CHARGE_CTL
################################
#read Charge control register @34h
CHARGE_CTL2=$(/usr/sbin/i2cget -y -f 0 0x34 0x34)
#echo "CHARGE_CTL2="$CHARGE_CTL2
# echo $CHARGE_CTL2
################################
#read battery voltage 79h, 78h 0 mV -> 000h, 1.1 mV/bit FFFh -> 4.5045 V
BAT_VOLT_MSB=$(/usr/sbin/i2cget -y -f 0 0x34 0x78)
BAT_VOLT_LSB=$(/usr/sbin/i2cget -y -f 0 0x34 0x79)
#echo $BAT_VOLT_MSB $BAT_VOLT_LSB
# bash math -- converts hex to decimal so `bc` won't complain later...
# MSB is 8 bits, LSB is lower 4 bits
BAT_BIN=$(( $(($BAT_VOLT_MSB << 4)) | $(($(($BAT_VOLT_LSB & 0x0F)) )) ))
BAT_VOLT=$(echo "($BAT_BIN*1.1)"|bc)
#echo "Battery voltage = "$BAT_VOLT"mV"
###################
#read Battery Discharge Current 7Ch, 7Dh 0 mV -> 000h, 0.5 mA/bit 1FFFh -> 1800 mA
#AXP209 datasheet is wrong, discharge current is in registers 7Ch 7Dh
#13 bits
BAT_IDISCHG_MSB=$(/usr/sbin/i2cget -y -f 0 0x34 0x7C)
BAT_IDISCHG_LSB=$(/usr/sbin/i2cget -y -f 0 0x34 0x7D)
#echo $BAT_IDISCHG_MSB $BAT_IDISCHG_LSB
BAT_IDISCHG_BIN=$(( $(($BAT_IDISCHG_MSB << 5)) | $(($(($BAT_IDISCHG_LSB & 0x1F)) )) ))
BAT_IDISCHG=$(echo "($BAT_IDISCHG_BIN*0.5)"|bc)
#echo "Battery discharge current = "$BAT_IDISCHG"mA"
###################
#read Battery Charge Current 7Ah, 7Bh 0 mV -> 000h, 0.5 mA/bit FFFh -> 1800 mA
#AXP209 datasheet is wrong, charge current is in registers 7Ah 7Bh
#(12 bits)
BAT_ICHG_MSB=$(/usr/sbin/i2cget -y -f 0 0x34 0x7A)
BAT_ICHG_LSB=$(/usr/sbin/i2cget -y -f 0 0x34 0x7B)
#echo $BAT_ICHG_MSB $BAT_ICHG_LSB
BAT_ICHG_BIN=$(( $(($BAT_ICHG_MSB << 4)) | $(($(($BAT_ICHG_LSB & 0x0F)) )) ))
BAT_ICHG=$(echo "($BAT_ICHG_BIN*0.5)"|bc)
#echo "Battery charge current = "$BAT_ICHG"mA"
###################
#read internal temperature 5eh, 5fh -144.7c -> 000h, 0.1c/bit FFFh -> 264.8c
TEMP_MSB=$(/usr/sbin/i2cget -y -f 0 0x34 0x5e)
TEMP_LSB=$(/usr/sbin/i2cget -y -f 0 0x34 0x5f)
# bash math -- converts hex to decimal so `bc` won't complain later...
# MSB is 8 bits, LSB is lower 4 bits
TEMP_BIN=$(( $(($TEMP_MSB << 4)) | $(($(($TEMP_LSB & 0x0F)) )) ))
TEMP_C=$(echo "($TEMP_BIN*0.1-144.7)"|bc)
#echo "Internal temperature = "$TEMP_C"c"
###################
#read fuel gauge B9h
BAT_GAUGE_HEX=$(/usr/sbin/i2cget -y -f 0 0x34 0xb9)
# bash math -- converts hex to decimal so `bc` won't complain later...
# MSB is 8 bits, LSB is lower 4 bits
BAT_GAUGE_DEC=$(($BAT_GAUGE_HEX))
#echo "Battery gauge = "$BAT_GAUGE_DEC"%"
echo -e "Batt:"$BAT_GAUGE_DEC"%"