combine lib to one
bms_master.cpp@0:b2692e0e4219, 2017-11-17 (annotated)
- Committer:
- roger5641
- Date:
- Fri Nov 17 09:02:10 2017 +0000
- Revision:
- 0:b2692e0e4219
combine to 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roger5641 | 0:b2692e0e4219 | 1 | // |
roger5641 | 0:b2692e0e4219 | 2 | //#include "mbed.h" |
roger5641 | 0:b2692e0e4219 | 3 | //#include "bms_master.h" |
roger5641 | 0:b2692e0e4219 | 4 | // |
roger5641 | 0:b2692e0e4219 | 5 | //void wakeup_idle(uint8_t total_ic) |
roger5641 | 0:b2692e0e4219 | 6 | //{ |
roger5641 | 0:b2692e0e4219 | 7 | // for (int i =0; i<total_ic; i++) |
roger5641 | 0:b2692e0e4219 | 8 | // { |
roger5641 | 0:b2692e0e4219 | 9 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 10 | // //delayMicroseconds(2); //Guarantees the isoSPI will be in ready mode |
roger5641 | 0:b2692e0e4219 | 11 | // spi_read_byte(0xff); |
roger5641 | 0:b2692e0e4219 | 12 | // CS_PIN = 1; |
roger5641 | 0:b2692e0e4219 | 13 | // } |
roger5641 | 0:b2692e0e4219 | 14 | //} |
roger5641 | 0:b2692e0e4219 | 15 | // |
roger5641 | 0:b2692e0e4219 | 16 | ////Generic wakeup commannd to wake the LTC6813 from sleep |
roger5641 | 0:b2692e0e4219 | 17 | //void wakeup_sleep(uint8_t total_ic) |
roger5641 | 0:b2692e0e4219 | 18 | //{ |
roger5641 | 0:b2692e0e4219 | 19 | // for (int i =0; i<total_ic; i++) |
roger5641 | 0:b2692e0e4219 | 20 | // { |
roger5641 | 0:b2692e0e4219 | 21 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 22 | // wait_ms(300); // Guarantees the LTC6813 will be in standby |
roger5641 | 0:b2692e0e4219 | 23 | // CS_PIN = 1; |
roger5641 | 0:b2692e0e4219 | 24 | // wait_ms(10); |
roger5641 | 0:b2692e0e4219 | 25 | // } |
roger5641 | 0:b2692e0e4219 | 26 | //} |
roger5641 | 0:b2692e0e4219 | 27 | // |
roger5641 | 0:b2692e0e4219 | 28 | // |
roger5641 | 0:b2692e0e4219 | 29 | ////Generic function to write 68xx commands. Function calculated PEC for tx_cmd data |
roger5641 | 0:b2692e0e4219 | 30 | //void cmd_68(uint8_t tx_cmd[2]) |
roger5641 | 0:b2692e0e4219 | 31 | //{ |
roger5641 | 0:b2692e0e4219 | 32 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 33 | // uint16_t cmd_pec; |
roger5641 | 0:b2692e0e4219 | 34 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 35 | // |
roger5641 | 0:b2692e0e4219 | 36 | // cmd[0] = tx_cmd[0]; |
roger5641 | 0:b2692e0e4219 | 37 | // cmd[1] = tx_cmd[1]; |
roger5641 | 0:b2692e0e4219 | 38 | // cmd_pec = pec15_calc(2, cmd); |
roger5641 | 0:b2692e0e4219 | 39 | // cmd[2] = (uint8_t)(cmd_pec >> 8); |
roger5641 | 0:b2692e0e4219 | 40 | // cmd[3] = (uint8_t)(cmd_pec); |
roger5641 | 0:b2692e0e4219 | 41 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 42 | // spi_write_array(4,cmd); |
roger5641 | 0:b2692e0e4219 | 43 | // CS_PIN = 1; |
roger5641 | 0:b2692e0e4219 | 44 | //} |
roger5641 | 0:b2692e0e4219 | 45 | // |
roger5641 | 0:b2692e0e4219 | 46 | ////Generic function to write 68xx commands and write payload data. Function calculated PEC for tx_cmd data |
roger5641 | 0:b2692e0e4219 | 47 | //void write_68(uint8_t total_ic , uint8_t tx_cmd[2], uint8_t data[]) |
roger5641 | 0:b2692e0e4219 | 48 | //{ |
roger5641 | 0:b2692e0e4219 | 49 | // const uint8_t BYTES_IN_REG = 6; |
roger5641 | 0:b2692e0e4219 | 50 | // const uint8_t CMD_LEN = 4+(8*total_ic); |
roger5641 | 0:b2692e0e4219 | 51 | // uint8_t *cmd; |
roger5641 | 0:b2692e0e4219 | 52 | // uint16_t data_pec; |
roger5641 | 0:b2692e0e4219 | 53 | // uint16_t cmd_pec; |
roger5641 | 0:b2692e0e4219 | 54 | // uint8_t cmd_index; |
roger5641 | 0:b2692e0e4219 | 55 | // |
roger5641 | 0:b2692e0e4219 | 56 | // cmd = (uint8_t *)malloc(CMD_LEN*sizeof(uint8_t)); |
roger5641 | 0:b2692e0e4219 | 57 | // cmd[0] = tx_cmd[0]; |
roger5641 | 0:b2692e0e4219 | 58 | // cmd[1] = tx_cmd[1]; |
roger5641 | 0:b2692e0e4219 | 59 | // cmd_pec = pec15_calc(2, cmd); |
roger5641 | 0:b2692e0e4219 | 60 | // cmd[2] = (uint8_t)(cmd_pec >> 8); |
roger5641 | 0:b2692e0e4219 | 61 | // cmd[3] = (uint8_t)(cmd_pec); |
roger5641 | 0:b2692e0e4219 | 62 | // cmd_index = 4; |
roger5641 | 0:b2692e0e4219 | 63 | // for (uint8_t current_ic = total_ic; current_ic > 0; current_ic--) // executes for each LTC681x in daisy chain, this loops starts with |
roger5641 | 0:b2692e0e4219 | 64 | // { |
roger5641 | 0:b2692e0e4219 | 65 | // // the last IC on the stack. The first configuration written is |
roger5641 | 0:b2692e0e4219 | 66 | // // received by the last IC in the daisy chain |
roger5641 | 0:b2692e0e4219 | 67 | // |
roger5641 | 0:b2692e0e4219 | 68 | // for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++) |
roger5641 | 0:b2692e0e4219 | 69 | // { |
roger5641 | 0:b2692e0e4219 | 70 | // cmd[cmd_index] = data[((current_ic-1)*6)+current_byte]; |
roger5641 | 0:b2692e0e4219 | 71 | // cmd_index = cmd_index + 1; |
roger5641 | 0:b2692e0e4219 | 72 | // } |
roger5641 | 0:b2692e0e4219 | 73 | // |
roger5641 | 0:b2692e0e4219 | 74 | // data_pec = (uint16_t)pec15_calc(BYTES_IN_REG, &data[(current_ic-1)*6]); // calculating the PEC for each Iss configuration register data |
roger5641 | 0:b2692e0e4219 | 75 | // cmd[cmd_index] = (uint8_t)(data_pec >> 8); |
roger5641 | 0:b2692e0e4219 | 76 | // cmd[cmd_index + 1] = (uint8_t)data_pec; |
roger5641 | 0:b2692e0e4219 | 77 | // cmd_index = cmd_index + 2; |
roger5641 | 0:b2692e0e4219 | 78 | // } |
roger5641 | 0:b2692e0e4219 | 79 | // |
roger5641 | 0:b2692e0e4219 | 80 | // |
roger5641 | 0:b2692e0e4219 | 81 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 82 | // spi_write_array(CMD_LEN, cmd); |
roger5641 | 0:b2692e0e4219 | 83 | // CS_PIN = 1; |
roger5641 | 0:b2692e0e4219 | 84 | // free(cmd); |
roger5641 | 0:b2692e0e4219 | 85 | //} |
roger5641 | 0:b2692e0e4219 | 86 | // |
roger5641 | 0:b2692e0e4219 | 87 | ////Generic function to write 68xx commands and read data. Function calculated PEC for tx_cmd data |
roger5641 | 0:b2692e0e4219 | 88 | //int8_t read_68( uint8_t total_ic, uint8_t tx_cmd[2], uint8_t *rx_data) |
roger5641 | 0:b2692e0e4219 | 89 | //{ |
roger5641 | 0:b2692e0e4219 | 90 | // const uint8_t BYTES_IN_REG = 8; |
roger5641 | 0:b2692e0e4219 | 91 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 92 | // uint8_t data[256]; |
roger5641 | 0:b2692e0e4219 | 93 | // int8_t pec_error = 0; |
roger5641 | 0:b2692e0e4219 | 94 | // uint16_t cmd_pec; |
roger5641 | 0:b2692e0e4219 | 95 | // uint16_t data_pec; |
roger5641 | 0:b2692e0e4219 | 96 | // uint16_t received_pec; |
roger5641 | 0:b2692e0e4219 | 97 | // |
roger5641 | 0:b2692e0e4219 | 98 | // // data = (uint8_t *) malloc((8*total_ic)*sizeof(uint8_t)); // This is a problem because it can fail |
roger5641 | 0:b2692e0e4219 | 99 | // |
roger5641 | 0:b2692e0e4219 | 100 | // cmd[0] = tx_cmd[0]; |
roger5641 | 0:b2692e0e4219 | 101 | // cmd[1] = tx_cmd[1]; |
roger5641 | 0:b2692e0e4219 | 102 | // cmd_pec = pec15_calc(2, cmd); |
roger5641 | 0:b2692e0e4219 | 103 | // cmd[2] = (uint8_t)(cmd_pec >> 8); |
roger5641 | 0:b2692e0e4219 | 104 | // cmd[3] = (uint8_t)(cmd_pec); |
roger5641 | 0:b2692e0e4219 | 105 | // |
roger5641 | 0:b2692e0e4219 | 106 | // |
roger5641 | 0:b2692e0e4219 | 107 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 108 | // spi_write_read(cmd, 4, data, (BYTES_IN_REG*total_ic)); //Read the configuration data of all ICs on the daisy chain into |
roger5641 | 0:b2692e0e4219 | 109 | // CS_PIN = 1; //rx_data[] array |
roger5641 | 0:b2692e0e4219 | 110 | // |
roger5641 | 0:b2692e0e4219 | 111 | // for (uint8_t current_ic = 0; current_ic < total_ic; current_ic++) //executes for each LTC681x in the daisy chain and packs the data |
roger5641 | 0:b2692e0e4219 | 112 | // { |
roger5641 | 0:b2692e0e4219 | 113 | // //into the r_comm array as well as check the received Config data |
roger5641 | 0:b2692e0e4219 | 114 | // //for any bit errors |
roger5641 | 0:b2692e0e4219 | 115 | // for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++) |
roger5641 | 0:b2692e0e4219 | 116 | // { |
roger5641 | 0:b2692e0e4219 | 117 | // rx_data[(current_ic*8)+current_byte] = data[current_byte + (current_ic*BYTES_IN_REG)]; |
roger5641 | 0:b2692e0e4219 | 118 | // } |
roger5641 | 0:b2692e0e4219 | 119 | // received_pec = (rx_data[(current_ic*8)+6]<<8) + rx_data[(current_ic*8)+7]; |
roger5641 | 0:b2692e0e4219 | 120 | // data_pec = pec15_calc(6, &rx_data[current_ic*8]); |
roger5641 | 0:b2692e0e4219 | 121 | // if (received_pec != data_pec) |
roger5641 | 0:b2692e0e4219 | 122 | // { |
roger5641 | 0:b2692e0e4219 | 123 | // pec_error = -1; |
roger5641 | 0:b2692e0e4219 | 124 | // } |
roger5641 | 0:b2692e0e4219 | 125 | // } |
roger5641 | 0:b2692e0e4219 | 126 | // |
roger5641 | 0:b2692e0e4219 | 127 | // |
roger5641 | 0:b2692e0e4219 | 128 | // return(pec_error); |
roger5641 | 0:b2692e0e4219 | 129 | //} |
roger5641 | 0:b2692e0e4219 | 130 | // |
roger5641 | 0:b2692e0e4219 | 131 | //// Calculates and returns the CRC15 |
roger5641 | 0:b2692e0e4219 | 132 | // |
roger5641 | 0:b2692e0e4219 | 133 | //uint16_t pec15_calc(uint8_t len, //Number of bytes that will be used to calculate a PEC |
roger5641 | 0:b2692e0e4219 | 134 | // uint8_t *data //Array of data that will be used to calculate a PEC |
roger5641 | 0:b2692e0e4219 | 135 | // ) |
roger5641 | 0:b2692e0e4219 | 136 | //{ |
roger5641 | 0:b2692e0e4219 | 137 | // uint16_t remainder,addr; |
roger5641 | 0:b2692e0e4219 | 138 | // |
roger5641 | 0:b2692e0e4219 | 139 | // remainder = 16;//initialize the PEC |
roger5641 | 0:b2692e0e4219 | 140 | // for (uint8_t i = 0; i<len; i++) // loops for each byte in data array |
roger5641 | 0:b2692e0e4219 | 141 | // { |
roger5641 | 0:b2692e0e4219 | 142 | // addr = ((remainder>>7)^data[i])&0xff;//calculate PEC table address |
roger5641 | 0:b2692e0e4219 | 143 | // |
roger5641 | 0:b2692e0e4219 | 144 | // remainder = (remainder<<8)^crc15Table[addr]; |
roger5641 | 0:b2692e0e4219 | 145 | // } |
roger5641 | 0:b2692e0e4219 | 146 | // return(remainder*2);//The CRC15 has a 0 in the LSB so the remainder must be multiplied by 2 |
roger5641 | 0:b2692e0e4219 | 147 | //} |
roger5641 | 0:b2692e0e4219 | 148 | // |
roger5641 | 0:b2692e0e4219 | 149 | ////Starts cell voltage conversion |
roger5641 | 0:b2692e0e4219 | 150 | //void LTC681x_adcv( |
roger5641 | 0:b2692e0e4219 | 151 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 152 | // uint8_t DCP, //Discharge Permit |
roger5641 | 0:b2692e0e4219 | 153 | // uint8_t CH //Cell Channels to be measured |
roger5641 | 0:b2692e0e4219 | 154 | //) |
roger5641 | 0:b2692e0e4219 | 155 | //{ |
roger5641 | 0:b2692e0e4219 | 156 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 157 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 158 | // |
roger5641 | 0:b2692e0e4219 | 159 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 160 | // cmd[0] = md_bits + 0x02; |
roger5641 | 0:b2692e0e4219 | 161 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 162 | // cmd[1] = md_bits + 0x60 + (DCP<<4) + CH; |
roger5641 | 0:b2692e0e4219 | 163 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 164 | //} |
roger5641 | 0:b2692e0e4219 | 165 | // |
roger5641 | 0:b2692e0e4219 | 166 | ////Starts cell voltage overlap conversion |
roger5641 | 0:b2692e0e4219 | 167 | //void LTC681x_adol( |
roger5641 | 0:b2692e0e4219 | 168 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 169 | // uint8_t DCP //Discharge Permit |
roger5641 | 0:b2692e0e4219 | 170 | //) |
roger5641 | 0:b2692e0e4219 | 171 | //{ |
roger5641 | 0:b2692e0e4219 | 172 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 173 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 174 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 175 | // cmd[0] = md_bits + 0x02; |
roger5641 | 0:b2692e0e4219 | 176 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 177 | // cmd[1] = md_bits + (DCP<<4) +0x01; |
roger5641 | 0:b2692e0e4219 | 178 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 179 | //} |
roger5641 | 0:b2692e0e4219 | 180 | // |
roger5641 | 0:b2692e0e4219 | 181 | ////Starts cell voltage self test conversion |
roger5641 | 0:b2692e0e4219 | 182 | //void LTC681x_cvst( |
roger5641 | 0:b2692e0e4219 | 183 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 184 | // uint8_t ST //Self Test |
roger5641 | 0:b2692e0e4219 | 185 | //) |
roger5641 | 0:b2692e0e4219 | 186 | //{ |
roger5641 | 0:b2692e0e4219 | 187 | // uint8_t cmd[2]; |
roger5641 | 0:b2692e0e4219 | 188 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 189 | // |
roger5641 | 0:b2692e0e4219 | 190 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 191 | // cmd[0] = md_bits + 0x02; |
roger5641 | 0:b2692e0e4219 | 192 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 193 | // cmd[1] = md_bits + ((ST)<<5) +0x07; |
roger5641 | 0:b2692e0e4219 | 194 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 195 | // |
roger5641 | 0:b2692e0e4219 | 196 | //} |
roger5641 | 0:b2692e0e4219 | 197 | // |
roger5641 | 0:b2692e0e4219 | 198 | ////Start an Auxiliary Register Self Test Conversion |
roger5641 | 0:b2692e0e4219 | 199 | //void LTC681x_axst( |
roger5641 | 0:b2692e0e4219 | 200 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 201 | // uint8_t ST //Self Test |
roger5641 | 0:b2692e0e4219 | 202 | //) |
roger5641 | 0:b2692e0e4219 | 203 | //{ |
roger5641 | 0:b2692e0e4219 | 204 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 205 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 206 | // |
roger5641 | 0:b2692e0e4219 | 207 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 208 | // cmd[0] = md_bits + 0x04; |
roger5641 | 0:b2692e0e4219 | 209 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 210 | // cmd[1] = md_bits + ((ST&0x03)<<5) +0x07; |
roger5641 | 0:b2692e0e4219 | 211 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 212 | // |
roger5641 | 0:b2692e0e4219 | 213 | //} |
roger5641 | 0:b2692e0e4219 | 214 | // |
roger5641 | 0:b2692e0e4219 | 215 | ////Start a Status Register Self Test Conversion |
roger5641 | 0:b2692e0e4219 | 216 | //void LTC681x_statst( |
roger5641 | 0:b2692e0e4219 | 217 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 218 | // uint8_t ST //Self Test |
roger5641 | 0:b2692e0e4219 | 219 | //) |
roger5641 | 0:b2692e0e4219 | 220 | //{ |
roger5641 | 0:b2692e0e4219 | 221 | // uint8_t cmd[2]; |
roger5641 | 0:b2692e0e4219 | 222 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 223 | // |
roger5641 | 0:b2692e0e4219 | 224 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 225 | // cmd[0] = md_bits + 0x04; |
roger5641 | 0:b2692e0e4219 | 226 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 227 | // cmd[1] = md_bits + ((ST&0x03)<<5) +0x0F; |
roger5641 | 0:b2692e0e4219 | 228 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 229 | // |
roger5641 | 0:b2692e0e4219 | 230 | //} |
roger5641 | 0:b2692e0e4219 | 231 | // |
roger5641 | 0:b2692e0e4219 | 232 | ////This function will block operation until the ADC has finished it's conversion |
roger5641 | 0:b2692e0e4219 | 233 | //uint32_t LTC681x_pollAdc() |
roger5641 | 0:b2692e0e4219 | 234 | //{ |
roger5641 | 0:b2692e0e4219 | 235 | // uint32_t counter = 0; |
roger5641 | 0:b2692e0e4219 | 236 | // uint8_t finished = 0; |
roger5641 | 0:b2692e0e4219 | 237 | // uint8_t current_time = 0; |
roger5641 | 0:b2692e0e4219 | 238 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 239 | // uint16_t cmd_pec; |
roger5641 | 0:b2692e0e4219 | 240 | // |
roger5641 | 0:b2692e0e4219 | 241 | // |
roger5641 | 0:b2692e0e4219 | 242 | // cmd[0] = 0x07; |
roger5641 | 0:b2692e0e4219 | 243 | // cmd[1] = 0x14; |
roger5641 | 0:b2692e0e4219 | 244 | // cmd_pec = pec15_calc(2, cmd); |
roger5641 | 0:b2692e0e4219 | 245 | // cmd[2] = (uint8_t)(cmd_pec >> 8); |
roger5641 | 0:b2692e0e4219 | 246 | // cmd[3] = (uint8_t)(cmd_pec); |
roger5641 | 0:b2692e0e4219 | 247 | // |
roger5641 | 0:b2692e0e4219 | 248 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 249 | // spi_write_array(4,cmd); |
roger5641 | 0:b2692e0e4219 | 250 | // |
roger5641 | 0:b2692e0e4219 | 251 | // while ((counter<200000)&&(finished == 0)) |
roger5641 | 0:b2692e0e4219 | 252 | // { |
roger5641 | 0:b2692e0e4219 | 253 | // current_time = spi_read_byte(0xff); |
roger5641 | 0:b2692e0e4219 | 254 | // if (current_time>0) |
roger5641 | 0:b2692e0e4219 | 255 | // { |
roger5641 | 0:b2692e0e4219 | 256 | // finished = 1; |
roger5641 | 0:b2692e0e4219 | 257 | // } |
roger5641 | 0:b2692e0e4219 | 258 | // else |
roger5641 | 0:b2692e0e4219 | 259 | // { |
roger5641 | 0:b2692e0e4219 | 260 | // counter = counter + 10; |
roger5641 | 0:b2692e0e4219 | 261 | // } |
roger5641 | 0:b2692e0e4219 | 262 | // } |
roger5641 | 0:b2692e0e4219 | 263 | // |
roger5641 | 0:b2692e0e4219 | 264 | // CS_PIN = 1; |
roger5641 | 0:b2692e0e4219 | 265 | // |
roger5641 | 0:b2692e0e4219 | 266 | // |
roger5641 | 0:b2692e0e4219 | 267 | // return(counter); |
roger5641 | 0:b2692e0e4219 | 268 | //} |
roger5641 | 0:b2692e0e4219 | 269 | // |
roger5641 | 0:b2692e0e4219 | 270 | ////Start a GPIO and Vref2 Conversion |
roger5641 | 0:b2692e0e4219 | 271 | //void LTC681x_adax( |
roger5641 | 0:b2692e0e4219 | 272 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 273 | // uint8_t CHG //GPIO Channels to be measured) |
roger5641 | 0:b2692e0e4219 | 274 | //) |
roger5641 | 0:b2692e0e4219 | 275 | //{ |
roger5641 | 0:b2692e0e4219 | 276 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 277 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 278 | // |
roger5641 | 0:b2692e0e4219 | 279 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 280 | // cmd[0] = md_bits + 0x04; |
roger5641 | 0:b2692e0e4219 | 281 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 282 | // cmd[1] = md_bits + 0x60 + CHG ; |
roger5641 | 0:b2692e0e4219 | 283 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 284 | // |
roger5641 | 0:b2692e0e4219 | 285 | //} |
roger5641 | 0:b2692e0e4219 | 286 | ////Start an GPIO Redundancy test |
roger5641 | 0:b2692e0e4219 | 287 | //void LTC681x_adaxd( |
roger5641 | 0:b2692e0e4219 | 288 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 289 | // uint8_t CHG //GPIO Channels to be measured) |
roger5641 | 0:b2692e0e4219 | 290 | //) |
roger5641 | 0:b2692e0e4219 | 291 | //{ |
roger5641 | 0:b2692e0e4219 | 292 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 293 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 294 | // |
roger5641 | 0:b2692e0e4219 | 295 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 296 | // cmd[0] = md_bits + 0x04; |
roger5641 | 0:b2692e0e4219 | 297 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 298 | // cmd[1] = md_bits + CHG ; |
roger5641 | 0:b2692e0e4219 | 299 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 300 | //} |
roger5641 | 0:b2692e0e4219 | 301 | // |
roger5641 | 0:b2692e0e4219 | 302 | ////Start a Status ADC Conversion |
roger5641 | 0:b2692e0e4219 | 303 | //void LTC681x_adstat( |
roger5641 | 0:b2692e0e4219 | 304 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 305 | // uint8_t CHST //GPIO Channels to be measured |
roger5641 | 0:b2692e0e4219 | 306 | //) |
roger5641 | 0:b2692e0e4219 | 307 | //{ |
roger5641 | 0:b2692e0e4219 | 308 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 309 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 310 | // |
roger5641 | 0:b2692e0e4219 | 311 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 312 | // cmd[0] = md_bits + 0x04; |
roger5641 | 0:b2692e0e4219 | 313 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 314 | // cmd[1] = md_bits + 0x68 + CHST ; |
roger5641 | 0:b2692e0e4219 | 315 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 316 | //} |
roger5641 | 0:b2692e0e4219 | 317 | // |
roger5641 | 0:b2692e0e4219 | 318 | //// Start a Status register redundancy test Conversion |
roger5641 | 0:b2692e0e4219 | 319 | //void LTC681x_adstatd( |
roger5641 | 0:b2692e0e4219 | 320 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 321 | // uint8_t CHST //GPIO Channels to be measured |
roger5641 | 0:b2692e0e4219 | 322 | //) |
roger5641 | 0:b2692e0e4219 | 323 | //{ |
roger5641 | 0:b2692e0e4219 | 324 | // uint8_t cmd[2]; |
roger5641 | 0:b2692e0e4219 | 325 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 326 | // |
roger5641 | 0:b2692e0e4219 | 327 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 328 | // cmd[0] = md_bits + 0x04; |
roger5641 | 0:b2692e0e4219 | 329 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 330 | // cmd[1] = md_bits + 0x08 + CHST ; |
roger5641 | 0:b2692e0e4219 | 331 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 332 | // |
roger5641 | 0:b2692e0e4219 | 333 | //} |
roger5641 | 0:b2692e0e4219 | 334 | // |
roger5641 | 0:b2692e0e4219 | 335 | // |
roger5641 | 0:b2692e0e4219 | 336 | //// Start an open wire Conversion |
roger5641 | 0:b2692e0e4219 | 337 | //void LTC681x_adow( |
roger5641 | 0:b2692e0e4219 | 338 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 339 | // uint8_t PUP //Discharge Permit |
roger5641 | 0:b2692e0e4219 | 340 | //) |
roger5641 | 0:b2692e0e4219 | 341 | //{ |
roger5641 | 0:b2692e0e4219 | 342 | // uint8_t cmd[2]; |
roger5641 | 0:b2692e0e4219 | 343 | // uint8_t md_bits; |
roger5641 | 0:b2692e0e4219 | 344 | // md_bits = (MD & 0x02) >> 1; |
roger5641 | 0:b2692e0e4219 | 345 | // cmd[0] = md_bits + 0x02; |
roger5641 | 0:b2692e0e4219 | 346 | // md_bits = (MD & 0x01) << 7; |
roger5641 | 0:b2692e0e4219 | 347 | // cmd[1] = md_bits + 0x28 + (PUP<<6) ;//+ CH; |
roger5641 | 0:b2692e0e4219 | 348 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 349 | //} |
roger5641 | 0:b2692e0e4219 | 350 | // |
roger5641 | 0:b2692e0e4219 | 351 | //// Reads the raw cell voltage register data |
roger5641 | 0:b2692e0e4219 | 352 | //void LTC681x_rdcv_reg(uint8_t reg, //Determines which cell voltage register is read back |
roger5641 | 0:b2692e0e4219 | 353 | // uint8_t total_ic, //the number of ICs in the |
roger5641 | 0:b2692e0e4219 | 354 | // uint8_t *data //An array of the unparsed cell codes |
roger5641 | 0:b2692e0e4219 | 355 | // ) |
roger5641 | 0:b2692e0e4219 | 356 | //{ |
roger5641 | 0:b2692e0e4219 | 357 | // const uint8_t REG_LEN = 8; //number of bytes in each ICs register + 2 bytes for the PEC |
roger5641 | 0:b2692e0e4219 | 358 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 359 | // uint16_t cmd_pec; |
roger5641 | 0:b2692e0e4219 | 360 | // |
roger5641 | 0:b2692e0e4219 | 361 | // if (reg == 1) //1: RDCVA |
roger5641 | 0:b2692e0e4219 | 362 | // { |
roger5641 | 0:b2692e0e4219 | 363 | // cmd[1] = 0x04; |
roger5641 | 0:b2692e0e4219 | 364 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 365 | // } |
roger5641 | 0:b2692e0e4219 | 366 | // else if (reg == 2) //2: RDCVB |
roger5641 | 0:b2692e0e4219 | 367 | // { |
roger5641 | 0:b2692e0e4219 | 368 | // cmd[1] = 0x06; |
roger5641 | 0:b2692e0e4219 | 369 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 370 | // } |
roger5641 | 0:b2692e0e4219 | 371 | // else if (reg == 3) //3: RDCVC |
roger5641 | 0:b2692e0e4219 | 372 | // { |
roger5641 | 0:b2692e0e4219 | 373 | // cmd[1] = 0x08; |
roger5641 | 0:b2692e0e4219 | 374 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 375 | // } |
roger5641 | 0:b2692e0e4219 | 376 | // else if (reg == 4) //4: RDCVD |
roger5641 | 0:b2692e0e4219 | 377 | // { |
roger5641 | 0:b2692e0e4219 | 378 | // cmd[1] = 0x0A; |
roger5641 | 0:b2692e0e4219 | 379 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 380 | // } |
roger5641 | 0:b2692e0e4219 | 381 | // else if (reg == 5) //4: RDCVE |
roger5641 | 0:b2692e0e4219 | 382 | // { |
roger5641 | 0:b2692e0e4219 | 383 | // cmd[1] = 0x09; |
roger5641 | 0:b2692e0e4219 | 384 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 385 | // } |
roger5641 | 0:b2692e0e4219 | 386 | // else if (reg == 6) //4: RDCVF |
roger5641 | 0:b2692e0e4219 | 387 | // { |
roger5641 | 0:b2692e0e4219 | 388 | // cmd[1] = 0x0B; |
roger5641 | 0:b2692e0e4219 | 389 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 390 | // } |
roger5641 | 0:b2692e0e4219 | 391 | // |
roger5641 | 0:b2692e0e4219 | 392 | // |
roger5641 | 0:b2692e0e4219 | 393 | // cmd_pec = pec15_calc(2, cmd); |
roger5641 | 0:b2692e0e4219 | 394 | // cmd[2] = (uint8_t)(cmd_pec >> 8); |
roger5641 | 0:b2692e0e4219 | 395 | // cmd[3] = (uint8_t)(cmd_pec); |
roger5641 | 0:b2692e0e4219 | 396 | // |
roger5641 | 0:b2692e0e4219 | 397 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 398 | // spi_write_read(cmd,4,data,(REG_LEN*total_ic)); |
roger5641 | 0:b2692e0e4219 | 399 | // CS_PIN = 1; |
roger5641 | 0:b2692e0e4219 | 400 | // |
roger5641 | 0:b2692e0e4219 | 401 | //} |
roger5641 | 0:b2692e0e4219 | 402 | // |
roger5641 | 0:b2692e0e4219 | 403 | ///* |
roger5641 | 0:b2692e0e4219 | 404 | //The function reads a single GPIO voltage register and stores thre read data |
roger5641 | 0:b2692e0e4219 | 405 | //in the *data point as a byte array. This function is rarely used outside of |
roger5641 | 0:b2692e0e4219 | 406 | //the LTC6811_rdaux() command. |
roger5641 | 0:b2692e0e4219 | 407 | //*/ |
roger5641 | 0:b2692e0e4219 | 408 | //void LTC681x_rdaux_reg(uint8_t reg, //Determines which GPIO voltage register is read back |
roger5641 | 0:b2692e0e4219 | 409 | // uint8_t total_ic, //The number of ICs in the system |
roger5641 | 0:b2692e0e4219 | 410 | // uint8_t *data //Array of the unparsed auxiliary codes |
roger5641 | 0:b2692e0e4219 | 411 | // ) |
roger5641 | 0:b2692e0e4219 | 412 | //{ |
roger5641 | 0:b2692e0e4219 | 413 | // const uint8_t REG_LEN = 8; // number of bytes in the register + 2 bytes for the PEC |
roger5641 | 0:b2692e0e4219 | 414 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 415 | // uint16_t cmd_pec; |
roger5641 | 0:b2692e0e4219 | 416 | // |
roger5641 | 0:b2692e0e4219 | 417 | // |
roger5641 | 0:b2692e0e4219 | 418 | // if (reg == 1) //Read back auxiliary group A |
roger5641 | 0:b2692e0e4219 | 419 | // { |
roger5641 | 0:b2692e0e4219 | 420 | // cmd[1] = 0x0C; |
roger5641 | 0:b2692e0e4219 | 421 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 422 | // } |
roger5641 | 0:b2692e0e4219 | 423 | // else if (reg == 2) //Read back auxiliary group B |
roger5641 | 0:b2692e0e4219 | 424 | // { |
roger5641 | 0:b2692e0e4219 | 425 | // cmd[1] = 0x0e; |
roger5641 | 0:b2692e0e4219 | 426 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 427 | // } |
roger5641 | 0:b2692e0e4219 | 428 | // else if (reg == 3) //Read back auxiliary group C |
roger5641 | 0:b2692e0e4219 | 429 | // { |
roger5641 | 0:b2692e0e4219 | 430 | // cmd[1] = 0x0D; |
roger5641 | 0:b2692e0e4219 | 431 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 432 | // } |
roger5641 | 0:b2692e0e4219 | 433 | // else if (reg == 4) //Read back auxiliary group D |
roger5641 | 0:b2692e0e4219 | 434 | // { |
roger5641 | 0:b2692e0e4219 | 435 | // cmd[1] = 0x0F; |
roger5641 | 0:b2692e0e4219 | 436 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 437 | // } |
roger5641 | 0:b2692e0e4219 | 438 | // else //Read back auxiliary group A |
roger5641 | 0:b2692e0e4219 | 439 | // { |
roger5641 | 0:b2692e0e4219 | 440 | // cmd[1] = 0x0C; |
roger5641 | 0:b2692e0e4219 | 441 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 442 | // } |
roger5641 | 0:b2692e0e4219 | 443 | // |
roger5641 | 0:b2692e0e4219 | 444 | // cmd_pec = pec15_calc(2, cmd); |
roger5641 | 0:b2692e0e4219 | 445 | // cmd[2] = (uint8_t)(cmd_pec >> 8); |
roger5641 | 0:b2692e0e4219 | 446 | // cmd[3] = (uint8_t)(cmd_pec); |
roger5641 | 0:b2692e0e4219 | 447 | // |
roger5641 | 0:b2692e0e4219 | 448 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 449 | // spi_write_read(cmd,4,data,(REG_LEN*total_ic)); |
roger5641 | 0:b2692e0e4219 | 450 | // CS_PIN = 1; |
roger5641 | 0:b2692e0e4219 | 451 | // |
roger5641 | 0:b2692e0e4219 | 452 | //} |
roger5641 | 0:b2692e0e4219 | 453 | // |
roger5641 | 0:b2692e0e4219 | 454 | ///* |
roger5641 | 0:b2692e0e4219 | 455 | //The function reads a single stat register and stores the read data |
roger5641 | 0:b2692e0e4219 | 456 | //in the *data point as a byte array. This function is rarely used outside of |
roger5641 | 0:b2692e0e4219 | 457 | //the LTC6811_rdstat() command. |
roger5641 | 0:b2692e0e4219 | 458 | //*/ |
roger5641 | 0:b2692e0e4219 | 459 | //void LTC681x_rdstat_reg(uint8_t reg, //Determines which stat register is read back |
roger5641 | 0:b2692e0e4219 | 460 | // uint8_t total_ic, //The number of ICs in the system |
roger5641 | 0:b2692e0e4219 | 461 | // uint8_t *data //Array of the unparsed stat codes |
roger5641 | 0:b2692e0e4219 | 462 | // ) |
roger5641 | 0:b2692e0e4219 | 463 | //{ |
roger5641 | 0:b2692e0e4219 | 464 | // const uint8_t REG_LEN = 8; // number of bytes in the register + 2 bytes for the PEC |
roger5641 | 0:b2692e0e4219 | 465 | // uint8_t cmd[4]; |
roger5641 | 0:b2692e0e4219 | 466 | // uint16_t cmd_pec; |
roger5641 | 0:b2692e0e4219 | 467 | // |
roger5641 | 0:b2692e0e4219 | 468 | // |
roger5641 | 0:b2692e0e4219 | 469 | // if (reg == 1) //Read back statiliary group A |
roger5641 | 0:b2692e0e4219 | 470 | // { |
roger5641 | 0:b2692e0e4219 | 471 | // cmd[1] = 0x10; |
roger5641 | 0:b2692e0e4219 | 472 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 473 | // } |
roger5641 | 0:b2692e0e4219 | 474 | // else if (reg == 2) //Read back statiliary group B |
roger5641 | 0:b2692e0e4219 | 475 | // { |
roger5641 | 0:b2692e0e4219 | 476 | // cmd[1] = 0x12; |
roger5641 | 0:b2692e0e4219 | 477 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 478 | // } |
roger5641 | 0:b2692e0e4219 | 479 | // |
roger5641 | 0:b2692e0e4219 | 480 | // else //Read back statiliary group A |
roger5641 | 0:b2692e0e4219 | 481 | // { |
roger5641 | 0:b2692e0e4219 | 482 | // cmd[1] = 0x10; |
roger5641 | 0:b2692e0e4219 | 483 | // cmd[0] = 0x00; |
roger5641 | 0:b2692e0e4219 | 484 | // } |
roger5641 | 0:b2692e0e4219 | 485 | // |
roger5641 | 0:b2692e0e4219 | 486 | // cmd_pec = pec15_calc(2, cmd); |
roger5641 | 0:b2692e0e4219 | 487 | // cmd[2] = (uint8_t)(cmd_pec >> 8); |
roger5641 | 0:b2692e0e4219 | 488 | // cmd[3] = (uint8_t)(cmd_pec); |
roger5641 | 0:b2692e0e4219 | 489 | // |
roger5641 | 0:b2692e0e4219 | 490 | // CS_PIN = 0; |
roger5641 | 0:b2692e0e4219 | 491 | // spi_write_read(cmd,4,data,(REG_LEN*total_ic)); |
roger5641 | 0:b2692e0e4219 | 492 | // CS_PIN = 1; |
roger5641 | 0:b2692e0e4219 | 493 | // |
roger5641 | 0:b2692e0e4219 | 494 | //} |
roger5641 | 0:b2692e0e4219 | 495 | // |
roger5641 | 0:b2692e0e4219 | 496 | ///* |
roger5641 | 0:b2692e0e4219 | 497 | //The command clears the cell voltage registers and intiallizes |
roger5641 | 0:b2692e0e4219 | 498 | //all values to 1. The register will read back hexadecimal 0xFF |
roger5641 | 0:b2692e0e4219 | 499 | //after the command is sent. |
roger5641 | 0:b2692e0e4219 | 500 | //*/ |
roger5641 | 0:b2692e0e4219 | 501 | //void LTC681x_clrcell() |
roger5641 | 0:b2692e0e4219 | 502 | //{ |
roger5641 | 0:b2692e0e4219 | 503 | // uint8_t cmd[2]= {0x07 , 0x11}; |
roger5641 | 0:b2692e0e4219 | 504 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 505 | //} |
roger5641 | 0:b2692e0e4219 | 506 | // |
roger5641 | 0:b2692e0e4219 | 507 | ///* |
roger5641 | 0:b2692e0e4219 | 508 | //The command clears the Auxiliary registers and initializes |
roger5641 | 0:b2692e0e4219 | 509 | //all values to 1. The register will read back hexadecimal 0xFF |
roger5641 | 0:b2692e0e4219 | 510 | //after the command is sent. |
roger5641 | 0:b2692e0e4219 | 511 | //*/ |
roger5641 | 0:b2692e0e4219 | 512 | //void LTC681x_clraux() |
roger5641 | 0:b2692e0e4219 | 513 | //{ |
roger5641 | 0:b2692e0e4219 | 514 | // uint8_t cmd[2]= {0x07 , 0x12}; |
roger5641 | 0:b2692e0e4219 | 515 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 516 | //} |
roger5641 | 0:b2692e0e4219 | 517 | // |
roger5641 | 0:b2692e0e4219 | 518 | // |
roger5641 | 0:b2692e0e4219 | 519 | ///* |
roger5641 | 0:b2692e0e4219 | 520 | //The command clears the Stat registers and intiallizes |
roger5641 | 0:b2692e0e4219 | 521 | //all values to 1. The register will read back hexadecimal 0xFF |
roger5641 | 0:b2692e0e4219 | 522 | //after the command is sent. |
roger5641 | 0:b2692e0e4219 | 523 | // |
roger5641 | 0:b2692e0e4219 | 524 | //*/ |
roger5641 | 0:b2692e0e4219 | 525 | //void LTC681x_clrstat() |
roger5641 | 0:b2692e0e4219 | 526 | //{ |
roger5641 | 0:b2692e0e4219 | 527 | // uint8_t cmd[2]= {0x07 , 0x13}; |
roger5641 | 0:b2692e0e4219 | 528 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 529 | //} |
roger5641 | 0:b2692e0e4219 | 530 | // |
roger5641 | 0:b2692e0e4219 | 531 | ////Starts the Mux Decoder diagnostic self test |
roger5641 | 0:b2692e0e4219 | 532 | //void LTC681x_diagn() |
roger5641 | 0:b2692e0e4219 | 533 | //{ |
roger5641 | 0:b2692e0e4219 | 534 | // uint8_t cmd[2] = {0x07 , 0x15}; |
roger5641 | 0:b2692e0e4219 | 535 | // cmd_68(cmd); |
roger5641 | 0:b2692e0e4219 | 536 | //} |
roger5641 | 0:b2692e0e4219 | 537 | // |
roger5641 | 0:b2692e0e4219 | 538 | ////Reads and parses the LTC681x cell voltage registers. |
roger5641 | 0:b2692e0e4219 | 539 | //uint8_t LTC681x_rdcv(uint8_t reg, // Controls which cell voltage register is read back. |
roger5641 | 0:b2692e0e4219 | 540 | // uint8_t total_ic, // the number of ICs in the system |
roger5641 | 0:b2692e0e4219 | 541 | // cell_asic ic[] // Array of the parsed cell codes |
roger5641 | 0:b2692e0e4219 | 542 | // ) |
roger5641 | 0:b2692e0e4219 | 543 | //{ |
roger5641 | 0:b2692e0e4219 | 544 | // int8_t pec_error = 0; |
roger5641 | 0:b2692e0e4219 | 545 | // uint8_t *cell_data; |
roger5641 | 0:b2692e0e4219 | 546 | // uint8_t c_ic = 0; |
roger5641 | 0:b2692e0e4219 | 547 | // cell_data = (uint8_t *) malloc((NUM_RX_BYT*total_ic)*sizeof(uint8_t)); |
roger5641 | 0:b2692e0e4219 | 548 | // |
roger5641 | 0:b2692e0e4219 | 549 | // if (reg == 0) |
roger5641 | 0:b2692e0e4219 | 550 | // { |
roger5641 | 0:b2692e0e4219 | 551 | // for (uint8_t cell_reg = 1; cell_reg<ic[0].ic_reg.num_cv_reg+1; cell_reg++) //executes once for each of the LTC6811 cell voltage registers |
roger5641 | 0:b2692e0e4219 | 552 | // { |
roger5641 | 0:b2692e0e4219 | 553 | // LTC681x_rdcv_reg(cell_reg, total_ic,cell_data ); |
roger5641 | 0:b2692e0e4219 | 554 | // for (int current_ic = 0; current_ic<total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 555 | // { |
roger5641 | 0:b2692e0e4219 | 556 | // if (ic->isospi_reverse == false) |
roger5641 | 0:b2692e0e4219 | 557 | // { |
roger5641 | 0:b2692e0e4219 | 558 | // c_ic = current_ic; |
roger5641 | 0:b2692e0e4219 | 559 | // } |
roger5641 | 0:b2692e0e4219 | 560 | // else |
roger5641 | 0:b2692e0e4219 | 561 | // { |
roger5641 | 0:b2692e0e4219 | 562 | // c_ic = total_ic - current_ic - 1; |
roger5641 | 0:b2692e0e4219 | 563 | // } |
roger5641 | 0:b2692e0e4219 | 564 | // pec_error = pec_error + parse_cells(current_ic,cell_reg, cell_data, |
roger5641 | 0:b2692e0e4219 | 565 | // &ic[c_ic].cells.c_codes[0], |
roger5641 | 0:b2692e0e4219 | 566 | // &ic[c_ic].cells.pec_match[0]); |
roger5641 | 0:b2692e0e4219 | 567 | // } |
roger5641 | 0:b2692e0e4219 | 568 | // } |
roger5641 | 0:b2692e0e4219 | 569 | // } |
roger5641 | 0:b2692e0e4219 | 570 | // |
roger5641 | 0:b2692e0e4219 | 571 | // else |
roger5641 | 0:b2692e0e4219 | 572 | // { |
roger5641 | 0:b2692e0e4219 | 573 | // LTC681x_rdcv_reg(reg, total_ic,cell_data); |
roger5641 | 0:b2692e0e4219 | 574 | // |
roger5641 | 0:b2692e0e4219 | 575 | // for (int current_ic = 0; current_ic<total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 576 | // { |
roger5641 | 0:b2692e0e4219 | 577 | // if (ic->isospi_reverse == false) |
roger5641 | 0:b2692e0e4219 | 578 | // { |
roger5641 | 0:b2692e0e4219 | 579 | // c_ic = current_ic; |
roger5641 | 0:b2692e0e4219 | 580 | // } |
roger5641 | 0:b2692e0e4219 | 581 | // else |
roger5641 | 0:b2692e0e4219 | 582 | // { |
roger5641 | 0:b2692e0e4219 | 583 | // c_ic = total_ic - current_ic - 1; |
roger5641 | 0:b2692e0e4219 | 584 | // } |
roger5641 | 0:b2692e0e4219 | 585 | // pec_error = pec_error + parse_cells(current_ic,reg, &cell_data[8*c_ic], |
roger5641 | 0:b2692e0e4219 | 586 | // &ic[c_ic].cells.c_codes[0], |
roger5641 | 0:b2692e0e4219 | 587 | // &ic[c_ic].cells.pec_match[0]); |
roger5641 | 0:b2692e0e4219 | 588 | // } |
roger5641 | 0:b2692e0e4219 | 589 | // } |
roger5641 | 0:b2692e0e4219 | 590 | // LTC681x_check_pec(total_ic,CELL,ic); |
roger5641 | 0:b2692e0e4219 | 591 | // free(cell_data); |
roger5641 | 0:b2692e0e4219 | 592 | // return(pec_error); |
roger5641 | 0:b2692e0e4219 | 593 | //} |
roger5641 | 0:b2692e0e4219 | 594 | // |
roger5641 | 0:b2692e0e4219 | 595 | ////helper function that parses voltage measurement registers |
roger5641 | 0:b2692e0e4219 | 596 | //int8_t parse_cells(uint8_t current_ic, uint8_t cell_reg, uint8_t cell_data[], uint16_t *cell_codes, uint8_t *ic_pec) |
roger5641 | 0:b2692e0e4219 | 597 | //{ |
roger5641 | 0:b2692e0e4219 | 598 | // |
roger5641 | 0:b2692e0e4219 | 599 | // const uint8_t BYT_IN_REG = 6; |
roger5641 | 0:b2692e0e4219 | 600 | // const uint8_t CELL_IN_REG = 3; |
roger5641 | 0:b2692e0e4219 | 601 | // int8_t pec_error = 0; |
roger5641 | 0:b2692e0e4219 | 602 | // uint16_t parsed_cell; |
roger5641 | 0:b2692e0e4219 | 603 | // uint16_t received_pec; |
roger5641 | 0:b2692e0e4219 | 604 | // uint16_t data_pec; |
roger5641 | 0:b2692e0e4219 | 605 | // uint8_t data_counter = current_ic*NUM_RX_BYT; //data counter |
roger5641 | 0:b2692e0e4219 | 606 | // |
roger5641 | 0:b2692e0e4219 | 607 | // |
roger5641 | 0:b2692e0e4219 | 608 | // for (uint8_t current_cell = 0; current_cell<CELL_IN_REG; current_cell++) // This loop parses the read back data into cell voltages, it |
roger5641 | 0:b2692e0e4219 | 609 | // { |
roger5641 | 0:b2692e0e4219 | 610 | // // loops once for each of the 3 cell voltage codes in the register |
roger5641 | 0:b2692e0e4219 | 611 | // |
roger5641 | 0:b2692e0e4219 | 612 | // parsed_cell = cell_data[data_counter] + (cell_data[data_counter + 1] << 8);//Each cell code is received as two bytes and is combined to |
roger5641 | 0:b2692e0e4219 | 613 | // // create the parsed cell voltage code |
roger5641 | 0:b2692e0e4219 | 614 | // cell_codes[current_cell + ((cell_reg - 1) * CELL_IN_REG)] = parsed_cell; |
roger5641 | 0:b2692e0e4219 | 615 | // data_counter = data_counter + 2; //Because cell voltage codes are two bytes the data counter |
roger5641 | 0:b2692e0e4219 | 616 | // //must increment by two for each parsed cell code |
roger5641 | 0:b2692e0e4219 | 617 | // } |
roger5641 | 0:b2692e0e4219 | 618 | // |
roger5641 | 0:b2692e0e4219 | 619 | // received_pec = (cell_data[data_counter] << 8) | cell_data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th |
roger5641 | 0:b2692e0e4219 | 620 | // //after the 6 cell voltage data bytes |
roger5641 | 0:b2692e0e4219 | 621 | // data_pec = pec15_calc(BYT_IN_REG, &cell_data[(current_ic) * NUM_RX_BYT]); |
roger5641 | 0:b2692e0e4219 | 622 | // |
roger5641 | 0:b2692e0e4219 | 623 | // if (received_pec != data_pec) |
roger5641 | 0:b2692e0e4219 | 624 | // { |
roger5641 | 0:b2692e0e4219 | 625 | // pec_error = 1; //The pec_error variable is simply set negative if any PEC errors |
roger5641 | 0:b2692e0e4219 | 626 | // ic_pec[cell_reg-1]=1; |
roger5641 | 0:b2692e0e4219 | 627 | // } |
roger5641 | 0:b2692e0e4219 | 628 | // else |
roger5641 | 0:b2692e0e4219 | 629 | // { |
roger5641 | 0:b2692e0e4219 | 630 | // ic_pec[cell_reg-1]=0; |
roger5641 | 0:b2692e0e4219 | 631 | // } |
roger5641 | 0:b2692e0e4219 | 632 | // data_counter=data_counter+2; |
roger5641 | 0:b2692e0e4219 | 633 | // return(pec_error); |
roger5641 | 0:b2692e0e4219 | 634 | //} |
roger5641 | 0:b2692e0e4219 | 635 | // |
roger5641 | 0:b2692e0e4219 | 636 | ///* |
roger5641 | 0:b2692e0e4219 | 637 | //The function is used |
roger5641 | 0:b2692e0e4219 | 638 | //to read the parsed GPIO codes of the LTC6811. This function will send the requested |
roger5641 | 0:b2692e0e4219 | 639 | //read commands parse the data and store the gpio voltages in aux_codes variable |
roger5641 | 0:b2692e0e4219 | 640 | //*/ |
roger5641 | 0:b2692e0e4219 | 641 | //int8_t LTC681x_rdaux(uint8_t reg, //Determines which GPIO voltage register is read back. |
roger5641 | 0:b2692e0e4219 | 642 | // uint8_t total_ic,//the number of ICs in the system |
roger5641 | 0:b2692e0e4219 | 643 | // cell_asic ic[]//A two dimensional array of the gpio voltage codes. |
roger5641 | 0:b2692e0e4219 | 644 | // ) |
roger5641 | 0:b2692e0e4219 | 645 | //{ |
roger5641 | 0:b2692e0e4219 | 646 | // uint8_t *data; |
roger5641 | 0:b2692e0e4219 | 647 | // int8_t pec_error = 0; |
roger5641 | 0:b2692e0e4219 | 648 | // uint8_t c_ic =0; |
roger5641 | 0:b2692e0e4219 | 649 | // data = (uint8_t *) malloc((NUM_RX_BYT*total_ic)*sizeof(uint8_t)); |
roger5641 | 0:b2692e0e4219 | 650 | // |
roger5641 | 0:b2692e0e4219 | 651 | // if (reg == 0) |
roger5641 | 0:b2692e0e4219 | 652 | // { |
roger5641 | 0:b2692e0e4219 | 653 | // for (uint8_t gpio_reg = 1; gpio_reg<ic[0].ic_reg.num_gpio_reg+1; gpio_reg++) //executes once for each of the LTC6811 aux voltage registers |
roger5641 | 0:b2692e0e4219 | 654 | // { |
roger5641 | 0:b2692e0e4219 | 655 | // LTC681x_rdaux_reg(gpio_reg, total_ic,data); //Reads the raw auxiliary register data into the data[] array |
roger5641 | 0:b2692e0e4219 | 656 | // for (int current_ic = 0; current_ic<total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 657 | // { |
roger5641 | 0:b2692e0e4219 | 658 | // if (ic->isospi_reverse == false) |
roger5641 | 0:b2692e0e4219 | 659 | // { |
roger5641 | 0:b2692e0e4219 | 660 | // c_ic = current_ic; |
roger5641 | 0:b2692e0e4219 | 661 | // } |
roger5641 | 0:b2692e0e4219 | 662 | // else |
roger5641 | 0:b2692e0e4219 | 663 | // { |
roger5641 | 0:b2692e0e4219 | 664 | // c_ic = total_ic - current_ic - 1; |
roger5641 | 0:b2692e0e4219 | 665 | // } |
roger5641 | 0:b2692e0e4219 | 666 | // pec_error = parse_cells(current_ic,gpio_reg, data, |
roger5641 | 0:b2692e0e4219 | 667 | // &ic[c_ic].aux.a_codes[0], |
roger5641 | 0:b2692e0e4219 | 668 | // &ic[c_ic].aux.pec_match[0]); |
roger5641 | 0:b2692e0e4219 | 669 | // |
roger5641 | 0:b2692e0e4219 | 670 | // } |
roger5641 | 0:b2692e0e4219 | 671 | // } |
roger5641 | 0:b2692e0e4219 | 672 | // } |
roger5641 | 0:b2692e0e4219 | 673 | // else |
roger5641 | 0:b2692e0e4219 | 674 | // { |
roger5641 | 0:b2692e0e4219 | 675 | // LTC681x_rdaux_reg(reg, total_ic, data); |
roger5641 | 0:b2692e0e4219 | 676 | // |
roger5641 | 0:b2692e0e4219 | 677 | // for (int current_ic = 0; current_ic<total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 678 | // { |
roger5641 | 0:b2692e0e4219 | 679 | // if (ic->isospi_reverse == false) |
roger5641 | 0:b2692e0e4219 | 680 | // { |
roger5641 | 0:b2692e0e4219 | 681 | // c_ic = current_ic; |
roger5641 | 0:b2692e0e4219 | 682 | // } |
roger5641 | 0:b2692e0e4219 | 683 | // else |
roger5641 | 0:b2692e0e4219 | 684 | // { |
roger5641 | 0:b2692e0e4219 | 685 | // c_ic = total_ic - current_ic - 1; |
roger5641 | 0:b2692e0e4219 | 686 | // } |
roger5641 | 0:b2692e0e4219 | 687 | // pec_error = parse_cells(current_ic,reg, data, |
roger5641 | 0:b2692e0e4219 | 688 | // &ic[c_ic].aux.a_codes[0], |
roger5641 | 0:b2692e0e4219 | 689 | // &ic[c_ic].aux.pec_match[0]); |
roger5641 | 0:b2692e0e4219 | 690 | // } |
roger5641 | 0:b2692e0e4219 | 691 | // |
roger5641 | 0:b2692e0e4219 | 692 | // } |
roger5641 | 0:b2692e0e4219 | 693 | // LTC681x_check_pec(total_ic,AUX,ic); |
roger5641 | 0:b2692e0e4219 | 694 | // free(data); |
roger5641 | 0:b2692e0e4219 | 695 | // return (pec_error); |
roger5641 | 0:b2692e0e4219 | 696 | //} |
roger5641 | 0:b2692e0e4219 | 697 | // |
roger5641 | 0:b2692e0e4219 | 698 | //// Reads and parses the LTC681x stat registers. |
roger5641 | 0:b2692e0e4219 | 699 | //int8_t LTC681x_rdstat(uint8_t reg, //Determines which Stat register is read back. |
roger5641 | 0:b2692e0e4219 | 700 | // uint8_t total_ic,//the number of ICs in the system |
roger5641 | 0:b2692e0e4219 | 701 | // cell_asic ic[] |
roger5641 | 0:b2692e0e4219 | 702 | // ) |
roger5641 | 0:b2692e0e4219 | 703 | // |
roger5641 | 0:b2692e0e4219 | 704 | //{ |
roger5641 | 0:b2692e0e4219 | 705 | // |
roger5641 | 0:b2692e0e4219 | 706 | // const uint8_t BYT_IN_REG = 6; |
roger5641 | 0:b2692e0e4219 | 707 | // const uint8_t GPIO_IN_REG = 3; |
roger5641 | 0:b2692e0e4219 | 708 | // |
roger5641 | 0:b2692e0e4219 | 709 | // uint8_t *data; |
roger5641 | 0:b2692e0e4219 | 710 | // uint8_t data_counter = 0; |
roger5641 | 0:b2692e0e4219 | 711 | // int8_t pec_error = 0; |
roger5641 | 0:b2692e0e4219 | 712 | // uint16_t parsed_stat; |
roger5641 | 0:b2692e0e4219 | 713 | // uint16_t received_pec; |
roger5641 | 0:b2692e0e4219 | 714 | // uint16_t data_pec; |
roger5641 | 0:b2692e0e4219 | 715 | // uint8_t c_ic = 0; |
roger5641 | 0:b2692e0e4219 | 716 | // data = (uint8_t *) malloc((NUM_RX_BYT*total_ic)*sizeof(uint8_t)); |
roger5641 | 0:b2692e0e4219 | 717 | // |
roger5641 | 0:b2692e0e4219 | 718 | // if (reg == 0) |
roger5641 | 0:b2692e0e4219 | 719 | // { |
roger5641 | 0:b2692e0e4219 | 720 | // |
roger5641 | 0:b2692e0e4219 | 721 | // for (uint8_t stat_reg = 1; stat_reg< 3; stat_reg++) //executes once for each of the LTC6811 stat voltage registers |
roger5641 | 0:b2692e0e4219 | 722 | // { |
roger5641 | 0:b2692e0e4219 | 723 | // data_counter = 0; |
roger5641 | 0:b2692e0e4219 | 724 | // LTC681x_rdstat_reg(stat_reg, total_ic,data); //Reads the raw statiliary register data into the data[] array |
roger5641 | 0:b2692e0e4219 | 725 | // |
roger5641 | 0:b2692e0e4219 | 726 | // for (uint8_t current_ic = 0 ; current_ic < total_ic; current_ic++) // executes for every LTC6811 in the daisy chain |
roger5641 | 0:b2692e0e4219 | 727 | // { |
roger5641 | 0:b2692e0e4219 | 728 | // if (ic->isospi_reverse == false) |
roger5641 | 0:b2692e0e4219 | 729 | // { |
roger5641 | 0:b2692e0e4219 | 730 | // c_ic = current_ic; |
roger5641 | 0:b2692e0e4219 | 731 | // } |
roger5641 | 0:b2692e0e4219 | 732 | // else |
roger5641 | 0:b2692e0e4219 | 733 | // { |
roger5641 | 0:b2692e0e4219 | 734 | // c_ic = total_ic - current_ic - 1; |
roger5641 | 0:b2692e0e4219 | 735 | // } |
roger5641 | 0:b2692e0e4219 | 736 | // // current_ic is used as the IC counter |
roger5641 | 0:b2692e0e4219 | 737 | // if (stat_reg ==1) |
roger5641 | 0:b2692e0e4219 | 738 | // { |
roger5641 | 0:b2692e0e4219 | 739 | // for (uint8_t current_gpio = 0; current_gpio< GPIO_IN_REG; current_gpio++) // This loop parses the read back data into GPIO voltages, it |
roger5641 | 0:b2692e0e4219 | 740 | // { |
roger5641 | 0:b2692e0e4219 | 741 | // // loops once for each of the 3 gpio voltage codes in the register |
roger5641 | 0:b2692e0e4219 | 742 | // |
roger5641 | 0:b2692e0e4219 | 743 | // parsed_stat = data[data_counter] + (data[data_counter+1]<<8); //Each gpio codes is received as two bytes and is combined to |
roger5641 | 0:b2692e0e4219 | 744 | // ic[c_ic].stat.stat_codes[current_gpio] = parsed_stat; |
roger5641 | 0:b2692e0e4219 | 745 | // data_counter=data_counter+2; //Because gpio voltage codes are two bytes the data counter |
roger5641 | 0:b2692e0e4219 | 746 | // |
roger5641 | 0:b2692e0e4219 | 747 | // } |
roger5641 | 0:b2692e0e4219 | 748 | // } |
roger5641 | 0:b2692e0e4219 | 749 | // else if (stat_reg == 2) |
roger5641 | 0:b2692e0e4219 | 750 | // { |
roger5641 | 0:b2692e0e4219 | 751 | // parsed_stat = data[data_counter] + (data[data_counter+1]<<8); //Each gpio codes is received as two bytes and is combined to |
roger5641 | 0:b2692e0e4219 | 752 | // data_counter = data_counter +2; |
roger5641 | 0:b2692e0e4219 | 753 | // ic[c_ic].stat.stat_codes[3] = parsed_stat; |
roger5641 | 0:b2692e0e4219 | 754 | // ic[c_ic].stat.flags[0] = data[data_counter++]; |
roger5641 | 0:b2692e0e4219 | 755 | // ic[c_ic].stat.flags[1] = data[data_counter++]; |
roger5641 | 0:b2692e0e4219 | 756 | // ic[c_ic].stat.flags[2] = data[data_counter++]; |
roger5641 | 0:b2692e0e4219 | 757 | // ic[c_ic].stat.mux_fail[0] = (data[data_counter] & 0x02)>>1; |
roger5641 | 0:b2692e0e4219 | 758 | // ic[c_ic].stat.thsd[0] = data[data_counter++] & 0x01; |
roger5641 | 0:b2692e0e4219 | 759 | // } |
roger5641 | 0:b2692e0e4219 | 760 | // |
roger5641 | 0:b2692e0e4219 | 761 | // received_pec = (data[data_counter]<<8)+ data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th |
roger5641 | 0:b2692e0e4219 | 762 | // //after the 6 gpio voltage data bytes |
roger5641 | 0:b2692e0e4219 | 763 | // data_pec = pec15_calc(BYT_IN_REG, &data[current_ic*NUM_RX_BYT]); |
roger5641 | 0:b2692e0e4219 | 764 | // |
roger5641 | 0:b2692e0e4219 | 765 | // if (received_pec != data_pec) |
roger5641 | 0:b2692e0e4219 | 766 | // { |
roger5641 | 0:b2692e0e4219 | 767 | // pec_error = -1; //The pec_error variable is simply set negative if any PEC errors |
roger5641 | 0:b2692e0e4219 | 768 | // ic[c_ic].stat.pec_match[stat_reg-1]=1; |
roger5641 | 0:b2692e0e4219 | 769 | // //are detected in the received serial data |
roger5641 | 0:b2692e0e4219 | 770 | // } |
roger5641 | 0:b2692e0e4219 | 771 | // else |
roger5641 | 0:b2692e0e4219 | 772 | // { |
roger5641 | 0:b2692e0e4219 | 773 | // ic[c_ic].stat.pec_match[stat_reg-1]=0; |
roger5641 | 0:b2692e0e4219 | 774 | // } |
roger5641 | 0:b2692e0e4219 | 775 | // |
roger5641 | 0:b2692e0e4219 | 776 | // data_counter=data_counter+2; //Because the transmitted PEC code is 2 bytes long the data_counter |
roger5641 | 0:b2692e0e4219 | 777 | // //must be incremented by 2 bytes to point to the next ICs gpio voltage data |
roger5641 | 0:b2692e0e4219 | 778 | // } |
roger5641 | 0:b2692e0e4219 | 779 | // |
roger5641 | 0:b2692e0e4219 | 780 | // |
roger5641 | 0:b2692e0e4219 | 781 | // } |
roger5641 | 0:b2692e0e4219 | 782 | // |
roger5641 | 0:b2692e0e4219 | 783 | // } |
roger5641 | 0:b2692e0e4219 | 784 | // else |
roger5641 | 0:b2692e0e4219 | 785 | // { |
roger5641 | 0:b2692e0e4219 | 786 | // |
roger5641 | 0:b2692e0e4219 | 787 | // LTC681x_rdstat_reg(reg, total_ic, data); |
roger5641 | 0:b2692e0e4219 | 788 | // for (int current_ic = 0 ; current_ic < total_ic; current_ic++) // executes for every LTC6811 in the daisy chain |
roger5641 | 0:b2692e0e4219 | 789 | // { |
roger5641 | 0:b2692e0e4219 | 790 | // // current_ic is used as an IC counter |
roger5641 | 0:b2692e0e4219 | 791 | // if (ic->isospi_reverse == false) |
roger5641 | 0:b2692e0e4219 | 792 | // { |
roger5641 | 0:b2692e0e4219 | 793 | // c_ic = current_ic; |
roger5641 | 0:b2692e0e4219 | 794 | // } |
roger5641 | 0:b2692e0e4219 | 795 | // else |
roger5641 | 0:b2692e0e4219 | 796 | // { |
roger5641 | 0:b2692e0e4219 | 797 | // c_ic = total_ic - current_ic - 1; |
roger5641 | 0:b2692e0e4219 | 798 | // } |
roger5641 | 0:b2692e0e4219 | 799 | // if (reg ==1) |
roger5641 | 0:b2692e0e4219 | 800 | // { |
roger5641 | 0:b2692e0e4219 | 801 | // for (uint8_t current_gpio = 0; current_gpio< GPIO_IN_REG; current_gpio++) // This loop parses the read back data into GPIO voltages, it |
roger5641 | 0:b2692e0e4219 | 802 | // { |
roger5641 | 0:b2692e0e4219 | 803 | // // loops once for each of the 3 gpio voltage codes in the register |
roger5641 | 0:b2692e0e4219 | 804 | // parsed_stat = data[data_counter] + (data[data_counter+1]<<8); //Each gpio codes is received as two bytes and is combined to |
roger5641 | 0:b2692e0e4219 | 805 | // // create the parsed gpio voltage code |
roger5641 | 0:b2692e0e4219 | 806 | // |
roger5641 | 0:b2692e0e4219 | 807 | // ic[c_ic].stat.stat_codes[current_gpio] = parsed_stat; |
roger5641 | 0:b2692e0e4219 | 808 | // data_counter=data_counter+2; //Because gpio voltage codes are two bytes the data counter |
roger5641 | 0:b2692e0e4219 | 809 | // //must increment by two for each parsed gpio voltage code |
roger5641 | 0:b2692e0e4219 | 810 | // |
roger5641 | 0:b2692e0e4219 | 811 | // } |
roger5641 | 0:b2692e0e4219 | 812 | // } |
roger5641 | 0:b2692e0e4219 | 813 | // else if (reg == 2) |
roger5641 | 0:b2692e0e4219 | 814 | // { |
roger5641 | 0:b2692e0e4219 | 815 | // parsed_stat = data[data_counter++] + (data[data_counter++]<<8); //Each gpio codes is received as two bytes and is combined to |
roger5641 | 0:b2692e0e4219 | 816 | // ic[c_ic].stat.stat_codes[3] = parsed_stat; |
roger5641 | 0:b2692e0e4219 | 817 | // ic[c_ic].stat.flags[0] = data[data_counter++]; |
roger5641 | 0:b2692e0e4219 | 818 | // ic[c_ic].stat.flags[1] = data[data_counter++]; |
roger5641 | 0:b2692e0e4219 | 819 | // ic[c_ic].stat.flags[2] = data[data_counter++]; |
roger5641 | 0:b2692e0e4219 | 820 | // ic[c_ic].stat.mux_fail[0] = (data[data_counter] & 0x02)>>1; |
roger5641 | 0:b2692e0e4219 | 821 | // ic[c_ic].stat.thsd[0] = data[data_counter++] & 0x01; |
roger5641 | 0:b2692e0e4219 | 822 | // } |
roger5641 | 0:b2692e0e4219 | 823 | // |
roger5641 | 0:b2692e0e4219 | 824 | // |
roger5641 | 0:b2692e0e4219 | 825 | // received_pec = (data[data_counter]<<8)+ data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th |
roger5641 | 0:b2692e0e4219 | 826 | // //after the 6 gpio voltage data bytes |
roger5641 | 0:b2692e0e4219 | 827 | // data_pec = pec15_calc(BYT_IN_REG, &data[current_ic*NUM_RX_BYT]); |
roger5641 | 0:b2692e0e4219 | 828 | // if (received_pec != data_pec) |
roger5641 | 0:b2692e0e4219 | 829 | // { |
roger5641 | 0:b2692e0e4219 | 830 | // pec_error = -1; //The pec_error variable is simply set negative if any PEC errors |
roger5641 | 0:b2692e0e4219 | 831 | // ic[c_ic].stat.pec_match[reg-1]=1; |
roger5641 | 0:b2692e0e4219 | 832 | // |
roger5641 | 0:b2692e0e4219 | 833 | // } |
roger5641 | 0:b2692e0e4219 | 834 | // |
roger5641 | 0:b2692e0e4219 | 835 | // data_counter=data_counter+2; |
roger5641 | 0:b2692e0e4219 | 836 | // } |
roger5641 | 0:b2692e0e4219 | 837 | // } |
roger5641 | 0:b2692e0e4219 | 838 | // LTC681x_check_pec(total_ic,STAT,ic); |
roger5641 | 0:b2692e0e4219 | 839 | // free(data); |
roger5641 | 0:b2692e0e4219 | 840 | // return (pec_error); |
roger5641 | 0:b2692e0e4219 | 841 | //} |
roger5641 | 0:b2692e0e4219 | 842 | // |
roger5641 | 0:b2692e0e4219 | 843 | ////Write the LTC681x CFGRA |
roger5641 | 0:b2692e0e4219 | 844 | //void LTC681x_wrcfg(uint8_t total_ic, //The number of ICs being written to |
roger5641 | 0:b2692e0e4219 | 845 | // cell_asic ic[] |
roger5641 | 0:b2692e0e4219 | 846 | // ) |
roger5641 | 0:b2692e0e4219 | 847 | //{ |
roger5641 | 0:b2692e0e4219 | 848 | // uint8_t cmd[2] = {0x00 , 0x01} ; |
roger5641 | 0:b2692e0e4219 | 849 | // uint8_t write_buffer[256]; |
roger5641 | 0:b2692e0e4219 | 850 | // uint8_t write_count = 0; |
roger5641 | 0:b2692e0e4219 | 851 | // uint8_t c_ic = 0; |
roger5641 | 0:b2692e0e4219 | 852 | // for (uint8_t current_ic = 0; current_ic<total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 853 | // { |
roger5641 | 0:b2692e0e4219 | 854 | // if (ic->isospi_reverse == true) |
roger5641 | 0:b2692e0e4219 | 855 | // { |
roger5641 | 0:b2692e0e4219 | 856 | // c_ic = current_ic; |
roger5641 | 0:b2692e0e4219 | 857 | // } |
roger5641 | 0:b2692e0e4219 | 858 | // else |
roger5641 | 0:b2692e0e4219 | 859 | // { |
roger5641 | 0:b2692e0e4219 | 860 | // c_ic = total_ic - current_ic - 1; |
roger5641 | 0:b2692e0e4219 | 861 | // } |
roger5641 | 0:b2692e0e4219 | 862 | // |
roger5641 | 0:b2692e0e4219 | 863 | // for (uint8_t data = 0; data<6; data++) |
roger5641 | 0:b2692e0e4219 | 864 | // { |
roger5641 | 0:b2692e0e4219 | 865 | // write_buffer[write_count] = ic[c_ic].config.tx_data[data]; |
roger5641 | 0:b2692e0e4219 | 866 | // write_count++; |
roger5641 | 0:b2692e0e4219 | 867 | // } |
roger5641 | 0:b2692e0e4219 | 868 | // } |
roger5641 | 0:b2692e0e4219 | 869 | // write_68(total_ic, cmd, write_buffer); |
roger5641 | 0:b2692e0e4219 | 870 | //} |
roger5641 | 0:b2692e0e4219 | 871 | // |
roger5641 | 0:b2692e0e4219 | 872 | ////Read CFGA |
roger5641 | 0:b2692e0e4219 | 873 | //int8_t LTC681x_rdcfg(uint8_t total_ic, //Number of ICs in the system |
roger5641 | 0:b2692e0e4219 | 874 | // cell_asic ic[] |
roger5641 | 0:b2692e0e4219 | 875 | // ) |
roger5641 | 0:b2692e0e4219 | 876 | //{ |
roger5641 | 0:b2692e0e4219 | 877 | // uint8_t cmd[2]= {0x00 , 0x02}; |
roger5641 | 0:b2692e0e4219 | 878 | // uint8_t read_buffer[256]; |
roger5641 | 0:b2692e0e4219 | 879 | // int8_t pec_error = 0; |
roger5641 | 0:b2692e0e4219 | 880 | // uint16_t data_pec; |
roger5641 | 0:b2692e0e4219 | 881 | // uint16_t calc_pec; |
roger5641 | 0:b2692e0e4219 | 882 | // uint8_t c_ic = 0; |
roger5641 | 0:b2692e0e4219 | 883 | // pec_error = read_68(total_ic, cmd, read_buffer); |
roger5641 | 0:b2692e0e4219 | 884 | // for (uint8_t current_ic = 0; current_ic<total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 885 | // { |
roger5641 | 0:b2692e0e4219 | 886 | // if (ic->isospi_reverse == false) |
roger5641 | 0:b2692e0e4219 | 887 | // { |
roger5641 | 0:b2692e0e4219 | 888 | // c_ic = current_ic; |
roger5641 | 0:b2692e0e4219 | 889 | // } |
roger5641 | 0:b2692e0e4219 | 890 | // else |
roger5641 | 0:b2692e0e4219 | 891 | // { |
roger5641 | 0:b2692e0e4219 | 892 | // c_ic = total_ic - current_ic - 1; |
roger5641 | 0:b2692e0e4219 | 893 | // } |
roger5641 | 0:b2692e0e4219 | 894 | // |
roger5641 | 0:b2692e0e4219 | 895 | // for (int byte=0; byte<8; byte++) |
roger5641 | 0:b2692e0e4219 | 896 | // { |
roger5641 | 0:b2692e0e4219 | 897 | // ic[c_ic].config.rx_data[byte] = read_buffer[byte+(8*current_ic)]; |
roger5641 | 0:b2692e0e4219 | 898 | // } |
roger5641 | 0:b2692e0e4219 | 899 | // calc_pec = pec15_calc(6,&read_buffer[8*current_ic]); |
roger5641 | 0:b2692e0e4219 | 900 | // data_pec = read_buffer[7+(8*current_ic)] | (read_buffer[6+(8*current_ic)]<<8); |
roger5641 | 0:b2692e0e4219 | 901 | // if (calc_pec != data_pec ) |
roger5641 | 0:b2692e0e4219 | 902 | // { |
roger5641 | 0:b2692e0e4219 | 903 | // ic[c_ic].config.rx_pec_match = 1; |
roger5641 | 0:b2692e0e4219 | 904 | // } |
roger5641 | 0:b2692e0e4219 | 905 | // else ic[c_ic].config.rx_pec_match = 0; |
roger5641 | 0:b2692e0e4219 | 906 | // } |
roger5641 | 0:b2692e0e4219 | 907 | // LTC681x_check_pec(total_ic,CFGR,ic); |
roger5641 | 0:b2692e0e4219 | 908 | // return(pec_error); |
roger5641 | 0:b2692e0e4219 | 909 | //} |
roger5641 | 0:b2692e0e4219 | 910 | // |
roger5641 | 0:b2692e0e4219 | 911 | ////Looks up the result pattern for digital filter self test |
roger5641 | 0:b2692e0e4219 | 912 | //uint16_t LTC681x_st_lookup( |
roger5641 | 0:b2692e0e4219 | 913 | // uint8_t MD, //ADC Mode |
roger5641 | 0:b2692e0e4219 | 914 | // uint8_t ST //Self Test |
roger5641 | 0:b2692e0e4219 | 915 | //) |
roger5641 | 0:b2692e0e4219 | 916 | //{ |
roger5641 | 0:b2692e0e4219 | 917 | // uint16_t test_pattern = 0; |
roger5641 | 0:b2692e0e4219 | 918 | // if (MD == 1) |
roger5641 | 0:b2692e0e4219 | 919 | // { |
roger5641 | 0:b2692e0e4219 | 920 | // if (ST == 1) |
roger5641 | 0:b2692e0e4219 | 921 | // { |
roger5641 | 0:b2692e0e4219 | 922 | // test_pattern = 0x9565; |
roger5641 | 0:b2692e0e4219 | 923 | // } |
roger5641 | 0:b2692e0e4219 | 924 | // else |
roger5641 | 0:b2692e0e4219 | 925 | // { |
roger5641 | 0:b2692e0e4219 | 926 | // test_pattern = 0x6A9A; |
roger5641 | 0:b2692e0e4219 | 927 | // } |
roger5641 | 0:b2692e0e4219 | 928 | // } |
roger5641 | 0:b2692e0e4219 | 929 | // else |
roger5641 | 0:b2692e0e4219 | 930 | // { |
roger5641 | 0:b2692e0e4219 | 931 | // if (ST == 1) |
roger5641 | 0:b2692e0e4219 | 932 | // { |
roger5641 | 0:b2692e0e4219 | 933 | // test_pattern = 0x9555; |
roger5641 | 0:b2692e0e4219 | 934 | // } |
roger5641 | 0:b2692e0e4219 | 935 | // else |
roger5641 | 0:b2692e0e4219 | 936 | // { |
roger5641 | 0:b2692e0e4219 | 937 | // test_pattern = 0x6AAA; |
roger5641 | 0:b2692e0e4219 | 938 | // } |
roger5641 | 0:b2692e0e4219 | 939 | // } |
roger5641 | 0:b2692e0e4219 | 940 | // return(test_pattern); |
roger5641 | 0:b2692e0e4219 | 941 | //} |
roger5641 | 0:b2692e0e4219 | 942 | // |
roger5641 | 0:b2692e0e4219 | 943 | ////Clears all of the DCC bits in the configuration registers |
roger5641 | 0:b2692e0e4219 | 944 | //void clear_discharge(uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 945 | //{ |
roger5641 | 0:b2692e0e4219 | 946 | // for (int i=0; i<total_ic; i++) |
roger5641 | 0:b2692e0e4219 | 947 | // { |
roger5641 | 0:b2692e0e4219 | 948 | // ic[i].config.tx_data[4] = 0; |
roger5641 | 0:b2692e0e4219 | 949 | // ic[i].config.tx_data[5] = 0; |
roger5641 | 0:b2692e0e4219 | 950 | // } |
roger5641 | 0:b2692e0e4219 | 951 | //} |
roger5641 | 0:b2692e0e4219 | 952 | // |
roger5641 | 0:b2692e0e4219 | 953 | //// Runs the Digital Filter Self Test |
roger5641 | 0:b2692e0e4219 | 954 | //int16_t LTC681x_run_cell_adc_st(uint8_t adc_reg,uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 955 | //{ |
roger5641 | 0:b2692e0e4219 | 956 | // int16_t error = 0; |
roger5641 | 0:b2692e0e4219 | 957 | // uint16_t expected_result = 0; |
roger5641 | 0:b2692e0e4219 | 958 | // for (int self_test = 1; self_test<3; self_test++) |
roger5641 | 0:b2692e0e4219 | 959 | // { |
roger5641 | 0:b2692e0e4219 | 960 | // |
roger5641 | 0:b2692e0e4219 | 961 | // expected_result = LTC681x_st_lookup(2,self_test); |
roger5641 | 0:b2692e0e4219 | 962 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 963 | // switch (adc_reg) |
roger5641 | 0:b2692e0e4219 | 964 | // { |
roger5641 | 0:b2692e0e4219 | 965 | // case CELL: |
roger5641 | 0:b2692e0e4219 | 966 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 967 | // LTC681x_clrcell(); |
roger5641 | 0:b2692e0e4219 | 968 | // LTC681x_cvst(2,self_test); |
roger5641 | 0:b2692e0e4219 | 969 | // LTC681x_pollAdc();//this isn't working |
roger5641 | 0:b2692e0e4219 | 970 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 971 | // error = LTC681x_rdcv(0, total_ic,ic); |
roger5641 | 0:b2692e0e4219 | 972 | // for (int cic = 0; cic < total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 973 | // { |
roger5641 | 0:b2692e0e4219 | 974 | // for (int channel=0; channel< ic[cic].ic_reg.cell_channels; channel++) |
roger5641 | 0:b2692e0e4219 | 975 | // { |
roger5641 | 0:b2692e0e4219 | 976 | // if (ic[cic].cells.c_codes[channel] != expected_result) |
roger5641 | 0:b2692e0e4219 | 977 | // { |
roger5641 | 0:b2692e0e4219 | 978 | // error = error+1; |
roger5641 | 0:b2692e0e4219 | 979 | // } |
roger5641 | 0:b2692e0e4219 | 980 | // } |
roger5641 | 0:b2692e0e4219 | 981 | // } |
roger5641 | 0:b2692e0e4219 | 982 | // break; |
roger5641 | 0:b2692e0e4219 | 983 | // case AUX: |
roger5641 | 0:b2692e0e4219 | 984 | // error = 0; |
roger5641 | 0:b2692e0e4219 | 985 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 986 | // LTC681x_clraux(); |
roger5641 | 0:b2692e0e4219 | 987 | // LTC681x_axst(2,self_test); |
roger5641 | 0:b2692e0e4219 | 988 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 989 | // wait_ms(10); |
roger5641 | 0:b2692e0e4219 | 990 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 991 | // LTC681x_rdaux(0, total_ic,ic); |
roger5641 | 0:b2692e0e4219 | 992 | // for (int cic = 0; cic < total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 993 | // { |
roger5641 | 0:b2692e0e4219 | 994 | // for (int channel=0; channel< ic[cic].ic_reg.aux_channels; channel++) |
roger5641 | 0:b2692e0e4219 | 995 | // { |
roger5641 | 0:b2692e0e4219 | 996 | // if (ic[cic].aux.a_codes[channel] != expected_result) |
roger5641 | 0:b2692e0e4219 | 997 | // { |
roger5641 | 0:b2692e0e4219 | 998 | // error = error+1; |
roger5641 | 0:b2692e0e4219 | 999 | // } |
roger5641 | 0:b2692e0e4219 | 1000 | // } |
roger5641 | 0:b2692e0e4219 | 1001 | // } |
roger5641 | 0:b2692e0e4219 | 1002 | // break; |
roger5641 | 0:b2692e0e4219 | 1003 | // case STAT: |
roger5641 | 0:b2692e0e4219 | 1004 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1005 | // LTC681x_clrstat(); |
roger5641 | 0:b2692e0e4219 | 1006 | // LTC681x_statst(2,self_test); |
roger5641 | 0:b2692e0e4219 | 1007 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 1008 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1009 | // error = LTC681x_rdstat(0,total_ic,ic); |
roger5641 | 0:b2692e0e4219 | 1010 | // for (int cic = 0; cic < total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 1011 | // { |
roger5641 | 0:b2692e0e4219 | 1012 | // for (int channel=0; channel< ic[cic].ic_reg.stat_channels; channel++) |
roger5641 | 0:b2692e0e4219 | 1013 | // { |
roger5641 | 0:b2692e0e4219 | 1014 | // if (ic[cic].stat.stat_codes[channel] != expected_result) |
roger5641 | 0:b2692e0e4219 | 1015 | // { |
roger5641 | 0:b2692e0e4219 | 1016 | // error = error+1; |
roger5641 | 0:b2692e0e4219 | 1017 | // } |
roger5641 | 0:b2692e0e4219 | 1018 | // } |
roger5641 | 0:b2692e0e4219 | 1019 | // } |
roger5641 | 0:b2692e0e4219 | 1020 | // break; |
roger5641 | 0:b2692e0e4219 | 1021 | // |
roger5641 | 0:b2692e0e4219 | 1022 | // default: |
roger5641 | 0:b2692e0e4219 | 1023 | // error = -1; |
roger5641 | 0:b2692e0e4219 | 1024 | // break; |
roger5641 | 0:b2692e0e4219 | 1025 | // } |
roger5641 | 0:b2692e0e4219 | 1026 | // } |
roger5641 | 0:b2692e0e4219 | 1027 | // return(error); |
roger5641 | 0:b2692e0e4219 | 1028 | //} |
roger5641 | 0:b2692e0e4219 | 1029 | // |
roger5641 | 0:b2692e0e4219 | 1030 | ////runs the redundancy self test |
roger5641 | 0:b2692e0e4219 | 1031 | //int16_t LTC681x_run_adc_redundancy_st(uint8_t adc_mode, uint8_t adc_reg, uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 1032 | //{ |
roger5641 | 0:b2692e0e4219 | 1033 | // int16_t error = 0; |
roger5641 | 0:b2692e0e4219 | 1034 | // for (int self_test = 1; self_test<3; self_test++) |
roger5641 | 0:b2692e0e4219 | 1035 | // { |
roger5641 | 0:b2692e0e4219 | 1036 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1037 | // switch (adc_reg) |
roger5641 | 0:b2692e0e4219 | 1038 | // { |
roger5641 | 0:b2692e0e4219 | 1039 | // case AUX: |
roger5641 | 0:b2692e0e4219 | 1040 | // LTC681x_clraux(); |
roger5641 | 0:b2692e0e4219 | 1041 | // LTC681x_adaxd(adc_mode,AUX_CH_ALL); |
roger5641 | 0:b2692e0e4219 | 1042 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 1043 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1044 | // error = LTC681x_rdaux(0, total_ic,ic); |
roger5641 | 0:b2692e0e4219 | 1045 | // for (int cic = 0; cic < total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 1046 | // { |
roger5641 | 0:b2692e0e4219 | 1047 | // for (int channel=0; channel< ic[cic].ic_reg.aux_channels; channel++) |
roger5641 | 0:b2692e0e4219 | 1048 | // { |
roger5641 | 0:b2692e0e4219 | 1049 | // if (ic[cic].aux.a_codes[channel] >= 65280) |
roger5641 | 0:b2692e0e4219 | 1050 | // { |
roger5641 | 0:b2692e0e4219 | 1051 | // error = error+1; |
roger5641 | 0:b2692e0e4219 | 1052 | // } |
roger5641 | 0:b2692e0e4219 | 1053 | // } |
roger5641 | 0:b2692e0e4219 | 1054 | // } |
roger5641 | 0:b2692e0e4219 | 1055 | // break; |
roger5641 | 0:b2692e0e4219 | 1056 | // case STAT: |
roger5641 | 0:b2692e0e4219 | 1057 | // LTC681x_clrstat(); |
roger5641 | 0:b2692e0e4219 | 1058 | // LTC681x_adstatd(adc_mode,STAT_CH_ALL); |
roger5641 | 0:b2692e0e4219 | 1059 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 1060 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1061 | // error = LTC681x_rdstat(0,total_ic,ic); |
roger5641 | 0:b2692e0e4219 | 1062 | // for (int cic = 0; cic < total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 1063 | // { |
roger5641 | 0:b2692e0e4219 | 1064 | // for (int channel=0; channel< ic[cic].ic_reg.stat_channels; channel++) |
roger5641 | 0:b2692e0e4219 | 1065 | // { |
roger5641 | 0:b2692e0e4219 | 1066 | // if (ic[cic].stat.stat_codes[channel] >= 65280) |
roger5641 | 0:b2692e0e4219 | 1067 | // { |
roger5641 | 0:b2692e0e4219 | 1068 | // error = error+1; |
roger5641 | 0:b2692e0e4219 | 1069 | // } |
roger5641 | 0:b2692e0e4219 | 1070 | // } |
roger5641 | 0:b2692e0e4219 | 1071 | // } |
roger5641 | 0:b2692e0e4219 | 1072 | // break; |
roger5641 | 0:b2692e0e4219 | 1073 | // |
roger5641 | 0:b2692e0e4219 | 1074 | // default: |
roger5641 | 0:b2692e0e4219 | 1075 | // error = -1; |
roger5641 | 0:b2692e0e4219 | 1076 | // break; |
roger5641 | 0:b2692e0e4219 | 1077 | // } |
roger5641 | 0:b2692e0e4219 | 1078 | // } |
roger5641 | 0:b2692e0e4219 | 1079 | // return(error); |
roger5641 | 0:b2692e0e4219 | 1080 | //} |
roger5641 | 0:b2692e0e4219 | 1081 | // |
roger5641 | 0:b2692e0e4219 | 1082 | ////Runs the datasheet algorithm for open wire |
roger5641 | 0:b2692e0e4219 | 1083 | //void LTC681x_run_openwire(uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 1084 | //{ |
roger5641 | 0:b2692e0e4219 | 1085 | // uint16_t OPENWIRE_THRESHOLD = 4000; |
roger5641 | 0:b2692e0e4219 | 1086 | // const uint8_t N_CHANNELS = ic[0].ic_reg.cell_channels; |
roger5641 | 0:b2692e0e4219 | 1087 | // |
roger5641 | 0:b2692e0e4219 | 1088 | // cell_asic pullUp_cell_codes[total_ic]; |
roger5641 | 0:b2692e0e4219 | 1089 | // cell_asic pullDwn_cell_codes[total_ic]; |
roger5641 | 0:b2692e0e4219 | 1090 | // cell_asic openWire_delta[total_ic]; |
roger5641 | 0:b2692e0e4219 | 1091 | // int8_t error; |
roger5641 | 0:b2692e0e4219 | 1092 | // |
roger5641 | 0:b2692e0e4219 | 1093 | // wakeup_sleep(total_ic); |
roger5641 | 0:b2692e0e4219 | 1094 | // LTC681x_adow(MD_7KHZ_3KHZ,PULL_UP_CURRENT); |
roger5641 | 0:b2692e0e4219 | 1095 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 1096 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1097 | // LTC681x_adow(MD_7KHZ_3KHZ,PULL_UP_CURRENT); |
roger5641 | 0:b2692e0e4219 | 1098 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 1099 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1100 | // error = LTC681x_rdcv(0, total_ic,pullUp_cell_codes); |
roger5641 | 0:b2692e0e4219 | 1101 | // |
roger5641 | 0:b2692e0e4219 | 1102 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1103 | // LTC681x_adow(MD_7KHZ_3KHZ,PULL_DOWN_CURRENT); |
roger5641 | 0:b2692e0e4219 | 1104 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 1105 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1106 | // LTC681x_adow(MD_7KHZ_3KHZ,PULL_DOWN_CURRENT); |
roger5641 | 0:b2692e0e4219 | 1107 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 1108 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1109 | // error = LTC681x_rdcv(0, total_ic,pullDwn_cell_codes); |
roger5641 | 0:b2692e0e4219 | 1110 | // |
roger5641 | 0:b2692e0e4219 | 1111 | // for (int cic=0; cic<total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 1112 | // { |
roger5641 | 0:b2692e0e4219 | 1113 | // ic[cic].system_open_wire =0; |
roger5641 | 0:b2692e0e4219 | 1114 | // for (int cell=0; cell<N_CHANNELS; cell++) |
roger5641 | 0:b2692e0e4219 | 1115 | // { |
roger5641 | 0:b2692e0e4219 | 1116 | // if (pullDwn_cell_codes[cic].cells.c_codes[cell]>pullUp_cell_codes[cic].cells.c_codes[cell]) |
roger5641 | 0:b2692e0e4219 | 1117 | // { |
roger5641 | 0:b2692e0e4219 | 1118 | // openWire_delta[cic].cells.c_codes[cell] = pullDwn_cell_codes[cic].cells.c_codes[cell] - pullUp_cell_codes[cic].cells.c_codes[cell] ; |
roger5641 | 0:b2692e0e4219 | 1119 | // } |
roger5641 | 0:b2692e0e4219 | 1120 | // else |
roger5641 | 0:b2692e0e4219 | 1121 | // { |
roger5641 | 0:b2692e0e4219 | 1122 | // openWire_delta[cic].cells.c_codes[cell] = 0; |
roger5641 | 0:b2692e0e4219 | 1123 | // } |
roger5641 | 0:b2692e0e4219 | 1124 | // |
roger5641 | 0:b2692e0e4219 | 1125 | // } |
roger5641 | 0:b2692e0e4219 | 1126 | // } |
roger5641 | 0:b2692e0e4219 | 1127 | // for (int cic=0; cic<total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 1128 | // { |
roger5641 | 0:b2692e0e4219 | 1129 | // for (int cell=1; cell<N_CHANNELS; cell++) |
roger5641 | 0:b2692e0e4219 | 1130 | // { |
roger5641 | 0:b2692e0e4219 | 1131 | // |
roger5641 | 0:b2692e0e4219 | 1132 | // if (openWire_delta[cic].cells.c_codes[cell]>OPENWIRE_THRESHOLD) |
roger5641 | 0:b2692e0e4219 | 1133 | // { |
roger5641 | 0:b2692e0e4219 | 1134 | // ic[cic].system_open_wire += (1<<cell); |
roger5641 | 0:b2692e0e4219 | 1135 | // |
roger5641 | 0:b2692e0e4219 | 1136 | // } |
roger5641 | 0:b2692e0e4219 | 1137 | // } |
roger5641 | 0:b2692e0e4219 | 1138 | // if (pullUp_cell_codes[cic].cells.c_codes[0] == 0) |
roger5641 | 0:b2692e0e4219 | 1139 | // { |
roger5641 | 0:b2692e0e4219 | 1140 | // ic[cic].system_open_wire += 1; |
roger5641 | 0:b2692e0e4219 | 1141 | // } |
roger5641 | 0:b2692e0e4219 | 1142 | // if (pullUp_cell_codes[cic].cells.c_codes[N_CHANNELS-1] == 0) |
roger5641 | 0:b2692e0e4219 | 1143 | // { |
roger5641 | 0:b2692e0e4219 | 1144 | // ic[cic].system_open_wire += (1<<(N_CHANNELS)); |
roger5641 | 0:b2692e0e4219 | 1145 | // } |
roger5641 | 0:b2692e0e4219 | 1146 | // } |
roger5641 | 0:b2692e0e4219 | 1147 | //} |
roger5641 | 0:b2692e0e4219 | 1148 | // |
roger5641 | 0:b2692e0e4219 | 1149 | //// Runs the ADC overlap test for the IC |
roger5641 | 0:b2692e0e4219 | 1150 | //uint16_t LTC681x_run_adc_overlap(uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 1151 | //{ |
roger5641 | 0:b2692e0e4219 | 1152 | // uint16_t error = 0; |
roger5641 | 0:b2692e0e4219 | 1153 | // int32_t measure_delta =0; |
roger5641 | 0:b2692e0e4219 | 1154 | // int16_t failure_pos_limit = 20; |
roger5641 | 0:b2692e0e4219 | 1155 | // int16_t failure_neg_limit = -20; |
roger5641 | 0:b2692e0e4219 | 1156 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1157 | // LTC681x_adol(MD_7KHZ_3KHZ,DCP_DISABLED); |
roger5641 | 0:b2692e0e4219 | 1158 | // LTC681x_pollAdc(); |
roger5641 | 0:b2692e0e4219 | 1159 | // wakeup_idle(total_ic); |
roger5641 | 0:b2692e0e4219 | 1160 | // error = LTC681x_rdcv(0, total_ic,ic); |
roger5641 | 0:b2692e0e4219 | 1161 | // for (int cic = 0; cic<total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 1162 | // { |
roger5641 | 0:b2692e0e4219 | 1163 | // measure_delta = (int32_t)ic[cic].cells.c_codes[6]-(int32_t)ic[cic].cells.c_codes[7]; |
roger5641 | 0:b2692e0e4219 | 1164 | // if ((measure_delta>failure_pos_limit) || (measure_delta<failure_neg_limit)) |
roger5641 | 0:b2692e0e4219 | 1165 | // { |
roger5641 | 0:b2692e0e4219 | 1166 | // error = error | (1<<(cic-1)); |
roger5641 | 0:b2692e0e4219 | 1167 | // } |
roger5641 | 0:b2692e0e4219 | 1168 | // } |
roger5641 | 0:b2692e0e4219 | 1169 | // return(error); |
roger5641 | 0:b2692e0e4219 | 1170 | //} |
roger5641 | 0:b2692e0e4219 | 1171 | // |
roger5641 | 0:b2692e0e4219 | 1172 | ////Helper function that increments PEC counters |
roger5641 | 0:b2692e0e4219 | 1173 | //void LTC681x_check_pec(uint8_t total_ic,uint8_t reg, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 1174 | //{ |
roger5641 | 0:b2692e0e4219 | 1175 | // switch (reg) |
roger5641 | 0:b2692e0e4219 | 1176 | // { |
roger5641 | 0:b2692e0e4219 | 1177 | // case CFGR: |
roger5641 | 0:b2692e0e4219 | 1178 | // for (int current_ic = 0 ; current_ic < total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 1179 | // { |
roger5641 | 0:b2692e0e4219 | 1180 | // ic[current_ic].crc_count.pec_count = ic[current_ic].crc_count.pec_count + ic[current_ic].config.rx_pec_match; |
roger5641 | 0:b2692e0e4219 | 1181 | // ic[current_ic].crc_count.cfgr_pec = ic[current_ic].crc_count.cfgr_pec + ic[current_ic].config.rx_pec_match; |
roger5641 | 0:b2692e0e4219 | 1182 | // } |
roger5641 | 0:b2692e0e4219 | 1183 | // break; |
roger5641 | 0:b2692e0e4219 | 1184 | // |
roger5641 | 0:b2692e0e4219 | 1185 | // case CFGRB: |
roger5641 | 0:b2692e0e4219 | 1186 | // for (int current_ic = 0 ; current_ic < total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 1187 | // { |
roger5641 | 0:b2692e0e4219 | 1188 | // ic[current_ic].crc_count.pec_count = ic[current_ic].crc_count.pec_count + ic[current_ic].configb.rx_pec_match; |
roger5641 | 0:b2692e0e4219 | 1189 | // ic[current_ic].crc_count.cfgr_pec = ic[current_ic].crc_count.cfgr_pec + ic[current_ic].configb.rx_pec_match; |
roger5641 | 0:b2692e0e4219 | 1190 | // } |
roger5641 | 0:b2692e0e4219 | 1191 | // break; |
roger5641 | 0:b2692e0e4219 | 1192 | // case CELL: |
roger5641 | 0:b2692e0e4219 | 1193 | // for (int current_ic = 0 ; current_ic < total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 1194 | // { |
roger5641 | 0:b2692e0e4219 | 1195 | // for (int i=0; i<ic[0].ic_reg.num_cv_reg; i++) |
roger5641 | 0:b2692e0e4219 | 1196 | // { |
roger5641 | 0:b2692e0e4219 | 1197 | // ic[current_ic].crc_count.pec_count = ic[current_ic].crc_count.pec_count + ic[current_ic].cells.pec_match[i]; |
roger5641 | 0:b2692e0e4219 | 1198 | // ic[current_ic].crc_count.cell_pec[i] = ic[current_ic].crc_count.cell_pec[i] + ic[current_ic].cells.pec_match[i]; |
roger5641 | 0:b2692e0e4219 | 1199 | // } |
roger5641 | 0:b2692e0e4219 | 1200 | // } |
roger5641 | 0:b2692e0e4219 | 1201 | // break; |
roger5641 | 0:b2692e0e4219 | 1202 | // case AUX: |
roger5641 | 0:b2692e0e4219 | 1203 | // for (int current_ic = 0 ; current_ic < total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 1204 | // { |
roger5641 | 0:b2692e0e4219 | 1205 | // for (int i=0; i<ic[0].ic_reg.num_gpio_reg; i++) |
roger5641 | 0:b2692e0e4219 | 1206 | // { |
roger5641 | 0:b2692e0e4219 | 1207 | // ic[current_ic].crc_count.pec_count = ic[current_ic].crc_count.pec_count + (ic[current_ic].aux.pec_match[i]); |
roger5641 | 0:b2692e0e4219 | 1208 | // ic[current_ic].crc_count.aux_pec[i] = ic[current_ic].crc_count.aux_pec[i] + (ic[current_ic].aux.pec_match[i]); |
roger5641 | 0:b2692e0e4219 | 1209 | // } |
roger5641 | 0:b2692e0e4219 | 1210 | // } |
roger5641 | 0:b2692e0e4219 | 1211 | // |
roger5641 | 0:b2692e0e4219 | 1212 | // break; |
roger5641 | 0:b2692e0e4219 | 1213 | // case STAT: |
roger5641 | 0:b2692e0e4219 | 1214 | // for (int current_ic = 0 ; current_ic < total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 1215 | // { |
roger5641 | 0:b2692e0e4219 | 1216 | // |
roger5641 | 0:b2692e0e4219 | 1217 | // for (int i=0; i<ic[0].ic_reg.num_stat_reg-1; i++) |
roger5641 | 0:b2692e0e4219 | 1218 | // { |
roger5641 | 0:b2692e0e4219 | 1219 | // ic[current_ic].crc_count.pec_count = ic[current_ic].crc_count.pec_count + ic[current_ic].stat.pec_match[i]; |
roger5641 | 0:b2692e0e4219 | 1220 | // ic[current_ic].crc_count.stat_pec[i] = ic[current_ic].crc_count.stat_pec[i] + ic[current_ic].stat.pec_match[i]; |
roger5641 | 0:b2692e0e4219 | 1221 | // } |
roger5641 | 0:b2692e0e4219 | 1222 | // } |
roger5641 | 0:b2692e0e4219 | 1223 | // break; |
roger5641 | 0:b2692e0e4219 | 1224 | // default: |
roger5641 | 0:b2692e0e4219 | 1225 | // break; |
roger5641 | 0:b2692e0e4219 | 1226 | // } |
roger5641 | 0:b2692e0e4219 | 1227 | //} |
roger5641 | 0:b2692e0e4219 | 1228 | // |
roger5641 | 0:b2692e0e4219 | 1229 | ////Helper Function to reset PEC counters |
roger5641 | 0:b2692e0e4219 | 1230 | //void LTC681x_reset_crc_count(uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 1231 | //{ |
roger5641 | 0:b2692e0e4219 | 1232 | // for (int current_ic = 0 ; current_ic < total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 1233 | // { |
roger5641 | 0:b2692e0e4219 | 1234 | // ic[current_ic].crc_count.pec_count = 0; |
roger5641 | 0:b2692e0e4219 | 1235 | // ic[current_ic].crc_count.cfgr_pec = 0; |
roger5641 | 0:b2692e0e4219 | 1236 | // for (int i=0; i<6; i++) |
roger5641 | 0:b2692e0e4219 | 1237 | // { |
roger5641 | 0:b2692e0e4219 | 1238 | // ic[current_ic].crc_count.cell_pec[i]=0; |
roger5641 | 0:b2692e0e4219 | 1239 | // |
roger5641 | 0:b2692e0e4219 | 1240 | // } |
roger5641 | 0:b2692e0e4219 | 1241 | // for (int i=0; i<4; i++) |
roger5641 | 0:b2692e0e4219 | 1242 | // { |
roger5641 | 0:b2692e0e4219 | 1243 | // ic[current_ic].crc_count.aux_pec[i]=0; |
roger5641 | 0:b2692e0e4219 | 1244 | // } |
roger5641 | 0:b2692e0e4219 | 1245 | // for (int i=0; i<2; i++) |
roger5641 | 0:b2692e0e4219 | 1246 | // { |
roger5641 | 0:b2692e0e4219 | 1247 | // ic[current_ic].crc_count.stat_pec[i]=0; |
roger5641 | 0:b2692e0e4219 | 1248 | // } |
roger5641 | 0:b2692e0e4219 | 1249 | // } |
roger5641 | 0:b2692e0e4219 | 1250 | //} |
roger5641 | 0:b2692e0e4219 | 1251 | // |
roger5641 | 0:b2692e0e4219 | 1252 | ////Helper function to intialize CFG variables. |
roger5641 | 0:b2692e0e4219 | 1253 | //void LTC681x_init_cfg(uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 1254 | //{ |
roger5641 | 0:b2692e0e4219 | 1255 | // bool REFON = true; |
roger5641 | 0:b2692e0e4219 | 1256 | // bool ADCOPT = false; |
roger5641 | 0:b2692e0e4219 | 1257 | // bool gpioBits[5] = {true,true,true,true,true}; |
roger5641 | 0:b2692e0e4219 | 1258 | // bool dccBits[12] = {false,false,false,false,false,false,false,false,false,false,false,false}; |
roger5641 | 0:b2692e0e4219 | 1259 | // for (uint8_t current_ic = 0; current_ic<total_ic; current_ic++) |
roger5641 | 0:b2692e0e4219 | 1260 | // { |
roger5641 | 0:b2692e0e4219 | 1261 | // for (int j =0; j<6; j++) |
roger5641 | 0:b2692e0e4219 | 1262 | // { |
roger5641 | 0:b2692e0e4219 | 1263 | // ic[current_ic].config.tx_data[j] = 0; |
roger5641 | 0:b2692e0e4219 | 1264 | // ic[current_ic].configb.tx_data[j] = 0; |
roger5641 | 0:b2692e0e4219 | 1265 | // } |
roger5641 | 0:b2692e0e4219 | 1266 | // LTC681x_set_cfgr(current_ic ,ic,REFON,ADCOPT,gpioBits,dccBits); |
roger5641 | 0:b2692e0e4219 | 1267 | // |
roger5641 | 0:b2692e0e4219 | 1268 | // } |
roger5641 | 0:b2692e0e4219 | 1269 | //} |
roger5641 | 0:b2692e0e4219 | 1270 | // |
roger5641 | 0:b2692e0e4219 | 1271 | ////Helper function to set CFGR variable |
roger5641 | 0:b2692e0e4219 | 1272 | //void LTC681x_set_cfgr(uint8_t nIC, cell_asic ic[], bool refon, bool adcopt, bool gpio[5],bool dcc[12]) |
roger5641 | 0:b2692e0e4219 | 1273 | //{ |
roger5641 | 0:b2692e0e4219 | 1274 | // LTC681x_set_cfgr_refon(nIC,ic,refon); |
roger5641 | 0:b2692e0e4219 | 1275 | // LTC681x_set_cfgr_adcopt(nIC,ic,adcopt); |
roger5641 | 0:b2692e0e4219 | 1276 | // LTC681x_set_cfgr_gpio(nIC,ic,gpio); |
roger5641 | 0:b2692e0e4219 | 1277 | // LTC681x_set_cfgr_dis(nIC,ic,dcc); |
roger5641 | 0:b2692e0e4219 | 1278 | //} |
roger5641 | 0:b2692e0e4219 | 1279 | // |
roger5641 | 0:b2692e0e4219 | 1280 | ////Helper function to set the REFON bit |
roger5641 | 0:b2692e0e4219 | 1281 | //void LTC681x_set_cfgr_refon(uint8_t nIC, cell_asic ic[], bool refon) |
roger5641 | 0:b2692e0e4219 | 1282 | //{ |
roger5641 | 0:b2692e0e4219 | 1283 | // if (refon) ic[nIC].config.tx_data[0] = ic[nIC].config.tx_data[0]|0x04; |
roger5641 | 0:b2692e0e4219 | 1284 | // else ic[nIC].config.tx_data[0] = ic[nIC].config.tx_data[0]&0xFB; |
roger5641 | 0:b2692e0e4219 | 1285 | //} |
roger5641 | 0:b2692e0e4219 | 1286 | // |
roger5641 | 0:b2692e0e4219 | 1287 | ////Helper function to set the adcopt bit |
roger5641 | 0:b2692e0e4219 | 1288 | //void LTC681x_set_cfgr_adcopt(uint8_t nIC, cell_asic ic[], bool adcopt) |
roger5641 | 0:b2692e0e4219 | 1289 | //{ |
roger5641 | 0:b2692e0e4219 | 1290 | // if (adcopt) ic[nIC].config.tx_data[0] = ic[nIC].config.tx_data[0]|0x01; |
roger5641 | 0:b2692e0e4219 | 1291 | // else ic[nIC].config.tx_data[0] = ic[nIC].config.tx_data[0]&0xFE; |
roger5641 | 0:b2692e0e4219 | 1292 | //} |
roger5641 | 0:b2692e0e4219 | 1293 | // |
roger5641 | 0:b2692e0e4219 | 1294 | ////Helper function to set GPIO bits |
roger5641 | 0:b2692e0e4219 | 1295 | //void LTC681x_set_cfgr_gpio(uint8_t nIC, cell_asic ic[],bool gpio[5]) |
roger5641 | 0:b2692e0e4219 | 1296 | //{ |
roger5641 | 0:b2692e0e4219 | 1297 | // for (int i =0; i<5; i++) |
roger5641 | 0:b2692e0e4219 | 1298 | // { |
roger5641 | 0:b2692e0e4219 | 1299 | // if (gpio[i])ic[nIC].config.tx_data[0] = ic[nIC].config.tx_data[0]|(0x01<<(i+3)); |
roger5641 | 0:b2692e0e4219 | 1300 | // else ic[nIC].config.tx_data[0] = ic[nIC].config.tx_data[0]&(~(0x01<<(i+3))); |
roger5641 | 0:b2692e0e4219 | 1301 | // } |
roger5641 | 0:b2692e0e4219 | 1302 | //} |
roger5641 | 0:b2692e0e4219 | 1303 | // |
roger5641 | 0:b2692e0e4219 | 1304 | ////Helper function to control discharge |
roger5641 | 0:b2692e0e4219 | 1305 | //void LTC681x_set_cfgr_dis(uint8_t nIC, cell_asic ic[],bool dcc[12]) |
roger5641 | 0:b2692e0e4219 | 1306 | //{ |
roger5641 | 0:b2692e0e4219 | 1307 | // for (int i =0; i<8; i++) |
roger5641 | 0:b2692e0e4219 | 1308 | // { |
roger5641 | 0:b2692e0e4219 | 1309 | // if (dcc[i])ic[nIC].config.tx_data[4] = ic[nIC].config.tx_data[4]|(0x01<<i); |
roger5641 | 0:b2692e0e4219 | 1310 | // else ic[nIC].config.tx_data[4] = ic[nIC].config.tx_data[4]& (~(0x01<<i)); |
roger5641 | 0:b2692e0e4219 | 1311 | // } |
roger5641 | 0:b2692e0e4219 | 1312 | // for (int i =0; i<4; i++) |
roger5641 | 0:b2692e0e4219 | 1313 | // { |
roger5641 | 0:b2692e0e4219 | 1314 | // if (dcc[i+8])ic[nIC].config.tx_data[5] = ic[nIC].config.tx_data[5]|(0x01<<i); |
roger5641 | 0:b2692e0e4219 | 1315 | // else ic[nIC].config.tx_data[5] = ic[nIC].config.tx_data[5]&(~(0x01<<i)); |
roger5641 | 0:b2692e0e4219 | 1316 | // } |
roger5641 | 0:b2692e0e4219 | 1317 | //} |
roger5641 | 0:b2692e0e4219 | 1318 | // |
roger5641 | 0:b2692e0e4219 | 1319 | ////*************************** LTC 6811 ****************************************// |
roger5641 | 0:b2692e0e4219 | 1320 | //void LTC6811_init_reg_limits(uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 1321 | //{ |
roger5641 | 0:b2692e0e4219 | 1322 | // for (uint8_t cic=0; cic<total_ic; cic++) |
roger5641 | 0:b2692e0e4219 | 1323 | // { |
roger5641 | 0:b2692e0e4219 | 1324 | // ic[cic].ic_reg.cell_channels=12; |
roger5641 | 0:b2692e0e4219 | 1325 | // ic[cic].ic_reg.stat_channels=4; |
roger5641 | 0:b2692e0e4219 | 1326 | // ic[cic].ic_reg.aux_channels=6; |
roger5641 | 0:b2692e0e4219 | 1327 | // ic[cic].ic_reg.num_cv_reg=4; |
roger5641 | 0:b2692e0e4219 | 1328 | // ic[cic].ic_reg.num_gpio_reg=2; |
roger5641 | 0:b2692e0e4219 | 1329 | // ic[cic].ic_reg.num_stat_reg=3; |
roger5641 | 0:b2692e0e4219 | 1330 | // } |
roger5641 | 0:b2692e0e4219 | 1331 | //} |
roger5641 | 0:b2692e0e4219 | 1332 | // |
roger5641 | 0:b2692e0e4219 | 1333 | ////Helper function to set discharge bit in CFG register |
roger5641 | 0:b2692e0e4219 | 1334 | //void LTC6811_set_discharge(int Cell, uint8_t total_ic, cell_asic ic[]) |
roger5641 | 0:b2692e0e4219 | 1335 | //{ |
roger5641 | 0:b2692e0e4219 | 1336 | // for (int i=0; i<total_ic; i++) |
roger5641 | 0:b2692e0e4219 | 1337 | // { |
roger5641 | 0:b2692e0e4219 | 1338 | // if (Cell<9) |
roger5641 | 0:b2692e0e4219 | 1339 | // { |
roger5641 | 0:b2692e0e4219 | 1340 | // ic[i].config.tx_data[4] = ic[i].config.tx_data[4] | (1<<(Cell-1)); |
roger5641 | 0:b2692e0e4219 | 1341 | // } |
roger5641 | 0:b2692e0e4219 | 1342 | // else if (Cell < 13) |
roger5641 | 0:b2692e0e4219 | 1343 | // { |
roger5641 | 0:b2692e0e4219 | 1344 | // ic[i].config.tx_data[5] = ic[i].config.tx_data[5] | (1<<(Cell-9)); |
roger5641 | 0:b2692e0e4219 | 1345 | // } |
roger5641 | 0:b2692e0e4219 | 1346 | // } |
roger5641 | 0:b2692e0e4219 | 1347 | //} |
roger5641 | 0:b2692e0e4219 | 1348 | // |
roger5641 | 0:b2692e0e4219 | 1349 | ////****************************** SPI **************************************// |
roger5641 | 0:b2692e0e4219 | 1350 | ///* |
roger5641 | 0:b2692e0e4219 | 1351 | //Writes an array of bytes out of the SPI port |
roger5641 | 0:b2692e0e4219 | 1352 | //*/ |
roger5641 | 0:b2692e0e4219 | 1353 | //void spi_write_array(uint8_t len, // Option: Number of bytes to be written on the SPI port |
roger5641 | 0:b2692e0e4219 | 1354 | // uint8_t data[] //Array of bytes to be written on the SPI port |
roger5641 | 0:b2692e0e4219 | 1355 | // ) |
roger5641 | 0:b2692e0e4219 | 1356 | //{ |
roger5641 | 0:b2692e0e4219 | 1357 | // for (uint8_t i = 0; i < len; i++) |
roger5641 | 0:b2692e0e4219 | 1358 | // { |
roger5641 | 0:b2692e0e4219 | 1359 | // spi.write((int8_t)data[i]); |
roger5641 | 0:b2692e0e4219 | 1360 | // } |
roger5641 | 0:b2692e0e4219 | 1361 | //} |
roger5641 | 0:b2692e0e4219 | 1362 | // |
roger5641 | 0:b2692e0e4219 | 1363 | ///* |
roger5641 | 0:b2692e0e4219 | 1364 | // Writes and read a set number of bytes using the SPI port. |
roger5641 | 0:b2692e0e4219 | 1365 | // |
roger5641 | 0:b2692e0e4219 | 1366 | //*/ |
roger5641 | 0:b2692e0e4219 | 1367 | // |
roger5641 | 0:b2692e0e4219 | 1368 | //void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port |
roger5641 | 0:b2692e0e4219 | 1369 | // uint8_t tx_len, //length of the tx data arry |
roger5641 | 0:b2692e0e4219 | 1370 | // uint8_t *rx_data,//Input: array that will store the data read by the SPI port |
roger5641 | 0:b2692e0e4219 | 1371 | // uint8_t rx_len //Option: number of bytes to be read from the SPI port |
roger5641 | 0:b2692e0e4219 | 1372 | // ) |
roger5641 | 0:b2692e0e4219 | 1373 | //{ |
roger5641 | 0:b2692e0e4219 | 1374 | // for (uint8_t i = 0; i < tx_len; i++) |
roger5641 | 0:b2692e0e4219 | 1375 | // { |
roger5641 | 0:b2692e0e4219 | 1376 | // spi.write(tx_Data[i]); |
roger5641 | 0:b2692e0e4219 | 1377 | // } |
roger5641 | 0:b2692e0e4219 | 1378 | // |
roger5641 | 0:b2692e0e4219 | 1379 | // for (uint8_t i = 0; i < rx_len; i++) |
roger5641 | 0:b2692e0e4219 | 1380 | // { |
roger5641 | 0:b2692e0e4219 | 1381 | // |
roger5641 | 0:b2692e0e4219 | 1382 | // rx_data[i] = (uint8_t)spi.write(0xFF); |
roger5641 | 0:b2692e0e4219 | 1383 | // } |
roger5641 | 0:b2692e0e4219 | 1384 | // |
roger5641 | 0:b2692e0e4219 | 1385 | //} |
roger5641 | 0:b2692e0e4219 | 1386 | // |
roger5641 | 0:b2692e0e4219 | 1387 | //uint8_t spi_read_byte(uint8_t tx_dat) |
roger5641 | 0:b2692e0e4219 | 1388 | //{ |
roger5641 | 0:b2692e0e4219 | 1389 | // uint8_t data; |
roger5641 | 0:b2692e0e4219 | 1390 | // data = (uint8_t)spi.write(0xFF); |
roger5641 | 0:b2692e0e4219 | 1391 | // return(data); |
roger5641 | 0:b2692e0e4219 | 1392 | //} |
roger5641 | 0:b2692e0e4219 | 1393 | // |
roger5641 | 0:b2692e0e4219 | 1394 | ////****************************** UserInterface **************************************// |
roger5641 | 0:b2692e0e4219 | 1395 | //char ui_buffer[UI_BUFFER_SIZE]; |
roger5641 | 0:b2692e0e4219 | 1396 | // |
roger5641 | 0:b2692e0e4219 | 1397 | //// Read data from the serial interface into the ui_buffer |
roger5641 | 0:b2692e0e4219 | 1398 | //uint8_t read_data() |
roger5641 | 0:b2692e0e4219 | 1399 | //{ |
roger5641 | 0:b2692e0e4219 | 1400 | // uint8_t index = 0; //index to hold current location in ui_buffer |
roger5641 | 0:b2692e0e4219 | 1401 | // int c; // single character used to store incoming keystrokes |
roger5641 | 0:b2692e0e4219 | 1402 | // while (index < UI_BUFFER_SIZE-1) |
roger5641 | 0:b2692e0e4219 | 1403 | // { |
roger5641 | 0:b2692e0e4219 | 1404 | // c = pc.getc(); //read one character |
roger5641 | 0:b2692e0e4219 | 1405 | // if (((char) c == '\r') || ((char) c == '\n')) break; // if carriage return or linefeed, stop and return data |
roger5641 | 0:b2692e0e4219 | 1406 | // if ( ((char) c == '\x7F') || ((char) c == '\x08') ) // remove previous character (decrement index) if Backspace/Delete key pressed index--; |
roger5641 | 0:b2692e0e4219 | 1407 | // { |
roger5641 | 0:b2692e0e4219 | 1408 | // if (index > 0) index--; |
roger5641 | 0:b2692e0e4219 | 1409 | // } |
roger5641 | 0:b2692e0e4219 | 1410 | // else if (c >= 0) |
roger5641 | 0:b2692e0e4219 | 1411 | // { |
roger5641 | 0:b2692e0e4219 | 1412 | // ui_buffer[index++]=(char) c; // put character into ui_buffer |
roger5641 | 0:b2692e0e4219 | 1413 | // } |
roger5641 | 0:b2692e0e4219 | 1414 | // } |
roger5641 | 0:b2692e0e4219 | 1415 | // ui_buffer[index]='\0'; // terminate string with NULL |
roger5641 | 0:b2692e0e4219 | 1416 | // |
roger5641 | 0:b2692e0e4219 | 1417 | // if ((char) c == '\r') // if the last character was a carriage return, also clear linefeed if it is next character |
roger5641 | 0:b2692e0e4219 | 1418 | // { |
roger5641 | 0:b2692e0e4219 | 1419 | // wait_ms(10); // allow 10ms for linefeed to appear on serial pins |
roger5641 | 0:b2692e0e4219 | 1420 | //// if (pc.peek() == '\n') |
roger5641 | 0:b2692e0e4219 | 1421 | // pc.getc(); // if linefeed appears, read it and throw it away |
roger5641 | 0:b2692e0e4219 | 1422 | // } |
roger5641 | 0:b2692e0e4219 | 1423 | // |
roger5641 | 0:b2692e0e4219 | 1424 | // return index; // return number of characters, not including null terminator |
roger5641 | 0:b2692e0e4219 | 1425 | //} |
roger5641 | 0:b2692e0e4219 | 1426 | // |
roger5641 | 0:b2692e0e4219 | 1427 | //// Read an integer from the serial interface. |
roger5641 | 0:b2692e0e4219 | 1428 | //// The routine can recognize Hex, Decimal, Octal, or Binary |
roger5641 | 0:b2692e0e4219 | 1429 | //// Example: |
roger5641 | 0:b2692e0e4219 | 1430 | //// Hex: 0x11 (0x prefix) |
roger5641 | 0:b2692e0e4219 | 1431 | //// Decimal: 17 |
roger5641 | 0:b2692e0e4219 | 1432 | //// Octal: 021 (leading zero prefix) |
roger5641 | 0:b2692e0e4219 | 1433 | //// Binary: B10001 (leading B prefix) |
roger5641 | 0:b2692e0e4219 | 1434 | //int32_t read_int() |
roger5641 | 0:b2692e0e4219 | 1435 | //{ |
roger5641 | 0:b2692e0e4219 | 1436 | // int32_t data; |
roger5641 | 0:b2692e0e4219 | 1437 | // read_data(); |
roger5641 | 0:b2692e0e4219 | 1438 | // if (ui_buffer[0] == 'm') |
roger5641 | 0:b2692e0e4219 | 1439 | // return('m'); |
roger5641 | 0:b2692e0e4219 | 1440 | // if ((ui_buffer[0] == 'B') || (ui_buffer[0] == 'b')) |
roger5641 | 0:b2692e0e4219 | 1441 | // { |
roger5641 | 0:b2692e0e4219 | 1442 | // data = strtol(ui_buffer+1, NULL, 2); |
roger5641 | 0:b2692e0e4219 | 1443 | // } |
roger5641 | 0:b2692e0e4219 | 1444 | // else |
roger5641 | 0:b2692e0e4219 | 1445 | // data = strtol(ui_buffer, NULL, 0); |
roger5641 | 0:b2692e0e4219 | 1446 | // return(data); |
roger5641 | 0:b2692e0e4219 | 1447 | //} |
roger5641 | 0:b2692e0e4219 | 1448 | // |
roger5641 | 0:b2692e0e4219 | 1449 | //// Read a character from the serial interface |
roger5641 | 0:b2692e0e4219 | 1450 | //int8_t read_char() |
roger5641 | 0:b2692e0e4219 | 1451 | //{ |
roger5641 | 0:b2692e0e4219 | 1452 | // read_data(); |
roger5641 | 0:b2692e0e4219 | 1453 | // return(ui_buffer[0]); |
roger5641 | 0:b2692e0e4219 | 1454 | //} |
roger5641 | 0:b2692e0e4219 | 1455 | // |
roger5641 | 0:b2692e0e4219 | 1456 | // |