-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpb-1600.cpp
407 lines (333 loc) · 11.9 KB
/
rpb-1600.cpp
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
#include "rpb-1600.h"
#include "rpb-1600-commands.h"
#include "Wire.h"
//----------------------------------------------------------------------
// Public Functions
//----------------------------------------------------------------------
RPB_1600::RPB_1600()
{
}
bool RPB_1600::Init(uint8_t chargerAddress)
{
my_charger_address = chargerAddress;
Wire.setClock(100000);
Wire.begin();
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Init complete!\n");
#endif
return true;
}
bool RPB_1600::getReadings(readings *data)
{
if (!readWithCommand(CMD_CODE_READ_VIN, CMD_LENGTH_READ_VIN))
{
return false;
}
data->v_in = parseLinearData();
if (!readWithCommand(CMD_CODE_READ_VOUT, CMD_LENGTH_READ_VOUT))
{
return false;
}
data->v_out = parseLinearVoltage(CMD_N_VALUE_READ_VOUT);
if (!readWithCommand(CMD_CODE_READ_IOUT, CMD_LENGTH_READ_IOUT))
{
return false;
}
data->i_out = parseLinearData();
if (!readWithCommand(CMD_CODE_READ_FAN_SPEED_1, CMD_LENGTH_READ_FAN_SPEED_1))
{
return false;
}
data->fan_speed_1 = parseLinearData();
if (!readWithCommand(CMD_CODE_READ_FAN_SPEED_2, CMD_LENGTH_READ_FAN_SPEED_2))
{
return false;
}
data->fan_speed_2 = parseLinearData();
return true;
}
bool RPB_1600::getChargeStatus(charge_status *status)
{
if (!readWithCommand(CMD_CODE_CHG_STATUS, CMD_LENGTH_CHG_STATUS))
{
return false;
}
parseChargeStatus(status);
return true;
}
bool RPB_1600::getCurveParams(curve_parameters *params)
{
/* Query the charger for all charge curve related commands, and populate the following items:
params->cc;
params->cv;
params->floating_voltage;
params->taper_current;
params->config;
params->cc_timeout;
params->cv_timeout;
params->status; */
if (!readWithCommand(CMD_CODE_CURVE_CC, CMD_LENGTH_CURVE_CC))
{
return false;
}
params->cc = parseLinearData();
if (!readWithCommand(CMD_CODE_CURVE_CV, CMD_LENGTH_CURVE_CV))
{
return false;
}
params->cv = parseLinearVoltage(CMD_N_VALUE_CURVE_CV);
if (!readWithCommand(CMD_CODE_CURVE_FV, CMD_LENGTH_CURVE_FV))
{
return false;
}
params->floating_voltage = parseLinearVoltage(CMD_N_VALUE_CURVE_FV);
if (!readWithCommand(CMD_CODE_CURVE_TC, CMD_LENGTH_CURVE_TC))
{
return false;
}
params->taper_current = parseLinearData();
if (!readWithCommand(CMD_CODE_CURVE_CONFIG, CMD_LENGTH_CURVE_CONFIG))
{
return false;
}
parseCurveConfig(¶ms->config);
if (!readWithCommand(CMD_CODE_CURVE_CC_TIMEOUT, CMD_LENGTH_CURVE_CC_TIMEOUT))
{
return false;
}
params->cc_timeout = parseLinearData();
if (!readWithCommand(CMD_CODE_CURVE_CV_TIMEOUT, CMD_LENGTH_CURVE_CV_TIMEOUT))
{
return false;
}
params->cv_timeout = parseLinearData();
if (!readWithCommand(CMD_CODE_CURVE_FLOAT_TIMEOUT, CMD_LENGTH_CURVE_FLOAT_TIMEOUT))
{
return false;
}
params->float_timeout = parseLinearData();
getChargeStatus(¶ms->status);
return true;
}
bool RPB_1600::readWithCommand(uint8_t commandID, uint8_t receiveLength)
{
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Attempting to read command 0x%x with length %d\n", commandID, receiveLength);
#endif
// Send the command ID to the charger, and don't terminate the transmission
Wire.beginTransmission(my_charger_address);
Wire.write(commandID); // Write to I2C Tx buffer
Wire.endTransmission(false);
// Request bytes from the device
Wire.beginTransmission(my_charger_address);
Wire.requestFrom(my_charger_address, receiveLength);
clearRXBuffer();
// Count the number of bytes we receive
uint8_t num_bytes = 0;
// Pull bytes from the internal i2c RX buffer
while (Wire.available())
{
// Write the received byte to the buffer
my_rx_buffer[num_bytes] = Wire.read();
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Received byte %d with value 0x%x\n", num_bytes, my_rx_buffer[num_bytes]);
#endif
// Make sure we aren't getting more bytes then we expect
if (++num_bytes > MAX_RECEIVE_BYTES)
{
return false;
}
}
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Total bytes received: %d\n", num_bytes);
Serial.printf("<RPB-1600 DEBUG> RX Buffer: [");
for (int i = 0; i < num_bytes; i++)
{
Serial.printf("0x%x,", my_rx_buffer[i]);
}
Serial.printf("]\n");
#endif
return num_bytes == receiveLength;
}
bool RPB_1600::writeTwoBytes(uint8_t commandID, uint8_t *data)
{
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Attempting to write 0x%x (low) and 0x%x (high) with command 0x%x\n", data[0], data[1], commandID);
#endif
Wire.beginTransmission(my_charger_address);
Wire.write(commandID);
uint8_t bytes_written = Wire.write(data, 2);
Wire.endTransmission();
return bytes_written == 2;
}
bool RPB_1600::writeLinearDataCommand(uint8_t commandID, int8_t N, int16_t value)
{
uint16_t Y = 0;
uint16_t denominator = 0x0001;
// The Y value is calculated using the following equation
// (reference PMBUS spec rev 1.1 section 7.1): Value = Y * 2 ^ N
// Y = Value / (2 ^ N)
// We'll calculate denominator and do the math based on the polarity of N
if (N > 0)
{
denominator <<= N;
Y = value / denominator;
}
else if (N < 0)
{
denominator <<= (-N);
Y = value * denominator;
}
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Y calculation: Value = %d Denom = %d Y = %d\n", value, denominator, Y);
#endif
return writeLinearDataHelper(commandID, N, Y);
}
//----------------------------------------------------------------------
// Private Functions
//----------------------------------------------------------------------
bool RPB_1600::writeLinearDataHelper(uint8_t commandID, int8_t N, int16_t Y)
{
// Make sure the N value isn't bigger then 5 bits
if (abs(N) > 15)
{
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> N value too large! Can't convert to linear format. N = %d\n", N);
#endif
return false;
}
// Make sure the Y (Mantissa) can fit in 11 bits
if (abs(Y) > 2047)
{
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Mantissa (Y) value too large! Can't convert to linear format. Y = %d\n", Y);
#endif
return false;
}
// The goal here is to truncate Y to 11 bits, and put those in the lowest 11 bits of the outgoing data
// Then we need to truncate N to 5 bits, and put those bits in the highest 5 bits of the outgoing data
// See Section 7.1 of the PMBus 1.1 specification for more info on the "Linear Data Format"
// Note that data[0] is the low byte (sent first) and data[1] is the high byte (sent second)
uint8_t data[2] = {0x00, 0x00};
// Mask the lowest 8 bits of the mantissa, and put those bits in the low byte of the outgoing data
data[0] = Y & 0x00FF;
// Mask the lowest 3 bits of the high byte of the mantissa, and put those bits in the high byte of the outgoing data
data[1] = (Y & 0x0700) >> 8;
// Mask the lowest 5 bits of N, and shift them to be the highest 5 bytes of the high byte
data[1] = (N & 0x1F) << 3;
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Attempting to write linear data with N = %d and Mantissa (Y) = %d\n", N, Y);
#endif
writeTwoBytes(commandID, data);
return true;
}
uint16_t RPB_1600::parseLinearData(void)
{
uint16_t rawData = my_rx_buffer[0] | (my_rx_buffer[1] << 8);
// Mask & shift out the "N" and "mantissa" values and convert them from 2s complement
int16_t rawN = (rawData & N_EXPONENT_MASK) >> N_EXPONENT_SHIFT;
int16_t N = UpscaleTwosComplement(rawN, N_EXPONENT_LENGTH);
int16_t rawMantissa = (rawData & MANTISSA_MASK);
int16_t mantissa = UpscaleTwosComplement(rawMantissa, MANTISSA_LENGTH);
uint16_t result = static_cast<uint16_t>(mantissa);
// The result is calculated by doing the following: result = mantissa * 2 ^ N
// however, N can be positive or negative, so we shift the mantissa accordingly
// to perform the multiplication by a power of 2.
if (N > 0)
{
result <<= N;
}
else if (N < 0)
{
result >>= (-N);
}
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Raw Data: 0x%x\n", rawData);
Serial.printf("<RPB-1600 DEBUG> Raw N: 0x%x\n", rawN);
Serial.printf("<RPB-1600 DEBUG> N: %d\n", N);
Serial.printf("<RPB-1600 DEBUG> Raw Mantissa : 0x%x\n", rawMantissa);
Serial.printf("<RPB-1600 DEBUG> Mantissa: %d\n", mantissa);
Serial.printf("<RPB-1600 DEBUG> Result: %d\n", result);
#endif
return result;
}
float RPB_1600::parseLinearVoltage(int8_t N)
{
uint16_t rawData = my_rx_buffer[0] | (my_rx_buffer[1] << 8);
// Calculate divisor using N
uint16_t divisor = 0x01 << abs(N);
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Parsing linear voltage w/N: %d\n", N);
#endif
float result = (float)rawData;
// Divide based on the polarity of N
if (N > 0)
{
result /= -divisor;
}
else if (N < 0)
{
result /= divisor;
}
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> Linear voltage result: %4.2fV\n", result);
#endif
return result;
}
void RPB_1600::parseCurveConfig(curve_config *config)
{
// Bits 0 & 1 of low byte
config->charge_curve_type = (my_rx_buffer[0] & 0x02);
// Bits 2 & 3 of low byte
config->temp_compensation = (my_rx_buffer[0] & 0x0C) >> 2;
// Bit 7 of low byte (0 = 3 stage, 1 = 2 stage)
config->num_charge_stages = (my_rx_buffer[0] && 0x40) ? 2 : 3;
// Bit 0 of high byte
config->cc_timeout_indication_enabled = (my_rx_buffer[1] && 0x01);
// Bit 1 of high byte
config->cv_timeout_indication_enabled = (my_rx_buffer[1] && 0x02);
// Bit 2 of high byte
config->float_stage_timeout_indication_enabled = (my_rx_buffer[1] && 0x04);
}
void RPB_1600::parseChargeStatus(charge_status *status)
{
// Low byte:
status->fully_charged = (my_rx_buffer[0] && 0x01); // Bit 0
status->in_cc_mode = (my_rx_buffer[0] && 0x02); // Bit 1
status->in_cv_mode = (my_rx_buffer[0] && 0x04); // Bit 2
status->in_float_mode = (my_rx_buffer[0] && 0x08); // Bit 3
// High byte:
status->EEPROM_error = (my_rx_buffer[1] && 0x01); // Bit 0
status->temp_compensation_short_circuit = (my_rx_buffer[1] && 0x04); // Bit 1
status->battery_detected = (my_rx_buffer[1] && 0x08); // Bit 3
status->timeout_flag_cc_mode = (my_rx_buffer[1] && 0x20); // Bit 5
status->timeout_flag_cv_mode = (my_rx_buffer[1] && 0x40); // Bit 6
status->timeout_flag_float_mode = (my_rx_buffer[1] && 0x80); // Bit 7
}
// Slightly modified version of this https://www.codeproject.com/Tips/1079637/Twos-Complement-for-Unusual-Integer-Sizes
int16_t RPB_1600::UpscaleTwosComplement(int16_t value, size_t length)
{
uint16_t mask = (~0) << length;
// Too small for complement?
if (length < 2)
{
return (~mask & value);
}
// Two's complement?
uint16_t msb = 1 << (length - 1);
if (value & msb)
{
return (mask | value);
}
else
{
return (~mask & value);
}
}
void RPB_1600::clearRXBuffer(void)
{
memset(my_rx_buffer, 0, MAX_RECEIVE_BYTES);
#ifdef RPB_1600_DEBUG
Serial.printf("<RPB-1600 DEBUG> RX Buffer Cleared!\n");
#endif
}