KIT Solar Car Project / Mbed 2 deprecated BMS_v1

Dependencies:   mbed INA226

Committer:
takuma1
Date:
Sat Oct 26 05:24:17 2019 +0000
Revision:
2:f6f76dde7e1d
Parent:
0:f06ed53310a3
Child:
3:a1368cd4b0a9
BMS;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
APS_Lab 0:f06ed53310a3 1
APS_Lab 0:f06ed53310a3 2 #include "mbed.h"
APS_Lab 0:f06ed53310a3 3 #include "bms.h"
APS_Lab 0:f06ed53310a3 4 #include "LTC681x.h"
APS_Lab 0:f06ed53310a3 5 #include "LTC6811.h"
APS_Lab 0:f06ed53310a3 6
APS_Lab 0:f06ed53310a3 7 #define UI_BUFFER_SIZE 64
APS_Lab 0:f06ed53310a3 8 #define SERIAL_TERMINATOR '\n'
APS_Lab 0:f06ed53310a3 9
APS_Lab 0:f06ed53310a3 10 #define ENABLED 1
APS_Lab 0:f06ed53310a3 11 #define DISABLED 0
APS_Lab 0:f06ed53310a3 12
APS_Lab 0:f06ed53310a3 13 #define DATALOG_ENABLED 1
APS_Lab 0:f06ed53310a3 14 #define DATALOG_DISABLED 0
takuma1 2:f6f76dde7e1d 15
takuma1 2:f6f76dde7e1d 16 Ticker ticker;
APS_Lab 0:f06ed53310a3 17 DigitalOut led1(LED1);
APS_Lab 0:f06ed53310a3 18 Serial pc(USBTX, USBRX);
takuma1 2:f6f76dde7e1d 19 CAN BMS_CAN(p9,p10);
takuma1 2:f6f76dde7e1d 20
APS_Lab 0:f06ed53310a3 21
APS_Lab 0:f06ed53310a3 22 void run_command(uint32_t cmd);
APS_Lab 0:f06ed53310a3 23 void measurement_loop(uint8_t datalog_en);
takuma1 2:f6f76dde7e1d 24 //void print_menu();
APS_Lab 0:f06ed53310a3 25 void print_cells(uint8_t datalog_en);
APS_Lab 0:f06ed53310a3 26 void print_open();
APS_Lab 0:f06ed53310a3 27 void print_aux(uint8_t datalog_en);
APS_Lab 0:f06ed53310a3 28 void print_stat();
APS_Lab 0:f06ed53310a3 29 void print_config();
APS_Lab 0:f06ed53310a3 30 void print_rxconfig();
APS_Lab 0:f06ed53310a3 31 void print_pec(void);
APS_Lab 0:f06ed53310a3 32 void serial_print_hex(uint8_t data);
APS_Lab 0:f06ed53310a3 33 void check_error(int error);
takuma1 2:f6f76dde7e1d 34 void wakeup();
takuma1 2:f6f76dde7e1d 35 void cell_read();
takuma1 2:f6f76dde7e1d 36 void spi_error();
takuma1 2:f6f76dde7e1d 37
APS_Lab 0:f06ed53310a3 38 //char get_char();
APS_Lab 0:f06ed53310a3 39 //void read_config_data(uint8_t cfg_data[][6], uint8_t nIC);
APS_Lab 0:f06ed53310a3 40
APS_Lab 0:f06ed53310a3 41 /**********************************************************
APS_Lab 0:f06ed53310a3 42 Setup Variables
APS_Lab 0:f06ed53310a3 43 The following variables can be modified to
APS_Lab 0:f06ed53310a3 44 configure the software.
APS_Lab 0:f06ed53310a3 45
APS_Lab 0:f06ed53310a3 46 ***********************************************************/
APS_Lab 0:f06ed53310a3 47 const uint8_t TOTAL_IC = 1;//!<number of ICs in the daisy chain
APS_Lab 0:f06ed53310a3 48 char ui_buffer[UI_BUFFER_SIZE];
APS_Lab 0:f06ed53310a3 49
APS_Lab 0:f06ed53310a3 50
APS_Lab 0:f06ed53310a3 51 //ADC Command Configurations
APS_Lab 0:f06ed53310a3 52 //const uint8_t ADC_OPT = ADC_OPT_DISABLED; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 53 const uint8_t ADC_CONVERSION_MODE = MD_7KHZ_3KHZ;//MD_7KHZ_3KHZ; //MD_26HZ_2KHZ;//MD_7KHZ_3KHZ; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 54 const uint8_t ADC_DCP = DCP_DISABLED; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 55 const uint8_t CELL_CH_TO_CONVERT = CELL_CH_ALL; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 56 const uint8_t AUX_CH_TO_CONVERT = AUX_CH_ALL; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 57 const uint8_t STAT_CH_TO_CONVERT = STAT_CH_ALL; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 58
APS_Lab 0:f06ed53310a3 59 const uint16_t MEASUREMENT_LOOP_TIME = 500;//milliseconds(mS)
APS_Lab 0:f06ed53310a3 60
APS_Lab 0:f06ed53310a3 61 //Under Voltage and Over Voltage Thresholds
takuma1 2:f6f76dde7e1d 62 const uint16_t OV_THRESHOLD = 41000; //
takuma1 2:f6f76dde7e1d 63 const uint16_t UV_THRESHOLD = 30000; //
APS_Lab 0:f06ed53310a3 64
APS_Lab 0:f06ed53310a3 65 //Loop Measurement Setup These Variables are ENABLED or DISABLED Remember ALL CAPS
APS_Lab 0:f06ed53310a3 66 const uint8_t WRITE_CONFIG = DISABLED; // This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 67 const uint8_t READ_CONFIG = DISABLED; // This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 68 const uint8_t MEASURE_CELL = ENABLED; // This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 69 const uint8_t MEASURE_AUX = DISABLED; // This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 70 const uint8_t MEASURE_STAT = DISABLED; //This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 71 const uint8_t PRINT_PEC = DISABLED; //This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 72
APS_Lab 0:f06ed53310a3 73
APS_Lab 0:f06ed53310a3 74 // Read data from the serial interface into the ui_buffer buffer
APS_Lab 0:f06ed53310a3 75 uint8_t read_data();
APS_Lab 0:f06ed53310a3 76
APS_Lab 0:f06ed53310a3 77 // Read a float value from the serial interface
APS_Lab 0:f06ed53310a3 78 float read_float();
APS_Lab 0:f06ed53310a3 79
APS_Lab 0:f06ed53310a3 80 // Read an integer from the serial interface.
APS_Lab 0:f06ed53310a3 81 // The routine can recognize Hex, Decimal, Octal, or Binary
APS_Lab 0:f06ed53310a3 82 // Example:
APS_Lab 0:f06ed53310a3 83 // Hex: 0x11 (0x prefix)
APS_Lab 0:f06ed53310a3 84 // Decimal: 17
APS_Lab 0:f06ed53310a3 85 // Octal: O21 (leading letter O prefix)
APS_Lab 0:f06ed53310a3 86 // Binary: B10001 (leading letter B prefix)
APS_Lab 0:f06ed53310a3 87 int32_t read_int();
takuma1 2:f6f76dde7e1d 88 int kaisuu;
APS_Lab 0:f06ed53310a3 89 // Read a string from the serial interface. Returns a pointer to the ui_buffer.
APS_Lab 0:f06ed53310a3 90 char *read_string();
APS_Lab 0:f06ed53310a3 91
APS_Lab 0:f06ed53310a3 92 // Read a character from the serial interface
APS_Lab 0:f06ed53310a3 93 int8_t read_char();
APS_Lab 0:f06ed53310a3 94
APS_Lab 0:f06ed53310a3 95 /************************************
APS_Lab 0:f06ed53310a3 96 END SETUP
APS_Lab 0:f06ed53310a3 97 *************************************/
APS_Lab 0:f06ed53310a3 98
APS_Lab 0:f06ed53310a3 99 /******************************************************
APS_Lab 0:f06ed53310a3 100 *** Global Battery Variables received from 681x commands
APS_Lab 0:f06ed53310a3 101 These variables store the results from the LTC6811
APS_Lab 0:f06ed53310a3 102 register reads and the array lengths must be based
APS_Lab 0:f06ed53310a3 103 on the number of ICs on the stack
APS_Lab 0:f06ed53310a3 104 ******************************************************/
APS_Lab 0:f06ed53310a3 105
APS_Lab 0:f06ed53310a3 106 cell_asic bms_ic[TOTAL_IC];
APS_Lab 0:f06ed53310a3 107
APS_Lab 0:f06ed53310a3 108 /*!*********************************************************************
APS_Lab 0:f06ed53310a3 109 \brief main loop
APS_Lab 0:f06ed53310a3 110 ***********************************************************************/
APS_Lab 0:f06ed53310a3 111 int main(void)
APS_Lab 0:f06ed53310a3 112 {
APS_Lab 0:f06ed53310a3 113 uint32_t user_command;
APS_Lab 0:f06ed53310a3 114
APS_Lab 0:f06ed53310a3 115 pc.baud(115200);
APS_Lab 0:f06ed53310a3 116 spi_enable();
APS_Lab 0:f06ed53310a3 117 LTC681x_init_cfg(TOTAL_IC, bms_ic);
APS_Lab 0:f06ed53310a3 118 LTC6811_reset_crc_count(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 119 LTC6811_init_reg_limits(TOTAL_IC,bms_ic);
takuma1 2:f6f76dde7e1d 120 wakeup();
takuma1 2:f6f76dde7e1d 121 //print_menu();
takuma1 2:f6f76dde7e1d 122
APS_Lab 0:f06ed53310a3 123
APS_Lab 0:f06ed53310a3 124 while(1) {
takuma1 2:f6f76dde7e1d 125
takuma1 2:f6f76dde7e1d 126 spi_error();
APS_Lab 0:f06ed53310a3 127 //pc.printf("check 00\n");
APS_Lab 0:f06ed53310a3 128 //while(!pc.readable()) {
APS_Lab 0:f06ed53310a3 129 wait(0.3);
APS_Lab 0:f06ed53310a3 130 // pc.printf("check 001\n");
APS_Lab 0:f06ed53310a3 131 //} // Check for user input
APS_Lab 0:f06ed53310a3 132 //pc.printf("check 01\n");
takuma1 2:f6f76dde7e1d 133 user_command = read_int();
APS_Lab 0:f06ed53310a3 134 run_command(user_command);
takuma1 2:f6f76dde7e1d 135
APS_Lab 0:f06ed53310a3 136 }
APS_Lab 0:f06ed53310a3 137 }
APS_Lab 0:f06ed53310a3 138
takuma1 2:f6f76dde7e1d 139 void wakeup(){
takuma1 2:f6f76dde7e1d 140 int countup;
takuma1 2:f6f76dde7e1d 141 int8_t error = 0;
takuma1 2:f6f76dde7e1d 142 uint32_t conv_time = 0;
takuma1 2:f6f76dde7e1d 143
takuma1 2:f6f76dde7e1d 144
takuma1 2:f6f76dde7e1d 145 wakeup_sleep(TOTAL_IC);
takuma1 2:f6f76dde7e1d 146 LTC6811_wrcfg(TOTAL_IC,bms_ic);
takuma1 2:f6f76dde7e1d 147 print_config();
takuma1 2:f6f76dde7e1d 148
takuma1 2:f6f76dde7e1d 149 wait(0.05);
takuma1 2:f6f76dde7e1d 150
takuma1 2:f6f76dde7e1d 151 wakeup_sleep(TOTAL_IC);
takuma1 2:f6f76dde7e1d 152 error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
takuma1 2:f6f76dde7e1d 153 check_error(error);
takuma1 2:f6f76dde7e1d 154 print_rxconfig();
takuma1 2:f6f76dde7e1d 155 wait(0.05);
takuma1 2:f6f76dde7e1d 156
takuma1 2:f6f76dde7e1d 157 wakeup_sleep(TOTAL_IC);
takuma1 2:f6f76dde7e1d 158 LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
takuma1 2:f6f76dde7e1d 159 conv_time = LTC6811_pollAdc();
takuma1 2:f6f76dde7e1d 160 pc.printf("cell conversion completed in:");
takuma1 2:f6f76dde7e1d 161 pc.printf("%.1f",((float)conv_time/1000));
takuma1 2:f6f76dde7e1d 162 pc.printf("mS\n");
takuma1 2:f6f76dde7e1d 163
takuma1 2:f6f76dde7e1d 164
takuma1 2:f6f76dde7e1d 165 }
takuma1 2:f6f76dde7e1d 166
takuma1 2:f6f76dde7e1d 167
takuma1 2:f6f76dde7e1d 168 void spi_error(){
takuma1 2:f6f76dde7e1d 169
takuma1 2:f6f76dde7e1d 170 int8_t error = 0;
takuma1 2:f6f76dde7e1d 171 int countup;
takuma1 2:f6f76dde7e1d 172
takuma1 2:f6f76dde7e1d 173 if(error = -1 ){
takuma1 2:f6f76dde7e1d 174 for(countup = 0; countup <= 100; countup++){
takuma1 2:f6f76dde7e1d 175 wakeup();
takuma1 2:f6f76dde7e1d 176 if(error = 1){
takuma1 2:f6f76dde7e1d 177 break;
takuma1 2:f6f76dde7e1d 178 }
takuma1 2:f6f76dde7e1d 179 }
takuma1 2:f6f76dde7e1d 180 }
takuma1 2:f6f76dde7e1d 181 if(error = 1){
takuma1 2:f6f76dde7e1d 182 cell_read();
takuma1 2:f6f76dde7e1d 183 }
takuma1 2:f6f76dde7e1d 184
takuma1 2:f6f76dde7e1d 185 }
takuma1 2:f6f76dde7e1d 186
APS_Lab 0:f06ed53310a3 187 /*!*****************************************
APS_Lab 0:f06ed53310a3 188 \brief executes the user command
APS_Lab 0:f06ed53310a3 189 *******************************************/
APS_Lab 0:f06ed53310a3 190
APS_Lab 0:f06ed53310a3 191 void run_command(uint32_t cmd)
APS_Lab 0:f06ed53310a3 192 {
APS_Lab 0:f06ed53310a3 193 int8_t error = 0;
APS_Lab 0:f06ed53310a3 194 uint32_t conv_time = 0;
APS_Lab 0:f06ed53310a3 195 // uint32_t user_command;
APS_Lab 0:f06ed53310a3 196 int8_t readIC=0;
APS_Lab 0:f06ed53310a3 197 char input = 0;
takuma1 2:f6f76dde7e1d 198
takuma1 2:f6f76dde7e1d 199 cell_read();
takuma1 2:f6f76dde7e1d 200
takuma1 2:f6f76dde7e1d 201
APS_Lab 0:f06ed53310a3 202 switch (cmd) {
APS_Lab 0:f06ed53310a3 203
APS_Lab 0:f06ed53310a3 204
takuma1 2:f6f76dde7e1d 205 case 3: // Start GPIO ADC Measurement
APS_Lab 0:f06ed53310a3 206 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 207 LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_TO_CONVERT);
APS_Lab 0:f06ed53310a3 208 LTC6811_pollAdc();
APS_Lab 0:f06ed53310a3 209 pc.printf("aux conversion completed\n");
APS_Lab 0:f06ed53310a3 210 pc.printf("\n");
APS_Lab 0:f06ed53310a3 211 break;
APS_Lab 0:f06ed53310a3 212
takuma1 2:f6f76dde7e1d 213 case 4: // Read AUX Voltage Registers
APS_Lab 0:f06ed53310a3 214 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 215 error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
APS_Lab 0:f06ed53310a3 216 check_error(error);
APS_Lab 0:f06ed53310a3 217 print_aux(DATALOG_DISABLED);
APS_Lab 0:f06ed53310a3 218 break;
APS_Lab 0:f06ed53310a3 219
takuma1 2:f6f76dde7e1d 220 case 5: // Start Status ADC Measurement
APS_Lab 0:f06ed53310a3 221 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 222 LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_TO_CONVERT);
APS_Lab 0:f06ed53310a3 223 LTC6811_pollAdc();
APS_Lab 0:f06ed53310a3 224 pc.printf("stat conversion completed\n");
APS_Lab 0:f06ed53310a3 225 pc.printf("\n");
APS_Lab 0:f06ed53310a3 226 break;
APS_Lab 0:f06ed53310a3 227
takuma1 2:f6f76dde7e1d 228 case 6: // Read Status registers
APS_Lab 0:f06ed53310a3 229 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 230 error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
APS_Lab 0:f06ed53310a3 231 check_error(error);
APS_Lab 0:f06ed53310a3 232 print_stat();
APS_Lab 0:f06ed53310a3 233 break;
APS_Lab 0:f06ed53310a3 234
takuma1 2:f6f76dde7e1d 235 case 7: // Loop Measurements
APS_Lab 0:f06ed53310a3 236 pc.printf("transmit 'm' to quit\n");
APS_Lab 0:f06ed53310a3 237 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 238 LTC6811_wrcfg(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 239 while (input != 'm') {
APS_Lab 0:f06ed53310a3 240 //if (pc.readable()) {
APS_Lab 0:f06ed53310a3 241 input = read_char();
APS_Lab 0:f06ed53310a3 242 //}
APS_Lab 0:f06ed53310a3 243
APS_Lab 0:f06ed53310a3 244 measurement_loop(DATALOG_DISABLED);
APS_Lab 0:f06ed53310a3 245
APS_Lab 0:f06ed53310a3 246 wait_ms(MEASUREMENT_LOOP_TIME);
APS_Lab 0:f06ed53310a3 247 }
APS_Lab 0:f06ed53310a3 248 //print_menu();
APS_Lab 0:f06ed53310a3 249 break;
APS_Lab 0:f06ed53310a3 250
takuma1 2:f6f76dde7e1d 251 case 8: // Run open wire self test
APS_Lab 0:f06ed53310a3 252 print_pec();
APS_Lab 0:f06ed53310a3 253
APS_Lab 0:f06ed53310a3 254 break;
APS_Lab 0:f06ed53310a3 255
takuma1 2:f6f76dde7e1d 256 case 9: // Read in raw configuration data
APS_Lab 0:f06ed53310a3 257 LTC6811_reset_crc_count(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 258 break;
APS_Lab 0:f06ed53310a3 259
takuma1 2:f6f76dde7e1d 260 case 10: // Run the ADC/Memory Self Test
APS_Lab 0:f06ed53310a3 261 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 262 error = LTC6811_run_cell_adc_st(CELL,ADC_CONVERSION_MODE,bms_ic);
APS_Lab 0:f06ed53310a3 263 pc.printf("%d", error);
APS_Lab 0:f06ed53310a3 264 pc.printf(" : errors detected in Digital Filter and CELL Memory\n");
APS_Lab 0:f06ed53310a3 265
APS_Lab 0:f06ed53310a3 266 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 267 error = LTC6811_run_cell_adc_st(AUX,ADC_CONVERSION_MODE, bms_ic);
APS_Lab 0:f06ed53310a3 268 pc.printf("%d",error);
APS_Lab 0:f06ed53310a3 269 pc.printf(" : errors detected in Digital Filter and AUX Memory\n");
APS_Lab 0:f06ed53310a3 270
APS_Lab 0:f06ed53310a3 271 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 272 error = LTC6811_run_cell_adc_st(STAT,ADC_CONVERSION_MODE, bms_ic);
APS_Lab 0:f06ed53310a3 273 pc.printf("%d",error);
APS_Lab 0:f06ed53310a3 274 pc.printf(" : errors detected in Digital Filter and STAT Memory\n");
takuma1 2:f6f76dde7e1d 275 //print_menu();
APS_Lab 0:f06ed53310a3 276 break;
APS_Lab 0:f06ed53310a3 277
takuma1 2:f6f76dde7e1d 278 case 11: // Enable a discharge transistor
APS_Lab 0:f06ed53310a3 279 pc.printf("Please enter the Spin number\n");
APS_Lab 0:f06ed53310a3 280 readIC = (int8_t)read_int();
APS_Lab 0:f06ed53310a3 281 LTC6811_set_discharge(readIC,TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 282 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 283 LTC6811_wrcfg(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 284 print_config();
APS_Lab 0:f06ed53310a3 285 break;
APS_Lab 0:f06ed53310a3 286
takuma1 2:f6f76dde7e1d 287 case 12: // Clear all discharge transistors
APS_Lab 0:f06ed53310a3 288 clear_discharge(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 289 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 290 LTC6811_wrcfg(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 291 print_config();
APS_Lab 0:f06ed53310a3 292 break;
APS_Lab 0:f06ed53310a3 293
takuma1 2:f6f76dde7e1d 294 case 13: // Clear all ADC measurement registers
APS_Lab 0:f06ed53310a3 295 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 296 LTC6811_clrcell();
APS_Lab 0:f06ed53310a3 297 LTC6811_clraux();
APS_Lab 0:f06ed53310a3 298 LTC6811_clrstat();
APS_Lab 0:f06ed53310a3 299 pc.printf("All Registers Cleared\n");
APS_Lab 0:f06ed53310a3 300 break;
APS_Lab 0:f06ed53310a3 301
takuma1 2:f6f76dde7e1d 302 case 14: // Run the Mux Decoder Self Test
APS_Lab 0:f06ed53310a3 303 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 304 LTC6811_diagn();
APS_Lab 0:f06ed53310a3 305 wait_ms(5);
APS_Lab 0:f06ed53310a3 306 error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
APS_Lab 0:f06ed53310a3 307 check_error(error);
APS_Lab 0:f06ed53310a3 308 error = 0;
APS_Lab 0:f06ed53310a3 309 for (int ic = 0;
APS_Lab 0:f06ed53310a3 310 ic<TOTAL_IC;
APS_Lab 0:f06ed53310a3 311 ic++) {
APS_Lab 0:f06ed53310a3 312 if (bms_ic[ic].stat.mux_fail[0] != 0) error++;
APS_Lab 0:f06ed53310a3 313 }
APS_Lab 0:f06ed53310a3 314 if (error==0) pc.printf("Mux Test: PASS\n");
APS_Lab 0:f06ed53310a3 315 else pc.printf("Mux Test: FAIL\n");
APS_Lab 0:f06ed53310a3 316
APS_Lab 0:f06ed53310a3 317 break;
APS_Lab 0:f06ed53310a3 318
takuma1 2:f6f76dde7e1d 319 case 15: // Run ADC Overlap self test
APS_Lab 0:f06ed53310a3 320 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 321 error = (int8_t)LTC6811_run_adc_overlap(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 322 if (error==0) pc.printf("Overlap Test: PASS\n");
APS_Lab 0:f06ed53310a3 323 else pc.printf("Overlap Test: FAIL\n");
APS_Lab 0:f06ed53310a3 324 break;
APS_Lab 0:f06ed53310a3 325
takuma1 2:f6f76dde7e1d 326 case 16: // Run ADC Redundancy self test
APS_Lab 0:f06ed53310a3 327 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 328 error = LTC6811_run_adc_redundancy_st(ADC_CONVERSION_MODE,AUX,TOTAL_IC, bms_ic);
APS_Lab 0:f06ed53310a3 329 pc.printf("%d",error);
APS_Lab 0:f06ed53310a3 330 pc.printf(" : errors detected in AUX Measurement\n");
APS_Lab 0:f06ed53310a3 331
APS_Lab 0:f06ed53310a3 332 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 333 error = LTC6811_run_adc_redundancy_st(ADC_CONVERSION_MODE,STAT,TOTAL_IC, bms_ic);
APS_Lab 0:f06ed53310a3 334 pc.printf("%d",error);
APS_Lab 0:f06ed53310a3 335 pc.printf(" : errors detected in STAT Measurement\n");
APS_Lab 0:f06ed53310a3 336 break;
APS_Lab 0:f06ed53310a3 337
takuma1 2:f6f76dde7e1d 338 case 17:
APS_Lab 0:f06ed53310a3 339 LTC6811_run_openwire(TOTAL_IC, bms_ic);
APS_Lab 0:f06ed53310a3 340 print_open();
APS_Lab 0:f06ed53310a3 341 break;
APS_Lab 0:f06ed53310a3 342
takuma1 2:f6f76dde7e1d 343 case 18: //Datalog print option Loop Measurements
APS_Lab 0:f06ed53310a3 344 pc.printf("transmit 'm' to quit\n");
APS_Lab 0:f06ed53310a3 345 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 346 LTC6811_wrcfg(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 347 while (input != 'm') {
APS_Lab 0:f06ed53310a3 348 //if (pc.readable()) {
APS_Lab 0:f06ed53310a3 349 input = read_char();
APS_Lab 0:f06ed53310a3 350 //}
APS_Lab 0:f06ed53310a3 351
APS_Lab 0:f06ed53310a3 352 measurement_loop(DATALOG_ENABLED);
APS_Lab 0:f06ed53310a3 353
APS_Lab 0:f06ed53310a3 354 wait_ms(MEASUREMENT_LOOP_TIME);
APS_Lab 0:f06ed53310a3 355 }
takuma1 2:f6f76dde7e1d 356 //print_menu();
APS_Lab 0:f06ed53310a3 357 break;
APS_Lab 0:f06ed53310a3 358
APS_Lab 0:f06ed53310a3 359 case 'm': //prints menu
takuma1 2:f6f76dde7e1d 360 //print_menu();
APS_Lab 0:f06ed53310a3 361 break;
APS_Lab 0:f06ed53310a3 362
APS_Lab 0:f06ed53310a3 363 default:
APS_Lab 0:f06ed53310a3 364 pc.printf("Incorrect Option\n");
APS_Lab 0:f06ed53310a3 365 break;
takuma1 2:f6f76dde7e1d 366 }
APS_Lab 0:f06ed53310a3 367 }
takuma1 2:f6f76dde7e1d 368 void cell_read(){
takuma1 2:f6f76dde7e1d 369
takuma1 2:f6f76dde7e1d 370 int8_t error = 0;
takuma1 2:f6f76dde7e1d 371 uint32_t conv_time = 0;
takuma1 2:f6f76dde7e1d 372
takuma1 2:f6f76dde7e1d 373 int8_t readIC=0;
takuma1 2:f6f76dde7e1d 374 wakeup_sleep(TOTAL_IC);
takuma1 2:f6f76dde7e1d 375 error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); // Set to read back all cell voltage registers
takuma1 2:f6f76dde7e1d 376 check_error(error);
takuma1 2:f6f76dde7e1d 377 print_cells(DATALOG_DISABLED);
takuma1 2:f6f76dde7e1d 378
takuma1 2:f6f76dde7e1d 379 }
takuma1 2:f6f76dde7e1d 380
APS_Lab 0:f06ed53310a3 381
APS_Lab 0:f06ed53310a3 382 void measurement_loop(uint8_t datalog_en)
APS_Lab 0:f06ed53310a3 383 {
APS_Lab 0:f06ed53310a3 384 int8_t error = 0;
APS_Lab 0:f06ed53310a3 385 if (WRITE_CONFIG == ENABLED) {
APS_Lab 0:f06ed53310a3 386 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 387 LTC6811_wrcfg(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 388 print_config();
APS_Lab 0:f06ed53310a3 389 }
APS_Lab 0:f06ed53310a3 390
APS_Lab 0:f06ed53310a3 391 if (READ_CONFIG == ENABLED) {
APS_Lab 0:f06ed53310a3 392 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 393 error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 394 check_error(error);
APS_Lab 0:f06ed53310a3 395 print_rxconfig();
APS_Lab 0:f06ed53310a3 396 }
APS_Lab 0:f06ed53310a3 397
APS_Lab 0:f06ed53310a3 398 if (MEASURE_CELL == ENABLED) {
APS_Lab 0:f06ed53310a3 399 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 400 LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
APS_Lab 0:f06ed53310a3 401 LTC6811_pollAdc();
APS_Lab 0:f06ed53310a3 402 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 403 error = LTC6811_rdcv(0, TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 404 check_error(error);
APS_Lab 0:f06ed53310a3 405 print_cells(datalog_en);
APS_Lab 0:f06ed53310a3 406
APS_Lab 0:f06ed53310a3 407 }
APS_Lab 0:f06ed53310a3 408
APS_Lab 0:f06ed53310a3 409 if (MEASURE_AUX == ENABLED) {
APS_Lab 0:f06ed53310a3 410 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 411 LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_ALL);
APS_Lab 0:f06ed53310a3 412 LTC6811_pollAdc();
APS_Lab 0:f06ed53310a3 413 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 414 error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
APS_Lab 0:f06ed53310a3 415 check_error(error);
APS_Lab 0:f06ed53310a3 416 print_aux(datalog_en);
APS_Lab 0:f06ed53310a3 417 }
APS_Lab 0:f06ed53310a3 418
APS_Lab 0:f06ed53310a3 419 if (MEASURE_STAT == ENABLED) {
APS_Lab 0:f06ed53310a3 420 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 421 LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_ALL);
APS_Lab 0:f06ed53310a3 422 LTC6811_pollAdc();
APS_Lab 0:f06ed53310a3 423 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 424 error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
APS_Lab 0:f06ed53310a3 425 check_error(error);
APS_Lab 0:f06ed53310a3 426 print_stat();
APS_Lab 0:f06ed53310a3 427 }
APS_Lab 0:f06ed53310a3 428
APS_Lab 0:f06ed53310a3 429 if (PRINT_PEC == ENABLED) {
APS_Lab 0:f06ed53310a3 430 print_pec();
APS_Lab 0:f06ed53310a3 431 }
APS_Lab 0:f06ed53310a3 432
APS_Lab 0:f06ed53310a3 433 }
APS_Lab 0:f06ed53310a3 434
APS_Lab 0:f06ed53310a3 435
APS_Lab 0:f06ed53310a3 436 /*!*********************************
APS_Lab 0:f06ed53310a3 437 \brief Prints the main menu
APS_Lab 0:f06ed53310a3 438 ***********************************/
takuma1 2:f6f76dde7e1d 439 /*
APS_Lab 0:f06ed53310a3 440 void print_menu()
APS_Lab 0:f06ed53310a3 441 {
APS_Lab 0:f06ed53310a3 442 pc.printf("Please enter LTC6811 Command\n");
APS_Lab 0:f06ed53310a3 443 pc.printf("Write Configuration: 1 | Reset PEC Counter: 11\n");
APS_Lab 0:f06ed53310a3 444 pc.printf("Read Configuration: 2 | Run ADC Self Test: 12\n");
APS_Lab 0:f06ed53310a3 445 pc.printf("Start Cell Voltage Conversion: 3 | Set Discharge: 13\n");
APS_Lab 0:f06ed53310a3 446 pc.printf("Read Cell Voltages: 4 | Clear Discharge: 14\n");
APS_Lab 0:f06ed53310a3 447 pc.printf("Start Aux Voltage Conversion: 5 | Clear Registers: 15\n");
APS_Lab 0:f06ed53310a3 448 pc.printf("Read Aux Voltages: 6 | Run Mux Self Test: 16\n");
APS_Lab 0:f06ed53310a3 449 pc.printf("Start Stat Voltage Conversion: 7 | Run ADC overlap Test: 17\n");
APS_Lab 0:f06ed53310a3 450 pc.printf("Read Stat Voltages: 8 | Run Digital Redundancy Test: 18\n");
APS_Lab 0:f06ed53310a3 451 pc.printf("loop Measurements: 9 | Run Open Wire Test: 19\n");
APS_Lab 0:f06ed53310a3 452 pc.printf("Read PEC Errors: 10 | Loop measurements with datalog output: 20\n");
APS_Lab 0:f06ed53310a3 453 pc.printf("\n");
APS_Lab 0:f06ed53310a3 454 pc.printf("Please enter command:\n");
APS_Lab 0:f06ed53310a3 455 pc.printf("\n");
APS_Lab 0:f06ed53310a3 456 }
takuma1 2:f6f76dde7e1d 457 */
APS_Lab 0:f06ed53310a3 458 /*!************************************************************
APS_Lab 0:f06ed53310a3 459 \brief Prints cell voltage codes to the serial port
APS_Lab 0:f06ed53310a3 460 *************************************************************/
APS_Lab 0:f06ed53310a3 461 void print_cells(uint8_t datalog_en)
APS_Lab 0:f06ed53310a3 462 {
APS_Lab 0:f06ed53310a3 463 for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 464
APS_Lab 0:f06ed53310a3 465 if (datalog_en == 0) {
APS_Lab 0:f06ed53310a3 466 pc.printf("IC%d, ", current_ic+1);
APS_Lab 0:f06ed53310a3 467 for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) {
APS_Lab 0:f06ed53310a3 468 pc.printf("C%d:", i+1);
APS_Lab 0:f06ed53310a3 469 pc.printf("%.4f, ", bms_ic[current_ic].cells.c_codes[i]*0.0001);
APS_Lab 0:f06ed53310a3 470 }
APS_Lab 0:f06ed53310a3 471 pc.printf("\n");
APS_Lab 0:f06ed53310a3 472 } else {
APS_Lab 0:f06ed53310a3 473 pc.printf("Cells, ");
APS_Lab 0:f06ed53310a3 474 for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) {
APS_Lab 0:f06ed53310a3 475 pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001);
APS_Lab 0:f06ed53310a3 476 }
APS_Lab 0:f06ed53310a3 477 }
APS_Lab 0:f06ed53310a3 478
APS_Lab 0:f06ed53310a3 479 }
APS_Lab 0:f06ed53310a3 480 pc.printf("\n");
APS_Lab 0:f06ed53310a3 481 }
APS_Lab 0:f06ed53310a3 482
APS_Lab 0:f06ed53310a3 483 /*!****************************************************************************
APS_Lab 0:f06ed53310a3 484 \brief Prints Open wire test results to the serial port
APS_Lab 0:f06ed53310a3 485 *****************************************************************************/
APS_Lab 0:f06ed53310a3 486 void print_open()
APS_Lab 0:f06ed53310a3 487 {
APS_Lab 0:f06ed53310a3 488 for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 489 if (bms_ic[current_ic].system_open_wire == 0) {
APS_Lab 0:f06ed53310a3 490 pc.printf("No Opens Detected on IC%d\n", current_ic+1);
APS_Lab 0:f06ed53310a3 491 } else {
APS_Lab 0:f06ed53310a3 492 for (int cell=0; cell<bms_ic[0].ic_reg.cell_channels+1; cell++) {
APS_Lab 0:f06ed53310a3 493 if ((bms_ic[current_ic].system_open_wire &(1<<cell))>0) {
APS_Lab 0:f06ed53310a3 494 pc.printf("There is an open wire on IC%d Channel: %d\n", current_ic + 1, cell);
APS_Lab 0:f06ed53310a3 495 }
APS_Lab 0:f06ed53310a3 496 }
APS_Lab 0:f06ed53310a3 497 }
APS_Lab 0:f06ed53310a3 498 }
APS_Lab 0:f06ed53310a3 499 }
APS_Lab 0:f06ed53310a3 500
APS_Lab 0:f06ed53310a3 501 /*!****************************************************************************
APS_Lab 0:f06ed53310a3 502 \brief Prints GPIO voltage codes and Vref2 voltage code onto the serial port
APS_Lab 0:f06ed53310a3 503 *****************************************************************************/
APS_Lab 0:f06ed53310a3 504 void print_aux(uint8_t datalog_en)
APS_Lab 0:f06ed53310a3 505 {
APS_Lab 0:f06ed53310a3 506
APS_Lab 0:f06ed53310a3 507 for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 508 if (datalog_en == 0) {
APS_Lab 0:f06ed53310a3 509 pc.printf(" IC%d", current_ic+1);
APS_Lab 0:f06ed53310a3 510 for (int i=0; i < 5; i++) {
APS_Lab 0:f06ed53310a3 511 pc.printf(" GPIO-%d:%.4f,", i+1, bms_ic[current_ic].aux.a_codes[i]*0.0001);
APS_Lab 0:f06ed53310a3 512 }
APS_Lab 0:f06ed53310a3 513 pc.printf("Vref2:%.4f\n", bms_ic[current_ic].aux.a_codes[5]*0.0001);
APS_Lab 0:f06ed53310a3 514 } else {
APS_Lab 0:f06ed53310a3 515 pc.printf("AUX, ");
APS_Lab 0:f06ed53310a3 516 for (int i=0; i < 6; i++) {
APS_Lab 0:f06ed53310a3 517 pc.printf("%.4f,", bms_ic[current_ic].aux.a_codes[i]*0.0001);
APS_Lab 0:f06ed53310a3 518 }
APS_Lab 0:f06ed53310a3 519 }
APS_Lab 0:f06ed53310a3 520 }
APS_Lab 0:f06ed53310a3 521 pc.printf("\n");
APS_Lab 0:f06ed53310a3 522 }
APS_Lab 0:f06ed53310a3 523
APS_Lab 0:f06ed53310a3 524 /*!****************************************************************************
APS_Lab 0:f06ed53310a3 525 \brief Prints Status voltage codes and Vref2 voltage code onto the serial port
APS_Lab 0:f06ed53310a3 526 *****************************************************************************/
APS_Lab 0:f06ed53310a3 527 void print_stat()
APS_Lab 0:f06ed53310a3 528 {
APS_Lab 0:f06ed53310a3 529
APS_Lab 0:f06ed53310a3 530 for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 531 pc.printf("IC%d", current_ic+1);
APS_Lab 0:f06ed53310a3 532 pc.printf(" SOC:%.4f,", bms_ic[current_ic].stat.stat_codes[0]*0.0001*20);
APS_Lab 0:f06ed53310a3 533 pc.printf(" Itemp:%.4f,", bms_ic[current_ic].stat.stat_codes[1]*0.0001);
APS_Lab 0:f06ed53310a3 534 pc.printf(" VregA:%.4f,", bms_ic[current_ic].stat.stat_codes[2]*0.0001);
APS_Lab 0:f06ed53310a3 535 pc.printf(" VregD:%.4f\n", bms_ic[current_ic].stat.stat_codes[3]*0.0001);
APS_Lab 0:f06ed53310a3 536 }
APS_Lab 0:f06ed53310a3 537
APS_Lab 0:f06ed53310a3 538 pc.printf("\n");
APS_Lab 0:f06ed53310a3 539 }
APS_Lab 0:f06ed53310a3 540
APS_Lab 0:f06ed53310a3 541 /*!******************************************************************************
APS_Lab 0:f06ed53310a3 542 \brief Prints the configuration data that is going to be written to the LTC6811
APS_Lab 0:f06ed53310a3 543 to the serial port.
APS_Lab 0:f06ed53310a3 544 ********************************************************************************/
APS_Lab 0:f06ed53310a3 545 void print_config()
APS_Lab 0:f06ed53310a3 546 {
APS_Lab 0:f06ed53310a3 547 int cfg_pec;
APS_Lab 0:f06ed53310a3 548
APS_Lab 0:f06ed53310a3 549 pc.printf("Written Configuration: \n");
APS_Lab 0:f06ed53310a3 550 for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 551 pc.printf(" IC ");
APS_Lab 0:f06ed53310a3 552 pc.printf("%d", current_ic+1);
APS_Lab 0:f06ed53310a3 553 pc.printf(": ");
APS_Lab 0:f06ed53310a3 554 pc.printf("0x");
APS_Lab 0:f06ed53310a3 555 serial_print_hex(bms_ic[current_ic].config.tx_data[0]);
APS_Lab 0:f06ed53310a3 556 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 557 serial_print_hex(bms_ic[current_ic].config.tx_data[1]);
APS_Lab 0:f06ed53310a3 558 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 559 serial_print_hex(bms_ic[current_ic].config.tx_data[2]);
APS_Lab 0:f06ed53310a3 560 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 561 serial_print_hex(bms_ic[current_ic].config.tx_data[3]);
APS_Lab 0:f06ed53310a3 562 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 563 serial_print_hex(bms_ic[current_ic].config.tx_data[4]);
APS_Lab 0:f06ed53310a3 564 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 565 serial_print_hex(bms_ic[current_ic].config.tx_data[5]);
APS_Lab 0:f06ed53310a3 566 pc.printf(", Calculated PEC: 0x");
APS_Lab 0:f06ed53310a3 567 cfg_pec = pec15_calc(6,&bms_ic[current_ic].config.tx_data[0]);
APS_Lab 0:f06ed53310a3 568 serial_print_hex((uint8_t)(cfg_pec>>8));
APS_Lab 0:f06ed53310a3 569 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 570 serial_print_hex((uint8_t)(cfg_pec));
APS_Lab 0:f06ed53310a3 571 pc.printf("\n");
APS_Lab 0:f06ed53310a3 572 }
APS_Lab 0:f06ed53310a3 573 pc.printf("\n");
APS_Lab 0:f06ed53310a3 574 }
APS_Lab 0:f06ed53310a3 575
APS_Lab 0:f06ed53310a3 576 /*!*****************************************************************
APS_Lab 0:f06ed53310a3 577 \brief Prints the configuration data that was read back from the
APS_Lab 0:f06ed53310a3 578 LTC6811 to the serial port.
APS_Lab 0:f06ed53310a3 579 *******************************************************************/
APS_Lab 0:f06ed53310a3 580 void print_rxconfig()
APS_Lab 0:f06ed53310a3 581 {
APS_Lab 0:f06ed53310a3 582 pc.printf("Received Configuration ");
APS_Lab 0:f06ed53310a3 583 for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 584 pc.printf(" IC ");
APS_Lab 0:f06ed53310a3 585 pc.printf("%d", current_ic+1);
APS_Lab 0:f06ed53310a3 586 pc.printf(": 0x");
APS_Lab 0:f06ed53310a3 587 serial_print_hex(bms_ic[current_ic].config.rx_data[0]);
APS_Lab 0:f06ed53310a3 588 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 589 serial_print_hex(bms_ic[current_ic].config.rx_data[1]);
APS_Lab 0:f06ed53310a3 590 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 591 serial_print_hex(bms_ic[current_ic].config.rx_data[2]);
APS_Lab 0:f06ed53310a3 592 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 593 serial_print_hex(bms_ic[current_ic].config.rx_data[3]);
APS_Lab 0:f06ed53310a3 594 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 595 serial_print_hex(bms_ic[current_ic].config.rx_data[4]);
APS_Lab 0:f06ed53310a3 596 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 597 serial_print_hex(bms_ic[current_ic].config.rx_data[5]);
APS_Lab 0:f06ed53310a3 598 pc.printf(", Received PEC: 0x");
APS_Lab 0:f06ed53310a3 599 serial_print_hex(bms_ic[current_ic].config.rx_data[6]);
APS_Lab 0:f06ed53310a3 600 pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 601 serial_print_hex(bms_ic[current_ic].config.rx_data[7]);
APS_Lab 0:f06ed53310a3 602 pc.printf("\n");
APS_Lab 0:f06ed53310a3 603 }
APS_Lab 0:f06ed53310a3 604 pc.printf("\n");
APS_Lab 0:f06ed53310a3 605 }
APS_Lab 0:f06ed53310a3 606
APS_Lab 0:f06ed53310a3 607 void print_pec()
APS_Lab 0:f06ed53310a3 608 {
APS_Lab 0:f06ed53310a3 609 for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 610 pc.printf("\n%d", bms_ic[current_ic].crc_count.pec_count);
APS_Lab 0:f06ed53310a3 611 pc.printf(" : PEC Errors Detected on IC");
APS_Lab 0:f06ed53310a3 612 pc.printf("%d\n", current_ic+1);
APS_Lab 0:f06ed53310a3 613 }
APS_Lab 0:f06ed53310a3 614 }
APS_Lab 0:f06ed53310a3 615
APS_Lab 0:f06ed53310a3 616
APS_Lab 0:f06ed53310a3 617 void serial_print_hex(uint8_t data)
APS_Lab 0:f06ed53310a3 618 {
APS_Lab 0:f06ed53310a3 619 if (data < 16) {
APS_Lab 0:f06ed53310a3 620 pc.printf("0x0%X", data);
APS_Lab 0:f06ed53310a3 621 } else
APS_Lab 0:f06ed53310a3 622 pc.printf("0x%X", data);
APS_Lab 0:f06ed53310a3 623 }
APS_Lab 0:f06ed53310a3 624
APS_Lab 0:f06ed53310a3 625 //Function to check error flag and print PEC error message
APS_Lab 0:f06ed53310a3 626 void check_error(int error)
APS_Lab 0:f06ed53310a3 627 {
APS_Lab 0:f06ed53310a3 628 if (error == -1) {
APS_Lab 0:f06ed53310a3 629 pc.printf("A PEC error was detected in the received data");
APS_Lab 0:f06ed53310a3 630 }
APS_Lab 0:f06ed53310a3 631 }
APS_Lab 0:f06ed53310a3 632
APS_Lab 0:f06ed53310a3 633
APS_Lab 0:f06ed53310a3 634 // hex conversion constants
APS_Lab 0:f06ed53310a3 635 char hex_digits[16]= {
APS_Lab 0:f06ed53310a3 636 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
APS_Lab 0:f06ed53310a3 637 };
APS_Lab 0:f06ed53310a3 638
APS_Lab 0:f06ed53310a3 639 // global variables
APS_Lab 0:f06ed53310a3 640
APS_Lab 0:f06ed53310a3 641 char hex_to_byte_buffer[5]= {
APS_Lab 0:f06ed53310a3 642 '0', 'x', '0', '0', '\0'
APS_Lab 0:f06ed53310a3 643 }; // buffer for ASCII hex to byte conversion
APS_Lab 0:f06ed53310a3 644 char byte_to_hex_buffer[3]= {
APS_Lab 0:f06ed53310a3 645 '\0','\0','\0'
APS_Lab 0:f06ed53310a3 646 };
APS_Lab 0:f06ed53310a3 647
takuma1 2:f6f76dde7e1d 648 // シリアル インターフェイスから ui_バッファへのデータの読み取り
APS_Lab 0:f06ed53310a3 649 uint8_t read_data()
APS_Lab 0:f06ed53310a3 650 {
takuma1 2:f6f76dde7e1d 651 uint8_t index = 0; // 内の現在の場所を保持するインデックス
takuma1 2:f6f76dde7e1d 652 int c; // 着信キーストロークの格納に使用される単一の文字
APS_Lab 0:f06ed53310a3 653 //pc.printf("check 1\n");
APS_Lab 0:f06ed53310a3 654 while (index < UI_BUFFER_SIZE-1) {
APS_Lab 0:f06ed53310a3 655 //pc.printf("check 2\n");
APS_Lab 0:f06ed53310a3 656 c = pc.getc(); //read one character
APS_Lab 0:f06ed53310a3 657 //return c;
APS_Lab 0:f06ed53310a3 658 //pc.printf("check 3\n");
APS_Lab 0:f06ed53310a3 659
takuma1 2:f6f76dde7e1d 660 if (((char) c == '\r') || ((char) c == '\n')) break; // キャリッジ リターンまたはラインフィードの場合は、データを停止して返します。
APS_Lab 0:f06ed53310a3 661 if ( ((char) c == '\x7F') || ((char) c == '\x08') ) { // remove previous character (decrement index) if Backspace/Delete key pressed index--;
APS_Lab 0:f06ed53310a3 662 if (index > 0) index--;
APS_Lab 0:f06ed53310a3 663 } else if (c >= 0) {
APS_Lab 0:f06ed53310a3 664 ui_buffer[index++]=(char) c; // put character into ui_buffer
APS_Lab 0:f06ed53310a3 665 }
APS_Lab 0:f06ed53310a3 666 //pc.printf("check 4\n");
APS_Lab 0:f06ed53310a3 667
APS_Lab 0:f06ed53310a3 668 }
APS_Lab 0:f06ed53310a3 669 ui_buffer[index]='\0'; // terminate string with NULL
APS_Lab 0:f06ed53310a3 670
APS_Lab 0:f06ed53310a3 671 if ((char) c == '\r') { // if the "last" character was a carriage return, also clear linefeed if it is next character
APS_Lab 0:f06ed53310a3 672 wait_ms(1);
APS_Lab 0:f06ed53310a3 673 //pc.printf("check 5\n");
APS_Lab 0:f06ed53310a3 674
APS_Lab 0:f06ed53310a3 675 if (pc.readable()==1) {
APS_Lab 0:f06ed53310a3 676 //pc.printf("check 6\n");
APS_Lab 0:f06ed53310a3 677 pc.getc(); // if linefeed appears, read it and throw it away
APS_Lab 0:f06ed53310a3 678 }
APS_Lab 0:f06ed53310a3 679 //pc.printf("check 7\n");
APS_Lab 0:f06ed53310a3 680
APS_Lab 0:f06ed53310a3 681 }
APS_Lab 0:f06ed53310a3 682 //pc.printf("check 8\n");
APS_Lab 0:f06ed53310a3 683
APS_Lab 0:f06ed53310a3 684 return index; // return number of characters, not including null terminator
APS_Lab 0:f06ed53310a3 685 }
APS_Lab 0:f06ed53310a3 686
APS_Lab 0:f06ed53310a3 687 // Read a float value from the serial interface
APS_Lab 0:f06ed53310a3 688 float read_float()
APS_Lab 0:f06ed53310a3 689 {
APS_Lab 0:f06ed53310a3 690 float data;
APS_Lab 0:f06ed53310a3 691 read_data();
APS_Lab 0:f06ed53310a3 692 data = atof(ui_buffer);
APS_Lab 0:f06ed53310a3 693 return(data);
APS_Lab 0:f06ed53310a3 694 }
APS_Lab 0:f06ed53310a3 695
APS_Lab 0:f06ed53310a3 696 // Read an integer from the serial interface.
APS_Lab 0:f06ed53310a3 697 // The routine can recognize Hex, Decimal, Octal, or Binary
APS_Lab 0:f06ed53310a3 698 // Example:
APS_Lab 0:f06ed53310a3 699 // Hex: 0x11 (0x prefix)
APS_Lab 0:f06ed53310a3 700 // Decimal: 17
APS_Lab 0:f06ed53310a3 701 // Octal: 021 (leading zero prefix)
APS_Lab 0:f06ed53310a3 702 // Binary: B10001 (leading B prefix)
APS_Lab 0:f06ed53310a3 703 int32_t read_int()
APS_Lab 0:f06ed53310a3 704 {
APS_Lab 0:f06ed53310a3 705 int32_t data;
APS_Lab 0:f06ed53310a3 706 read_data();
APS_Lab 0:f06ed53310a3 707 if (ui_buffer[0] == 'm')
APS_Lab 0:f06ed53310a3 708 return('m');
APS_Lab 0:f06ed53310a3 709 if ((ui_buffer[0] == 'B') || (ui_buffer[0] == 'b')) {
APS_Lab 0:f06ed53310a3 710 data = strtol(ui_buffer+1, NULL, 2);
APS_Lab 0:f06ed53310a3 711 } else
APS_Lab 0:f06ed53310a3 712 data = strtol(ui_buffer, NULL, 0);
APS_Lab 0:f06ed53310a3 713 return(data);
APS_Lab 0:f06ed53310a3 714 }
APS_Lab 0:f06ed53310a3 715
APS_Lab 0:f06ed53310a3 716 // Read a string from the serial interface. Returns a pointer to the ui_buffer.
APS_Lab 0:f06ed53310a3 717 char *read_string()
APS_Lab 0:f06ed53310a3 718 {
APS_Lab 0:f06ed53310a3 719 read_data();
APS_Lab 0:f06ed53310a3 720 return(ui_buffer);
APS_Lab 0:f06ed53310a3 721 }
APS_Lab 0:f06ed53310a3 722
APS_Lab 0:f06ed53310a3 723 // Read a character from the serial interface
APS_Lab 0:f06ed53310a3 724 int8_t read_char()
APS_Lab 0:f06ed53310a3 725 {
APS_Lab 0:f06ed53310a3 726 read_data();
APS_Lab 0:f06ed53310a3 727 return(ui_buffer[0]);
APS_Lab 0:f06ed53310a3 728 }