req

Dependents:   BMS_BMUCore_Max_DummyData BMS_BMUCore_Max

Fork of LTC6804 by Max Vigdorchik

Committer:
DasSidG
Date:
Fri Jun 23 17:40:29 2017 +0000
Revision:
2:04d9634b435b
Parent:
1:698bebac5005
Child:
3:f664ee0ebb1a
Code now verified to work with 33 cells (rather than 36); Relocated print statements to resolve potential CAN buffer overflow issue where packets were missing on the receiving end

Who changed what in which revision?

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