KIT Solar Car Project / Mbed 2 deprecated BMS_v1

Dependencies:   mbed INA226

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "bms.h"
00004 #include "LTC681x.h"
00005 #include "LTC6811.h"
00006 #include "INA226.hpp"
00007 
00008 
00009 
00010 #define UI_BUFFER_SIZE 64
00011 #define SERIAL_TERMINATOR '\n'
00012 
00013 #define ENABLED 1
00014 #define DISABLED 0
00015 
00016 #define DATALOG_ENABLED 1
00017 #define DATALOG_DISABLED 0
00018 
00019 
00020 
00021 DigitalOut led1(LED1);
00022 Serial pc(USBTX, USBRX);
00023 CAN canSlave(p30,p29);
00024 I2C i2c(p28,p27);
00025 DigitalOut led2(LED2);
00026 DigitalOut BAT_MIN_safty(p21);
00027 DigitalOut BAT_MAX_safty(p22);
00028 //Timer timer;  //書き込み時間を計測するタイマ
00029 
00030 // CAN関係
00031 short int forSend = 0;        //データ送信時に一時的に使用する変数
00032 bool CANsendOK = 0;  //CAN送信完了時,セットする(mainでのprintfのため)
00033 CANMessage msgSlave1;    //CAN送信用
00034 short int  canSlaveID = 0x20;   //canSlaveのIDをに設定
00035 void Handler_canSend();
00036 bool flag_can = 1;
00037 
00038 //測定関係の関数(変更の際触る必要がない)
00039 void run_command(uint32_t cmd); //測定コマンド送信関数
00040 void measurement_loop(uint8_t datalog_en);
00041 void print_cells(uint8_t datalog_en); //セル電圧の測定表示関数
00042 void print_open();
00043 void print_aux(uint8_t datalog_en);
00044 void print_stat();
00045 void print_config();
00046 void print_rxconfig();
00047 void print_pec(void);
00048 void serial_print_hex(uint8_t data);
00049 void check_error(int error);
00050 
00051 //測定関係関数(使ってる)
00052 void wakeup();//ICの起動関数、消費電力削減関数
00053 void cell_read();//セルの電圧の読み込みコマンド
00054 void spi_error();//SPIエラー処理関数
00055 void ic_check();//ICが測定可能状態か診断して次のコマンドに持ち込むための関数
00056 void spi_check();//SPIエラーの確認関数
00057 void print_CAN(uint8_t datalog_en);//セルの電圧の表示コマンドと過充電過放電検出
00058 void BAT_safty();
00059 void print_math();
00060 void can_sent1();
00061 void can_sent2();
00062 void can_sent3();
00063 void can_sent4();
00064 //void can_set();
00065 void can_wait();
00066 void ic_set();
00067 
00068 
00069 
00070 
00071 const uint8_t TOTAL_IC = 1; //IC数
00072 char ui_buffer[UI_BUFFER_SIZE];
00073 
00074 
00075 
00076 const uint8_t ADC_CONVERSION_MODE = MD_7KHZ_3KHZ;
00077 const uint8_t ADC_DCP = DCP_DISABLED; 
00078 const uint8_t CELL_CH_TO_CONVERT = CELL_CH_ALL; 
00079 const uint8_t AUX_CH_TO_CONVERT = AUX_CH_ALL; 
00080 const uint8_t STAT_CH_TO_CONVERT = STAT_CH_ALL; 
00081 
00082 const uint16_t MEASUREMENT_LOOP_TIME = 10;//milliseconds(mS)
00083 const uint16_t OV_THRESHOLD = 41000; //
00084 const uint16_t UV_THRESHOLD = 30000; // 
00085 
00086 
00087 const uint8_t WRITE_CONFIG = DISABLED; 
00088 const uint8_t READ_CONFIG = DISABLED;
00089 const uint8_t MEASURE_CELL = ENABLED; 
00090 const uint8_t MEASURE_AUX = DISABLED; 
00091 const uint8_t MEASURE_STAT = DISABLED; 
00092 const uint8_t PRINT_PEC = DISABLED; 
00093 short n =0;
00094 
00095 uint8_t read_data();
00096 
00097 float read_float();
00098 INA226 VCmonitor(i2c); // INA226
00099 
00100 int32_t read_int();
00101 float BAT_MIN ; 
00102 float BAT_MAX ;
00103 float BAT_AVG ;
00104 double BAT_C;
00105 float BAT_SUM;
00106 unsigned short BAT_CELL[23];
00107 
00108 char *read_string();
00109 /*
00110 int Replace1;
00111 int Replace2; 
00112 int Replace3;
00113 int Replace4;
00114 */
00115 
00116 int8_t read_char();
00117 
00118 cell_asic bms_ic[TOTAL_IC];
00119 
00120 
00121 void Handler_canSend() {
00122     
00123      int Replace1 = 0;
00124      int Replace2 = 0;
00125      int Replace3 = 0;
00126      short int Replace4 = 0;
00127 
00128     short n =0;
00129     int BAT_SUM = 0; // 0にする
00130     int BAT_MIN = 55876;
00131     double C;
00132     //unsigned short BTA_MAX = 0;
00133     
00134     for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) {
00135         for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) {
00136                                                                        
00137                      //pc.printf("C%d:", i+1);
00138                     printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001);
00139                      BAT_SUM = bms_ic[current_ic].cells.c_codes[i] +  BAT_SUM;
00140                      
00141                      if(bms_ic[current_ic].cells.c_codes[i] > 10000){ 
00142                      if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){
00143                            BAT_MIN = bms_ic[current_ic].cells.c_codes[i];
00144                                                                       }
00145                                                                       }
00146                                                                  
00147                      if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){
00148                            BAT_MAX = bms_ic[current_ic].cells.c_codes[i];
00149                                                                        }
00150                                                                        }
00151                                                                       
00152                     if(VCmonitor.getCurrent(&C) == 0){       // INA226
00153                         BAT_C = C;
00154                         printf("C,%f\n",C);
00155                                                     }
00156             
00157         }
00158 // BMA_SUM
00159     Replace1 = (int)(BAT_SUM / 100); 
00160     msgSlave1.data[0] = Replace1 / 100; 
00161     msgSlave1.data[1] = Replace1 % 100;    
00162 //BAT_MIN     
00163     Replace2 = (int)(BAT_MIN / 10); 
00164     msgSlave1.data[2] = Replace2 / 100;   
00165     msgSlave1.data[3] = Replace2 % 100;
00166 //BAT_MAX
00167     Replace3 = (int)(BAT_MAX /10);        
00168     msgSlave1.data[4] = Replace3 / 100;
00169     msgSlave1.data[5] = Replace3 % 100;
00170 //BAT_C
00171 
00172   //short BAT_C = -233; 
00173     if(BAT_C < 1000){
00174         BAT_C = BAT_C + 9002;
00175                     }
00176         
00177     if(BAT_C < 0){   //正電流か負電流を判定する
00178         BAT_C = BAT_C + 9001;  //負だと正の電流に変更する。さらにモニタ側でマイナスに戻すための定数を9001とする
00179                                //最大放電電流は50000mA 充電電流は-40000mAであるため9001とした。
00180                  }
00181                  
00182     Replace4 = (short int)(BAT_C / 1); 
00183     msgSlave1.data[6] = Replace4 / 100;   
00184     msgSlave1.data[7] = Replace4 % 100; 
00185     pc.printf("BAT_C %d\n",Replace4 );   
00186     printf("Data in msgSlave1.data[6] : %d\n\r", msgSlave1.data[6]); 
00187     
00188     
00189     if(canSlave.write(msgSlave1)){   //格納したデータを送信する
00190         led1 = !led1;
00191         CANsendOK = 1;
00192                                  }
00193     ic_check();
00194 }
00195 
00196 void Handler_canRecieve(){ //canMasterから送信要求が来たとき,データ送信するための関数
00197     if( flag_can ){ 
00198         if( canSlave.read( msgSlave1 ) ){ //msgに送られたデータが入る   
00199             led2 = !led2;
00200             if( msgSlave1.id == canSlaveID ){ //IDがcanSlaveIDであれば処理する   
00201                 Handler_canSend();
00202             }
00203         }
00204     }
00205 }
00206 
00207 int main(void)
00208 {
00209     uint32_t user_command;
00210     //short int BAT_C = 2322;
00211     pc.baud(115200);
00212     
00213     //printf("main()\n\r");
00214     canSlave.attach(&Handler_canRecieve, CAN::RxIrq);   //CAN受信割り込みの設定
00215     msgSlave1.id = canSlaveID; //CAN送信側(slave)のIDを決定
00216     msgSlave1.len = 8;   //CAN送信側で送るデータのバイト数
00217     
00218     while(1) { 
00219         BAT_MIN = 0;
00220         BAT_MAX = 0;   // 0にする
00221         /*
00222     if ( fp == NULL )
00223     {
00224         pc.printf("USB fileopen!\r\n");
00225         exit(1);
00226     }
00227         FILE *fp = fopen( "/usb/test.csv", "w");   //ファイルを開く   "W"は新規作成して書き込みっていう命令?
00228       */
00229           ic_check();
00230         //timer.start();    //書き込み時間測定開始
00231         //timer.stop();     //書き込み時間測定終了
00232         //fclose(fp);         //ファイルを閉じる
00233          if( CANsendOK ) {
00234             CANsendOK = 0;     
00235             printf("Data in msgSlave1.data[0] : %d\n\r", msgSlave1.data[0]);    //CANで送信したデータをそのまま表示
00236             printf("Data in msgSlave1.data[1] : %d\n\r", msgSlave1.data[1]);    //上に同じ
00237             printf("Data in msgSlave1.data[2] : %d\n\r", msgSlave1.data[2]);    //上に同じ
00238             printf("Data in msgSlave1.data[3] : %d\n\r", msgSlave1.data[3]);  
00239             printf("Data in msgSlave1.data[4] : %d\n\r", msgSlave1.data[4]);  
00240             printf("Data in msgSlave1.data[5] : %d\n\r", msgSlave1.data[5]);  
00241             printf("Data in msgSlave1.data[6] : %d\n\r", msgSlave1.data[6]);   
00242             printf("Data in msgSlave1.data[7] : %d\n\r", msgSlave1.data[7]);   //上に同じ
00243             printf("\n\r");
00244                         } 
00245         BAT_safty();
00246     }
00247 }
00248 
00249 /*
00250 void ic_set(){
00251         
00252   //      __disable_irq();
00253 //        flag_can = 0;
00254         ic_check();
00255         spi_check();
00256         spi_error();
00257         wakeup();
00258         cell_read();
00259            }
00260 */
00261 void ic_check(){  //1
00262 
00263 //__disable_irq();
00264    
00265     spi_enable();
00266     LTC681x_init_cfg(TOTAL_IC, bms_ic);
00267     LTC6811_reset_crc_count(TOTAL_IC,bms_ic);
00268     LTC6811_init_reg_limits(TOTAL_IC,bms_ic);
00269     wakeup_sleep(TOTAL_IC);
00270             LTC6811_wrcfg(TOTAL_IC,bms_ic);
00271             print_config();
00272             spi_check();           
00273 }
00274     
00275     
00276 void spi_check(){  //2
00277 
00278 //__disable_irq();
00279      int countup;
00280     int8_t error = 0;
00281     
00282      wakeup_sleep(TOTAL_IC);
00283             error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
00284             check_error(error);
00285             print_rxconfig();           
00286             spi_error();    
00287                }
00288 
00289 void wakeup(){  //3
00290 
00291  //__disable_irq();
00292     int countup;
00293     int8_t error = 0;
00294     uint32_t conv_time = 0;
00295                  
00296                  
00297             wakeup_sleep(TOTAL_IC);
00298             LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
00299             conv_time = LTC6811_pollAdc();
00300             cell_read();
00301             }
00302     
00303     
00304 void spi_error(){
00305              
00306           //  __disable_irq();
00307             
00308             int8_t error = 0;
00309             int countup;
00310             /*
00311             if(error = -1 ){
00312             for(countup = 0; countup <= 2; countup++){
00313              spi_check();
00314             if(error =1){             
00315                  break;
00316                         }
00317                                                         }                                       
00318                            }
00319              if(error = 1){
00320                    wakeup();
00321                           }
00322                           */
00323              wakeup();
00324                  }
00325                  
00326 void cell_read(){  //4  電圧読み取り
00327 
00328  //__disable_irq();
00329     int8_t error = 0;
00330     uint32_t conv_time = 0;
00331     int8_t readIC=0;
00332     
00333          wakeup_sleep(TOTAL_IC);
00334             error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); 
00335             check_error(error);
00336             //print_cells(DATALOG_DISABLED);
00337              print_CAN(DATALOG_DISABLED);
00338                 }           
00339 
00340 void print_CAN(uint8_t datalog_en){
00341     /*
00342 __disable_irq();
00343     short n =0;
00344     int BAT_SUM = 0; // 0にする
00345     int BAT_MIN = 55876;
00346     double C;
00347     //unsigned short BTA_MAX = 0;
00348         
00349     for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) {
00350         for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) {
00351                                                                        
00352                      //pc.printf("C%d:", i+1);
00353                     //pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001);
00354                      BAT_SUM = bms_ic[current_ic].cells.c_codes[i] +  BAT_SUM;           
00355                      if(bms_ic[current_ic].cells.c_codes[i] > 10000){ 
00356                      if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){
00357                            BAT_MIN = bms_ic[current_ic].cells.c_codes[i];
00358                                                                       }
00359                                                                       }
00360                                                                  
00361                      if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){
00362                            BAT_MAX = bms_ic[current_ic].cells.c_codes[i];
00363                                                                        }
00364                                                                       
00365                     if(VCmonitor.getCurrent(&C) == 0){       // INA226
00366                         BAT_C = C;
00367                         //printf("C,%f\n",C);
00368                                                     }
00369             }
00370         } 
00371         /*
00372     for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) {
00373         
00374         //if (datalog_en == 0) {
00375             //pc.printf("IC%d, ", current_ic+1);
00376             //for(n = 0; n <= 12; n++){
00377         for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) {
00378                     
00379                     //BAT_CELL= bms_ic[current_ic].cells.c_codes[i];
00380                                                                         
00381                     //fprintf(fp,"C%d:", i+1);
00382                     //fprintf(fp,"%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001);
00383                      BAT_SUM = bms_ic[current_ic].cells.c_codes[i] +  BAT_SUM;
00384                      
00385                      if(bms_ic[current_ic].cells.c_codes[i] > 10000){
00386                      if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){
00387                            BAT_MIN = bms_ic[current_ic].cells.c_codes[i];
00388                                                                       }
00389                                                                       }
00390                     //unsigned short BTA_MAX = 0;                                                 
00391                      if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){
00392                            BAT_MAX = bms_ic[current_ic].cells.c_codes[i];
00393                                                                        }                                                             
00394                     if(VCmonitor.getCurrent(&C) == 0){       // INA226
00395                         BAT_C = C;
00396                                                      }
00397                                                      
00398     }
00399         }             
00400                          
00401                     fprintf(fp,"SUM");
00402                     fprintf(fp,"%0.4f, ",BAT_SUM*0.0001);
00403                     BAT_AVG = BAT_SUM / 20;
00404                     fprintf(fp,"AVG");
00405                     fprintf(fp,"%.4f, ",BAT_AVG*0.0001);
00406                     fprintf(fp,"MIN");
00407                     fprintf(fp,"%.4f, ",BAT_MIN*0.0001);
00408                     fprintf(fp,"MAX");
00409                     fprintf(fp,"%.4f, ",BAT_MAX*0.0001);
00410                     fprintf(fp,"BAT_Current,%f\n",BAT_Current);           
00411     */
00412     }
00413        
00414 void BAT_safty(){
00415       if( BAT_MIN < 25000 ){
00416                             BAT_MIN_safty = 1 ;
00417                             wait(1);
00418                             BAT_MIN_safty = 0 ;         
00419                            } 
00420      if( BAT_MAX > 42000){
00421                             BAT_MAX_safty = 1;
00422                             wait(1);
00423                             BAT_MAX_safty = 0;
00424                           }
00425      if(BAT_C > 60000){
00426                             BAT_MIN_safty = 1 ;
00427                             wait(1);
00428                             BAT_MIN_safty = 0 ;
00429                             }
00430                 }
00431                 
00432                 
00433 void measurement_loop(uint8_t datalog_en)
00434 {
00435     int8_t error = 0;
00436     if (WRITE_CONFIG == ENABLED) {
00437         wakeup_sleep(TOTAL_IC);
00438         LTC6811_wrcfg(TOTAL_IC,bms_ic);
00439         print_config();
00440     }
00441 
00442     if (READ_CONFIG == ENABLED) {
00443         wakeup_sleep(TOTAL_IC);
00444         error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
00445         check_error(error);
00446         print_rxconfig();
00447     }
00448 
00449     if (MEASURE_CELL == ENABLED) {
00450         wakeup_idle(TOTAL_IC);
00451         LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
00452         LTC6811_pollAdc();
00453         wakeup_idle(TOTAL_IC);
00454         error = LTC6811_rdcv(0, TOTAL_IC,bms_ic);
00455         check_error(error);
00456         print_cells(datalog_en);
00457 
00458     }
00459 
00460     if (MEASURE_AUX == ENABLED) {
00461         wakeup_idle(TOTAL_IC);
00462         LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_ALL);
00463         LTC6811_pollAdc();
00464         wakeup_idle(TOTAL_IC);
00465         error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
00466         check_error(error);
00467         print_aux(datalog_en);
00468     }
00469 
00470     if (MEASURE_STAT == ENABLED) {
00471         wakeup_idle(TOTAL_IC);
00472         LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_ALL);
00473         LTC6811_pollAdc();
00474         wakeup_idle(TOTAL_IC);
00475         error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
00476         check_error(error);
00477         print_stat();
00478     }
00479 
00480     if (PRINT_PEC == ENABLED) {
00481         print_pec();
00482     }
00483 
00484 }
00485 
00486 
00487 
00488 void print_cells(uint8_t datalog_en)
00489 {
00490     for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) {
00491 
00492         if (datalog_en == 0) {
00493             pc.printf("IC%d, ", current_ic+1);
00494             for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) {
00495                 pc.printf("C%d:", i+1);
00496                 pc.printf("%.4f, ", bms_ic[current_ic].cells.c_codes[i]*0.0001);
00497                 } 
00498                    
00499             pc.printf("\n");
00500             }
00501             
00502         else {
00503             pc.printf("Cells, ");
00504             for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) {
00505                 pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001);  
00506                 
00507             }
00508         }
00509         
00510     }
00511     pc.printf("\n");
00512 }
00513 
00514 void print_open()
00515 {
00516     for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
00517         if (bms_ic[current_ic].system_open_wire == 0) {
00518             pc.printf("No Opens Detected on IC%d\n", current_ic+1);
00519         } else {
00520             for (int cell=0; cell<bms_ic[0].ic_reg.cell_channels+1; cell++) {
00521                 if ((bms_ic[current_ic].system_open_wire &(1<<cell))>0) {
00522                     pc.printf("There is an open wire on IC%d Channel: %d\n", current_ic + 1, cell);
00523                 }
00524             }
00525         }
00526     }
00527 }
00528 
00529 
00530 void print_aux(uint8_t datalog_en)
00531 {
00532 
00533     for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
00534         if (datalog_en == 0) {
00535             pc.printf(" IC%d", current_ic+1);
00536             for (int i=0; i < 5; i++) {
00537                 pc.printf(" GPIO-%d:%.4f,", i+1, bms_ic[current_ic].aux.a_codes[i]*0.0001);
00538             }
00539             pc.printf("Vref2:%.4f\n", bms_ic[current_ic].aux.a_codes[5]*0.0001);
00540         } else {
00541             pc.printf("AUX, ");
00542             for (int i=0; i < 6; i++) {
00543                 pc.printf("%.4f,", bms_ic[current_ic].aux.a_codes[i]*0.0001);
00544             }
00545         }
00546     }
00547     pc.printf("\n");
00548 }
00549 
00550 
00551 void print_stat()
00552 {
00553 
00554     for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) {
00555         pc.printf("IC%d", current_ic+1);
00556         pc.printf(" SOC:%.4f,", bms_ic[current_ic].stat.stat_codes[0]*0.0001*20);
00557         pc.printf(" Itemp:%.4f,", bms_ic[current_ic].stat.stat_codes[1]*0.0001);
00558         pc.printf(" VregA:%.4f,", bms_ic[current_ic].stat.stat_codes[2]*0.0001);
00559         pc.printf(" VregD:%.4f\n", bms_ic[current_ic].stat.stat_codes[3]*0.0001);
00560     }
00561 
00562     pc.printf("\n");
00563 }
00564 
00565 
00566 void print_config()
00567 {
00568     int cfg_pec;
00569 
00570     //pc.printf("Written Configuration: \n");
00571     for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++) {
00572         //pc.printf(" IC ");
00573         //pc.printf("%d", current_ic+1);
00574        // pc.printf(": ");
00575         //pc.printf("0x");
00576         serial_print_hex(bms_ic[current_ic].config.tx_data[0]);
00577        // pc.printf(", 0x");
00578         serial_print_hex(bms_ic[current_ic].config.tx_data[1]);
00579         //pc.printf(", 0x");
00580         serial_print_hex(bms_ic[current_ic].config.tx_data[2]);
00581         //pc.printf(", 0x");
00582         serial_print_hex(bms_ic[current_ic].config.tx_data[3]);
00583        // pc.printf(", 0x");
00584         serial_print_hex(bms_ic[current_ic].config.tx_data[4]);
00585        // pc.printf(", 0x");
00586         serial_print_hex(bms_ic[current_ic].config.tx_data[5]);
00587         //pc.printf(", Calculated PEC: 0x");
00588         cfg_pec = pec15_calc(6,&bms_ic[current_ic].config.tx_data[0]);
00589         serial_print_hex((uint8_t)(cfg_pec>>8));
00590         //pc.printf(", 0x");
00591         serial_print_hex((uint8_t)(cfg_pec));
00592        // pc.printf("\n");
00593     }
00594    // pc.printf("\n");
00595 }
00596 
00597 
00598 void print_rxconfig()
00599 {
00600     //pc.printf("Received Configuration ");
00601     for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) {
00602        // pc.printf(" IC ");
00603       //  pc.printf("%d", current_ic+1);
00604         //pc.printf(": 0x");
00605         serial_print_hex(bms_ic[current_ic].config.rx_data[0]);
00606         //pc.printf(", 0x");
00607         serial_print_hex(bms_ic[current_ic].config.rx_data[1]);
00608         //pc.printf(", 0x");
00609         serial_print_hex(bms_ic[current_ic].config.rx_data[2]);
00610         //pc.printf(", 0x");
00611         serial_print_hex(bms_ic[current_ic].config.rx_data[3]);
00612         //pc.printf(", 0x");
00613         serial_print_hex(bms_ic[current_ic].config.rx_data[4]);
00614        // pc.printf(", 0x");
00615         serial_print_hex(bms_ic[current_ic].config.rx_data[5]);
00616         //pc.printf(", Received PEC: 0x");
00617         serial_print_hex(bms_ic[current_ic].config.rx_data[6]);
00618         //pc.printf(", 0x");
00619         serial_print_hex(bms_ic[current_ic].config.rx_data[7]);
00620        // pc.printf("\n");
00621     }
00622     pc.printf("\n");
00623 }
00624 
00625 void print_pec()
00626 {
00627     for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) {
00628         pc.printf("\n%d", bms_ic[current_ic].crc_count.pec_count);
00629         pc.printf(" : PEC Errors Detected on IC");
00630         pc.printf("%d\n", current_ic+1);
00631     }
00632 }
00633 
00634 
00635 void serial_print_hex(uint8_t data)
00636 {
00637     /*
00638     if (data < 16) {
00639         //pc.printf("0x0%X", data);
00640     } else
00641        pc.printf("0x%X", data);
00642        */
00643 }
00644 
00645 //Function to check error flag and print PEC error message
00646 void check_error(int error)
00647 {
00648     if (error == -1) {
00649         pc.printf("A PEC error was detected in the received data");
00650     }
00651 }
00652 
00653 
00654 
00655 char hex_digits[16]= {
00656     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
00657 };
00658 
00659 
00660 
00661 char hex_to_byte_buffer[5]= {
00662     '0', 'x', '0', '0', '\0'
00663 };               // buffer for ASCII hex to byte conversion
00664 char byte_to_hex_buffer[3]= {
00665     '\0','\0','\0'
00666 };
00667 
00668 // シリアル インターフェイスから ui_バッファへのデータの読み取り
00669 uint8_t read_data()
00670 {
00671     uint8_t index = 0; // 内の現在の場所を保持するインデックス
00672     int c; // 着信キーストロークの格納に使用される単一の文字
00673     //pc.printf("check 1\n");
00674     while (index < UI_BUFFER_SIZE-1) {
00675         //pc.printf("check 2\n");
00676         c = pc.getc(); 
00677         //return c;
00678         //pc.printf("check 3\n");
00679         
00680         if (((char) c == '\r') || ((char) c == '\n')) break; 
00681         if ( ((char) c == '\x7F') || ((char) c == '\x08') ) { 
00682             if (index > 0) index--;
00683         } else if (c >= 0) {
00684             ui_buffer[index++]=(char) c; 
00685         }
00686         //pc.printf("check 4\n");
00687         
00688     }
00689     ui_buffer[index]='\0';  
00690 
00691     if ((char) c == '\r') {  
00692         wait_ms(1);
00693         //pc.printf("check 5\n");
00694         
00695         if (pc.readable()==1) {
00696             //pc.printf("check 6\n");
00697             pc.getc(); 
00698         }
00699         
00700         
00701     }
00702     
00703         
00704     return index; 
00705 }
00706 
00707 
00708 float read_float()
00709 {
00710     float data;
00711     read_data();
00712     data = atof(ui_buffer);
00713     return(data);
00714 }
00715 
00716 
00717 int32_t read_int()
00718 {
00719     int32_t data;
00720     read_data();
00721     if (ui_buffer[0] == 'm')
00722         return('m');
00723     if ((ui_buffer[0] == 'B') || (ui_buffer[0] == 'b')) {
00724         data = strtol(ui_buffer+1, NULL, 2);
00725     } else
00726         data = strtol(ui_buffer, NULL, 0);
00727     return(data);
00728 }
00729 
00730 char *read_string()
00731 {
00732     read_data();
00733     return(ui_buffer);
00734 }
00735 
00736 
00737 int8_t read_char()
00738 {
00739     read_data();
00740     return(ui_buffer[0]);
00741 }