req
Dependents: BMS_BMUCore_Max_DummyData BMS_BMUCore_Max
Fork of LTC6804 by
Cell_Voltage.cpp@4:1e49052d2fd1, 2017-07-02 (annotated)
- Committer:
- maxv008
- Date:
- Sun Jul 02 01:44:40 2017 +0000
- Revision:
- 4:1e49052d2fd1
- Parent:
- 3:f664ee0ebb1a
- Child:
- 5:324a19dcfdec
Undid Previous change (although still up in the air as to which version is correct, neither worked in a quick test but most likely hardware fault in that test)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maxv008 | 0:5163dda740af | 1 | #include "SPI_I2C_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; |
maxv008 | 4:1e49052d2fd1 | 29 | //uint8_t write [NO_CMUS][6]; |
maxv008 | 4:1e49052d2fd1 | 30 | //for(int i = 0; i < NO_CMUS; i++) |
maxv008 | 4:1e49052d2fd1 | 31 | // for(int j = 0; j < 6; j++) |
maxv008 | 4:1e49052d2fd1 | 32 | // write[i][j] = 0x00; |
DasSidG | 2:04d9634b435b | 33 | |
maxv008 | 0:5163dda740af | 34 | //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 | 35 | //spi.frequency(spiBitrate); |
maxv008 | 0:5163dda740af | 36 | wake_LTC6804();//ensures CMU's are in ready state and wakes it up from low power mode |
DasSidG | 2:04d9634b435b | 37 | LTC6804_wrcfg(NO_CMUS,write); |
maxv008 | 0:5163dda740af | 38 | wait_us(330); |
maxv008 | 0:5163dda740af | 39 | wake_LTC6804(); //This might need to be removed |
maxv008 | 0:5163dda740af | 40 | LTC6804_acquireVoltageTx(); |
maxv008 | 0:5163dda740af | 41 | wait_us(930); |
maxv008 | 0:5163dda740af | 42 | LTC6804_acquireAllVoltageRegRx(0, NO_CMUS, cellcodes); |
maxv008 | 0:5163dda740af | 43 | } |
maxv008 | 0:5163dda740af | 44 | |
maxv008 | 0:5163dda740af | 45 | /** |
maxv008 | 0:5163dda740af | 46 | Sets the balancing transistors by adjusting the configuration states of the |
maxv008 | 0:5163dda740af | 47 | LTC6804. This version of the function writes all 12 states for a chosen |
maxv008 | 0:5163dda740af | 48 | transistor. If other forms end up being more useful I will add other overloaded |
maxv008 | 0:5163dda740af | 49 | versions. |
maxv008 | 0:5163dda740af | 50 | |
maxv008 | 0:5163dda740af | 51 | @param uint8_t ic The specific IC to write to (can be changed in favor of a |
maxv008 | 0:5163dda740af | 52 | states array of size 12*total_ic if preferred) |
maxv008 | 0:5163dda740af | 53 | |
maxv008 | 0:5163dda740af | 54 | @param uint8_t states[12] For Sn in S1-S12 set states[n-1] to 1 to enable |
maxv008 | 0:5163dda740af | 55 | balancing and 0 to disable balancing. |
maxv008 | 0:5163dda740af | 56 | */ |
maxv008 | 0:5163dda740af | 57 | void LTC6804_balance(uint8_t ic, uint8_t states[12]) |
maxv008 | 0:5163dda740af | 58 | { |
maxv008 | 0:5163dda740af | 59 | uint8_t total_ic = NO_CMUS; |
maxv008 | 0:5163dda740af | 60 | //Consider using this to define the configs: (uint8_t *)malloc(total_ic*sizeof(uint8_t)); |
maxv008 | 0:5163dda740af | 61 | uint8_t r_config[total_ic][8]; |
maxv008 | 0:5163dda740af | 62 | uint8_t w_config[total_ic][6];//Size is smaller because there aren't PEC codes |
maxv008 | 0:5163dda740af | 63 | wake_LTC6804(); |
maxv008 | 0:5163dda740af | 64 | wait_us(330); |
maxv008 | 0:5163dda740af | 65 | LTC6804_rdcfg(total_ic, r_config); |
maxv008 | 0:5163dda740af | 66 | |
maxv008 | 0:5163dda740af | 67 | /*for (int i=0; i<8; i++) { |
maxv008 | 0:5163dda740af | 68 | printf("TEST %d config \r\n", (uint8_t)r_config[0][i]); |
maxv008 | 0:5163dda740af | 69 | } */ |
maxv008 | 0:5163dda740af | 70 | |
maxv008 | 0:5163dda740af | 71 | uint8_t cfgr4 = 0; //This entire configuration is DCC states |
maxv008 | 0:5163dda740af | 72 | uint8_t cfgr5 = r_config[ic][5]; |
maxv008 | 0:5163dda740af | 73 | |
maxv008 | 0:5163dda740af | 74 | for(int i = 0; i < 8; i++) |
maxv008 | 0:5163dda740af | 75 | { |
maxv008 | 0:5163dda740af | 76 | //Note: This disgusting thing is written by someone who has not used c++ in a long time |
maxv008 | 0:5163dda740af | 77 | cfgr4 = states[i] ? cfgr4 | (1u << i) : cfgr4 & ~(1u << i); |
maxv008 | 0:5163dda740af | 78 | } |
maxv008 | 0:5163dda740af | 79 | |
maxv008 | 0:5163dda740af | 80 | for(int i = 8; i < 12; i++) |
maxv008 | 0:5163dda740af | 81 | { |
maxv008 | 0:5163dda740af | 82 | cfgr5 = states[i] ? cfgr5 | (1u << (i-8)) : cfgr5 & ~(1u << (i-8)); |
maxv008 | 0:5163dda740af | 83 | } |
maxv008 | 0:5163dda740af | 84 | |
maxv008 | 0:5163dda740af | 85 | //printf("cfgr4 %d \r\n", (uint8_t)cfgr4); |
maxv008 | 0:5163dda740af | 86 | //printf("cfgr5 %d \r\n", (uint8_t)cfgr5); |
maxv008 | 0:5163dda740af | 87 | for(int i =0 ; i < total_ic; i++) |
maxv008 | 0:5163dda740af | 88 | { |
maxv008 | 0:5163dda740af | 89 | for(int j = 0; j < 6; j++) |
maxv008 | 0:5163dda740af | 90 | { |
maxv008 | 0:5163dda740af | 91 | w_config[i][j] = r_config[i][j]; |
maxv008 | 0:5163dda740af | 92 | } |
maxv008 | 0:5163dda740af | 93 | } |
maxv008 | 0:5163dda740af | 94 | w_config[ic][4] = cfgr4; |
maxv008 | 0:5163dda740af | 95 | w_config[ic][5] = cfgr5; |
maxv008 | 0:5163dda740af | 96 | |
maxv008 | 0:5163dda740af | 97 | wake_LTC6804(); |
maxv008 | 0:5163dda740af | 98 | wait_us(330); |
maxv008 | 0:5163dda740af | 99 | LTC6804_wrcfg(total_ic,w_config); //Make sure this is written in the write order |
maxv008 | 0:5163dda740af | 100 | } |
maxv008 | 0:5163dda740af | 101 | /** |
maxv008 | 0:5163dda740af | 102 | Takes a set of voltages corresponding to cell voltage measurements and a voltage |
maxv008 | 0:5163dda740af | 103 | limit (presumably 4200 mV) and then drains all cells as needed. If current state |
maxv008 | 0:5163dda740af | 104 | of balancing is needed outside of this function, it can be modified. |
maxv008 | 0:5163dda740af | 105 | |
maxv008 | 0:5163dda740af | 106 | Current implementation of this function just uses the hard limit, so it might end |
maxv008 | 0:5163dda740af | 107 | up constantly enabling and disabling balancing as it hits the limit then goes under. |
maxv008 | 0:5163dda740af | 108 | That behavior can be improved upon if needed. One idea is to use derivative of |
maxv008 | 0:5163dda740af | 109 | voltage with time to predict when it will hit maxVoltage so there is no risk of |
maxv008 | 0:5163dda740af | 110 | going over, or allow them to drain such that all of the cells are at the same voltage. |
maxv008 | 0:5163dda740af | 111 | |
maxv008 | 0:5163dda740af | 112 | @param uint16_t voltages[][12] Measured voltages to be used for deciding what to |
maxv008 | 0:5163dda740af | 113 | balance. The dimensions must be [total_ic][12]. |
maxv008 | 0:5163dda740af | 114 | |
maxv008 | 0:5163dda740af | 115 | @param uint16_t maxVoltage voltage limit for the cells |
maxv008 | 0:5163dda740af | 116 | */ |
maxv008 | 0:5163dda740af | 117 | void LTC6804_balanceVoltage(uint16_t voltages[][12], uint16_t maxVoltage) |
maxv008 | 0:5163dda740af | 118 | { |
maxv008 | 0:5163dda740af | 119 | uint8_t total_ic = NO_CMUS; |
maxv008 | 0:5163dda740af | 120 | //Consider making states a parameter as a pointer so it can be referenced outside of the function easier. |
maxv008 | 0:5163dda740af | 121 | uint8_t states[total_ic][12]; |
maxv008 | 0:5163dda740af | 122 | for(int i = 0; i < total_ic; i++) |
maxv008 | 0:5163dda740af | 123 | { |
maxv008 | 0:5163dda740af | 124 | for(int j = 0; j < 12; j++) |
maxv008 | 0:5163dda740af | 125 | { |
maxv008 | 0:5163dda740af | 126 | states[i][j] = voltages[i][j] >= maxVoltage ? 1 : 0; //Not sure if ternary operator is needed in C. |
maxv008 | 0:5163dda740af | 127 | } |
maxv008 | 0:5163dda740af | 128 | LTC6804_balance(i, states[i]); |
maxv008 | 0:5163dda740af | 129 | } |
maxv008 | 0:5163dda740af | 130 | } |