APS Lab
/
BMS_3029
LTC6811 Battery Management System with ADuCM3029.
Embed:
(wiki syntax)
Show/hide line numbers
LTC6811.cpp
00001 /* 00002 LTC6811-1: Multicell Battery Monitor 00003 LTC6811.cpp 00004 */ 00005 00006 #include "mbed.h" 00007 #include "LTC681x.h" 00008 #include "LTC6811.h" 00009 00010 00011 void LTC6811_init_reg_limits(uint8_t total_ic, cell_asic ic[]) 00012 { 00013 for (uint8_t cic=0; cic<total_ic; cic++) 00014 { 00015 ic[cic].ic_reg.cell_channels=12; 00016 ic[cic].ic_reg.stat_channels=4; 00017 ic[cic].ic_reg.aux_channels=6; 00018 ic[cic].ic_reg.num_cv_reg=4; 00019 ic[cic].ic_reg.num_gpio_reg=2; 00020 ic[cic].ic_reg.num_stat_reg=3; 00021 } 00022 } 00023 00024 /* 00025 Starts cell voltage conversion 00026 */ 00027 void LTC6811_adcv( 00028 uint8_t MD, //ADC Mode 00029 uint8_t DCP, //Discharge Permit 00030 uint8_t CH //Cell Channels to be measured 00031 ) 00032 { 00033 LTC681x_adcv(MD,DCP,CH); 00034 } 00035 00036 //Starts cell voltage and SOC conversion 00037 void LTC6811_adcvsc( 00038 uint8_t MD, //ADC Mode 00039 uint8_t DCP //Discharge Permit 00040 ) 00041 { 00042 LTC681x_adcvsc(MD,DCP); 00043 } 00044 00045 // Starts cell voltage and GPIO 1&2 conversion 00046 void LTC6811_adcvax( 00047 uint8_t MD, //ADC Mode 00048 uint8_t DCP //Discharge Permit 00049 ) 00050 { 00051 LTC681x_adcvax(MD,DCP); 00052 } 00053 00054 //Starts cell voltage overlap conversion 00055 void LTC6811_adol( 00056 uint8_t MD, //ADC Mode 00057 uint8_t DCP //Discharge Permit 00058 ) 00059 { 00060 LTC681x_adol(MD,DCP); 00061 } 00062 00063 //Starts cell voltage self test conversion 00064 void LTC6811_cvst( 00065 uint8_t MD, //ADC Mode 00066 uint8_t ST //Self Test 00067 ) 00068 { 00069 LTC681x_cvst(MD,ST); 00070 } 00071 00072 //Start an Auxiliary Register Self Test Conversion 00073 void LTC6811_axst( 00074 uint8_t MD, //ADC Mode 00075 uint8_t ST //Self Test 00076 ) 00077 { 00078 LTC681x_axst(MD,ST); 00079 } 00080 00081 //Start a Status Register Self Test Conversion 00082 void LTC6811_statst( 00083 uint8_t MD, //ADC Mode 00084 uint8_t ST //Self Test 00085 ) 00086 { 00087 LTC681x_statst(MD,ST); 00088 } 00089 00090 //Sends the poll adc command 00091 uint8_t LTC6811_pladc() 00092 { 00093 return(LTC681x_pladc()); 00094 } 00095 00096 //This function will block operation until the ADC has finished it's conversion 00097 uint32_t LTC6811_pollAdc() 00098 { 00099 return(LTC681x_pollAdc()); 00100 } 00101 00102 //Start a GPIO and Vref2 Conversion 00103 void LTC6811_adax( 00104 uint8_t MD, //ADC Mode 00105 uint8_t CHG //GPIO Channels to be measured) 00106 ) 00107 { 00108 LTC681x_adax(MD,CHG); 00109 } 00110 00111 //Start an GPIO Redundancy test 00112 void LTC6811_adaxd( 00113 uint8_t MD, //ADC Mode 00114 uint8_t CHG //GPIO Channels to be measured) 00115 ) 00116 { 00117 LTC681x_adaxd(MD,CHG); 00118 } 00119 00120 //Start a Status ADC Conversion 00121 void LTC6811_adstat( 00122 uint8_t MD, //ADC Mode 00123 uint8_t CHST //GPIO Channels to be measured 00124 ) 00125 { 00126 LTC681x_adstat(MD,CHST); 00127 } 00128 00129 // Start a Status register redundancy test Conversion 00130 void LTC6811_adstatd( 00131 uint8_t MD, //ADC Mode 00132 uint8_t CHST //GPIO Channels to be measured 00133 ) 00134 { 00135 LTC681x_adstatd(MD,CHST); 00136 } 00137 00138 00139 // Start an open wire Conversion 00140 void LTC6811_adow( 00141 uint8_t MD, //ADC Mode 00142 uint8_t PUP //Discharge Permit 00143 ) 00144 { 00145 LTC681x_adow(MD,PUP); 00146 } 00147 00148 // Reads and parses the LTC6811 cell voltage registers. 00149 uint8_t LTC6811_rdcv(uint8_t reg, // Controls which cell voltage register is read back. 00150 uint8_t total_ic, // the number of ICs in the system 00151 cell_asic ic[] // Array of the parsed cell codes 00152 ) 00153 { 00154 00155 int8_t pec_error = 0; 00156 pec_error = LTC681x_rdcv(reg,total_ic,ic); 00157 return(pec_error); 00158 } 00159 00160 /* 00161 The function is used 00162 to read the parsed GPIO codes of the LTC6811. This function will send the requested 00163 read commands parse the data and store the gpio voltages in aux_codes variable 00164 */ 00165 int8_t LTC6811_rdaux(uint8_t reg, //Determines which GPIO voltage register is read back. 00166 uint8_t total_ic,//the number of ICs in the system 00167 cell_asic ic[]//A two dimensional array of the gpio voltage codes. 00168 ) 00169 { 00170 int8_t pec_error = 0; 00171 LTC681x_rdaux(reg,total_ic,ic); 00172 return (pec_error); 00173 } 00174 00175 /* 00176 Reads and parses the LTC6811 stat registers. 00177 The function is used 00178 to read the parsed stat codes of the LTC6811. This function will send the requested 00179 read commands parse the data and store the stat voltages in stat_codes variable 00180 */ 00181 int8_t LTC6811_rdstat(uint8_t reg, //Determines which Stat register is read back. 00182 uint8_t total_ic,//the number of ICs in the system 00183 cell_asic ic[] 00184 ) 00185 { 00186 int8_t pec_error = 0; 00187 pec_error = LTC681x_rdstat(reg,total_ic,ic); 00188 return (pec_error); 00189 } 00190 00191 /* 00192 The command clears the cell voltage registers and intiallizes 00193 all values to 1. The register will read back hexadecimal 0xFF 00194 after the command is sent. 00195 */ 00196 void LTC6811_clrcell() 00197 { 00198 LTC681x_clrcell(); 00199 } 00200 00201 /* 00202 The command clears the Auxiliary registers and initializes 00203 all values to 1. The register will read back hexadecimal 0xFF 00204 after the command is sent. 00205 */ 00206 void LTC6811_clraux() 00207 { 00208 LTC681x_clraux(); 00209 } 00210 00211 /* 00212 The command clears the Stat registers and intiallizes 00213 all values to 1. The register will read back hexadecimal 0xFF 00214 after the command is sent. 00215 00216 */ 00217 void LTC6811_clrstat() 00218 { 00219 LTC681x_clrstat(); 00220 } 00221 00222 /* 00223 The command clears the Sctrl registers and initializes 00224 all values to 0. The register will read back hexadecimal 0x00 00225 after the command is sent. 00226 */ 00227 void LTC6811_clrsctrl() 00228 { 00229 LTC681x_clrsctrl(); 00230 } 00231 00232 //Starts the Mux Decoder diagnostic self test 00233 void LTC6811_diagn() 00234 { 00235 LTC681x_diagn(); 00236 } 00237 00238 /* 00239 This command will write the configuration registers of the LTC6811-1s 00240 connected in a daisy chain stack. The configuration is written in descending 00241 order so the last device's configuration is written first. 00242 */ 00243 void LTC6811_wrcfg(uint8_t total_ic, //The number of ICs being written to 00244 cell_asic ic[] //A two dimensional array of the configuration data that will be written 00245 ) 00246 { 00247 LTC681x_wrcfg(total_ic,ic); 00248 } 00249 00250 00251 /* 00252 Reads configuration registers of a LTC6811 daisy chain 00253 */ 00254 int8_t LTC6811_rdcfg(uint8_t total_ic, //Number of ICs in the system 00255 cell_asic ic[] //A two dimensional array that the function stores the read configuration data. 00256 ) 00257 { 00258 int8_t pec_error = 0; 00259 pec_error = LTC681x_rdcfg(total_ic,ic); 00260 return(pec_error); 00261 } 00262 00263 /* 00264 Writes the pwm registers of a LTC6811 daisy chain 00265 */ 00266 void LTC6811_wrpwm(uint8_t total_ic, 00267 uint8_t pwmReg, //The number of ICs being written to 00268 cell_asic ic[] //A two dimensional array of the configuration data that will be written 00269 ) 00270 { 00271 LTC681x_wrpwm(total_ic,pwmReg,ic); 00272 } 00273 00274 00275 /* 00276 Reads pwm registers of a LTC6811 daisy chain 00277 */ 00278 int8_t LTC6811_rdpwm(uint8_t total_ic, //Number of ICs in the system 00279 uint8_t pwmReg, 00280 cell_asic ic[] //A two dimensional array that the function stores the read configuration data. 00281 ) 00282 { 00283 int8_t pec_error =0; 00284 pec_error = LTC681x_rdpwm(total_ic,pwmReg,ic); 00285 return(pec_error); 00286 } 00287 00288 /* 00289 Writes the COMM registers of a LTC6811 daisy chain 00290 */ 00291 void LTC6811_wrcomm(uint8_t total_ic, //The number of ICs being written to 00292 cell_asic ic[] //A two dimensional array of the comm data that will be written 00293 ) 00294 { 00295 LTC681x_wrcomm(total_ic,ic); 00296 } 00297 00298 /* 00299 Reads COMM registers of a LTC6811 daisy chain 00300 */ 00301 int8_t LTC6811_rdcomm(uint8_t total_ic, //Number of ICs in the system 00302 cell_asic ic[] //A two dimensional array that the function stores the read configuration data. 00303 ) 00304 { 00305 int8_t pec_error = 0; 00306 LTC681x_rdcomm(total_ic, ic); 00307 return(pec_error); 00308 } 00309 00310 /* 00311 Shifts data in COMM register out over LTC6811 SPI/I2C port 00312 */ 00313 void LTC6811_stcomm() 00314 { 00315 LTC681x_stcomm(); 00316 } 00317 00318 //Helper function to set discharge bit in CFG register 00319 void LTC6811_set_discharge(int Cell, uint8_t total_ic, cell_asic ic[]) 00320 { 00321 for (int i=0; i<total_ic; i++) 00322 { 00323 if (Cell<9) 00324 { 00325 ic[i].config.tx_data[4] = ic[i].config.tx_data[4] | (1<<(Cell-1)); 00326 } 00327 else if (Cell < 13) 00328 { 00329 ic[i].config.tx_data[5] = ic[i].config.tx_data[5] | (1<<(Cell-9)); 00330 } 00331 } 00332 } 00333 00334 // Runs the Digital Filter Self Test 00335 int16_t LTC6811_run_cell_adc_st(uint8_t adc_reg,uint8_t total_ic, cell_asic ic[]) 00336 { 00337 int16_t error = 0; 00338 error = LTC681x_run_cell_adc_st(adc_reg,total_ic,ic); 00339 return(error); 00340 } 00341 00342 //runs the redundancy self test 00343 int16_t LTC6811_run_adc_redundancy_st(uint8_t adc_mode, uint8_t adc_reg, uint8_t total_ic, cell_asic ic[]) 00344 { 00345 int16_t error = 0; 00346 LTC681x_run_adc_redundancy_st(adc_mode,adc_reg,total_ic,ic); 00347 return(error); 00348 } 00349 //Runs the datasheet algorithm for open wire 00350 void LTC6811_run_openwire(uint8_t total_ic, cell_asic ic[]) 00351 { 00352 LTC681x_run_openwire(total_ic,ic); 00353 } 00354 // Runs the ADC overlap test for the IC 00355 uint16_t LTC6811_run_adc_overlap(uint8_t total_ic, cell_asic ic[]) 00356 { 00357 uint16_t error = 0; 00358 LTC681x_run_adc_overlap(total_ic, ic); 00359 return(error); 00360 } 00361 00362 void LTC6811_max_min(uint8_t total_ic, cell_asic ic_cells[], 00363 cell_asic ic_min[], 00364 cell_asic ic_max[], 00365 cell_asic ic_delta[]) 00366 { 00367 for (int j=0; j < total_ic; j++) 00368 { 00369 for (int i = 0; i< 12; i++) 00370 { 00371 if (ic_cells[j].cells.c_codes[i]>ic_max[j].cells.c_codes[i])ic_max[j].cells.c_codes[i]=ic_cells[j].cells.c_codes[i]; 00372 else if (ic_cells[j].cells.c_codes[i]<ic_min[j].cells.c_codes[i])ic_min[j].cells.c_codes[i]=ic_cells[j].cells.c_codes[i]; 00373 ic_delta[j].cells.c_codes[i] = ic_max[j].cells.c_codes[i] - ic_min[j].cells.c_codes[i]; 00374 } 00375 } 00376 00377 00378 00379 00380 } 00381 00382 void LTC6811_init_max_min(uint8_t total_ic, cell_asic ic[],cell_asic ic_max[],cell_asic ic_min[]) 00383 { 00384 for (int j=0; j < total_ic; j++) 00385 { 00386 for (int i = 0; i< ic[j].ic_reg.cell_channels; i++) 00387 { 00388 ic_max[j].cells.c_codes[i]=0; 00389 ic_min[j].cells.c_codes[i]=0xFFFF; 00390 } 00391 } 00392 00393 } 00394 00395 //Helper function that increments PEC counters 00396 void LTC6811_check_pec(uint8_t total_ic,uint8_t reg, cell_asic ic[]) 00397 { 00398 LTC681x_check_pec(total_ic,reg,ic); 00399 } 00400 00401 //Helper Function to reset PEC counters 00402 void LTC6811_reset_crc_count(uint8_t total_ic, cell_asic ic[]) 00403 { 00404 LTC681x_reset_crc_count(total_ic,ic); 00405 } 00406 00407 //Helper function to intialize CFG variables. 00408 void LTC6811_init_cfg(uint8_t total_ic, cell_asic ic[]) 00409 { 00410 LTC681x_init_cfg(total_ic,ic); 00411 } 00412 //Helper function to set CFGR variable 00413 void LTC6811_set_cfgr(uint8_t nIC, cell_asic ic[], bool refon, bool adcopt, bool gpio[5],bool dcc[12]) 00414 { 00415 LTC681x_set_cfgr_refon(nIC,ic,refon); 00416 LTC681x_set_cfgr_adcopt(nIC,ic,adcopt); 00417 LTC681x_set_cfgr_gpio(nIC,ic,gpio); 00418 LTC681x_set_cfgr_dis(nIC,ic,dcc); 00419 } 00420 //Helper function to set the REFON bit 00421 void LTC6811_set_cfgr_refon(uint8_t nIC, cell_asic ic[], bool refon) 00422 { 00423 LTC681x_set_cfgr_refon(nIC,ic,refon); 00424 } 00425 //Helper function to set the adcopt bit 00426 void LTC6811_set_cfgr_adcopt(uint8_t nIC, cell_asic ic[], bool adcopt) 00427 { 00428 LTC681x_set_cfgr_adcopt(nIC,ic,adcopt); 00429 } 00430 //Helper function to set GPIO bits 00431 void LTC6811_set_cfgr_gpio(uint8_t nIC, cell_asic ic[],bool gpio[5]) 00432 { 00433 LTC681x_set_cfgr_gpio(nIC,ic,gpio); 00434 } 00435 //Helper function to control discharge 00436 void LTC6811_set_cfgr_dis(uint8_t nIC, cell_asic ic[],bool dcc[12]) 00437 { 00438 LTC681x_set_cfgr_dis(nIC,ic,dcc); 00439 } 00440 //Helper Function to set uv value in CFG register 00441 void LTC6811_set_cfgr_uv(uint8_t nIC, cell_asic ic[],uint16_t uv) 00442 { 00443 LTC681x_set_cfgr_uv(nIC, ic, uv); 00444 } 00445 //helper function to set OV value in CFG register 00446 void LTC6811_set_cfgr_ov(uint8_t nIC, cell_asic ic[],uint16_t ov) 00447 { 00448 LTC681x_set_cfgr_ov( nIC, ic, ov); 00449 }
Generated on Wed Jul 13 2022 20:45:57 by 1.7.2