Roger Weng
/
BMS
BMS
main.cpp@7:5f7232441106, 2018-01-17 (annotated)
- Committer:
- roger5641
- Date:
- Wed Jan 17 08:28:06 2018 +0000
- Revision:
- 7:5f7232441106
- Parent:
- 6:c2ab60b20d11
- Child:
- 8:404c4d795fde
check open wire test; 20 command some problems
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roger5641 | 2:e0ec3ed506ea | 1 | /* |
roger5641 | 0:3547f7580dbd | 2 | Copyright (c) 2017, Linear Technology Corp.(LTC) |
roger5641 | 0:3547f7580dbd | 3 | All rights reserved. |
roger5641 | 0:3547f7580dbd | 4 | |
roger5641 | 0:3547f7580dbd | 5 | Redistribution and use in source and binary forms, with or without |
roger5641 | 0:3547f7580dbd | 6 | modification, are permitted provided that the following conditions are met: |
roger5641 | 0:3547f7580dbd | 7 | |
roger5641 | 0:3547f7580dbd | 8 | 1. Redistributions of source code must retain the above copyright notice, this |
roger5641 | 0:3547f7580dbd | 9 | list of conditions and the following disclaimer. |
roger5641 | 0:3547f7580dbd | 10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
roger5641 | 0:3547f7580dbd | 11 | this list of conditions and the following disclaimer in the documentation |
roger5641 | 0:3547f7580dbd | 12 | and/or other materials provided with the distribution. |
roger5641 | 0:3547f7580dbd | 13 | |
roger5641 | 0:3547f7580dbd | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
roger5641 | 0:3547f7580dbd | 15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
roger5641 | 0:3547f7580dbd | 16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
roger5641 | 0:3547f7580dbd | 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
roger5641 | 0:3547f7580dbd | 18 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
roger5641 | 0:3547f7580dbd | 19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
roger5641 | 0:3547f7580dbd | 20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
roger5641 | 0:3547f7580dbd | 21 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
roger5641 | 0:3547f7580dbd | 22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
roger5641 | 0:3547f7580dbd | 23 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
roger5641 | 0:3547f7580dbd | 24 | |
roger5641 | 0:3547f7580dbd | 25 | The views and conclusions contained in the software and documentation are those |
roger5641 | 0:3547f7580dbd | 26 | of the authors and should not be interpreted as representing official policies, |
roger5641 | 0:3547f7580dbd | 27 | either expressed or implied, of Linear Technology Corp. |
roger5641 | 0:3547f7580dbd | 28 | |
roger5641 | 0:3547f7580dbd | 29 | The Linear Technology Linduino is not affiliated with the official Arduino team. |
roger5641 | 0:3547f7580dbd | 30 | However, the Linduino is only possible because of the Arduino team's commitment |
roger5641 | 0:3547f7580dbd | 31 | to the open-source community. Please, visit http://www.arduino.cc and |
roger5641 | 0:3547f7580dbd | 32 | http://store.arduino.cc , and consider a purchase that will help fund their |
roger5641 | 0:3547f7580dbd | 33 | ongoing work. |
roger5641 | 0:3547f7580dbd | 34 | |
roger5641 | 0:3547f7580dbd | 35 | Copyright 2017 Linear Technology Corp. (LTC) |
roger5641 | 0:3547f7580dbd | 36 | */ |
roger5641 | 0:3547f7580dbd | 37 | |
roger5641 | 0:3547f7580dbd | 38 | #include "mbed.h" |
roger5641 | 0:3547f7580dbd | 39 | #include <stdint.h> |
roger5641 | 2:e0ec3ed506ea | 40 | #include "bms_master.h" |
roger5641 | 0:3547f7580dbd | 41 | |
roger5641 | 0:3547f7580dbd | 42 | #define ENABLED 1 |
roger5641 | 0:3547f7580dbd | 43 | #define DISABLED 0 |
roger5641 | 0:3547f7580dbd | 44 | |
roger5641 | 0:3547f7580dbd | 45 | #define DATALOG_ENABLED 1 |
roger5641 | 0:3547f7580dbd | 46 | #define DATALOG_DISABLED 0 |
roger5641 | 0:3547f7580dbd | 47 | |
roger5641 | 5:4c6c92733561 | 48 | DigitalOut fan1(D6); |
roger5641 | 5:4c6c92733561 | 49 | DigitalOut fan2(D7); |
roger5641 | 5:4c6c92733561 | 50 | DigitalOut fan3(D8); |
roger5641 | 5:4c6c92733561 | 51 | DigitalOut fan4(D9); |
roger5641 | 5:4c6c92733561 | 52 | |
roger5641 | 5:4c6c92733561 | 53 | DigitalOut led1(A1); |
roger5641 | 5:4c6c92733561 | 54 | DigitalOut led2(A0); |
roger5641 | 5:4c6c92733561 | 55 | DigitalOut led3(D3); |
roger5641 | 5:4c6c92733561 | 56 | DigitalOut led4(D4); |
roger5641 | 5:4c6c92733561 | 57 | DigitalOut led5(D5); |
roger5641 | 5:4c6c92733561 | 58 | |
roger5641 | 0:3547f7580dbd | 59 | //char get_char(); |
roger5641 | 0:3547f7580dbd | 60 | void print_menu(); |
roger5641 | 0:3547f7580dbd | 61 | void read_config_data(uint8_t cfg_data[][6], uint8_t nIC); |
roger5641 | 0:3547f7580dbd | 62 | void print_cells(uint8_t datalog_en); |
roger5641 | 0:3547f7580dbd | 63 | void print_open(); |
roger5641 | 0:3547f7580dbd | 64 | void print_config(); |
roger5641 | 0:3547f7580dbd | 65 | void print_rxconfig(); |
roger5641 | 0:3547f7580dbd | 66 | void print_aux(uint8_t datalog_en); |
roger5641 | 0:3547f7580dbd | 67 | void print_stat(); |
roger5641 | 0:3547f7580dbd | 68 | void check_error(int error); |
roger5641 | 0:3547f7580dbd | 69 | /********************************************************** |
roger5641 | 0:3547f7580dbd | 70 | Setup Variables |
roger5641 | 0:3547f7580dbd | 71 | The following variables can be modified to |
roger5641 | 0:3547f7580dbd | 72 | configure the software. |
roger5641 | 0:3547f7580dbd | 73 | |
roger5641 | 0:3547f7580dbd | 74 | ***********************************************************/ |
roger5641 | 7:5f7232441106 | 75 | const uint8_t TOTAL_IC = 2;//!<number of ICs in the daisy chain |
roger5641 | 0:3547f7580dbd | 76 | |
roger5641 | 0:3547f7580dbd | 77 | //ADC Command Configurations |
roger5641 | 0:3547f7580dbd | 78 | const uint8_t ADC_OPT = ADC_OPT_DISABLED; // See LTC6811_daisy.h for Options |
roger5641 | 0:3547f7580dbd | 79 | const uint8_t ADC_CONVERSION_MODE = MD_7KHZ_3KHZ;//MD_7KHZ_3KHZ; //MD_26HZ_2KHZ;//MD_7KHZ_3KHZ; // See LTC6811_daisy.h for Options |
roger5641 | 0:3547f7580dbd | 80 | const uint8_t ADC_DCP = DCP_DISABLED; // See LTC6811_daisy.h for Options |
roger5641 | 0:3547f7580dbd | 81 | const uint8_t CELL_CH_TO_CONVERT = CELL_CH_ALL; // See LTC6811_daisy.h for Options |
roger5641 | 0:3547f7580dbd | 82 | const uint8_t AUX_CH_TO_CONVERT = AUX_CH_ALL; // See LTC6811_daisy.h for Options |
roger5641 | 0:3547f7580dbd | 83 | const uint8_t STAT_CH_TO_CONVERT = STAT_CH_ALL; // See LTC6811_daisy.h for Options |
roger5641 | 0:3547f7580dbd | 84 | |
roger5641 | 0:3547f7580dbd | 85 | const uint16_t MEASUREMENT_LOOP_TIME = 500;//milliseconds(mS) |
roger5641 | 0:3547f7580dbd | 86 | |
roger5641 | 0:3547f7580dbd | 87 | //Under Voltage and Over Voltage Thresholds |
roger5641 | 0:3547f7580dbd | 88 | const uint16_t OV_THRESHOLD = 41000; // Over voltage threshold ADC Code. LSB = 0.0001 |
roger5641 | 0:3547f7580dbd | 89 | const uint16_t UV_THRESHOLD = 30000; // Under voltage threshold ADC Code. LSB = 0.0001 |
roger5641 | 0:3547f7580dbd | 90 | |
roger5641 | 0:3547f7580dbd | 91 | //Loop Measurement Setup These Variables are ENABLED or DISABLED Remember ALL CAPS |
roger5641 | 0:3547f7580dbd | 92 | const uint8_t WRITE_CONFIG = DISABLED; // This is ENABLED or DISABLED |
roger5641 | 0:3547f7580dbd | 93 | const uint8_t READ_CONFIG = DISABLED; // This is ENABLED or DISABLED |
roger5641 | 0:3547f7580dbd | 94 | const uint8_t MEASURE_CELL = ENABLED; // This is ENABLED or DISABLED |
roger5641 | 0:3547f7580dbd | 95 | const uint8_t MEASURE_AUX = DISABLED; // This is ENABLED or DISABLED |
roger5641 | 0:3547f7580dbd | 96 | const uint8_t MEASURE_STAT = DISABLED; //This is ENABLED or DISABLED |
roger5641 | 0:3547f7580dbd | 97 | const uint8_t PRINT_PEC = DISABLED; //This is ENABLED or DISABLED |
roger5641 | 0:3547f7580dbd | 98 | /************************************ |
roger5641 | 0:3547f7580dbd | 99 | END SETUP |
roger5641 | 0:3547f7580dbd | 100 | *************************************/ |
roger5641 | 0:3547f7580dbd | 101 | |
roger5641 | 0:3547f7580dbd | 102 | /****************************************************** |
roger5641 | 0:3547f7580dbd | 103 | *** Global Battery Variables received from 681x commands |
roger5641 | 0:3547f7580dbd | 104 | These variables store the results from the LTC6811 |
roger5641 | 0:3547f7580dbd | 105 | register reads and the array lengths must be based |
roger5641 | 0:3547f7580dbd | 106 | on the number of ICs on the stack |
roger5641 | 0:3547f7580dbd | 107 | ******************************************************/ |
roger5641 | 0:3547f7580dbd | 108 | |
roger5641 | 0:3547f7580dbd | 109 | cell_asic bms_ic[TOTAL_IC]; |
roger5641 | 0:3547f7580dbd | 110 | void run_command(uint32_t cmd); |
roger5641 | 0:3547f7580dbd | 111 | void measurement_loop(uint8_t datalog_en); |
roger5641 | 0:3547f7580dbd | 112 | void print_pec(void); |
roger5641 | 0:3547f7580dbd | 113 | void serial_print_hex(uint8_t data); |
roger5641 | 0:3547f7580dbd | 114 | |
roger5641 | 0:3547f7580dbd | 115 | |
roger5641 | 0:3547f7580dbd | 116 | /*!********************************************************************* |
roger5641 | 0:3547f7580dbd | 117 | \brief main loop |
roger5641 | 0:3547f7580dbd | 118 | ***********************************************************************/ |
roger5641 | 0:3547f7580dbd | 119 | int main(void) |
roger5641 | 0:3547f7580dbd | 120 | { |
roger5641 | 2:e0ec3ed506ea | 121 | pc.baud(115200); |
roger5641 | 2:e0ec3ed506ea | 122 | CS_PIN = 1; |
roger5641 | 6:c2ab60b20d11 | 123 | spi.format(8,3); |
roger5641 | 2:e0ec3ed506ea | 124 | spi.frequency(1000000); // 1MHz clock rate |
roger5641 | 2:e0ec3ed506ea | 125 | |
roger5641 | 6:c2ab60b20d11 | 126 | led1 = 1; |
roger5641 | 7:5f7232441106 | 127 | led2 = 0; |
roger5641 | 6:c2ab60b20d11 | 128 | led3 = 1; |
roger5641 | 6:c2ab60b20d11 | 129 | led4 = 1; |
roger5641 | 6:c2ab60b20d11 | 130 | led5 = 1; |
roger5641 | 6:c2ab60b20d11 | 131 | |
roger5641 | 2:e0ec3ed506ea | 132 | LTC681x_init_cfg(TOTAL_IC, bms_ic); |
roger5641 | 2:e0ec3ed506ea | 133 | LTC681x_reset_crc_count(TOTAL_IC,bms_ic); |
roger5641 | 2:e0ec3ed506ea | 134 | LTC6811_init_reg_limits(TOTAL_IC,bms_ic); |
roger5641 | 2:e0ec3ed506ea | 135 | print_menu(); |
roger5641 | 2:e0ec3ed506ea | 136 | |
roger5641 | 2:e0ec3ed506ea | 137 | while(1) |
roger5641 | 2:e0ec3ed506ea | 138 | { |
roger5641 | 2:e0ec3ed506ea | 139 | if (pc.readable()) // Check for user input |
roger5641 | 2:e0ec3ed506ea | 140 | { |
roger5641 | 2:e0ec3ed506ea | 141 | uint32_t user_command; |
roger5641 | 2:e0ec3ed506ea | 142 | user_command = read_int(); // Read the user command |
roger5641 | 4:47d6a0b63e38 | 143 | pc.printf("%d\n",user_command); |
roger5641 | 2:e0ec3ed506ea | 144 | run_command(user_command); |
roger5641 | 2:e0ec3ed506ea | 145 | } |
roger5641 | 2:e0ec3ed506ea | 146 | } |
roger5641 | 0:3547f7580dbd | 147 | } |
roger5641 | 0:3547f7580dbd | 148 | |
roger5641 | 0:3547f7580dbd | 149 | |
roger5641 | 0:3547f7580dbd | 150 | /*!***************************************** |
roger5641 | 0:3547f7580dbd | 151 | \brief executes the user command |
roger5641 | 0:3547f7580dbd | 152 | *******************************************/ |
roger5641 | 0:3547f7580dbd | 153 | |
roger5641 | 0:3547f7580dbd | 154 | void run_command(uint32_t cmd) |
roger5641 | 0:3547f7580dbd | 155 | { |
roger5641 | 0:3547f7580dbd | 156 | int8_t error = 0; |
roger5641 | 0:3547f7580dbd | 157 | uint32_t conv_time = 0; |
roger5641 | 5:4c6c92733561 | 158 | // uint32_t user_command; |
roger5641 | 0:3547f7580dbd | 159 | int8_t readIC=0; |
roger5641 | 0:3547f7580dbd | 160 | char input = 0; |
roger5641 | 0:3547f7580dbd | 161 | switch (cmd) |
roger5641 | 0:3547f7580dbd | 162 | { |
roger5641 | 0:3547f7580dbd | 163 | |
roger5641 | 0:3547f7580dbd | 164 | case 1: // Write Configuration Register |
roger5641 | 0:3547f7580dbd | 165 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 166 | LTC681x_wrcfg(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 167 | print_config(); |
roger5641 | 0:3547f7580dbd | 168 | break; |
roger5641 | 0:3547f7580dbd | 169 | |
roger5641 | 0:3547f7580dbd | 170 | case 2: // Read Configuration Register |
roger5641 | 0:3547f7580dbd | 171 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 172 | error = LTC681x_rdcfg(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 173 | check_error(error); |
roger5641 | 0:3547f7580dbd | 174 | print_rxconfig(); |
roger5641 | 0:3547f7580dbd | 175 | break; |
roger5641 | 0:3547f7580dbd | 176 | |
roger5641 | 0:3547f7580dbd | 177 | case 3: // Start Cell ADC Measurement |
roger5641 | 0:3547f7580dbd | 178 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 179 | LTC681x_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT); |
roger5641 | 2:e0ec3ed506ea | 180 | conv_time = LTC681x_pollAdc(); |
roger5641 | 0:3547f7580dbd | 181 | pc.printf("cell conversion completed in:"); |
roger5641 | 0:3547f7580dbd | 182 | pc.printf("%.1f",((float)conv_time/1000)); |
roger5641 | 0:3547f7580dbd | 183 | pc.printf("mS"); |
roger5641 | 4:47d6a0b63e38 | 184 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 185 | break; |
roger5641 | 0:3547f7580dbd | 186 | |
roger5641 | 0:3547f7580dbd | 187 | case 4: // Read Cell Voltage Registers |
roger5641 | 0:3547f7580dbd | 188 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 189 | error = LTC681x_rdcv(0, TOTAL_IC,bms_ic); // Set to read back all cell voltage registers |
roger5641 | 0:3547f7580dbd | 190 | check_error(error); |
roger5641 | 0:3547f7580dbd | 191 | print_cells(DATALOG_DISABLED); |
roger5641 | 0:3547f7580dbd | 192 | break; |
roger5641 | 0:3547f7580dbd | 193 | |
roger5641 | 0:3547f7580dbd | 194 | case 5: // Start GPIO ADC Measurement |
roger5641 | 0:3547f7580dbd | 195 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 196 | LTC681x_adax(ADC_CONVERSION_MODE , AUX_CH_TO_CONVERT); |
roger5641 | 2:e0ec3ed506ea | 197 | LTC681x_pollAdc(); |
roger5641 | 0:3547f7580dbd | 198 | pc.printf("aux conversion completed"); |
roger5641 | 4:47d6a0b63e38 | 199 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 200 | break; |
roger5641 | 0:3547f7580dbd | 201 | |
roger5641 | 0:3547f7580dbd | 202 | case 6: // Read AUX Voltage Registers |
roger5641 | 0:3547f7580dbd | 203 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 204 | error = LTC681x_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers |
roger5641 | 0:3547f7580dbd | 205 | check_error(error); |
roger5641 | 0:3547f7580dbd | 206 | print_aux(DATALOG_DISABLED); |
roger5641 | 0:3547f7580dbd | 207 | break; |
roger5641 | 0:3547f7580dbd | 208 | |
roger5641 | 0:3547f7580dbd | 209 | case 7: // Start Status ADC Measurement |
roger5641 | 0:3547f7580dbd | 210 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 211 | LTC681x_adstat(ADC_CONVERSION_MODE, STAT_CH_TO_CONVERT); |
roger5641 | 2:e0ec3ed506ea | 212 | LTC681x_pollAdc(); |
roger5641 | 0:3547f7580dbd | 213 | pc.printf("stat conversion completed"); |
roger5641 | 4:47d6a0b63e38 | 214 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 215 | break; |
roger5641 | 0:3547f7580dbd | 216 | |
roger5641 | 0:3547f7580dbd | 217 | case 8: // Read Status registers |
roger5641 | 0:3547f7580dbd | 218 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 219 | error = LTC681x_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers |
roger5641 | 0:3547f7580dbd | 220 | check_error(error); |
roger5641 | 0:3547f7580dbd | 221 | print_stat(); |
roger5641 | 0:3547f7580dbd | 222 | break; |
roger5641 | 0:3547f7580dbd | 223 | |
roger5641 | 0:3547f7580dbd | 224 | case 9: // Loop Measurements |
roger5641 | 0:3547f7580dbd | 225 | pc.printf("transmit 'm' to quit"); |
roger5641 | 0:3547f7580dbd | 226 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 227 | LTC681x_wrcfg(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 228 | while (input != 'm') |
roger5641 | 0:3547f7580dbd | 229 | { |
roger5641 | 0:3547f7580dbd | 230 | if (pc.readable() > 0) |
roger5641 | 0:3547f7580dbd | 231 | { |
roger5641 | 0:3547f7580dbd | 232 | input = read_char(); |
roger5641 | 0:3547f7580dbd | 233 | } |
roger5641 | 0:3547f7580dbd | 234 | |
roger5641 | 0:3547f7580dbd | 235 | measurement_loop(DATALOG_DISABLED); |
roger5641 | 0:3547f7580dbd | 236 | |
roger5641 | 0:3547f7580dbd | 237 | wait_ms(MEASUREMENT_LOOP_TIME); |
roger5641 | 0:3547f7580dbd | 238 | } |
roger5641 | 0:3547f7580dbd | 239 | //print_menu(); |
roger5641 | 0:3547f7580dbd | 240 | break; |
roger5641 | 0:3547f7580dbd | 241 | |
roger5641 | 0:3547f7580dbd | 242 | case 10: // Run open wire self test |
roger5641 | 0:3547f7580dbd | 243 | print_pec(); |
roger5641 | 0:3547f7580dbd | 244 | |
roger5641 | 0:3547f7580dbd | 245 | break; |
roger5641 | 0:3547f7580dbd | 246 | |
roger5641 | 0:3547f7580dbd | 247 | case 11: // Read in raw configuration data |
roger5641 | 2:e0ec3ed506ea | 248 | LTC681x_reset_crc_count(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 249 | break; |
roger5641 | 0:3547f7580dbd | 250 | |
roger5641 | 0:3547f7580dbd | 251 | case 12: // Run the ADC/Memory Self Test |
roger5641 | 0:3547f7580dbd | 252 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 253 | error = LTC681x_run_cell_adc_st(CELL,ADC_CONVERSION_MODE,bms_ic); |
roger5641 | 0:3547f7580dbd | 254 | pc.printf("%d",error); |
roger5641 | 0:3547f7580dbd | 255 | pc.printf(" : errors detected in Digital Filter and CELL Memory \n"); |
roger5641 | 0:3547f7580dbd | 256 | |
roger5641 | 0:3547f7580dbd | 257 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 258 | error = LTC681x_run_cell_adc_st(AUX,ADC_CONVERSION_MODE, bms_ic); |
roger5641 | 0:3547f7580dbd | 259 | pc.printf("%d",error); |
roger5641 | 0:3547f7580dbd | 260 | pc.printf(" : errors detected in Digital Filter and AUX Memory \n"); |
roger5641 | 0:3547f7580dbd | 261 | |
roger5641 | 0:3547f7580dbd | 262 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 263 | error = LTC681x_run_cell_adc_st(STAT,ADC_CONVERSION_MODE, bms_ic); |
roger5641 | 0:3547f7580dbd | 264 | pc.printf("%d",error); |
roger5641 | 0:3547f7580dbd | 265 | pc.printf(" : errors detected in Digital Filter and STAT Memory \n"); |
roger5641 | 0:3547f7580dbd | 266 | print_menu(); |
roger5641 | 0:3547f7580dbd | 267 | break; |
roger5641 | 0:3547f7580dbd | 268 | |
roger5641 | 0:3547f7580dbd | 269 | case 13: // Enable a discharge transistor |
roger5641 | 0:3547f7580dbd | 270 | pc.printf("Please enter the Spin number"); |
roger5641 | 0:3547f7580dbd | 271 | readIC = (int8_t)read_int(); |
roger5641 | 0:3547f7580dbd | 272 | LTC6811_set_discharge(readIC,TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 273 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 274 | LTC681x_wrcfg(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 275 | print_config(); |
roger5641 | 0:3547f7580dbd | 276 | break; |
roger5641 | 0:3547f7580dbd | 277 | |
roger5641 | 0:3547f7580dbd | 278 | case 14: // Clear all discharge transistors |
roger5641 | 0:3547f7580dbd | 279 | clear_discharge(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 280 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 281 | LTC681x_wrcfg(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 282 | print_config(); |
roger5641 | 0:3547f7580dbd | 283 | break; |
roger5641 | 0:3547f7580dbd | 284 | |
roger5641 | 0:3547f7580dbd | 285 | case 15: // Clear all ADC measurement registers |
roger5641 | 0:3547f7580dbd | 286 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 287 | LTC681x_clrcell(); |
roger5641 | 2:e0ec3ed506ea | 288 | LTC681x_clraux(); |
roger5641 | 2:e0ec3ed506ea | 289 | LTC681x_clrstat(); |
roger5641 | 4:47d6a0b63e38 | 290 | pc.printf("All Registers Cleared\n"); |
roger5641 | 0:3547f7580dbd | 291 | break; |
roger5641 | 0:3547f7580dbd | 292 | |
roger5641 | 0:3547f7580dbd | 293 | case 16: // Run the Mux Decoder Self Test |
roger5641 | 0:3547f7580dbd | 294 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 295 | LTC681x_diagn(); |
roger5641 | 0:3547f7580dbd | 296 | wait_ms(5); |
roger5641 | 2:e0ec3ed506ea | 297 | error = LTC681x_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers |
roger5641 | 0:3547f7580dbd | 298 | check_error(error); |
roger5641 | 0:3547f7580dbd | 299 | error = 0; |
roger5641 | 0:3547f7580dbd | 300 | for (int ic = 0; ic<TOTAL_IC; ic++) |
roger5641 | 0:3547f7580dbd | 301 | { |
roger5641 | 0:3547f7580dbd | 302 | if (bms_ic[ic].stat.mux_fail[0] != 0) error++; |
roger5641 | 0:3547f7580dbd | 303 | } |
roger5641 | 0:3547f7580dbd | 304 | if (error==0) pc.printf("Mux Test: PASS "); |
roger5641 | 0:3547f7580dbd | 305 | else pc.printf("Mux Test: FAIL "); |
roger5641 | 0:3547f7580dbd | 306 | |
roger5641 | 0:3547f7580dbd | 307 | break; |
roger5641 | 0:3547f7580dbd | 308 | |
roger5641 | 0:3547f7580dbd | 309 | case 17: // Run ADC Overlap self test |
roger5641 | 0:3547f7580dbd | 310 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 311 | error = (int8_t)LTC681x_run_adc_overlap(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 312 | if (error==0) pc.printf("Overlap Test: PASS "); |
roger5641 | 0:3547f7580dbd | 313 | else pc.printf("Overlap Test: FAIL"); |
roger5641 | 0:3547f7580dbd | 314 | break; |
roger5641 | 0:3547f7580dbd | 315 | |
roger5641 | 0:3547f7580dbd | 316 | case 18: // Run ADC Redundancy self test |
roger5641 | 0:3547f7580dbd | 317 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 318 | error = LTC681x_run_adc_redundancy_st(ADC_CONVERSION_MODE,AUX,TOTAL_IC, bms_ic); |
roger5641 | 0:3547f7580dbd | 319 | pc.printf("%d",error); |
roger5641 | 0:3547f7580dbd | 320 | pc.printf(" : errors detected in AUX Measurement \n"); |
roger5641 | 0:3547f7580dbd | 321 | |
roger5641 | 0:3547f7580dbd | 322 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 323 | error = LTC681x_run_adc_redundancy_st(ADC_CONVERSION_MODE,STAT,TOTAL_IC, bms_ic); |
roger5641 | 0:3547f7580dbd | 324 | pc.printf("%d",error); |
roger5641 | 0:3547f7580dbd | 325 | pc.printf(" : errors detected in STAT Measurement \n"); |
roger5641 | 0:3547f7580dbd | 326 | break; |
roger5641 | 0:3547f7580dbd | 327 | |
roger5641 | 0:3547f7580dbd | 328 | case 19: |
roger5641 | 2:e0ec3ed506ea | 329 | LTC681x_run_openwire(TOTAL_IC, bms_ic); |
roger5641 | 0:3547f7580dbd | 330 | print_open(); |
roger5641 | 0:3547f7580dbd | 331 | break; |
roger5641 | 0:3547f7580dbd | 332 | |
roger5641 | 0:3547f7580dbd | 333 | case 20: //Datalog print option Loop Measurements |
roger5641 | 0:3547f7580dbd | 334 | pc.printf("transmit 'm' to quit"); |
roger5641 | 0:3547f7580dbd | 335 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 336 | LTC681x_wrcfg(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 337 | while (input != 'm') |
roger5641 | 0:3547f7580dbd | 338 | { |
roger5641 | 0:3547f7580dbd | 339 | if (pc.readable() > 0) |
roger5641 | 0:3547f7580dbd | 340 | { |
roger5641 | 0:3547f7580dbd | 341 | input = read_char(); |
roger5641 | 0:3547f7580dbd | 342 | } |
roger5641 | 0:3547f7580dbd | 343 | |
roger5641 | 0:3547f7580dbd | 344 | measurement_loop(DATALOG_ENABLED); |
roger5641 | 0:3547f7580dbd | 345 | |
roger5641 | 0:3547f7580dbd | 346 | wait_ms(MEASUREMENT_LOOP_TIME); |
roger5641 | 0:3547f7580dbd | 347 | } |
roger5641 | 0:3547f7580dbd | 348 | print_menu(); |
roger5641 | 0:3547f7580dbd | 349 | break; |
roger5641 | 0:3547f7580dbd | 350 | |
roger5641 | 0:3547f7580dbd | 351 | case 'm': //prints menu |
roger5641 | 0:3547f7580dbd | 352 | print_menu(); |
roger5641 | 0:3547f7580dbd | 353 | break; |
roger5641 | 0:3547f7580dbd | 354 | |
roger5641 | 0:3547f7580dbd | 355 | default: |
roger5641 | 4:47d6a0b63e38 | 356 | pc.printf("Incorrect Option\n"); |
roger5641 | 0:3547f7580dbd | 357 | break; |
roger5641 | 0:3547f7580dbd | 358 | } |
roger5641 | 0:3547f7580dbd | 359 | } |
roger5641 | 0:3547f7580dbd | 360 | |
roger5641 | 0:3547f7580dbd | 361 | void measurement_loop(uint8_t datalog_en) |
roger5641 | 0:3547f7580dbd | 362 | { |
roger5641 | 0:3547f7580dbd | 363 | int8_t error = 0; |
roger5641 | 0:3547f7580dbd | 364 | if (WRITE_CONFIG == ENABLED) |
roger5641 | 0:3547f7580dbd | 365 | { |
roger5641 | 0:3547f7580dbd | 366 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 367 | LTC681x_wrcfg(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 368 | print_config(); |
roger5641 | 0:3547f7580dbd | 369 | } |
roger5641 | 0:3547f7580dbd | 370 | |
roger5641 | 0:3547f7580dbd | 371 | if (READ_CONFIG == ENABLED) |
roger5641 | 0:3547f7580dbd | 372 | { |
roger5641 | 0:3547f7580dbd | 373 | wakeup_sleep(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 374 | error = LTC681x_rdcfg(TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 375 | check_error(error); |
roger5641 | 0:3547f7580dbd | 376 | print_rxconfig(); |
roger5641 | 0:3547f7580dbd | 377 | } |
roger5641 | 0:3547f7580dbd | 378 | |
roger5641 | 0:3547f7580dbd | 379 | if (MEASURE_CELL == ENABLED) |
roger5641 | 0:3547f7580dbd | 380 | { |
roger5641 | 0:3547f7580dbd | 381 | wakeup_idle(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 382 | LTC681x_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT); |
roger5641 | 2:e0ec3ed506ea | 383 | LTC681x_pollAdc(); |
roger5641 | 0:3547f7580dbd | 384 | wakeup_idle(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 385 | error = LTC681x_rdcv(0, TOTAL_IC,bms_ic); |
roger5641 | 0:3547f7580dbd | 386 | check_error(error); |
roger5641 | 0:3547f7580dbd | 387 | print_cells(datalog_en); |
roger5641 | 0:3547f7580dbd | 388 | |
roger5641 | 0:3547f7580dbd | 389 | } |
roger5641 | 0:3547f7580dbd | 390 | |
roger5641 | 0:3547f7580dbd | 391 | if (MEASURE_AUX == ENABLED) |
roger5641 | 0:3547f7580dbd | 392 | { |
roger5641 | 0:3547f7580dbd | 393 | wakeup_idle(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 394 | LTC681x_adax(ADC_CONVERSION_MODE , AUX_CH_ALL); |
roger5641 | 2:e0ec3ed506ea | 395 | LTC681x_pollAdc(); |
roger5641 | 0:3547f7580dbd | 396 | wakeup_idle(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 397 | error = LTC681x_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers |
roger5641 | 0:3547f7580dbd | 398 | check_error(error); |
roger5641 | 0:3547f7580dbd | 399 | print_aux(datalog_en); |
roger5641 | 0:3547f7580dbd | 400 | } |
roger5641 | 0:3547f7580dbd | 401 | |
roger5641 | 0:3547f7580dbd | 402 | if (MEASURE_STAT == ENABLED) |
roger5641 | 0:3547f7580dbd | 403 | { |
roger5641 | 0:3547f7580dbd | 404 | wakeup_idle(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 405 | LTC681x_adstat(ADC_CONVERSION_MODE, STAT_CH_ALL); |
roger5641 | 2:e0ec3ed506ea | 406 | LTC681x_pollAdc(); |
roger5641 | 0:3547f7580dbd | 407 | wakeup_idle(TOTAL_IC); |
roger5641 | 2:e0ec3ed506ea | 408 | error = LTC681x_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers |
roger5641 | 0:3547f7580dbd | 409 | check_error(error); |
roger5641 | 0:3547f7580dbd | 410 | print_stat(); |
roger5641 | 0:3547f7580dbd | 411 | } |
roger5641 | 0:3547f7580dbd | 412 | |
roger5641 | 0:3547f7580dbd | 413 | if (PRINT_PEC == ENABLED) |
roger5641 | 0:3547f7580dbd | 414 | { |
roger5641 | 0:3547f7580dbd | 415 | print_pec(); |
roger5641 | 0:3547f7580dbd | 416 | } |
roger5641 | 0:3547f7580dbd | 417 | |
roger5641 | 0:3547f7580dbd | 418 | } |
roger5641 | 0:3547f7580dbd | 419 | |
roger5641 | 0:3547f7580dbd | 420 | |
roger5641 | 0:3547f7580dbd | 421 | /*!********************************* |
roger5641 | 0:3547f7580dbd | 422 | \brief Prints the main menu |
roger5641 | 0:3547f7580dbd | 423 | ***********************************/ |
roger5641 | 0:3547f7580dbd | 424 | void print_menu() |
roger5641 | 0:3547f7580dbd | 425 | { |
roger5641 | 4:47d6a0b63e38 | 426 | pc.printf("Please enter LTC6811 Command\n"); |
roger5641 | 4:47d6a0b63e38 | 427 | pc.printf("Write Configuration: 1 | Reset PEC Counter: 11\n"); |
roger5641 | 4:47d6a0b63e38 | 428 | pc.printf("Read Configuration: 2 | Run ADC Self Test: 12\n"); |
roger5641 | 4:47d6a0b63e38 | 429 | pc.printf("Start Cell Voltage Conversion: 3 | Set Discharge: 13\n"); |
roger5641 | 4:47d6a0b63e38 | 430 | pc.printf("Read Cell Voltages: 4 | Clear Discharge: 14\n"); |
roger5641 | 4:47d6a0b63e38 | 431 | pc.printf("Start Aux Voltage Conversion: 5 | Clear Registers: 15\n"); |
roger5641 | 4:47d6a0b63e38 | 432 | pc.printf("Read Aux Voltages: 6 | Run Mux Self Test: 16\n"); |
roger5641 | 4:47d6a0b63e38 | 433 | pc.printf("Start Stat Voltage Conversion: 7 | Run ADC overlap Test: 17\n"); |
roger5641 | 4:47d6a0b63e38 | 434 | pc.printf("Read Stat Voltages: 8 | Run Digital Redundancy Test: 18\n"); |
roger5641 | 4:47d6a0b63e38 | 435 | pc.printf("loop Measurements: 9 | Run Open Wire Test: 19\n"); |
roger5641 | 4:47d6a0b63e38 | 436 | pc.printf("Read PEC Errors: 10 | Loop measurements with datalog output: 20\n"); |
roger5641 | 0:3547f7580dbd | 437 | pc.printf("\r"); |
roger5641 | 4:47d6a0b63e38 | 438 | pc.printf("Please enter command: \n"); |
roger5641 | 0:3547f7580dbd | 439 | pc.printf("\r"); |
roger5641 | 0:3547f7580dbd | 440 | } |
roger5641 | 0:3547f7580dbd | 441 | |
roger5641 | 0:3547f7580dbd | 442 | /*!************************************************************ |
roger5641 | 0:3547f7580dbd | 443 | \brief Prints cell voltage codes to the pc port |
roger5641 | 0:3547f7580dbd | 444 | *************************************************************/ |
roger5641 | 0:3547f7580dbd | 445 | void print_cells(uint8_t datalog_en) |
roger5641 | 0:3547f7580dbd | 446 | { |
roger5641 | 0:3547f7580dbd | 447 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) |
roger5641 | 0:3547f7580dbd | 448 | { |
roger5641 | 0:3547f7580dbd | 449 | if (datalog_en == 0) |
roger5641 | 0:3547f7580dbd | 450 | { |
roger5641 | 0:3547f7580dbd | 451 | pc.printf(" IC "); |
roger5641 | 0:3547f7580dbd | 452 | pc.printf("%d",current_ic+1); |
roger5641 | 0:3547f7580dbd | 453 | pc.printf(", "); |
roger5641 | 0:3547f7580dbd | 454 | for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) |
roger5641 | 0:3547f7580dbd | 455 | { |
roger5641 | 0:3547f7580dbd | 456 | |
roger5641 | 0:3547f7580dbd | 457 | pc.printf(" C"); |
roger5641 | 0:3547f7580dbd | 458 | pc.printf("%d",i+1); |
roger5641 | 0:3547f7580dbd | 459 | pc.printf(":"); |
roger5641 | 0:3547f7580dbd | 460 | pc.printf("%.4f",bms_ic[current_ic].cells.c_codes[i]*0.0001); |
roger5641 | 0:3547f7580dbd | 461 | pc.printf(","); |
roger5641 | 0:3547f7580dbd | 462 | } |
roger5641 | 0:3547f7580dbd | 463 | pc.printf("\r"); |
roger5641 | 0:3547f7580dbd | 464 | } |
roger5641 | 0:3547f7580dbd | 465 | else |
roger5641 | 0:3547f7580dbd | 466 | { |
roger5641 | 0:3547f7580dbd | 467 | pc.printf("Cells, "); |
roger5641 | 0:3547f7580dbd | 468 | for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) |
roger5641 | 0:3547f7580dbd | 469 | { |
roger5641 | 0:3547f7580dbd | 470 | pc.printf("%.4f",bms_ic[current_ic].cells.c_codes[i]*0.0001); |
roger5641 | 0:3547f7580dbd | 471 | pc.printf(","); |
roger5641 | 0:3547f7580dbd | 472 | } |
roger5641 | 0:3547f7580dbd | 473 | |
roger5641 | 0:3547f7580dbd | 474 | } |
roger5641 | 0:3547f7580dbd | 475 | } |
roger5641 | 4:47d6a0b63e38 | 476 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 477 | } |
roger5641 | 0:3547f7580dbd | 478 | |
roger5641 | 0:3547f7580dbd | 479 | /*!**************************************************************************** |
roger5641 | 0:3547f7580dbd | 480 | \brief Prints Open wire test results to the pc port |
roger5641 | 0:3547f7580dbd | 481 | *****************************************************************************/ |
roger5641 | 0:3547f7580dbd | 482 | void print_open() |
roger5641 | 0:3547f7580dbd | 483 | { |
roger5641 | 0:3547f7580dbd | 484 | for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) |
roger5641 | 0:3547f7580dbd | 485 | { |
roger5641 | 0:3547f7580dbd | 486 | if (bms_ic[current_ic].system_open_wire == 0) |
roger5641 | 0:3547f7580dbd | 487 | { |
roger5641 | 0:3547f7580dbd | 488 | pc.printf("No Opens Detected on IC: "); |
roger5641 | 0:3547f7580dbd | 489 | pc.printf("%d",current_ic+1); |
roger5641 | 7:5f7232441106 | 490 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 491 | } |
roger5641 | 0:3547f7580dbd | 492 | else |
roger5641 | 0:3547f7580dbd | 493 | { |
roger5641 | 0:3547f7580dbd | 494 | for (int cell=0; cell<bms_ic[0].ic_reg.cell_channels+1; cell++) |
roger5641 | 0:3547f7580dbd | 495 | { |
roger5641 | 0:3547f7580dbd | 496 | if ((bms_ic[current_ic].system_open_wire &(1<<cell))>0) |
roger5641 | 0:3547f7580dbd | 497 | { |
roger5641 | 0:3547f7580dbd | 498 | pc.printf("There is an open wire on IC: "); |
roger5641 | 0:3547f7580dbd | 499 | pc.printf("%d",current_ic + 1); |
roger5641 | 0:3547f7580dbd | 500 | pc.printf(" Channel: "); |
roger5641 | 0:3547f7580dbd | 501 | pc.printf("%d",cell); |
roger5641 | 7:5f7232441106 | 502 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 503 | } |
roger5641 | 0:3547f7580dbd | 504 | } |
roger5641 | 0:3547f7580dbd | 505 | } |
roger5641 | 0:3547f7580dbd | 506 | } |
roger5641 | 4:47d6a0b63e38 | 507 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 508 | } |
roger5641 | 0:3547f7580dbd | 509 | |
roger5641 | 0:3547f7580dbd | 510 | /*!**************************************************************************** |
roger5641 | 0:3547f7580dbd | 511 | \brief Prints GPIO voltage codes and Vref2 voltage code onto the pc port |
roger5641 | 0:3547f7580dbd | 512 | *****************************************************************************/ |
roger5641 | 0:3547f7580dbd | 513 | void print_aux(uint8_t datalog_en) |
roger5641 | 0:3547f7580dbd | 514 | { |
roger5641 | 0:3547f7580dbd | 515 | for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) |
roger5641 | 0:3547f7580dbd | 516 | { |
roger5641 | 0:3547f7580dbd | 517 | if (datalog_en == 0) |
roger5641 | 0:3547f7580dbd | 518 | { |
roger5641 | 0:3547f7580dbd | 519 | pc.printf(" IC "); |
roger5641 | 0:3547f7580dbd | 520 | pc.printf("%d",current_ic+1); |
roger5641 | 0:3547f7580dbd | 521 | for (int i=0; i < 5; i++) |
roger5641 | 0:3547f7580dbd | 522 | { |
roger5641 | 0:3547f7580dbd | 523 | pc.printf(" GPIO-"); |
roger5641 | 0:3547f7580dbd | 524 | pc.printf("%d",i+1); |
roger5641 | 0:3547f7580dbd | 525 | pc.printf(":"); |
roger5641 | 0:3547f7580dbd | 526 | pc.printf("%.4f",bms_ic[current_ic].aux.a_codes[i]*0.0001); |
roger5641 | 0:3547f7580dbd | 527 | pc.printf(","); |
roger5641 | 0:3547f7580dbd | 528 | } |
roger5641 | 0:3547f7580dbd | 529 | pc.printf(" Vref2"); |
roger5641 | 0:3547f7580dbd | 530 | pc.printf(":"); |
roger5641 | 0:3547f7580dbd | 531 | pc.printf("%.4f",bms_ic[current_ic].aux.a_codes[5]*0.0001); |
roger5641 | 4:47d6a0b63e38 | 532 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 533 | } |
roger5641 | 0:3547f7580dbd | 534 | else |
roger5641 | 0:3547f7580dbd | 535 | { |
roger5641 | 0:3547f7580dbd | 536 | pc.printf("AUX, "); |
roger5641 | 0:3547f7580dbd | 537 | |
roger5641 | 0:3547f7580dbd | 538 | for (int i=0; i < 6; i++) |
roger5641 | 0:3547f7580dbd | 539 | { |
roger5641 | 0:3547f7580dbd | 540 | pc.printf("%.4f",bms_ic[current_ic].aux.a_codes[i]*0.0001); |
roger5641 | 0:3547f7580dbd | 541 | pc.printf(","); |
roger5641 | 0:3547f7580dbd | 542 | } |
roger5641 | 0:3547f7580dbd | 543 | } |
roger5641 | 0:3547f7580dbd | 544 | } |
roger5641 | 4:47d6a0b63e38 | 545 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 546 | } |
roger5641 | 0:3547f7580dbd | 547 | |
roger5641 | 0:3547f7580dbd | 548 | /*!**************************************************************************** |
roger5641 | 0:3547f7580dbd | 549 | \brief Prints Status voltage codes and Vref2 voltage code onto the pc port |
roger5641 | 0:3547f7580dbd | 550 | *****************************************************************************/ |
roger5641 | 0:3547f7580dbd | 551 | void print_stat() |
roger5641 | 0:3547f7580dbd | 552 | { |
roger5641 | 0:3547f7580dbd | 553 | for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) |
roger5641 | 0:3547f7580dbd | 554 | { |
roger5641 | 0:3547f7580dbd | 555 | pc.printf(" IC "); |
roger5641 | 0:3547f7580dbd | 556 | pc.printf("%d",current_ic+1); |
roger5641 | 0:3547f7580dbd | 557 | pc.printf(" SOC:"); |
roger5641 | 0:3547f7580dbd | 558 | pc.printf("%.4f",bms_ic[current_ic].stat.stat_codes[0]*0.0001*20); |
roger5641 | 0:3547f7580dbd | 559 | pc.printf(","); |
roger5641 | 0:3547f7580dbd | 560 | pc.printf(" Itemp:"); |
roger5641 | 0:3547f7580dbd | 561 | pc.printf("%.4f",bms_ic[current_ic].stat.stat_codes[1]*0.0001); |
roger5641 | 0:3547f7580dbd | 562 | pc.printf(","); |
roger5641 | 0:3547f7580dbd | 563 | pc.printf(" VregA:"); |
roger5641 | 0:3547f7580dbd | 564 | pc.printf("%.4f",bms_ic[current_ic].stat.stat_codes[2]*0.0001); |
roger5641 | 0:3547f7580dbd | 565 | pc.printf(","); |
roger5641 | 0:3547f7580dbd | 566 | pc.printf(" VregD:"); |
roger5641 | 0:3547f7580dbd | 567 | pc.printf("%.4f",bms_ic[current_ic].stat.stat_codes[3]*0.0001); |
roger5641 | 0:3547f7580dbd | 568 | pc.printf("\r"); |
roger5641 | 0:3547f7580dbd | 569 | } |
roger5641 | 4:47d6a0b63e38 | 570 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 571 | } |
roger5641 | 0:3547f7580dbd | 572 | |
roger5641 | 0:3547f7580dbd | 573 | /*!****************************************************************************** |
roger5641 | 0:3547f7580dbd | 574 | \brief Prints the configuration data that is going to be written to the LTC6811 |
roger5641 | 0:3547f7580dbd | 575 | to the pc port. |
roger5641 | 0:3547f7580dbd | 576 | ********************************************************************************/ |
roger5641 | 0:3547f7580dbd | 577 | void print_config() |
roger5641 | 0:3547f7580dbd | 578 | { |
roger5641 | 0:3547f7580dbd | 579 | int cfg_pec; |
roger5641 | 0:3547f7580dbd | 580 | |
roger5641 | 0:3547f7580dbd | 581 | pc.printf("Written Configuration: "); |
roger5641 | 0:3547f7580dbd | 582 | for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++) |
roger5641 | 0:3547f7580dbd | 583 | { |
roger5641 | 0:3547f7580dbd | 584 | pc.printf(" IC "); |
roger5641 | 0:3547f7580dbd | 585 | pc.printf("%d",current_ic+1); |
roger5641 | 0:3547f7580dbd | 586 | pc.printf(": "); |
roger5641 | 0:3547f7580dbd | 587 | pc.printf("0x"); |
roger5641 | 0:3547f7580dbd | 588 | serial_print_hex(bms_ic[current_ic].config.tx_data[0]); |
roger5641 | 0:3547f7580dbd | 589 | pc.printf(", 0x"); |
roger5641 | 0:3547f7580dbd | 590 | serial_print_hex(bms_ic[current_ic].config.tx_data[1]); |
roger5641 | 0:3547f7580dbd | 591 | pc.printf(", 0x"); |
roger5641 | 0:3547f7580dbd | 592 | serial_print_hex(bms_ic[current_ic].config.tx_data[2]); |
roger5641 | 0:3547f7580dbd | 593 | pc.printf(", 0x"); |
roger5641 | 0:3547f7580dbd | 594 | serial_print_hex(bms_ic[current_ic].config.tx_data[3]); |
roger5641 | 0:3547f7580dbd | 595 | pc.printf(", 0x"); |
roger5641 | 0:3547f7580dbd | 596 | serial_print_hex(bms_ic[current_ic].config.tx_data[4]); |
roger5641 | 0:3547f7580dbd | 597 | pc.printf(", 0x"); |
roger5641 | 0:3547f7580dbd | 598 | serial_print_hex(bms_ic[current_ic].config.tx_data[5]); |
roger5641 | 0:3547f7580dbd | 599 | pc.printf(", Calculated PEC: 0x"); |
roger5641 | 0:3547f7580dbd | 600 | cfg_pec = pec15_calc(6,&bms_ic[current_ic].config.tx_data[0]); |
roger5641 | 0:3547f7580dbd | 601 | serial_print_hex((uint8_t)(cfg_pec>>8)); |
roger5641 | 0:3547f7580dbd | 602 | pc.printf(", 0x"); |
roger5641 | 0:3547f7580dbd | 603 | serial_print_hex((uint8_t)(cfg_pec)); |
roger5641 | 0:3547f7580dbd | 604 | pc.printf("\r"); |
roger5641 | 0:3547f7580dbd | 605 | } |
roger5641 | 4:47d6a0b63e38 | 606 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 607 | } |
roger5641 | 0:3547f7580dbd | 608 | |
roger5641 | 0:3547f7580dbd | 609 | /*!***************************************************************** |
roger5641 | 0:3547f7580dbd | 610 | \brief Prints the configuration data that was read back from the |
roger5641 | 0:3547f7580dbd | 611 | LTC6811 to the pc port. |
roger5641 | 0:3547f7580dbd | 612 | *******************************************************************/ |
roger5641 | 0:3547f7580dbd | 613 | void print_rxconfig() |
roger5641 | 0:3547f7580dbd | 614 | { |
roger5641 | 4:47d6a0b63e38 | 615 | pc.printf("Received Configuration "); |
roger5641 | 4:47d6a0b63e38 | 616 | for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) |
roger5641 | 4:47d6a0b63e38 | 617 | { |
roger5641 | 4:47d6a0b63e38 | 618 | pc.printf(" IC "); |
roger5641 | 4:47d6a0b63e38 | 619 | pc.printf("%d",current_ic+1); |
roger5641 | 4:47d6a0b63e38 | 620 | pc.printf(": 0x"); |
roger5641 | 4:47d6a0b63e38 | 621 | serial_print_hex(bms_ic[current_ic].config.rx_data[0]); |
roger5641 | 4:47d6a0b63e38 | 622 | pc.printf(", 0x"); |
roger5641 | 4:47d6a0b63e38 | 623 | serial_print_hex(bms_ic[current_ic].config.rx_data[1]); |
roger5641 | 4:47d6a0b63e38 | 624 | pc.printf(", 0x"); |
roger5641 | 4:47d6a0b63e38 | 625 | serial_print_hex(bms_ic[current_ic].config.rx_data[2]); |
roger5641 | 4:47d6a0b63e38 | 626 | pc.printf(", 0x"); |
roger5641 | 4:47d6a0b63e38 | 627 | serial_print_hex(bms_ic[current_ic].config.rx_data[3]); |
roger5641 | 4:47d6a0b63e38 | 628 | pc.printf(", 0x"); |
roger5641 | 4:47d6a0b63e38 | 629 | serial_print_hex(bms_ic[current_ic].config.rx_data[4]); |
roger5641 | 4:47d6a0b63e38 | 630 | pc.printf(", 0x"); |
roger5641 | 4:47d6a0b63e38 | 631 | serial_print_hex(bms_ic[current_ic].config.rx_data[5]); |
roger5641 | 4:47d6a0b63e38 | 632 | pc.printf(", Received PEC: 0x"); |
roger5641 | 4:47d6a0b63e38 | 633 | serial_print_hex(bms_ic[current_ic].config.rx_data[6]); |
roger5641 | 4:47d6a0b63e38 | 634 | pc.printf(", 0x"); |
roger5641 | 4:47d6a0b63e38 | 635 | serial_print_hex(bms_ic[current_ic].config.rx_data[7]); |
roger5641 | 4:47d6a0b63e38 | 636 | pc.printf("\r"); |
roger5641 | 4:47d6a0b63e38 | 637 | } |
roger5641 | 4:47d6a0b63e38 | 638 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 639 | } |
roger5641 | 0:3547f7580dbd | 640 | |
roger5641 | 0:3547f7580dbd | 641 | void print_pec() |
roger5641 | 0:3547f7580dbd | 642 | { |
roger5641 | 0:3547f7580dbd | 643 | for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) |
roger5641 | 0:3547f7580dbd | 644 | { |
roger5641 | 0:3547f7580dbd | 645 | pc.printf(""); |
roger5641 | 0:3547f7580dbd | 646 | pc.printf("%d",bms_ic[current_ic].crc_count.pec_count); |
roger5641 | 0:3547f7580dbd | 647 | pc.printf(" : PEC Errors Detected on IC"); |
roger5641 | 0:3547f7580dbd | 648 | pc.printf("%d",current_ic+1); |
roger5641 | 0:3547f7580dbd | 649 | } |
roger5641 | 4:47d6a0b63e38 | 650 | pc.printf("\n\r"); |
roger5641 | 0:3547f7580dbd | 651 | } |
roger5641 | 0:3547f7580dbd | 652 | |
roger5641 | 0:3547f7580dbd | 653 | |
roger5641 | 0:3547f7580dbd | 654 | void serial_print_hex(uint8_t data) |
roger5641 | 0:3547f7580dbd | 655 | { |
roger5641 | 0:3547f7580dbd | 656 | if (data< 16) |
roger5641 | 0:3547f7580dbd | 657 | { |
roger5641 | 0:3547f7580dbd | 658 | pc.printf("0"); |
roger5641 | 0:3547f7580dbd | 659 | pc.printf("%X",data); |
roger5641 | 0:3547f7580dbd | 660 | } |
roger5641 | 0:3547f7580dbd | 661 | else |
roger5641 | 0:3547f7580dbd | 662 | pc.printf("%X",data); |
roger5641 | 0:3547f7580dbd | 663 | } |
roger5641 | 0:3547f7580dbd | 664 | |
roger5641 | 0:3547f7580dbd | 665 | //Function to check error flag and print PEC error message |
roger5641 | 0:3547f7580dbd | 666 | void check_error(int error) |
roger5641 | 0:3547f7580dbd | 667 | { |
roger5641 | 0:3547f7580dbd | 668 | if (error == -1) |
roger5641 | 0:3547f7580dbd | 669 | { |
roger5641 | 4:47d6a0b63e38 | 670 | pc.printf("A PEC error was detected in the received data\n"); |
roger5641 | 0:3547f7580dbd | 671 | } |
roger5641 | 0:3547f7580dbd | 672 | } |