BMS_T2

Dependencies:   INA226

Committer:
takuma1
Date:
Tue Oct 13 07:20:11 2020 +0000
Revision:
3:61174d4de67d
Parent:
2:3bbbe439ec11
BMS_T2;

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"
takuma1 2:3bbbe439ec11 6 #include "CAN.h"
takuma1 2:3bbbe439ec11 7 #include "INA226.hpp"
takuma1 3:61174d4de67d 8
takuma1 2:3bbbe439ec11 9
APS_Lab 0:f06ed53310a3 10
APS_Lab 0:f06ed53310a3 11 #define UI_BUFFER_SIZE 64
APS_Lab 0:f06ed53310a3 12 #define SERIAL_TERMINATOR '\n'
APS_Lab 0:f06ed53310a3 13
APS_Lab 0:f06ed53310a3 14 #define ENABLED 1
APS_Lab 0:f06ed53310a3 15 #define DISABLED 0
APS_Lab 0:f06ed53310a3 16
APS_Lab 0:f06ed53310a3 17 #define DATALOG_ENABLED 1
APS_Lab 0:f06ed53310a3 18 #define DATALOG_DISABLED 0
takuma1 2:3bbbe439ec11 19
takuma1 2:3bbbe439ec11 20
takuma1 2:3bbbe439ec11 21
APS_Lab 0:f06ed53310a3 22 DigitalOut led1(LED1);
APS_Lab 0:f06ed53310a3 23 Serial pc(USBTX, USBRX);
takuma1 2:3bbbe439ec11 24 CAN can1(p9,p10);
takuma1 2:3bbbe439ec11 25 CAN can2(p30,p29);
takuma1 2:3bbbe439ec11 26 I2C i2c(p28,p27);
takuma1 2:3bbbe439ec11 27 DigitalOut BAT_MIN_safty(p21);
takuma1 2:3bbbe439ec11 28 DigitalOut BAT_MAX_safty(p22);
takuma1 2:3bbbe439ec11 29 INA226 VCmonitor(i2c);
takuma1 3:61174d4de67d 30
takuma1 2:3bbbe439ec11 31
APS_Lab 0:f06ed53310a3 32
APS_Lab 0:f06ed53310a3 33 void run_command(uint32_t cmd);
APS_Lab 0:f06ed53310a3 34 void measurement_loop(uint8_t datalog_en);
takuma1 2:3bbbe439ec11 35 //void print_menu();
APS_Lab 0:f06ed53310a3 36 void print_cells(uint8_t datalog_en);
APS_Lab 0:f06ed53310a3 37 void print_open();
APS_Lab 0:f06ed53310a3 38 void print_aux(uint8_t datalog_en);
APS_Lab 0:f06ed53310a3 39 void print_stat();
APS_Lab 0:f06ed53310a3 40 void print_config();
APS_Lab 0:f06ed53310a3 41 void print_rxconfig();
APS_Lab 0:f06ed53310a3 42 void print_pec(void);
APS_Lab 0:f06ed53310a3 43 void serial_print_hex(uint8_t data);
APS_Lab 0:f06ed53310a3 44 void check_error(int error);
takuma1 2:3bbbe439ec11 45 void wakeup();
takuma1 2:3bbbe439ec11 46 void cell_read();
takuma1 2:3bbbe439ec11 47 void spi_error();
takuma1 2:3bbbe439ec11 48 void ic_check();
takuma1 2:3bbbe439ec11 49 void spi_check();
takuma1 2:3bbbe439ec11 50 void print_CAN(uint8_t datalog_en);
takuma1 2:3bbbe439ec11 51 void print_math();
takuma1 2:3bbbe439ec11 52 void can_sent1();
takuma1 2:3bbbe439ec11 53 void can_sent2();
takuma1 2:3bbbe439ec11 54 void can_sent3();
takuma1 2:3bbbe439ec11 55 void can_sent4();
takuma1 2:3bbbe439ec11 56 //void can_set();
takuma1 2:3bbbe439ec11 57 void can_wait();
takuma1 2:3bbbe439ec11 58 void ic_set();
takuma1 2:3bbbe439ec11 59
takuma1 2:3bbbe439ec11 60
APS_Lab 0:f06ed53310a3 61 //char get_char();
APS_Lab 0:f06ed53310a3 62 //void read_config_data(uint8_t cfg_data[][6], uint8_t nIC);
APS_Lab 0:f06ed53310a3 63
takuma1 2:3bbbe439ec11 64 const uint8_t TOTAL_IC = 3;//!<number of ICs in the daisy chain
APS_Lab 0:f06ed53310a3 65 char ui_buffer[UI_BUFFER_SIZE];
APS_Lab 0:f06ed53310a3 66
APS_Lab 0:f06ed53310a3 67
APS_Lab 0:f06ed53310a3 68 //ADC Command Configurations
APS_Lab 0:f06ed53310a3 69 //const uint8_t ADC_OPT = ADC_OPT_DISABLED; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 70 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 71 const uint8_t ADC_DCP = DCP_DISABLED; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 72 const uint8_t CELL_CH_TO_CONVERT = CELL_CH_ALL; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 73 const uint8_t AUX_CH_TO_CONVERT = AUX_CH_ALL; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 74 const uint8_t STAT_CH_TO_CONVERT = STAT_CH_ALL; // See LTC6811_daisy.h for Options
APS_Lab 0:f06ed53310a3 75
takuma1 2:3bbbe439ec11 76 const uint16_t MEASUREMENT_LOOP_TIME = 100;//milliseconds(mS)
APS_Lab 0:f06ed53310a3 77
APS_Lab 0:f06ed53310a3 78 //Under Voltage and Over Voltage Thresholds
takuma1 2:3bbbe439ec11 79 const uint16_t OV_THRESHOLD = 41000; //
takuma1 2:3bbbe439ec11 80 const uint16_t UV_THRESHOLD = 30000; //
APS_Lab 0:f06ed53310a3 81
APS_Lab 0:f06ed53310a3 82 //Loop Measurement Setup These Variables are ENABLED or DISABLED Remember ALL CAPS
APS_Lab 0:f06ed53310a3 83 const uint8_t WRITE_CONFIG = DISABLED; // This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 84 const uint8_t READ_CONFIG = DISABLED; // This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 85 const uint8_t MEASURE_CELL = ENABLED; // This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 86 const uint8_t MEASURE_AUX = DISABLED; // This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 87 const uint8_t MEASURE_STAT = DISABLED; //This is ENABLED or DISABLED
APS_Lab 0:f06ed53310a3 88 const uint8_t PRINT_PEC = DISABLED; //This is ENABLED or DISABLED
takuma1 2:3bbbe439ec11 89 short n =0;
APS_Lab 0:f06ed53310a3 90
APS_Lab 0:f06ed53310a3 91 uint8_t read_data();
APS_Lab 0:f06ed53310a3 92
APS_Lab 0:f06ed53310a3 93 float read_float();
APS_Lab 0:f06ed53310a3 94
takuma1 2:3bbbe439ec11 95
APS_Lab 0:f06ed53310a3 96 int32_t read_int();
APS_Lab 0:f06ed53310a3 97
takuma1 2:3bbbe439ec11 98 int BAT_MIN, BAT_MAX, BAT_AVG;
takuma1 2:3bbbe439ec11 99 double BAT_Current;
takuma1 2:3bbbe439ec11 100 long BAT_SUM;
takuma1 2:3bbbe439ec11 101 unsigned short BAT_CELL[27];
APS_Lab 0:f06ed53310a3 102
takuma1 2:3bbbe439ec11 103 char *read_string();
takuma1 2:3bbbe439ec11 104 int8_t read_char();
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 int main(void)
APS_Lab 0:f06ed53310a3 109 {
APS_Lab 0:f06ed53310a3 110 uint32_t user_command;
APS_Lab 0:f06ed53310a3 111
APS_Lab 0:f06ed53310a3 112 pc.baud(115200);
takuma1 2:3bbbe439ec11 113
takuma1 2:3bbbe439ec11 114 while(1) {
takuma1 2:3bbbe439ec11 115 BAT_MIN = 0;
takuma1 2:3bbbe439ec11 116 BAT_MAX = 0; // 0にする
takuma1 2:3bbbe439ec11 117 ic_set();
takuma1 2:3bbbe439ec11 118 }
takuma1 2:3bbbe439ec11 119 }
takuma1 2:3bbbe439ec11 120
takuma1 2:3bbbe439ec11 121
takuma1 2:3bbbe439ec11 122
takuma1 2:3bbbe439ec11 123 void ic_set(){
takuma1 2:3bbbe439ec11 124
takuma1 2:3bbbe439ec11 125 //__disable_irq();
takuma1 2:3bbbe439ec11 126 ic_check();
takuma1 2:3bbbe439ec11 127 spi_check();
takuma1 2:3bbbe439ec11 128 spi_error();
takuma1 2:3bbbe439ec11 129 wakeup();
takuma1 2:3bbbe439ec11 130 cell_read();
takuma1 2:3bbbe439ec11 131
takuma1 2:3bbbe439ec11 132 }
takuma1 2:3bbbe439ec11 133
takuma1 2:3bbbe439ec11 134 void ic_check(){ //1
takuma1 2:3bbbe439ec11 135
takuma1 2:3bbbe439ec11 136 //__disable_irq();
takuma1 2:3bbbe439ec11 137 pc.baud(115200);
APS_Lab 0:f06ed53310a3 138 spi_enable();
APS_Lab 0:f06ed53310a3 139 LTC681x_init_cfg(TOTAL_IC, bms_ic);
APS_Lab 0:f06ed53310a3 140 LTC6811_reset_crc_count(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 141 LTC6811_init_reg_limits(TOTAL_IC,bms_ic);
takuma1 2:3bbbe439ec11 142 wakeup_sleep(TOTAL_IC);
takuma1 2:3bbbe439ec11 143 LTC6811_wrcfg(TOTAL_IC,bms_ic);
takuma1 2:3bbbe439ec11 144 print_config();
takuma1 2:3bbbe439ec11 145 //printf("1");
takuma1 2:3bbbe439ec11 146 //spi_check();
takuma1 2:3bbbe439ec11 147
takuma1 2:3bbbe439ec11 148 }
takuma1 2:3bbbe439ec11 149
takuma1 2:3bbbe439ec11 150 void spi_check(){ //2
APS_Lab 0:f06ed53310a3 151
takuma1 2:3bbbe439ec11 152 __disable_irq();
takuma1 2:3bbbe439ec11 153 int countup;
takuma1 2:3bbbe439ec11 154 int8_t error = 0;
takuma1 2:3bbbe439ec11 155
takuma1 2:3bbbe439ec11 156 wakeup_sleep(TOTAL_IC);
takuma1 2:3bbbe439ec11 157 error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
takuma1 2:3bbbe439ec11 158 check_error(error);
takuma1 2:3bbbe439ec11 159 print_rxconfig();
takuma1 2:3bbbe439ec11 160 //printf("2");
takuma1 2:3bbbe439ec11 161 //spi_error();
takuma1 2:3bbbe439ec11 162 }
APS_Lab 0:f06ed53310a3 163
takuma1 2:3bbbe439ec11 164
takuma1 2:3bbbe439ec11 165 void wakeup(){ //3
APS_Lab 0:f06ed53310a3 166
takuma1 2:3bbbe439ec11 167 __disable_irq();
takuma1 2:3bbbe439ec11 168 int countup;
APS_Lab 0:f06ed53310a3 169 int8_t error = 0;
APS_Lab 0:f06ed53310a3 170 uint32_t conv_time = 0;
takuma1 2:3bbbe439ec11 171
takuma1 2:3bbbe439ec11 172
APS_Lab 0:f06ed53310a3 173 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 174 LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
APS_Lab 0:f06ed53310a3 175 conv_time = LTC6811_pollAdc();
takuma1 2:3bbbe439ec11 176 //printf("3");
takuma1 2:3bbbe439ec11 177 //cell_read();
takuma1 2:3bbbe439ec11 178 }
takuma1 2:3bbbe439ec11 179
takuma1 2:3bbbe439ec11 180
takuma1 2:3bbbe439ec11 181 void spi_error(){
takuma1 2:3bbbe439ec11 182
takuma1 2:3bbbe439ec11 183 //__disable_irq();
takuma1 2:3bbbe439ec11 184
takuma1 2:3bbbe439ec11 185 int8_t error = 0;
takuma1 2:3bbbe439ec11 186 int countup;
takuma1 2:3bbbe439ec11 187 /*
takuma1 2:3bbbe439ec11 188 if(error = -1 ){
takuma1 2:3bbbe439ec11 189 for(countup = 0; countup <= 2; countup++){
takuma1 2:3bbbe439ec11 190 spi_check();
takuma1 2:3bbbe439ec11 191 if(error =1){
takuma1 2:3bbbe439ec11 192 break;
takuma1 2:3bbbe439ec11 193 }
takuma1 2:3bbbe439ec11 194 }
takuma1 2:3bbbe439ec11 195 }
takuma1 2:3bbbe439ec11 196 if(error = 1){
takuma1 2:3bbbe439ec11 197 wakeup();
takuma1 2:3bbbe439ec11 198 }
takuma1 2:3bbbe439ec11 199 */
takuma1 2:3bbbe439ec11 200
takuma1 2:3bbbe439ec11 201 //wakeup();
takuma1 2:3bbbe439ec11 202
takuma1 2:3bbbe439ec11 203
takuma1 2:3bbbe439ec11 204 }
takuma1 2:3bbbe439ec11 205
takuma1 2:3bbbe439ec11 206 void cell_read(){ //4 電圧読み取り
APS_Lab 0:f06ed53310a3 207
takuma1 2:3bbbe439ec11 208 __disable_irq();
takuma1 2:3bbbe439ec11 209 int8_t error = 0;
takuma1 2:3bbbe439ec11 210 uint32_t conv_time = 0;
takuma1 2:3bbbe439ec11 211 int8_t readIC=0;
takuma1 2:3bbbe439ec11 212
takuma1 2:3bbbe439ec11 213 wakeup_sleep(TOTAL_IC);
takuma1 2:3bbbe439ec11 214 error = LTC6811_rdcv(0, TOTAL_IC,bms_ic);
takuma1 2:3bbbe439ec11 215 check_error(error);
takuma1 2:3bbbe439ec11 216 //print_cells(DATALOG_DISABLED);
takuma1 2:3bbbe439ec11 217 print_CAN(DATALOG_DISABLED);
takuma1 2:3bbbe439ec11 218 //printf("4");
takuma1 2:3bbbe439ec11 219
takuma1 2:3bbbe439ec11 220 }
takuma1 2:3bbbe439ec11 221
takuma1 2:3bbbe439ec11 222
APS_Lab 0:f06ed53310a3 223
takuma1 2:3bbbe439ec11 224
takuma1 2:3bbbe439ec11 225 void print_CAN(uint8_t datalog_en){
takuma1 2:3bbbe439ec11 226
takuma1 2:3bbbe439ec11 227 __disable_irq();
takuma1 2:3bbbe439ec11 228 short n =0;
takuma1 2:3bbbe439ec11 229 int BAT_SUM = 0; // 0にする
takuma1 2:3bbbe439ec11 230 int BAT_MIN = 55876;
takuma1 2:3bbbe439ec11 231 double V,C;
takuma1 2:3bbbe439ec11 232 int BTA_MAX = 0;
takuma1 2:3bbbe439ec11 233 float tempC, sum, sum_2, ave, ave_2;
takuma1 2:3bbbe439ec11 234 int i, j, k, l; //平均化処理に使用
takuma1 2:3bbbe439ec11 235 int r = 5; //平均する値の個数
takuma1 2:3bbbe439ec11 236
takuma1 2:3bbbe439ec11 237 for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) {
takuma1 2:3bbbe439ec11 238
takuma1 2:3bbbe439ec11 239 //if (datalog_en == 0) {
takuma1 2:3bbbe439ec11 240 //pc.printf("IC%d, ", current_ic+1);
takuma1 2:3bbbe439ec11 241 //for(n = 0; n <= 12; n++){
takuma1 2:3bbbe439ec11 242 for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) {
takuma1 2:3bbbe439ec11 243
takuma1 2:3bbbe439ec11 244 //BAT_CELL[n] = bms_ic[current_ic].cells.c_codes[i];
takuma1 2:3bbbe439ec11 245
takuma1 2:3bbbe439ec11 246
takuma1 2:3bbbe439ec11 247 pc.printf("C%d:", i+1);
takuma1 2:3bbbe439ec11 248 pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001);
takuma1 2:3bbbe439ec11 249 BAT_SUM = bms_ic[current_ic].cells.c_codes[i] + BAT_SUM;
takuma1 2:3bbbe439ec11 250
takuma1 2:3bbbe439ec11 251 if(bms_ic[current_ic].cells.c_codes[i] > 10000){
takuma1 2:3bbbe439ec11 252 if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){
takuma1 2:3bbbe439ec11 253 BAT_MIN = bms_ic[current_ic].cells.c_codes[i];
takuma1 2:3bbbe439ec11 254 }
takuma1 2:3bbbe439ec11 255 }
takuma1 2:3bbbe439ec11 256
takuma1 2:3bbbe439ec11 257 if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){
takuma1 2:3bbbe439ec11 258 BAT_MAX = bms_ic[current_ic].cells.c_codes[i];
takuma1 2:3bbbe439ec11 259
takuma1 2:3bbbe439ec11 260 }
takuma1 2:3bbbe439ec11 261
takuma1 3:61174d4de67d 262 /*
takuma1 2:3bbbe439ec11 263
takuma1 2:3bbbe439ec11 264 //平均化処理
takuma1 2:3bbbe439ec11 265
takuma1 2:3bbbe439ec11 266 sum = 0;
takuma1 2:3bbbe439ec11 267 ave = 0;
takuma1 2:3bbbe439ec11 268 i = 0;
takuma1 2:3bbbe439ec11 269 for( i = 0; i < r; i++)
takuma1 2:3bbbe439ec11 270 {
takuma1 2:3bbbe439ec11 271 float temp = max.read_temp();
takuma1 2:3bbbe439ec11 272 sum += temp;
takuma1 2:3bbbe439ec11 273 }
takuma1 2:3bbbe439ec11 274
takuma1 2:3bbbe439ec11 275 ave = sum / r;
takuma1 2:3bbbe439ec11 276 printf("\n\rT: %f",ave );
takuma1 2:3bbbe439ec11 277
takuma1 2:3bbbe439ec11 278
takuma1 2:3bbbe439ec11 279
takuma1 2:3bbbe439ec11 280 max.deselect();
takuma1 2:3bbbe439ec11 281 max_2.select_2();
takuma1 2:3bbbe439ec11 282
takuma1 2:3bbbe439ec11 283 //平均化処理
takuma1 2:3bbbe439ec11 284 sum_2 = 0;
takuma1 2:3bbbe439ec11 285 ave_2 = 0;
takuma1 2:3bbbe439ec11 286 j = 0;
takuma1 2:3bbbe439ec11 287 for( j = 0; j < r; j++)
takuma1 2:3bbbe439ec11 288 {
takuma1 2:3bbbe439ec11 289 float temp_2 = max_2.read_temp_2();
takuma1 2:3bbbe439ec11 290 sum_2 += temp_2;
takuma1 2:3bbbe439ec11 291 }
takuma1 2:3bbbe439ec11 292
takuma1 2:3bbbe439ec11 293 ave_2 = sum_2 / r;
takuma1 2:3bbbe439ec11 294 printf("\n\rT: %f",ave_2 );
takuma1 2:3bbbe439ec11 295
takuma1 2:3bbbe439ec11 296
takuma1 2:3bbbe439ec11 297
takuma1 2:3bbbe439ec11 298 max.deselect();
takuma1 2:3bbbe439ec11 299 max_2.deselect_2();
takuma1 2:3bbbe439ec11 300
takuma1 2:3bbbe439ec11 301
takuma1 2:3bbbe439ec11 302
takuma1 2:3bbbe439ec11 303 max.deselect();
takuma1 2:3bbbe439ec11 304 max_2.deselect_2();
takuma1 2:3bbbe439ec11 305
takuma1 2:3bbbe439ec11 306
takuma1 2:3bbbe439ec11 307
takuma1 2:3bbbe439ec11 308
takuma1 2:3bbbe439ec11 309 //BAT_Current = C;
takuma1 2:3bbbe439ec11 310
takuma1 3:61174d4de67d 311 */
takuma1 2:3bbbe439ec11 312
takuma1 2:3bbbe439ec11 313
APS_Lab 0:f06ed53310a3 314 }
APS_Lab 0:f06ed53310a3 315
takuma1 2:3bbbe439ec11 316 }
takuma1 2:3bbbe439ec11 317
takuma1 2:3bbbe439ec11 318
takuma1 2:3bbbe439ec11 319 pc.printf("\n");
takuma1 2:3bbbe439ec11 320 pc.printf("SUM");
takuma1 2:3bbbe439ec11 321 pc.printf("%.4f, ",BAT_SUM*0.0001);
takuma1 2:3bbbe439ec11 322 BAT_AVG = BAT_SUM / 27;
takuma1 2:3bbbe439ec11 323 pc.printf("AVG");
takuma1 2:3bbbe439ec11 324 pc.printf("%.4f, ",BAT_AVG*0.0001);
takuma1 2:3bbbe439ec11 325 pc.printf("MIN");
takuma1 2:3bbbe439ec11 326 pc.printf("%.4f, ",BAT_MIN*0.0001);
takuma1 2:3bbbe439ec11 327 pc.printf("MAX");
takuma1 2:3bbbe439ec11 328 pc.printf("%.4f, ",BAT_MAX*0.0001);
takuma1 2:3bbbe439ec11 329 //pc.printf("BAT_Current");
takuma1 2:3bbbe439ec11 330 if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0)){
takuma1 2:3bbbe439ec11 331 printf("V,%f,C,%f\n",V,C);
takuma1 2:3bbbe439ec11 332 BAT_Current = C;
takuma1 2:3bbbe439ec11 333
takuma1 2:3bbbe439ec11 334 }
takuma1 2:3bbbe439ec11 335 pc.printf("%f, ",BAT_Current);
takuma1 2:3bbbe439ec11 336 pc.printf("\n");
takuma1 2:3bbbe439ec11 337 pc.printf("\n");
takuma1 2:3bbbe439ec11 338 printf("\n\rT: %f",ave );
takuma1 2:3bbbe439ec11 339 pc.printf("\n");
takuma1 2:3bbbe439ec11 340 printf("\n\rT: %f",ave_2 );
takuma1 2:3bbbe439ec11 341 pc.printf("\n");
takuma1 2:3bbbe439ec11 342
takuma1 2:3bbbe439ec11 343 if( BAT_MIN < 25000 ){
takuma1 2:3bbbe439ec11 344 BAT_MIN_safty = 1 ;
takuma1 2:3bbbe439ec11 345 wait(1);
takuma1 2:3bbbe439ec11 346 BAT_MIN_safty = 0 ;
takuma1 2:3bbbe439ec11 347 }
takuma1 2:3bbbe439ec11 348 if( BAT_MAX > 42000){
takuma1 2:3bbbe439ec11 349 BAT_MAX_safty = 1;
takuma1 2:3bbbe439ec11 350 wait(1);
takuma1 2:3bbbe439ec11 351 BAT_MAX_safty = 0;
takuma1 2:3bbbe439ec11 352
takuma1 2:3bbbe439ec11 353 }
takuma1 2:3bbbe439ec11 354 }
takuma1 2:3bbbe439ec11 355
takuma1 2:3bbbe439ec11 356
takuma1 2:3bbbe439ec11 357 /*
takuma1 2:3bbbe439ec11 358 void can_sent4(){
takuma1 2:3bbbe439ec11 359
takuma1 2:3bbbe439ec11 360 if(can1.write(CANMessage(271,&BAT_MAX2))){
takuma1 2:3bbbe439ec11 361 pc.printf("10CANMessage:%d\n",BAT_MAX2);
takuma1 2:3bbbe439ec11 362 }
takuma1 2:3bbbe439ec11 363 if(can1.write(CANMessage(272,&BAT_MAX3))){
takuma1 2:3bbbe439ec11 364 pc.printf("11CANMessage:%d\n",BAT_MAX3);
takuma1 2:3bbbe439ec11 365 }
takuma1 2:3bbbe439ec11 366 if(can1.write(CANMessage(273,&BAT_MAX4))){
takuma1 2:3bbbe439ec11 367 pc.printf("12CANMessage:%d\n",BAT_MAX4);
takuma1 2:3bbbe439ec11 368 }
takuma1 2:3bbbe439ec11 369
takuma1 2:3bbbe439ec11 370 }*/
takuma1 2:3bbbe439ec11 371 /*
takuma1 2:3bbbe439ec11 372 void can_set(){
takuma1 2:3bbbe439ec11 373
takuma1 2:3bbbe439ec11 374 //NVIC_SetPriority(TIMER3_IRQn, 0);
takuma1 2:3bbbe439ec11 375 //printf("aaaaa");
takuma1 2:3bbbe439ec11 376
takuma1 2:3bbbe439ec11 377 BAT_SUM1 = 0;
takuma1 2:3bbbe439ec11 378 BAT_SUM2 = 0;
takuma1 2:3bbbe439ec11 379 BAT_SUM3 = 0;
takuma1 2:3bbbe439ec11 380 BAT_SUM4 = 0;
takuma1 2:3bbbe439ec11 381 BAT_MIN1 = 0;
takuma1 2:3bbbe439ec11 382 BAT_MIN2 = 0;
takuma1 2:3bbbe439ec11 383 BAT_MIN3 = 0;
takuma1 2:3bbbe439ec11 384 BAT_MIN4 = 0;
takuma1 2:3bbbe439ec11 385 BAT_MAX1 = 0;
takuma1 2:3bbbe439ec11 386 BAT_MAX2 = 0;
takuma1 2:3bbbe439ec11 387 BAT_MAX3 = 0;
takuma1 2:3bbbe439ec11 388 BAT_MAX4 = 0;
takuma1 2:3bbbe439ec11 389 BAT_Current1 = 0;
takuma1 2:3bbbe439ec11 390 BAT_Current2 = 0;
takuma1 2:3bbbe439ec11 391
takuma1 2:3bbbe439ec11 392
takuma1 2:3bbbe439ec11 393 int data1 = 0; //BAT_SUM
takuma1 2:3bbbe439ec11 394 int data2 = 0;
takuma1 2:3bbbe439ec11 395 int data3 = 0; //BAT_MAX
takuma1 2:3bbbe439ec11 396 int data4 = 0; //BAT_Current
takuma1 2:3bbbe439ec11 397
APS_Lab 0:f06ed53310a3 398
takuma1 2:3bbbe439ec11 399 BAT_SUM = data1;
takuma1 2:3bbbe439ec11 400 data2 = BAT_MIN;
takuma1 2:3bbbe439ec11 401 data3 = BAT_MAX;
takuma1 2:3bbbe439ec11 402 data4 = BAT_Current;
takuma1 2:3bbbe439ec11 403
takuma1 2:3bbbe439ec11 404 BAT_SUM1 = data1 / 1000; //114
takuma1 2:3bbbe439ec11 405 BAT_SUM2 = data1 % 1000; //755
takuma1 2:3bbbe439ec11 406 BAT_SUM3 = BAT_SUM2 / 10; //75
takuma1 2:3bbbe439ec11 407 BAT_SUM4 = BAT_SUM2 % 10; // 5
takuma1 2:3bbbe439ec11 408
takuma1 2:3bbbe439ec11 409 BAT_MIN1 = data2 / 1000;
takuma1 2:3bbbe439ec11 410 BAT_MIN2 = data2 % 1000;
takuma1 2:3bbbe439ec11 411 BAT_MIN3 = BAT_MIN2 / 10;
takuma1 2:3bbbe439ec11 412 BAT_MIN4 = BAT_MIN2 % 10;
takuma1 2:3bbbe439ec11 413
takuma1 2:3bbbe439ec11 414 BAT_MAX = data3 / 1000;
takuma1 2:3bbbe439ec11 415 BAT_MAX2 = data3 % 1000;
takuma1 2:3bbbe439ec11 416 BAT_MAX3 = BAT_MAX2 / 10;
takuma1 2:3bbbe439ec11 417 BAT_MAX4 = BAT_MAX2 % 10;
takuma1 2:3bbbe439ec11 418
takuma1 2:3bbbe439ec11 419 BAT_Current1 = data4 / 100;
takuma1 2:3bbbe439ec11 420 BAT_Current2 = data4 % 100;
takuma1 2:3bbbe439ec11 421 printf("can_set\n");
takuma1 2:3bbbe439ec11 422 printf("aaaa%f",BAT_SUM);
APS_Lab 0:f06ed53310a3 423 }
takuma1 2:3bbbe439ec11 424 */
takuma1 2:3bbbe439ec11 425
APS_Lab 0:f06ed53310a3 426 void measurement_loop(uint8_t datalog_en)
APS_Lab 0:f06ed53310a3 427 {
APS_Lab 0:f06ed53310a3 428 int8_t error = 0;
APS_Lab 0:f06ed53310a3 429 if (WRITE_CONFIG == ENABLED) {
APS_Lab 0:f06ed53310a3 430 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 431 LTC6811_wrcfg(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 432 print_config();
APS_Lab 0:f06ed53310a3 433 }
APS_Lab 0:f06ed53310a3 434
APS_Lab 0:f06ed53310a3 435 if (READ_CONFIG == ENABLED) {
APS_Lab 0:f06ed53310a3 436 wakeup_sleep(TOTAL_IC);
APS_Lab 0:f06ed53310a3 437 error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 438 check_error(error);
APS_Lab 0:f06ed53310a3 439 print_rxconfig();
APS_Lab 0:f06ed53310a3 440 }
APS_Lab 0:f06ed53310a3 441
APS_Lab 0:f06ed53310a3 442 if (MEASURE_CELL == ENABLED) {
APS_Lab 0:f06ed53310a3 443 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 444 LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
APS_Lab 0:f06ed53310a3 445 LTC6811_pollAdc();
APS_Lab 0:f06ed53310a3 446 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 447 error = LTC6811_rdcv(0, TOTAL_IC,bms_ic);
APS_Lab 0:f06ed53310a3 448 check_error(error);
APS_Lab 0:f06ed53310a3 449 print_cells(datalog_en);
APS_Lab 0:f06ed53310a3 450
APS_Lab 0:f06ed53310a3 451 }
APS_Lab 0:f06ed53310a3 452
APS_Lab 0:f06ed53310a3 453 if (MEASURE_AUX == ENABLED) {
APS_Lab 0:f06ed53310a3 454 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 455 LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_ALL);
APS_Lab 0:f06ed53310a3 456 LTC6811_pollAdc();
APS_Lab 0:f06ed53310a3 457 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 458 error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
APS_Lab 0:f06ed53310a3 459 check_error(error);
APS_Lab 0:f06ed53310a3 460 print_aux(datalog_en);
APS_Lab 0:f06ed53310a3 461 }
APS_Lab 0:f06ed53310a3 462
APS_Lab 0:f06ed53310a3 463 if (MEASURE_STAT == ENABLED) {
APS_Lab 0:f06ed53310a3 464 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 465 LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_ALL);
APS_Lab 0:f06ed53310a3 466 LTC6811_pollAdc();
APS_Lab 0:f06ed53310a3 467 wakeup_idle(TOTAL_IC);
APS_Lab 0:f06ed53310a3 468 error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
APS_Lab 0:f06ed53310a3 469 check_error(error);
APS_Lab 0:f06ed53310a3 470 print_stat();
APS_Lab 0:f06ed53310a3 471 }
APS_Lab 0:f06ed53310a3 472
APS_Lab 0:f06ed53310a3 473 if (PRINT_PEC == ENABLED) {
APS_Lab 0:f06ed53310a3 474 print_pec();
APS_Lab 0:f06ed53310a3 475 }
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
APS_Lab 0:f06ed53310a3 481 void print_cells(uint8_t datalog_en)
APS_Lab 0:f06ed53310a3 482 {
APS_Lab 0:f06ed53310a3 483 for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 484
APS_Lab 0:f06ed53310a3 485 if (datalog_en == 0) {
APS_Lab 0:f06ed53310a3 486 pc.printf("IC%d, ", current_ic+1);
takuma1 2:3bbbe439ec11 487 for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) {
APS_Lab 0:f06ed53310a3 488 pc.printf("C%d:", i+1);
APS_Lab 0:f06ed53310a3 489 pc.printf("%.4f, ", bms_ic[current_ic].cells.c_codes[i]*0.0001);
takuma1 2:3bbbe439ec11 490 }
takuma1 2:3bbbe439ec11 491
APS_Lab 0:f06ed53310a3 492 pc.printf("\n");
takuma1 2:3bbbe439ec11 493 }
takuma1 2:3bbbe439ec11 494
takuma1 2:3bbbe439ec11 495 else {
APS_Lab 0:f06ed53310a3 496 pc.printf("Cells, ");
APS_Lab 0:f06ed53310a3 497 for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) {
takuma1 2:3bbbe439ec11 498 pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001);
takuma1 2:3bbbe439ec11 499
APS_Lab 0:f06ed53310a3 500 }
APS_Lab 0:f06ed53310a3 501 }
takuma1 2:3bbbe439ec11 502
APS_Lab 0:f06ed53310a3 503 }
APS_Lab 0:f06ed53310a3 504 pc.printf("\n");
APS_Lab 0:f06ed53310a3 505 }
APS_Lab 0:f06ed53310a3 506
APS_Lab 0:f06ed53310a3 507 void print_open()
APS_Lab 0:f06ed53310a3 508 {
APS_Lab 0:f06ed53310a3 509 for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 510 if (bms_ic[current_ic].system_open_wire == 0) {
APS_Lab 0:f06ed53310a3 511 pc.printf("No Opens Detected on IC%d\n", current_ic+1);
APS_Lab 0:f06ed53310a3 512 } else {
APS_Lab 0:f06ed53310a3 513 for (int cell=0; cell<bms_ic[0].ic_reg.cell_channels+1; cell++) {
APS_Lab 0:f06ed53310a3 514 if ((bms_ic[current_ic].system_open_wire &(1<<cell))>0) {
APS_Lab 0:f06ed53310a3 515 pc.printf("There is an open wire on IC%d Channel: %d\n", current_ic + 1, cell);
APS_Lab 0:f06ed53310a3 516 }
APS_Lab 0:f06ed53310a3 517 }
APS_Lab 0:f06ed53310a3 518 }
APS_Lab 0:f06ed53310a3 519 }
APS_Lab 0:f06ed53310a3 520 }
APS_Lab 0:f06ed53310a3 521
takuma1 2:3bbbe439ec11 522
APS_Lab 0:f06ed53310a3 523 void print_aux(uint8_t datalog_en)
APS_Lab 0:f06ed53310a3 524 {
APS_Lab 0:f06ed53310a3 525
APS_Lab 0:f06ed53310a3 526 for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 527 if (datalog_en == 0) {
APS_Lab 0:f06ed53310a3 528 pc.printf(" IC%d", current_ic+1);
APS_Lab 0:f06ed53310a3 529 for (int i=0; i < 5; i++) {
APS_Lab 0:f06ed53310a3 530 pc.printf(" GPIO-%d:%.4f,", i+1, bms_ic[current_ic].aux.a_codes[i]*0.0001);
APS_Lab 0:f06ed53310a3 531 }
APS_Lab 0:f06ed53310a3 532 pc.printf("Vref2:%.4f\n", bms_ic[current_ic].aux.a_codes[5]*0.0001);
APS_Lab 0:f06ed53310a3 533 } else {
APS_Lab 0:f06ed53310a3 534 pc.printf("AUX, ");
APS_Lab 0:f06ed53310a3 535 for (int i=0; i < 6; i++) {
APS_Lab 0:f06ed53310a3 536 pc.printf("%.4f,", bms_ic[current_ic].aux.a_codes[i]*0.0001);
APS_Lab 0:f06ed53310a3 537 }
APS_Lab 0:f06ed53310a3 538 }
APS_Lab 0:f06ed53310a3 539 }
APS_Lab 0:f06ed53310a3 540 pc.printf("\n");
APS_Lab 0:f06ed53310a3 541 }
APS_Lab 0:f06ed53310a3 542
takuma1 2:3bbbe439ec11 543
APS_Lab 0:f06ed53310a3 544 void print_stat()
APS_Lab 0:f06ed53310a3 545 {
APS_Lab 0:f06ed53310a3 546
APS_Lab 0:f06ed53310a3 547 for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 548 pc.printf("IC%d", current_ic+1);
APS_Lab 0:f06ed53310a3 549 pc.printf(" SOC:%.4f,", bms_ic[current_ic].stat.stat_codes[0]*0.0001*20);
APS_Lab 0:f06ed53310a3 550 pc.printf(" Itemp:%.4f,", bms_ic[current_ic].stat.stat_codes[1]*0.0001);
APS_Lab 0:f06ed53310a3 551 pc.printf(" VregA:%.4f,", bms_ic[current_ic].stat.stat_codes[2]*0.0001);
APS_Lab 0:f06ed53310a3 552 pc.printf(" VregD:%.4f\n", bms_ic[current_ic].stat.stat_codes[3]*0.0001);
APS_Lab 0:f06ed53310a3 553 }
APS_Lab 0:f06ed53310a3 554
APS_Lab 0:f06ed53310a3 555 pc.printf("\n");
APS_Lab 0:f06ed53310a3 556 }
APS_Lab 0:f06ed53310a3 557
takuma1 2:3bbbe439ec11 558
APS_Lab 0:f06ed53310a3 559 void print_config()
APS_Lab 0:f06ed53310a3 560 {
APS_Lab 0:f06ed53310a3 561 int cfg_pec;
APS_Lab 0:f06ed53310a3 562
takuma1 2:3bbbe439ec11 563 //pc.printf("Written Configuration: \n");
APS_Lab 0:f06ed53310a3 564 for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++) {
takuma1 2:3bbbe439ec11 565 //pc.printf(" IC ");
takuma1 2:3bbbe439ec11 566 //pc.printf("%d", current_ic+1);
takuma1 2:3bbbe439ec11 567 // pc.printf(": ");
takuma1 2:3bbbe439ec11 568 //pc.printf("0x");
APS_Lab 0:f06ed53310a3 569 serial_print_hex(bms_ic[current_ic].config.tx_data[0]);
takuma1 2:3bbbe439ec11 570 // pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 571 serial_print_hex(bms_ic[current_ic].config.tx_data[1]);
takuma1 2:3bbbe439ec11 572 //pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 573 serial_print_hex(bms_ic[current_ic].config.tx_data[2]);
takuma1 2:3bbbe439ec11 574 //pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 575 serial_print_hex(bms_ic[current_ic].config.tx_data[3]);
takuma1 2:3bbbe439ec11 576 // pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 577 serial_print_hex(bms_ic[current_ic].config.tx_data[4]);
takuma1 2:3bbbe439ec11 578 // pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 579 serial_print_hex(bms_ic[current_ic].config.tx_data[5]);
takuma1 2:3bbbe439ec11 580 //pc.printf(", Calculated PEC: 0x");
APS_Lab 0:f06ed53310a3 581 cfg_pec = pec15_calc(6,&bms_ic[current_ic].config.tx_data[0]);
APS_Lab 0:f06ed53310a3 582 serial_print_hex((uint8_t)(cfg_pec>>8));
takuma1 2:3bbbe439ec11 583 //pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 584 serial_print_hex((uint8_t)(cfg_pec));
takuma1 2:3bbbe439ec11 585 // pc.printf("\n");
APS_Lab 0:f06ed53310a3 586 }
takuma1 2:3bbbe439ec11 587 // pc.printf("\n");
APS_Lab 0:f06ed53310a3 588 }
APS_Lab 0:f06ed53310a3 589
takuma1 2:3bbbe439ec11 590
APS_Lab 0:f06ed53310a3 591 void print_rxconfig()
APS_Lab 0:f06ed53310a3 592 {
takuma1 2:3bbbe439ec11 593 //pc.printf("Received Configuration ");
APS_Lab 0:f06ed53310a3 594 for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) {
takuma1 2:3bbbe439ec11 595 // pc.printf(" IC ");
takuma1 2:3bbbe439ec11 596 // pc.printf("%d", current_ic+1);
takuma1 2:3bbbe439ec11 597 //pc.printf(": 0x");
APS_Lab 0:f06ed53310a3 598 serial_print_hex(bms_ic[current_ic].config.rx_data[0]);
takuma1 2:3bbbe439ec11 599 //pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 600 serial_print_hex(bms_ic[current_ic].config.rx_data[1]);
takuma1 2:3bbbe439ec11 601 //pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 602 serial_print_hex(bms_ic[current_ic].config.rx_data[2]);
takuma1 2:3bbbe439ec11 603 //pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 604 serial_print_hex(bms_ic[current_ic].config.rx_data[3]);
takuma1 2:3bbbe439ec11 605 //pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 606 serial_print_hex(bms_ic[current_ic].config.rx_data[4]);
takuma1 2:3bbbe439ec11 607 // pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 608 serial_print_hex(bms_ic[current_ic].config.rx_data[5]);
takuma1 2:3bbbe439ec11 609 //pc.printf(", Received PEC: 0x");
APS_Lab 0:f06ed53310a3 610 serial_print_hex(bms_ic[current_ic].config.rx_data[6]);
takuma1 2:3bbbe439ec11 611 //pc.printf(", 0x");
APS_Lab 0:f06ed53310a3 612 serial_print_hex(bms_ic[current_ic].config.rx_data[7]);
takuma1 2:3bbbe439ec11 613 // pc.printf("\n");
APS_Lab 0:f06ed53310a3 614 }
APS_Lab 0:f06ed53310a3 615 pc.printf("\n");
APS_Lab 0:f06ed53310a3 616 }
APS_Lab 0:f06ed53310a3 617
APS_Lab 0:f06ed53310a3 618 void print_pec()
APS_Lab 0:f06ed53310a3 619 {
APS_Lab 0:f06ed53310a3 620 for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) {
APS_Lab 0:f06ed53310a3 621 pc.printf("\n%d", bms_ic[current_ic].crc_count.pec_count);
APS_Lab 0:f06ed53310a3 622 pc.printf(" : PEC Errors Detected on IC");
APS_Lab 0:f06ed53310a3 623 pc.printf("%d\n", current_ic+1);
APS_Lab 0:f06ed53310a3 624 }
APS_Lab 0:f06ed53310a3 625 }
APS_Lab 0:f06ed53310a3 626
APS_Lab 0:f06ed53310a3 627
APS_Lab 0:f06ed53310a3 628 void serial_print_hex(uint8_t data)
APS_Lab 0:f06ed53310a3 629 {
takuma1 2:3bbbe439ec11 630 /*
APS_Lab 0:f06ed53310a3 631 if (data < 16) {
takuma1 2:3bbbe439ec11 632 //pc.printf("0x0%X", data);
APS_Lab 0:f06ed53310a3 633 } else
takuma1 2:3bbbe439ec11 634 pc.printf("0x%X", data);
takuma1 2:3bbbe439ec11 635 */
APS_Lab 0:f06ed53310a3 636 }
APS_Lab 0:f06ed53310a3 637
APS_Lab 0:f06ed53310a3 638 //Function to check error flag and print PEC error message
APS_Lab 0:f06ed53310a3 639 void check_error(int error)
APS_Lab 0:f06ed53310a3 640 {
APS_Lab 0:f06ed53310a3 641 if (error == -1) {
APS_Lab 0:f06ed53310a3 642 pc.printf("A PEC error was detected in the received data");
APS_Lab 0:f06ed53310a3 643 }
APS_Lab 0:f06ed53310a3 644 }
APS_Lab 0:f06ed53310a3 645
APS_Lab 0:f06ed53310a3 646
takuma1 2:3bbbe439ec11 647
APS_Lab 0:f06ed53310a3 648 char hex_digits[16]= {
APS_Lab 0:f06ed53310a3 649 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
APS_Lab 0:f06ed53310a3 650 };
APS_Lab 0:f06ed53310a3 651
takuma1 2:3bbbe439ec11 652
APS_Lab 0:f06ed53310a3 653
APS_Lab 0:f06ed53310a3 654 char hex_to_byte_buffer[5]= {
APS_Lab 0:f06ed53310a3 655 '0', 'x', '0', '0', '\0'
APS_Lab 0:f06ed53310a3 656 }; // buffer for ASCII hex to byte conversion
APS_Lab 0:f06ed53310a3 657 char byte_to_hex_buffer[3]= {
APS_Lab 0:f06ed53310a3 658 '\0','\0','\0'
APS_Lab 0:f06ed53310a3 659 };
APS_Lab 0:f06ed53310a3 660
takuma1 2:3bbbe439ec11 661 // シリアル インターフェイスから ui_バッファへのデータの読み取り
APS_Lab 0:f06ed53310a3 662 uint8_t read_data()
APS_Lab 0:f06ed53310a3 663 {
takuma1 2:3bbbe439ec11 664 uint8_t index = 0; // 内の現在の場所を保持するインデックス
takuma1 2:3bbbe439ec11 665 int c; // 着信キーストロークの格納に使用される単一の文字
APS_Lab 0:f06ed53310a3 666 //pc.printf("check 1\n");
APS_Lab 0:f06ed53310a3 667 while (index < UI_BUFFER_SIZE-1) {
APS_Lab 0:f06ed53310a3 668 //pc.printf("check 2\n");
takuma1 2:3bbbe439ec11 669 c = pc.getc();
APS_Lab 0:f06ed53310a3 670 //return c;
APS_Lab 0:f06ed53310a3 671 //pc.printf("check 3\n");
APS_Lab 0:f06ed53310a3 672
takuma1 2:3bbbe439ec11 673 if (((char) c == '\r') || ((char) c == '\n')) break;
takuma1 2:3bbbe439ec11 674 if ( ((char) c == '\x7F') || ((char) c == '\x08') ) {
APS_Lab 0:f06ed53310a3 675 if (index > 0) index--;
APS_Lab 0:f06ed53310a3 676 } else if (c >= 0) {
takuma1 2:3bbbe439ec11 677 ui_buffer[index++]=(char) c;
APS_Lab 0:f06ed53310a3 678 }
APS_Lab 0:f06ed53310a3 679 //pc.printf("check 4\n");
APS_Lab 0:f06ed53310a3 680
APS_Lab 0:f06ed53310a3 681 }
takuma1 2:3bbbe439ec11 682 ui_buffer[index]='\0';
APS_Lab 0:f06ed53310a3 683
takuma1 2:3bbbe439ec11 684 if ((char) c == '\r') {
APS_Lab 0:f06ed53310a3 685 wait_ms(1);
APS_Lab 0:f06ed53310a3 686 //pc.printf("check 5\n");
APS_Lab 0:f06ed53310a3 687
APS_Lab 0:f06ed53310a3 688 if (pc.readable()==1) {
APS_Lab 0:f06ed53310a3 689 //pc.printf("check 6\n");
takuma1 2:3bbbe439ec11 690 pc.getc();
APS_Lab 0:f06ed53310a3 691 }
takuma1 2:3bbbe439ec11 692
APS_Lab 0:f06ed53310a3 693
APS_Lab 0:f06ed53310a3 694 }
takuma1 2:3bbbe439ec11 695
APS_Lab 0:f06ed53310a3 696
takuma1 2:3bbbe439ec11 697 return index;
APS_Lab 0:f06ed53310a3 698 }
APS_Lab 0:f06ed53310a3 699
takuma1 2:3bbbe439ec11 700
APS_Lab 0:f06ed53310a3 701 float read_float()
APS_Lab 0:f06ed53310a3 702 {
APS_Lab 0:f06ed53310a3 703 float data;
APS_Lab 0:f06ed53310a3 704 read_data();
APS_Lab 0:f06ed53310a3 705 data = atof(ui_buffer);
APS_Lab 0:f06ed53310a3 706 return(data);
APS_Lab 0:f06ed53310a3 707 }
APS_Lab 0:f06ed53310a3 708
takuma1 2:3bbbe439ec11 709
APS_Lab 0:f06ed53310a3 710 int32_t read_int()
APS_Lab 0:f06ed53310a3 711 {
APS_Lab 0:f06ed53310a3 712 int32_t data;
APS_Lab 0:f06ed53310a3 713 read_data();
APS_Lab 0:f06ed53310a3 714 if (ui_buffer[0] == 'm')
APS_Lab 0:f06ed53310a3 715 return('m');
APS_Lab 0:f06ed53310a3 716 if ((ui_buffer[0] == 'B') || (ui_buffer[0] == 'b')) {
APS_Lab 0:f06ed53310a3 717 data = strtol(ui_buffer+1, NULL, 2);
APS_Lab 0:f06ed53310a3 718 } else
APS_Lab 0:f06ed53310a3 719 data = strtol(ui_buffer, NULL, 0);
APS_Lab 0:f06ed53310a3 720 return(data);
APS_Lab 0:f06ed53310a3 721 }
APS_Lab 0:f06ed53310a3 722
APS_Lab 0:f06ed53310a3 723 char *read_string()
APS_Lab 0:f06ed53310a3 724 {
APS_Lab 0:f06ed53310a3 725 read_data();
APS_Lab 0:f06ed53310a3 726 return(ui_buffer);
APS_Lab 0:f06ed53310a3 727 }
APS_Lab 0:f06ed53310a3 728
takuma1 2:3bbbe439ec11 729
APS_Lab 0:f06ed53310a3 730 int8_t read_char()
APS_Lab 0:f06ed53310a3 731 {
APS_Lab 0:f06ed53310a3 732 read_data();
APS_Lab 0:f06ed53310a3 733 return(ui_buffer[0]);
APS_Lab 0:f06ed53310a3 734 }