LTC6811 Battery Management System with ADuCM3029.

Committer:
APS_Lab
Date:
Fri Feb 09 04:43:04 2018 +0000
Revision:
1:4dd3e328a30b
Parent:
0:f06ed53310a3
Improved for SPI.

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