-
Notifications
You must be signed in to change notification settings - Fork 100
/
ltc6813.c
453 lines (361 loc) · 11.5 KB
/
ltc6813.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
/*
Copyright 2018 - 2020 Benjamin Vedder [email protected]
This file is part of the VESC BMS firmware.
The VESC BMS firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC BMS firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ltc6813.h"
#include "utils.h"
#include "main.h"
// Macros
#define APPEND_16b(cmd, data, ind) data[ind++] = ((cmd) >> 8) & 0xFF;data[ind++] = (cmd) & 0xFF
#define SET4_LO_HI(byte, lo, hi) byte = (((hi) & 0x0F) << 4) | ((lo) & 0x0F)
#define CLEAR_BUFFER_6(buffer) buffer[0] = 0; buffer[1] = 0; buffer[2] = 0; buffer[3] = 0; buffer[4] = 0; buffer[5] = 0
#undef SET_BIT
#define SET_BIT(byte, bit, set) byte |= set ? 1 << bit : 0
// Private variables
static THD_WORKING_AREA(ltc_thd_wa, 2048);
static volatile float m_v_pack = 0.0;
static volatile float m_v_cell[18] = {0.0};
static volatile float m_v_cell_no_mute[18] = {0.0};
static volatile float m_v_cell_pu_diff[18] = {0.0};
static volatile float m_v_gpio[9] = {-1.0};
static volatile float m_last_temp = 0.0;
static volatile bool m_discharge_state[18] = {false};
static mutex_t ltc_mtx;
// Private functions
static THD_FUNCTION(ltc_thd, arg);
static void write_cmd(uint16_t cmd);
static bool read_reg_group(uint16_t cmd, uint8_t *buffer);
static bool write_reg_group(uint16_t cmd, const uint8_t *buffer);
static void ltc_wakeup(void);
static void read_cell_voltages(float *cells);
static void poll_adc(void);
static uint16_t calc_pec(uint8_t len, const uint8_t *data);
static uint8_t spi_exchange(uint8_t x);
static void spi_transfer(uint8_t *in_buf, const uint8_t *out_buf, int length);
static void spi_begin(void);
static void spi_end(void);
static void spi_delay(void);
void ltc_init(void) {
palSetLineMode(LINE_LTC_MISO, PAL_MODE_INPUT_PULLUP);
palSetLineMode(LINE_LTC_SCLK, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_LTC_CS, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_LTC_MOSI, PAL_MODE_OUTPUT_PUSHPULL);
palSetLine(LINE_LTC_MOSI);
chMtxObjectInit(<c_mtx);
chThdCreateStatic(ltc_thd_wa, sizeof(ltc_thd_wa), NORMALPRIO - 1, ltc_thd, 0);
(void)ltc_wakeup();
}
float ltc_last_pack_voltage(void) {
return m_v_pack;
}
float ltc_last_temp(void) {
return m_last_temp;
}
float ltc_last_cell_voltage(int cell) {
if (cell < 0 || cell > 17) {
return -1.0;
}
return m_v_cell[cell];
}
float ltc_last_cell_voltage_no_mute(int cell) {
if (cell < 0 || cell > 17) {
return -1.0;
}
return m_v_cell_no_mute[cell];
}
float ltc_last_pu_diff_voltage(int cell) {
if (cell < 0 || cell > 17) {
return -1.0;
}
return m_v_cell_pu_diff[cell];
}
float ltc_last_gpio_voltage(int gpio) {
if (gpio <= 0 || gpio > 9) {
return -1.0;
}
return m_v_gpio[gpio - 1];
}
void ltc_set_dsc(int cell, bool set) {
if (cell < 0 || cell > 17) {
return;
}
m_discharge_state[cell] = set;
}
bool ltc_get_dsc(int cell) {
if (cell < 0 || cell > 17) {
return false;
}
return m_discharge_state[cell];
}
void ltc_sleep(void) {
uint8_t buffer[6];
CLEAR_BUFFER_6(buffer);
buffer[0] |= LTC_GPIO1 | LTC_GPIO2 | LTC_GPIO3 | LTC_GPIO4 | LTC_GPIO5;
write_reg_group(LTC_WRCFGA, buffer);
}
bool ltc_self_test(void) {
bool res = true;
chMtxLock(<c_mtx);
float cells_st[18];
write_cmd(LTC_CVST | LTC_MD10 | LTC_ST01);
poll_adc();
read_cell_voltages((float*)cells_st);
for (int i = 0;i < 18;i++) {
if ((uint16_t)(cells_st[i] * 1e4) != 0x9555) {
res = false;
break;
}
}
chMtxUnlock(<c_mtx);
return res;
}
static THD_FUNCTION(ltc_thd, p) {
(void)p;
chRegSetThreadName("LTC");
while (!chThdShouldTerminateX()) {
chMtxLock(<c_mtx);
uint8_t buffer[100];
// ltc_wakeup();
CLEAR_BUFFER_6(buffer);
buffer[0] |= LTC_REFON; // Keep reference on, otherwise some measurements flicker
buffer[0] |= LTC_GPIO1 | LTC_GPIO2 | LTC_GPIO3 | LTC_GPIO4 | LTC_GPIO5;
SET_BIT(buffer[4], 0, m_discharge_state[0]);
SET_BIT(buffer[4], 1, m_discharge_state[1]);
SET_BIT(buffer[4], 2, m_discharge_state[2]);
SET_BIT(buffer[4], 3, m_discharge_state[3]);
SET_BIT(buffer[4], 4, m_discharge_state[4]);
SET_BIT(buffer[4], 5, m_discharge_state[5]);
SET_BIT(buffer[4], 6, m_discharge_state[6]);
SET_BIT(buffer[4], 7, m_discharge_state[7]);
SET_BIT(buffer[5], 3, m_discharge_state[11]);
SET_BIT(buffer[5], 2, m_discharge_state[10]);
SET_BIT(buffer[5], 1, m_discharge_state[9]);
SET_BIT(buffer[5], 0, m_discharge_state[8]);
write_reg_group(LTC_WRCFGA, buffer);
CLEAR_BUFFER_6(buffer);
SET_BIT(buffer[0], 7, m_discharge_state[15]);
SET_BIT(buffer[0], 6, m_discharge_state[14]);
SET_BIT(buffer[0], 5, m_discharge_state[13]);
SET_BIT(buffer[0], 4, m_discharge_state[12]);
SET_BIT(buffer[1], 1, m_discharge_state[17]);
SET_BIT(buffer[1], 0, m_discharge_state[16]);
write_reg_group(LTC_WRCFGB, buffer);
write_cmd(LTC_ADCV | LTC_MD10 | LTC_DCP);
poll_adc();
read_cell_voltages((float*)m_v_cell_no_mute);
write_cmd(LTC_MUTE);
chThdSleepMilliseconds(10);
write_cmd(LTC_ADCV | LTC_MD10);
poll_adc();
read_cell_voltages((float*)m_v_cell);
// Open wire check
// float cells_pu[18], cells_pd[18];
// write_cmd(LTC_ADOW | LTC_MD10 | LTC_PUP);
// poll_adc();
// read_cell_voltages(cells_pd);
// write_cmd(LTC_ADOW | LTC_MD10);
// poll_adc();
// read_cell_voltages(cells_pu);
// for (int i = 0;i < 18;i++) {
// m_v_cell_pu_diff[i] = cells_pu[i] - cells_pd[i];
// }
write_cmd(LTC_ADSTAT | LTC_MD10);
poll_adc();
write_cmd(LTC_ADAX | LTC_MD10 | LTC_CHG000);
poll_adc();
write_cmd(LTC_UNMUTE);
if (read_reg_group(LTC_RDSTATA, buffer)) {
m_v_pack = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4 * 30.0;
m_last_temp = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) * 100e-6 / 7.6e-3 - 276.0;
}
if (read_reg_group(LTC_RDAUXA, buffer)) {
m_v_gpio[0] = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4;
m_v_gpio[1] = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) / 1e4;
m_v_gpio[2] = (float)((uint16_t)buffer[4] | (uint16_t)buffer[5] << 8) / 1e4;
}
if (read_reg_group(LTC_RDAUXB, buffer)) {
m_v_gpio[3] = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4;
m_v_gpio[4] = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) / 1e4;
}
chMtxUnlock(<c_mtx);
chThdSleepMilliseconds(90);
}
}
static void write_cmd(uint16_t cmd) {
uint8_t buffer[4];
int ind = 0;
APPEND_16b(cmd, buffer, ind);
uint16_t pec = calc_pec(ind, buffer);
APPEND_16b(pec, buffer, ind);
spi_begin();
spi_transfer(0, buffer, ind);
spi_end();
}
static bool read_reg_group(uint16_t cmd, uint8_t *buffer) {
uint8_t buffer_send[4];
int ind = 0;
uint16_t pec = 0;
APPEND_16b(cmd, buffer_send, ind);
pec = calc_pec(ind, buffer_send);
APPEND_16b(pec, buffer_send, ind);
spi_begin();
spi_transfer(0, buffer_send, ind);
spi_transfer(buffer, 0, 8);
spi_end();
pec = calc_pec(6, buffer);
if (pec == (((uint16_t)buffer[6] << 8) | (uint16_t)buffer[7])) {
return true;
} else {
return false;
}
}
static bool write_reg_group(uint16_t cmd, const uint8_t *buffer) {
uint8_t buffer_send[4];
int ind = 0;
uint16_t pec = 0;
APPEND_16b(cmd, buffer_send, ind);
pec = calc_pec(ind, buffer_send);
APPEND_16b(pec, buffer_send, ind);
spi_begin();
spi_transfer(0, buffer_send, ind);
spi_transfer(0, buffer, 6);
pec = calc_pec(6, buffer);
ind = 0;
APPEND_16b(pec, buffer_send, ind);
spi_transfer(0, buffer_send, ind);
spi_end();
return true;
}
static void poll_adc(void) {
systime_t start = chVTGetSystemTimeX();
uint8_t buffer[4];
int ind = 0;
APPEND_16b(LTC_PLADC, buffer, ind);
uint16_t pec = calc_pec(ind, buffer);
APPEND_16b(pec, buffer, ind);
spi_begin();
spi_transfer(0, buffer, ind);
int sleep_cnt = 0;
uint8_t res;
spi_transfer(&res, 0, 1);
while (!res) {
chThdSleep(1);
spi_transfer(&res, 0, 1);
sleep_cnt++;
// Timeout
if (TIME_I2MS(chVTTimeElapsedSinceX(start)) > 1500) {
break;
}
}
// main_printf_usb("Elapsed uS: %d\r\n", TIME_I2US(chVTTimeElapsedSinceX(start)));
spi_end();
}
static void ltc_wakeup(void) {
spi_begin();
spi_exchange(0xFF);
spi_end();
}
static uint16_t calc_pec(uint8_t len, const uint8_t *data) {
uint16_t crc = 0x10;
while (len--) {
crc ^= ((uint16_t)(*data++) << 7);
for (int i = 0;i < 8;i++) {
uint16_t eor = crc & 0x4000 ? 0x4599 : 0;
crc <<= 1;
crc ^= eor;
}
}
return (crc & 0x7fff) << 1;
};
static void read_cell_voltages(float *cells) {
uint8_t buffer[8];
if (read_reg_group(LTC_RDCVA, buffer)) {
cells[0] = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4;
cells[1] = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) / 1e4;
cells[2] = (float)((uint16_t)buffer[4] | (uint16_t)buffer[5] << 8) / 1e4;
}
if (read_reg_group(LTC_RDCVB, buffer)) {
cells[3] = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4;
cells[4] = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) / 1e4;
cells[5] = (float)((uint16_t)buffer[4] | (uint16_t)buffer[5] << 8) / 1e4;
}
if (read_reg_group(LTC_RDCVC, buffer)) {
cells[6] = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4;
cells[7] = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) / 1e4;
cells[8] = (float)((uint16_t)buffer[4] | (uint16_t)buffer[5] << 8) / 1e4;
}
if (read_reg_group(LTC_RDCVD, buffer)) {
cells[9] = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4;
cells[10] = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) / 1e4;
cells[11] = (float)((uint16_t)buffer[4] | (uint16_t)buffer[5] << 8) / 1e4;
}
if (read_reg_group(LTC_RDCVE, buffer)) {
cells[12] = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4;
cells[13] = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) / 1e4;
cells[14] = (float)((uint16_t)buffer[4] | (uint16_t)buffer[5] << 8) / 1e4;
}
if (read_reg_group(LTC_RDCVF, buffer)) {
cells[15] = (float)((uint16_t)buffer[0] | (uint16_t)buffer[1] << 8) / 1e4;
cells[16] = (float)((uint16_t)buffer[2] | (uint16_t)buffer[3] << 8) / 1e4;
cells[17] = (float)((uint16_t)buffer[4] | (uint16_t)buffer[5] << 8) / 1e4;
}
}
// Software SPI
static uint8_t spi_exchange(uint8_t x) {
uint8_t rx;
spi_transfer(&rx, &x, 1);
return rx;
}
static void spi_transfer(uint8_t *in_buf, const uint8_t *out_buf, int length) {
for (int i = 0;i < length;i++) {
uint8_t send = out_buf ? out_buf[i] : 0xFF;
uint8_t receive = 0;
for (int bit = 0;bit < 8;bit++) {
palWriteLine(LINE_LTC_MOSI, send >> 7);
send <<= 1;
palClearLine(LINE_LTC_SCLK);
spi_delay();
palSetLine(LINE_LTC_SCLK);
int samples = 0;
samples += palReadLine(LINE_LTC_MISO);
__NOP();
samples += palReadLine(LINE_LTC_MISO);
__NOP();
samples += palReadLine(LINE_LTC_MISO);
__NOP();
samples += palReadLine(LINE_LTC_MISO);
__NOP();
samples += palReadLine(LINE_LTC_MISO);
receive <<= 1;
if (samples > 2) {
receive |= 1;
}
spi_delay();
}
if (in_buf) {
in_buf[i] = receive;
}
}
}
static void spi_begin(void) {
palClearLine(LINE_LTC_CS);
}
static void spi_end(void) {
palSetLine(LINE_LTC_CS);
chThdSleep(1);
}
static void spi_delay(void) {
for (volatile int i = 0;i < 3;i++) {
__NOP();
}
}