req

Dependents:   BMS_BMUCore_Max_DummyData BMS_BMUCore_Max

Fork of LTC6804 by Max Vigdorchik

Committer:
DasSidG
Date:
Sat Sep 16 01:04:41 2017 +0000
Revision:
9:1c94bbb97eaa
Parent:
8:e3a5393dfbac
Removed some couts which were generating warnings as they were placed after a break

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DasSidG 5:324a19dcfdec 1 //#include "SPI_Parser.h"
maxv008 0:5163dda740af 2 #include "mbed.h"
maxv008 0:5163dda740af 3 #include "CAN_Data.h"
maxv008 0:5163dda740af 4 #include "CAN_IDs.h"
maxv008 0:5163dda740af 5 #include <stdlib.h>
maxv008 1:698bebac5005 6 #include "Cell_Voltage.h"
maxv008 0:5163dda740af 7
maxv008 0:5163dda740af 8 //Define SoC ALCC digital in pins might be necessary as a setup, testing needed
maxv008 0:5163dda740af 9 //DigitalIn alcc(p20);
maxv008 0:5163dda740af 10 //Timer t;
maxv008 0:5163dda740af 11
maxv008 0:5163dda740af 12 /**
maxv008 0:5163dda740af 13 * Sets the values of the cellvoltage array with units 100 uV from all of the CMUs
maxv008 0:5163dda740af 14 * Uses the NO_CMUS parameter for the first dimension of cellcodes
maxv008 0:5163dda740af 15 * @param cellcodes[NO_CMUS][12] Will hold the voltage values for every cell in uV.
maxv008 0:5163dda740af 16 */
maxv008 0:5163dda740af 17 void LTC6804_acquireVoltage(uint16_t cellcodes [][12])
maxv008 0:5163dda740af 18 {
maxv008 3:f664ee0ebb1a 19 //This code seems to have had an error for multiple CMUs in writing the config.
maxv008 3:f664ee0ebb1a 20 //The "write" array is being changed, but this is untested, revert change
maxv008 3:f664ee0ebb1a 21 //if necessary
maxv008 4:1e49052d2fd1 22 uint8_t write [1][6];
DasSidG 2:04d9634b435b 23 write[0][0]=0x00;
DasSidG 2:04d9634b435b 24 write[0][1]=0x00;
DasSidG 2:04d9634b435b 25 write[0][2]=0x00;
DasSidG 2:04d9634b435b 26 write[0][3]=0x00;
DasSidG 2:04d9634b435b 27 write[0][4]=0x00;
maxv008 4:1e49052d2fd1 28 write[0][5]=0x00;
DasSidG 2:04d9634b435b 29
DasSidG 8:e3a5393dfbac 30 LTC6804_init(MD_FAST, DCP_DISABLED, CELL_CH_ALL, AUX_CH_VREF2);
DasSidG 8:e3a5393dfbac 31 /*uint8_t write [NO_CMUS][6];
DasSidG 8:e3a5393dfbac 32 for(int i = 0; i < NO_CMUS; i++)
DasSidG 8:e3a5393dfbac 33 for(int j = 0; j < 6; j++)
DasSidG 8:e3a5393dfbac 34 write[i][j] = 0x00;
DasSidG 8:e3a5393dfbac 35 }*/
maxv008 0:5163dda740af 36 //spi.format(8,3); //All data transfer on LTC6804 occur in byte groups. LTC6820 set up such that POL=1 and PHA=3, this corresponds to mode 3 in mbed library. spi.frequency(spiBitrate);
maxv008 0:5163dda740af 37 //spi.frequency(spiBitrate);
maxv008 0:5163dda740af 38 wake_LTC6804();//ensures CMU's are in ready state and wakes it up from low power mode
DasSidG 2:04d9634b435b 39 LTC6804_wrcfg(NO_CMUS,write);
maxv008 0:5163dda740af 40 wait_us(330);
maxv008 0:5163dda740af 41 wake_LTC6804(); //This might need to be removed
maxv008 0:5163dda740af 42 LTC6804_acquireVoltageTx();
maxv008 0:5163dda740af 43 wait_us(930);
maxv008 0:5163dda740af 44 LTC6804_acquireAllVoltageRegRx(0, NO_CMUS, cellcodes);
maxv008 0:5163dda740af 45 }
maxv008 0:5163dda740af 46
maxv008 0:5163dda740af 47 /**
maxv008 0:5163dda740af 48 Sets the balancing transistors by adjusting the configuration states of the
maxv008 0:5163dda740af 49 LTC6804. This version of the function writes all 12 states for a chosen
maxv008 0:5163dda740af 50 transistor. If other forms end up being more useful I will add other overloaded
maxv008 0:5163dda740af 51 versions.
maxv008 0:5163dda740af 52
maxv008 0:5163dda740af 53 @param uint8_t ic The specific IC to write to (can be changed in favor of a
maxv008 0:5163dda740af 54 states array of size 12*total_ic if preferred)
maxv008 0:5163dda740af 55
maxv008 0:5163dda740af 56 @param uint8_t states[12] For Sn in S1-S12 set states[n-1] to 1 to enable
maxv008 0:5163dda740af 57 balancing and 0 to disable balancing.
maxv008 0:5163dda740af 58 */
maxv008 0:5163dda740af 59 void LTC6804_balance(uint8_t ic, uint8_t states[12])
maxv008 0:5163dda740af 60 {
maxv008 0:5163dda740af 61 uint8_t total_ic = NO_CMUS;
maxv008 0:5163dda740af 62 //Consider using this to define the configs: (uint8_t *)malloc(total_ic*sizeof(uint8_t));
maxv008 0:5163dda740af 63 uint8_t r_config[total_ic][8];
maxv008 0:5163dda740af 64 uint8_t w_config[total_ic][6];//Size is smaller because there aren't PEC codes
maxv008 0:5163dda740af 65 wake_LTC6804();
maxv008 0:5163dda740af 66 wait_us(330);
maxv008 0:5163dda740af 67 LTC6804_rdcfg(total_ic, r_config);
maxv008 0:5163dda740af 68
maxv008 0:5163dda740af 69 /*for (int i=0; i<8; i++) {
maxv008 0:5163dda740af 70 printf("TEST %d config \r\n", (uint8_t)r_config[0][i]);
maxv008 0:5163dda740af 71 } */
maxv008 0:5163dda740af 72
maxv008 0:5163dda740af 73 uint8_t cfgr4 = 0; //This entire configuration is DCC states
maxv008 0:5163dda740af 74 uint8_t cfgr5 = r_config[ic][5];
maxv008 0:5163dda740af 75
maxv008 0:5163dda740af 76 for(int i = 0; i < 8; i++)
maxv008 0:5163dda740af 77 {
maxv008 0:5163dda740af 78 //Note: This disgusting thing is written by someone who has not used c++ in a long time
maxv008 0:5163dda740af 79 cfgr4 = states[i] ? cfgr4 | (1u << i) : cfgr4 & ~(1u << i);
maxv008 0:5163dda740af 80 }
maxv008 0:5163dda740af 81
maxv008 0:5163dda740af 82 for(int i = 8; i < 12; i++)
maxv008 0:5163dda740af 83 {
maxv008 0:5163dda740af 84 cfgr5 = states[i] ? cfgr5 | (1u << (i-8)) : cfgr5 & ~(1u << (i-8));
maxv008 0:5163dda740af 85 }
maxv008 0:5163dda740af 86
maxv008 0:5163dda740af 87 //printf("cfgr4 %d \r\n", (uint8_t)cfgr4);
maxv008 0:5163dda740af 88 //printf("cfgr5 %d \r\n", (uint8_t)cfgr5);
maxv008 0:5163dda740af 89 for(int i =0 ; i < total_ic; i++)
maxv008 0:5163dda740af 90 {
maxv008 0:5163dda740af 91 for(int j = 0; j < 6; j++)
maxv008 0:5163dda740af 92 {
maxv008 0:5163dda740af 93 w_config[i][j] = r_config[i][j];
maxv008 0:5163dda740af 94 }
maxv008 0:5163dda740af 95 }
maxv008 0:5163dda740af 96 w_config[ic][4] = cfgr4;
maxv008 0:5163dda740af 97 w_config[ic][5] = cfgr5;
maxv008 0:5163dda740af 98
maxv008 0:5163dda740af 99 wake_LTC6804();
maxv008 0:5163dda740af 100 wait_us(330);
maxv008 0:5163dda740af 101 LTC6804_wrcfg(total_ic,w_config); //Make sure this is written in the write order
maxv008 0:5163dda740af 102 }
maxv008 0:5163dda740af 103 /**
maxv008 0:5163dda740af 104 Takes a set of voltages corresponding to cell voltage measurements and a voltage
maxv008 0:5163dda740af 105 limit (presumably 4200 mV) and then drains all cells as needed. If current state
maxv008 0:5163dda740af 106 of balancing is needed outside of this function, it can be modified.
maxv008 0:5163dda740af 107
maxv008 0:5163dda740af 108 Current implementation of this function just uses the hard limit, so it might end
maxv008 0:5163dda740af 109 up constantly enabling and disabling balancing as it hits the limit then goes under.
maxv008 0:5163dda740af 110 That behavior can be improved upon if needed. One idea is to use derivative of
maxv008 0:5163dda740af 111 voltage with time to predict when it will hit maxVoltage so there is no risk of
maxv008 0:5163dda740af 112 going over, or allow them to drain such that all of the cells are at the same voltage.
maxv008 0:5163dda740af 113
maxv008 0:5163dda740af 114 @param uint16_t voltages[][12] Measured voltages to be used for deciding what to
maxv008 0:5163dda740af 115 balance. The dimensions must be [total_ic][12].
maxv008 0:5163dda740af 116
maxv008 0:5163dda740af 117 @param uint16_t maxVoltage voltage limit for the cells
maxv008 0:5163dda740af 118 */
maxv008 0:5163dda740af 119 void LTC6804_balanceVoltage(uint16_t voltages[][12], uint16_t maxVoltage)
maxv008 0:5163dda740af 120 {
maxv008 0:5163dda740af 121 uint8_t total_ic = NO_CMUS;
maxv008 0:5163dda740af 122 //Consider making states a parameter as a pointer so it can be referenced outside of the function easier.
maxv008 0:5163dda740af 123 uint8_t states[total_ic][12];
maxv008 0:5163dda740af 124 for(int i = 0; i < total_ic; i++)
maxv008 0:5163dda740af 125 {
maxv008 0:5163dda740af 126 for(int j = 0; j < 12; j++)
maxv008 0:5163dda740af 127 {
maxv008 0:5163dda740af 128 states[i][j] = voltages[i][j] >= maxVoltage ? 1 : 0; //Not sure if ternary operator is needed in C.
maxv008 0:5163dda740af 129 }
maxv008 0:5163dda740af 130 LTC6804_balance(i, states[i]);
maxv008 0:5163dda740af 131 }
maxv008 0:5163dda740af 132 }