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