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