-
Notifications
You must be signed in to change notification settings - Fork 3
/
bq2040_capacity.c
188 lines (155 loc) · 5.39 KB
/
bq2040_capacity.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
/*
Program bq2040_capacity
Before running this program, attach your parallel port i2c plug
to the internal CMOS Flash on your battery controller PCB.
This is a delicate wiretapping operation.
The internal CMOS Flash is definitely NOT accessible using
the external connector of the battery.
Put together by Frank Rysanek <[email protected]>
Based heavily on the "eeprom" program by Chris <[email protected]>,
as distributed with the lm_sensors package.
*/
/*
This program is hereby placed into the public domain.
Of course the program is provided without warranty of any kind.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <linux/i2c-dev.h>
#define DEFAULT_I2C_BUS "/dev/i2c-0"
#define DEFAULT_EEPROM_ADDR 0x50 /* the 24C16 sits on i2c address 0x50 */
#define MAX_BYTES 8 /* max number of bytes to write in one piece */
/* write len bytes (stored in buf) to eeprom at address addr, page-offset offset */
/* if len=0 (buf may be NULL in this case) you can reposition the eeprom's read-pointer */
/* return 0 on success, -1 on failure */
int eeprom_write(int fd,
unsigned int addr,
unsigned int offset,
unsigned char *buf,
unsigned char len
){
struct i2c_rdwr_ioctl_data msg_rdwr;
struct i2c_msg i2cmsg;
int i;
char _buf[MAX_BYTES + 1];
if(len>MAX_BYTES){
fprintf(stderr,"I can only write MAX_BYTES bytes at a time!\n");
return -1;
}
if(len+offset >256){
fprintf(stderr,"Sorry, len(%d)+offset(%d) > 256 (page boundary)\n",
len,offset);
return -1;
}
_buf[0]=offset; /* _buf[0] is the offset into the eeprom page! */
for(i=0;i<len;i++) /* copy buf[0..n] -> _buf[1..n+1] */
_buf[1+i]=buf[i];
msg_rdwr.msgs = &i2cmsg;
msg_rdwr.nmsgs = 1;
i2cmsg.addr = addr;
i2cmsg.flags = 0;
i2cmsg.len = 1+len;
i2cmsg.buf = _buf;
if((i=ioctl(fd,I2C_RDWR,&msg_rdwr))<0){
perror("ioctl()");
fprintf(stderr,"ioctl returned %d\n",i);
return -1;
}
if(len>0)
fprintf(stderr,"Wrote %d bytes to eeprom at 0x%02x, offset %08x\n",
len,addr,offset);
else
fprintf(stderr,"Positioned pointer in eeprom at 0x%02x to offset %08x\n",
addr,offset);
return 0;
}
int main(int argc, char **argv){
/* filedescriptor and name of device */
int d;
int unlock=0;
char i = 0;
char *dn=DEFAULT_I2C_BUS;
unsigned char buf[3];
unsigned int addr=DEFAULT_EEPROM_ADDR;
int rwmode=0;
int capacity=1234;
while((i=getopt(argc,argv,"hc:d:u"))>=0){
switch(i){
case 'h':
fprintf(stderr,"\n");
fprintf(stderr,"The purpose of this util is to help during refurbishments of battery\n");
fprintf(stderr,"packs equipped with the BQ2040 gas gauge chip.\n");
fprintf(stderr,"\n");
fprintf(stderr,"You'll need the primitive parallel port I2C adapter described with\n");
fprintf(stderr,"the i2c-pport driver. Don't use this on the internal busses such as\n");
fprintf(stderr,"i2c-piix4 - you'd nuke your RAM DIMMs!!!\n");
fprintf(stderr,"\n");
fprintf(stderr,"Use this util to talk to the 24C01 flash of BQ2040 - NOT to the BQ2040 itself!\n");
fprintf(stderr,"I.e., you need to tap the flash chip's pins directly with a soldering iron.\n");
fprintf(stderr,"The external SMBUS of the battery is not the right one for this purpose!\n");
fprintf(stderr,"\n");
fprintf(stderr,"### Usage: ###\n");
fprintf(stderr,"\n");
fprintf(stderr," %s [-c capacity] [-d /dev/i2c-whatever] [-u]\n",argv[0]);
fprintf(stderr,"\n");
fprintf(stderr,"The default capacity (in mAh) is 1234, the default bus is i2c-0.\n");
fprintf(stderr,"Use the -u option to turn off the write protection of BQ2040.\n");
fprintf(stderr,"(seems no use, really, as the reset doesn't seem to work.)\n\n");
exit(1);
break;
case 'c':
if(sscanf(optarg,"%d",&capacity)!=1)
{
fprintf(stderr,"Cannot parse '%s' as new capacity in mAh.\n",
optarg);
exit(1);
}
break;
case 'd':
dn=optarg;
break;
case 'u':
unlock=1;
break;
default:
break;
}
}
if((d=open(dn,O_RDWR))<0){
fprintf(stderr,"Could not open i2c at %s\n",dn);
perror(dn);
exit(1);
}
fprintf(stderr,"i2c-devicenode is : %s\n",dn);
if (unlock == 1)
{
fprintf(stderr,"Removing the write-protect lock - writing 0x0000 at 0x3C...\n");
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x00;
if(eeprom_write(d,DEFAULT_EEPROM_ADDR,0x3C,buf,2)<0)
{
fprintf(stderr,"Failed to write the data at 0x3C. Exiting.\n");
exit(1);
}
fprintf(stderr,"Write operation successful.\n");
}
fprintf(stderr,"Desired new capacity: %d\n - writing it at 0x60...", capacity);
buf[0] = capacity & 0x000000FF;
buf[1] = (capacity & 0x0000FF00) >> 8;
buf[2] = 0;
if(eeprom_write(d,DEFAULT_EEPROM_ADDR,0x60,buf,2)<0)
{
fprintf(stderr,"Failed to write the new capacity at 0x60. Exiting.\n");
exit(1);
}
fprintf(stderr,"Write operation successful.\n");
close(d);
fprintf(stderr,"The write operation appears to have succeeded.\n");
exit(0);
}