Hardware Abstraction Layer for FreeRTOS
ltc6811.h
Go to the documentation of this file.
1 #ifndef LTC6811_H_
2 #define LTC6811_H_
3 #include <hal.h>
4 #include <i2c.h>
5 #include <adc.h>
6 #include <spi.h>
7 
19 struct adc_ltc6811;
20 struct ltc6811 {
21  struct hal gen;
26  const uint32_t numberOfSlaves;
30  const uint32_t allSlavesMask;
34  const uint32_t cellUnderVoltage;
38  const uint32_t cellOverVoltage;
42  uint32_t index;
46  OS_DEFINE_TASK(adcTask, 500);
50  bool adcIsRunning;
54  int32_t selectSlaves;
58  struct ltc6811_slave **slaves;
62  struct spi_slave *spi;
66  bool (*callback)(struct ltc6811 *ltc, void *data);
70  void *callbackData;
71 #ifdef CONFIG_LTC6811_I2C
72 
75  bool sendI2CToAll;
76 #endif
77 };
78 
79 #ifdef CONFIG_LTC6811_I2C
80 struct i2c_ltc6811;
81 #endif
82 struct ltc6811_slave {
83  struct hal gen;
87  uint32_t index;
91  struct ltc6811 *master;
95  struct adc_ltc6811 **adcs;
99  struct adc_ltc6811 **gpioADCS;
100 #ifdef CONFIG_LTC6811_I2C
101 
104  struct i2c_ltc6811 *i2c;
105 #endif
106 };
107 
108 #ifdef CONFIG_LTC6811_I2C
109 
112 struct i2c_ltc6811 {
113  struct i2c_generic gen;
114  struct ltc6811_slave *ltc;
115 };
116 #endif
117 
120 struct adc_ltc6811 {
121  struct adc_generic gen;
122  struct ltc6811_slave *ltc;
123  uint16_t value;
124 };
125 
127 /*
128  * TDOD Missing Commands?
129  *
130  * Write Configuration Register Group B (WRCFGB)
131  * Read Configuration Register Group A (RDCFGA)
132  * Read Configuration Register Group B (RDCFGB)
133  * Read Cell Voltage Register Group E (RDCVE)
134  * Read Cell Voltage Register Group F (RDCVF)
135  * Read Auxiliary Register Group C (RDAUXC)
136  * Read Auxiliary Register Group D (RDAUXD)
137  * Write PWM/S Control Register Group B (WRPSB)
138  * Read PWM/S Control Register Group B (RDPSB)
139  */
143 #define LTC_CMD_WRCFGA (0x1)
144 
148 #define LTC_CMD_WRCFGA_0_GPIO_PULLUP(gpio) ((1 << gpio) << 3)
149 
155 #define LTC_CMD_WRCFGA_0_REFON BIT(2)
156 
161 #define LTC_CMD_WRCFGA_0_DTEN BIT(1)
162 
167 #define LTC_CMD_WRCFGA_0_ADCOPT BIT(0)
168 
172 #define LTC_CMD_WRCFGA_4_DCC(x) BIT(x)
173 
181 #define LTC_CMD_WRCFGA_5_DCTO(DCTO) (DCTO << 4)
182 
186 #define LTC_CMD_RDCFGA (0x2)
187 
190 #define LTC_CMD_RDCVA (0x4)
191 
194 #define LTC_CMD_RDCVB (0x6)
195 
198 #define LTC_CMD_RDCVC (0x8)
199 
202 #define LTC_CMD_RDCVD (0xA)
203 
206 #define LTC_CMD_RDAUXA (0xC)
207 
210 #define LTC_CMD_RDAUXB (0xE)
211 
214 #define LTC_CMD_RDSTATA (0x10)
215 
218 #define LTC_CMD_RDSTATB (0x12)
219 
222 #define LTC_CMD_WRSCTRL (0x14)
223 
226 #define LTC_CMD_WRPWM (0x20)
227 
230 #define LTC_CMD_RDSCTRL (0x16)
231 
234 #define LTC_CMD_RDPWM (0x22)
235 
238 #define LTC_CMD_STSCTRL (0x19)
239 
242 #define LTC_CMD_CLRSCTRL (0x18)
243 
265 #define LTC_CMD_ADCV(CH, DCP, MD) (BIT(5) | BIT(6) | BIT(9) | (((CH) & 0x7) << 0) | (((DCP) & 0x1) << 4) | (((MD) & 0x3) << 7))
266 #define LTC_CMD_ADCV_NORM_DCP_ALL (0x370)
267 
290 #define LTC_CMD_ADOW(CH, DCP, PUP, MD) (BIT(3) | BIT(5) | BIT(9) | (((CH) & 0x7) << 0) |(((PUP) & 0x1) << 6) ((DCP & 0x1) << 4) | (((MD) & 0x3) << 7))
291 #define LTC_CMD_ADOW_NORM_PUP_ALL (0x378)
292 #define LTC_CMD_ADOW_NORM_PDN_ALL (0x338)
293 
309 #define LTC_CMD_CVST(ST, MD) (BIT(0) | BIT(1) | BIT(2) | BIT(9) | (((ST) & 0x3) << 5) | (((MD) & 0x3) << 7))
310 #define LTC_CMD_CVST_NORM_ST1 (0x327)
311 
322 #define LTC_CMD_ADOL(DCP, MD) (BIT(0) | BIT(9) | (((DCP) & 0x1) << 4) | (((MD) & 0x3) << 7))
323 #define LTC_CMD_ADOL_NORM_DCP (0x311)
324 
345 #define LTC_CMD_ADAX(CHG, MD) (BIT(5) | BIT(6) | BIT(10) | (((CHG) & 0x7) << 0) | (((MD & 0x3) << 7)))
346 #define LTC_CMD_ADAX_NORM_GPIO1 (0x561)
347 #define LTC_CMD_ADAX_NORM_GPIO2 (0x562)
348 
369 #define LTC_CMD_ADAXD(CHG, MD) (BIT(10) | (((CHG) & 0x7) << 0) | (((MD & 0x3) << 7)))
370 #define LTC_CMD_ADAXD_NORM_GPIO1 (0x501)
371 #define LTC_CMD_ADAXD_NORM_GPIO2 (0x502)
372 
388 #define LTC_CMD_AXST(ST, MD) (BIT(0) | BIT(1) | BIT(2) | BIT(10) | (((ST) & 0x3) << 5) | (((MD) & 0x3) << 7))
389 #define LTC_CMD_AXST_NORM_ST1 (0x527)
390 
410 #define LTC_CMD_ADSTAT(CHST, MD) (BIT(3) | BIT(5) | BIT(6) | BIT(10) | (((CHST) & 0x7) << 0) | (((MD) & 0x3) << 7))
411 #define LTC_CMD_ADSTAT_NORM_ALL (0x568)
412 
431 #define LTC_CMD_ADSTATD(CHST, MD) (BIT(3) | BIT(10) | (((CHST) & 0x7) << 0) | (((MD) & 0x3) << 7))
432 #define LTC_CMD_ADSTATD_NORM_ALL (0x508)
433 
449 #define LTC_CMD_STATST(ST, MD) (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(10) | (((ST) & 0x3) << 5) | (((MD) & 0x3) << 7))
450 #define LTC_CMD_STATST_NORM_ST1 (0x52F)
451 
462 #define LTC_CMD_ADCVAX(DCP, MD) (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(6) | BIT(10) | (((DCP) & 0x1) << 4) | (((MD) & 0x3) << 7))
463 #define LTC_CMD_ADCVAX_NORM_DCP (0x57F)
464 
475 #define LTC_CMD_ADCVSC(DCP, MD) (BIT(0) | BIT(1) | BIT(2) | BIT(6) | BIT(10) | (((DCP) & 0x1) << 4) | (((MD) & 0x3) << 7))
476 #define LTC_CMD_ADCVSC_NORM_DCP (0x577)
477 
480 #define LTC_CMD_CLRCELL (0x711)
481 
484 #define LTC_CMD_CLRAUX (0x712)
485 
488 #define LTC_CMD_CLRSTAT (0x713)
489 
492 #define LTC_CMD_PLADC (0x714)
493 
496 #define LTC_CMD_DIAGN (0x715)
497 
500 #define LTC_CMD_WRCOMM (0x721)
501 
504 #define LTC_CMD_RDCOMM (0x722)
505 
508 #define LTC_CMD_STCOMM (0x723)
509 
513 typedef int16_t ltc_cmd_t;
514 
520 struct ltc6811 *ltc6811_init(uint32_t index);
526 struct ltc6811_slave *ltc6811_slave_init(uint32_t index);
537 int32_t ltc6811_connect(struct ltc6811 *ltc, struct spi *spi, uint8_t cs, uint16_t gpio, uint32_t baudrate);
546 int32_t ltc6811_write(struct ltc6811 *ltc, ltc_cmd_t cmd, uint8_t *data, uint32_t len);
555 int32_t ltc6811_read(struct ltc6811 *ltc, ltc_cmd_t cmd, uint8_t *data, uint32_t len);
562 uint16_t ltc6811_calcPEC(uint8_t* data, uint8_t len);
571 int32_t ltc6811_writeRegister(struct ltc6811 *ltc, ltc_cmd_t cmd, uint8_t *newData, uint8_t *oldData);
579 int32_t ltc6811_readRegister(struct ltc6811 *ltc, ltc_cmd_t cmd, uint8_t *registerContent);
580 int32_t ltc6811_setADCCallback(struct ltc6811 *ltc, bool (*callback)(struct ltc6811 *ltc, void *data), void *data);
581 int32_t ltc6811_startADC(struct ltc6811 *ltc);
582 int32_t ltc6811_stopADC(struct ltc6811 *ltc);
583 
585 #define LTC6811_ADC_DEV(masterid, id, adcID) \
586  struct adc_ltc6811 ltc6811_adc_##masterid##_##id##_##adcID = { \
587  ADC_INIT_DEV(ltc6811), \
588  .ltc = &ltc6811_slave_##masterid##_##id, \
589  }; \
590  ADC_ADDDEV(ltc6811, ltc6811_adc_##masterid##_##id##_##adcID)
591 #define ADC_PRV
592 #include <adc_prv.h>
593 extern const struct adc_ops ltc6811_adc_ops;
595 #define LTC6811_ADDDEV(id, numberofslave, _cellUnderVoltage, _cellOverVoltage) \
596  struct ltc6811_slave *ltc6811_slaves_##id[numberofslave]; \
597  struct ltc6811 ltc6811_dev_##id = { \
598  HAL_NAME("LTC6811 " #id) \
599  .gen.init = false, \
600  .numberOfSlaves = numberofslave, \
601  .allSlavesMask = ((1 << numberofslave) - 1), \
602  .selectSlaves = ((1 << numberofslave) - 1), \
603  .slaves = ltc6811_slaves_##id, \
604  .cellUnderVoltage = (((_cellUnderVoltage) / (16 * 100E-3)) - 1), \
605  .cellOverVoltage = ((_cellOverVoltage) / (16 * 100E-3)), \
606  }; \
607  HAL_ADD(ltc6811, ltc6811_dev_##id);
608 
609 #define LTC6811_SLAVE_ADDDEV(masterid, id) \
610  extern struct adc_ltc6811 *ltc6811_adcs_##id[12]; \
611  struct ltc6811_slave ltc6811_slave_##masterid##_##id = { \
612  HAL_NAME("LTC6811 " #id) \
613  .gen.init = false, \
614  .master = &ltc6811_dev_##masterid,\
615  .adcs = ltc6811_adcs_##id, \
616  }; \
617  HAL_ADD(ltc6811, ltc6811_slave_##masterid##_##id); \
618  LTC6811_ADC_DEV(masterid, id, 0); \
619  LTC6811_ADC_DEV(masterid, id, 1); \
620  LTC6811_ADC_DEV(masterid, id, 2); \
621  LTC6811_ADC_DEV(masterid, id, 3); \
622  LTC6811_ADC_DEV(masterid, id, 4); \
623  LTC6811_ADC_DEV(masterid, id, 5); \
624  LTC6811_ADC_DEV(masterid, id, 6); \
625  LTC6811_ADC_DEV(masterid, id, 7); \
626  LTC6811_ADC_DEV(masterid, id, 8); \
627  LTC6811_ADC_DEV(masterid, id, 9); \
628  LTC6811_ADC_DEV(masterid, id, 10); \
629  LTC6811_ADC_DEV(masterid, id, 11); \
630  struct adc_ltc6811 *ltc6811_adcs_##id[12] = { \
631  &ltc6811_adc_##masterid##_##id##_0, \
632  &ltc6811_adc_##masterid##_##id##_1, \
633  &ltc6811_adc_##masterid##_##id##_2, \
634  &ltc6811_adc_##masterid##_##id##_3, \
635  &ltc6811_adc_##masterid##_##id##_4, \
636  &ltc6811_adc_##masterid##_##id##_5, \
637  &ltc6811_adc_##masterid##_##id##_6, \
638  &ltc6811_adc_##masterid##_##id##_7, \
639  &ltc6811_adc_##masterid##_##id##_8, \
640  &ltc6811_adc_##masterid##_##id##_9, \
641  &ltc6811_adc_##masterid##_##id##_10, \
642  &ltc6811_adc_##masterid##_##id##_11, \
643  };
644 
645 #define LTC6811_ID(id) HAL_GET_ID(hal, ltc6811, ltc6811_dev_##id)
646 #define LTC6811_SLAVE_ID(masterid, id) HAL_GET_ID(hal, ltc6811, ltc6811_slave_##masterid##_##id)
647 #define LTC6811_ADC_ID(masterid, id, adcid) HAL_GET_ID(adc, ltc6811, ltc6811_adc_##masterid##_##id##_##adcid)
648 #define LTC6811_GET_ALL_ADCS(masterid, id, adcs) { \
649  (adcs)[0] = adc_init(LTC6811_ADC_ID(masterid, id, 0), 12,0); \
650  (adcs)[1] = adc_init(LTC6811_ADC_ID(masterid, id, 1), 12,0); \
651  (adcs)[2] = adc_init(LTC6811_ADC_ID(masterid, id, 2), 12,0); \
652  (adcs)[3] = adc_init(LTC6811_ADC_ID(masterid, id, 3), 12,0); \
653  (adcs)[4] = adc_init(LTC6811_ADC_ID(masterid, id, 4), 12,0); \
654  (adcs)[5] = adc_init(LTC6811_ADC_ID(masterid, id, 5), 12,0); \
655  (adcs)[6] = adc_init(LTC6811_ADC_ID(masterid, id, 6), 12,0); \
656  (adcs)[7] = adc_init(LTC6811_ADC_ID(masterid, id, 7), 12,0); \
657  (adcs)[8] = adc_init(LTC6811_ADC_ID(masterid, id, 8), 12,0); \
658  (adcs)[9] = adc_init(LTC6811_ADC_ID(masterid, id, 9), 12,0); \
659  (adcs)[10] = adc_init(LTC6811_ADC_ID(masterid, id, 10), 12,0); \
660  (adcs)[11] = adc_init(LTC6811_ADC_ID(masterid, id, 11), 12,0); \
661 } \
662 
663 
664 #endif
i2c_generic
Definition: i2c.h:71
ltc6811_slave_init
struct ltc6811_slave * ltc6811_slave_init(uint32_t index)
hal.h
spi.h
ltc6811_startADC
int32_t ltc6811_startADC(struct ltc6811 *ltc)
ltc6811_stopADC
int32_t ltc6811_stopADC(struct ltc6811 *ltc)
adc.h
ltc6811_init
struct ltc6811 * ltc6811_init(uint32_t index)
hal
Definition: hal.h:48
ltc6811_read
int32_t ltc6811_read(struct ltc6811 *ltc, ltc_cmd_t cmd, uint8_t *data, uint32_t len)
adc_prv.h
OS_DEFINE_TASK
#define OS_DEFINE_TASK(name, stackSize)
Definition: os.h:52
ltc6811_readRegister
int32_t ltc6811_readRegister(struct ltc6811 *ltc, ltc_cmd_t cmd, uint8_t *registerContent)
ltc6811_writeRegister
int32_t ltc6811_writeRegister(struct ltc6811 *ltc, ltc_cmd_t cmd, uint8_t *newData, uint8_t *oldData)
ltc6811_setADCCallback
int32_t ltc6811_setADCCallback(struct ltc6811 *ltc, bool(*callback)(struct ltc6811 *ltc, void *data), void *data)
ltc6811_calcPEC
uint16_t ltc6811_calcPEC(uint8_t *data, uint8_t len)
ltc_cmd_t
int16_t ltc_cmd_t
Definition: ltc6811.h:513
ltc6811_connect
int32_t ltc6811_connect(struct ltc6811 *ltc, struct spi *spi, uint8_t cs, uint16_t gpio, uint32_t baudrate)
ltc6811_write
int32_t ltc6811_write(struct ltc6811 *ltc, ltc_cmd_t cmd, uint8_t *data, uint32_t len)
i2c.h
adc_generic
Definition: adc.h:45