f211

Dependencies:   mbed-dev1

Fork of OBD21121 by Surendar S

Revision:
10:fe1b976a3f9c
Parent:
9:a3bdc9a71466
Child:
11:853d12fe7ec0
--- a/main.cpp	Sat Apr 29 06:52:46 2017 +0000
+++ b/main.cpp	Mon May 08 10:44:36 2017 +0000
@@ -11,10 +11,12 @@
 #define pushDebugData 1
 
 const int canSpeeds[3]= {CANSPEED_125,CANSPEED_500,CANSPEED_250};
-Serial serialPorts[2] = {Serial(P0_11,P0_13),Serial(P0_15,P0_14)};
+//Serial serialPorts[2] = {Serial(P0_11,P0_13),Serial(P0_15,P0_14)};
+Serial *serialPorts[2] = {NULL,NULL};
 Serial *kLineUART=NULL;
-DigitalOut *klineWakeup=new DigitalOut(P0_28);
-CAN canBus(P0_22, P0_23);
+DigitalOut *klineWakeup=NULL;
+DigitalOut *canDisableKey=NULL;
+CAN *canBus=NULL;
 
 Ticker timeCounter;
 Timer OBDTimer;
@@ -23,15 +25,20 @@
 
 void clearOBDCommand();
 void flushBuffer();
+void processUserCommand(char *_incomingDataInInterFace);
 void sendCommandToVehicle(char *_cmd);
 void setKLineMsg(char *_cmd);
 void flushBuffer();
 void kLinekeepalive();
 char tolower(unsigned char ch);
 int findStringLength(char *_cmd);
+int checkCarStatus();
+void initPins();
 
+int _powerSaveFlag=0;
+int isKeepAliveCommand=0;
 int isPoweredupNow=1;
-int interFace=1;
+int interFace=0;
 int debug=1;
 int currentOBDProtocol=0;
 const int protCan=1;
@@ -48,7 +55,7 @@
 int incomingDataInOBDCounter=0;
 const int waitTimeForOBDComm=2000;
 const int sleepTime=25000;
-const int powerSaveTime=120;
+const int powerSaveTime=180;
 int OBDCmdReceived=0;
 int interfaceCmdReceived=0;
 int lastOBDCmdRcvdTime=0;
@@ -58,14 +65,18 @@
 int kLineCommandCount=0;
 char kByte=0;
 
+void flip()
+{
+//    led = !led;
+}
 void readInterface()
 {
     char c;
-    while(serialPorts[interFace].readable()) {
+    while(serialPorts[interFace]->readable()) {
         lastInterfaceCmdRcvdTime=0;
-        c = serialPorts[interFace].getc();
+        c = serialPorts[interFace]->getc();
 
-        serialPorts[debug].putc(c);
+        serialPorts[debug]->putc(c);
 
         if(incomingDataInInterFaceCounter<incomingDataInInterFaceSize && c!='\r' && c!='\n') {
             incomingDataInInterFace[incomingDataInInterFaceCounter] = c;
@@ -74,7 +85,7 @@
 
         if(c=='\r') {
             interfaceCmdReceived=1;
-            serialPorts[debug].puts("nl\r\n");
+            serialPorts[debug]->puts("nl\r\n");
         }
 
     }
@@ -82,15 +93,20 @@
 
 void canReader()
 {
-    if (canBus.read(can_MsgRx)) {
+//    serialPorts[debug]->puts("a");
+    if (canBus->read(can_MsgRx)) {
         lastOBDCmdRcvdTime=0;
         if ((can_MsgRx.id == PID_REPLY)) {
+            for (int i = 0; i < (int)can_MsgRx.len && currentOBDProtocol!=0; i++) {
+                char c;
+//                sprintf(c, "%02X",can_MsgRx.data[i]);
+//                serialPorts[interFace].puts(c);
+                if(incomingDataInOBDCounter< incomingDataInOBDSize) {
+                    incomingDataInOBD[incomingDataInOBDCounter]=can_MsgRx.data[i];
+                    incomingDataInOBDCounter++;
+                }
+            }
             OBDCmdReceived=1;
-            for (int i = 0; i < (int)can_MsgRx.len && currentOBDProtocol!=0; i++) {
-                char c[2];
-                sprintf(c, "%02X",can_MsgRx.data[i]);
-                serialPorts[interFace].puts(c);
-            }
         }
     }
 }
@@ -100,7 +116,11 @@
     char c;
     while(kLineUART->readable()) {
         c=kLineUART->getc();
-        serialPorts[interFace].printf("%02x",c);
+        lastOBDCmdRcvdTime=0;
+        if(incomingDataInOBDCounter< incomingDataInOBDSize) {
+            incomingDataInOBD[incomingDataInOBDCounter]=c;
+            incomingDataInOBDCounter++;
+        }
     }
 }
 
@@ -109,69 +129,76 @@
 {
     lastInterfaceCmdRcvdTime++;
     lastOBDCmdRcvdTime++;
-    KlineReadCount++;
+    /*KlineReadCount++;
     if(currentOBDProtocol==protkLine14230 && KlineReadCount>=3) {
         KlineReadCount=0;
         if(incomingDataInInterFaceCounter) {
             return;
         }
         kLinekeepalive();
-    }
+    }*/
 
 }
 
 int main()
 {
-    serialPorts[interFace].baud(9600);
-    serialPorts[debug].puts("f2119\r\n");
+    //Serial serialPorts[2] = {Serial(P0_11,P0_13),Serial(P0_15,P0_14)};
+
+    initPins();
+    serialPorts[interFace]->baud(9600);
+    serialPorts[debug]->puts("f2120\r\n");
     incomingDataInInterFace=(char *)malloc(incomingDataInInterFaceSize*sizeof(char));
     incomingDataInOBD=(char *)malloc(incomingDataInOBDSize*sizeof(char));
+
     *klineWakeup=0;
-
     clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
     clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
-    wait_ms(300);
+
+    wait_ms(1000);
     timeCounter.attach(&timeCounterFunction, 1.0);
-    serialPorts[interFace].attach(&readInterface);
-    serialPorts[interFace].puts("TU211 v1.0\r\n");
+    serialPorts[interFace]->attach(&readInterface);
+    serialPorts[interFace]->puts("+TU211 v1.0\r");
+    serialPorts[interFace]->puts("OK\r\n");
+    wait_ms(300);
     while(1) {
         if(interfaceCmdReceived && checkForLocalInterfaceCommand()) {
-            serialPorts[debug].puts("Going to clear UART data\r\n");
+            serialPorts[debug]->puts("Going to clear UART data\r\n");
             clearInterfaceCommand();
-            serialPorts[debug].puts("UART data cleared\r\n");
+            serialPorts[debug]->puts("UART data cleared\r\n");
         }
         if(currentOBDProtocol==0 && isPoweredupNow) {
             isPoweredupNow=0;
 #if sendDebugMessage
-            serialPorts[debug].puts("Going to figure protocol\r\n");
+            serialPorts[debug]->puts("Going to figure protocol\r\n");
 #endif
-            serialPorts[debug].puts("Searching...\r\n");
+            serialPorts[debug]->puts("Searching...\r\n");
             checkCommunicationProtocol();
             if(!currentOBDProtocol) {
 #if sendDebugMessage
-                serialPorts[debug].puts("!Unknownprotocol\r\n");
+                serialPorts[debug]->puts("!Unknownprotocol\r\n");
 #endif
             } else if(currentOBDProtocol==protCan) {
+                *canDisableKey=0;
 #if sendDebugMessage
-                serialPorts[debug].puts("can Protocol\r\n");
+                serialPorts[debug]->puts("can Protocol\r\n");
 #endif
-                serialPorts[interFace].puts("OK\r\n");
             } else if(currentOBDProtocol==protkLine14230) {
+                *canDisableKey=1;
 #if sendDebugMessage
-                serialPorts[debug].puts("Kline Protocol\r\n");
+                serialPorts[debug]->puts("Kline Protocol\r\n");
 #endif
             }
         }
         if(interfaceCmdReceived && checkForLocalInterfaceCommand()) {
-            serialPorts[debug].puts("Going to clear UART data\r\n");
+            serialPorts[debug]->puts("Going to clear UART data\r\n");
             clearInterfaceCommand();
-            serialPorts[debug].puts("UART data cleared\r\n");
+            serialPorts[debug]->puts("UART data cleared\r\n");
         }
         if(interfaceCmdReceived && currentOBDProtocol) {
-            serialPorts[debug].puts("Going to process user command\r\n");
+            serialPorts[debug]->puts("Going to process user command\r\n");
             clearOBDCommand();
             if(currentOBDProtocol==protCan) {
-                processUserCommand();
+                processUserCommand(incomingDataInInterFace);
             } else if(currentOBDProtocol==protkLine14230) {
                 while(isVehicleCommandProcessing);
                 flushBuffer();
@@ -179,22 +206,27 @@
                 kLineUART->attach(&readKLine);*/
                 sendCommandToVehicle(incomingDataInInterFace);
                 wait_ms(500);
-                serialPorts[debug].printf("\r\n");
+                serialPorts[debug]->printf("\r\n");
             } else {
                 clearInterfaceCommand();
-                serialPorts[interFace].puts("?\r\n");
+                serialPorts[interFace]->puts("?\r\n");
 #if sendDebugMessage
-                serialPorts[debug].puts("?\r\n");
+                serialPorts[debug]->puts("?\r\n");
 #endif
             }
         } else if(interfaceCmdReceived) {
             clearInterfaceCommand();
-            serialPorts[interFace].puts("?\r\n");
+            serialPorts[interFace]->puts("?\r\n");
 #if sendDebugMessage
-            serialPorts[debug].puts("?\r\n");
+            serialPorts[debug]->puts("?\r\n");
 #endif
         }
-        sleepProcess();
+//        sleepProcess();
+        if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) {
+            powerSaveMode();
+        } else {
+            sleepProcess();
+        }
         /*if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) {
             powerSaveMode();
         } else {
@@ -210,20 +242,17 @@
     int i=0;
     do {
 #if sendDebugMessage
-        serialPorts[debug].puts("CanBusCheck\r\n");
+        serialPorts[debug]->puts("CanBusCheck\r\n");
 #endif
-        canBus.reset();
-        canBus.frequency(canSpeeds[i]);
-        canBus.attach(NULL);
-        canBus.attach(&canReader);
+        canBus->reset();
+        canBus->frequency(canSpeeds[i]);
+        canBus->attach(NULL);
+        canBus->attach(&canReader);
         OBDTimer.reset();
         OBDTimer.start();
         OBDCmdReceived=0;
         setCanMessage("010D");
-        if (canBus.write(CANMessage(PID_REQUEST, can_msg, 8))) {
-            /*#if sendDebugMessage
-                       serialPorts[debug].printf("OBDTIMER:%d\r\n",OBDTimer.read_ms());
-            #endif*/
+        if (canBus->write(CANMessage(PID_REQUEST, can_msg, 8))) {
             while(OBDTimer.read_ms() < waitTimeForOBDComm) {
                 if(OBDCmdReceived) {
                     currentOBDProtocol=protCan;
@@ -236,13 +265,13 @@
         i++;
     } while(i<3 && canFrequency==0);
     OBDTimer.stop();
-    canBus.attach(NULL);
+    canBus->attach(NULL);
     if(!currentOBDProtocol) {
         //Kline interface check
         initializeKLine();
     }
 #if sendDebugMessage
-    serialPorts[debug].puts("check Communication protocal EXT\r\n");
+    serialPorts[debug]->puts("check Communication protocal EXT\r\n");
 #endif
 }
 void clearMemoryLocation( char *base, int _size)
@@ -253,9 +282,9 @@
 }
 void setCanMessage(char* _cmd)
 {
-    serialPorts[debug].puts("user Command :");
-    serialPorts[debug].puts(_cmd);
-    serialPorts[debug].puts("\r\n");
+    serialPorts[debug]->puts("user Command :");
+    serialPorts[debug]->puts(_cmd);
+    serialPorts[debug]->puts("\r\n");
     int len=findStringLength(_cmd)/2;
     char _mode[2]="";
     for(int i=0; i<2; i++) {
@@ -272,18 +301,32 @@
     can_msg[6] = 0;
     can_msg[7] = 0;
 }
-void processUserCommand()
+void processUserCommand(char *_incomingDataInInterFace)
 {
     OBDTimer.reset();
-    setCanMessage(incomingDataInInterFace);
+    setCanMessage(_incomingDataInInterFace);
     OBDTimer.start();
     OBDCmdReceived=0;
-    canBus.attach(NULL);
-    canBus.attach(&canReader);
-    if (canBus.write(CANMessage(PID_REQUEST, can_msg, 8))) {
+    clearOBDCommand();
+    canBus->attach(NULL);
+    canBus->attach(&canReader);
+    if (canBus->write(CANMessage(PID_REQUEST, can_msg, 8))) {
         while(OBDTimer.read_ms() < waitTimeForOBDComm) {
             if(OBDCmdReceived) {
                 clearInterfaceCommand();
+                if(_powerSaveFlag) {
+                    serialPorts[interFace]->printf("+TUAWAKE\r\n");
+                    _powerSaveFlag=0;
+                    break;
+                }
+                int len=incomingDataInOBD[0];
+                for(int i=1; i<=len; i++) {
+                    if(incomingDataInOBD[i]!=0x00) {
+                        serialPorts[interFace]->printf("%02X",incomingDataInOBD[i]);
+                    } else {
+                        serialPorts[interFace]->printf("%02X",00);
+                    }
+                }
                 break;
             }
         }
@@ -291,20 +334,33 @@
     OBDTimer.stop();
     if(!OBDCmdReceived) {
         clearInterfaceCommand();
-        serialPorts[interFace].puts("?");
+        if(!_powerSaveFlag) {
+            serialPorts[interFace]->puts("?");
+        }
     }
-    serialPorts[interFace].puts("\r\n");
+    clearOBDCommand();
+    if(!_powerSaveFlag) {
+        serialPorts[interFace]->puts("\r\n");
+    }
 }
 void sleepProcess()
 {
 
 #if pushDebugData
-    serialPorts[debug].puts("SPe\r\n");
+    serialPorts[debug]->puts("SPe\r\n");
 #endif
     OBDTimer.reset();
     OBDTimer.start();
     while(OBDTimer.read_ms()<sleepTime && !interfaceCmdReceived) {
         wait_ms(50);
+        KlineReadCount=KlineReadCount+50;
+        if(currentOBDProtocol==protkLine14230 && KlineReadCount>=3000) {
+            KlineReadCount=0;
+            if(incomingDataInInterFaceCounter) {
+                continue;
+            }
+            kLinekeepalive();
+        }
     }
     OBDTimer.stop();
 }
@@ -312,50 +368,112 @@
 void powerSaveMode()
 {
 #if pushDebugData
-    serialPorts[debug].puts("PowerSaveOn\r\n");
+    serialPorts[debug]->puts("Ignition test\r\n");
 #endif
-    wait(20);
+    clearInterfaceCommand();
+    clearOBDCommand();
+    strcpy(incomingDataInInterFace,"010D");
+    _powerSaveFlag=1;
+    if(currentOBDProtocol==protCan) {
+        processUserCommand(incomingDataInInterFace);
+    } else if(currentOBDProtocol==protkLine14230) {
+        while(isVehicleCommandProcessing);
+        flushBuffer();
+        sendCommandToVehicle(incomingDataInInterFace);
+        wait_ms(500);
+        serialPorts[debug]->printf("\r\n");
+    }
 #if pushDebugData
-    serialPorts[debug].puts("PowerSaveOff\r\n");
+    serialPorts[debug]->puts("Ignition test done\r\n");
 #endif
-}
-int checkForLocalInterfaceCommand()
-{
-    serialPorts[debug].puts("CommandDETECTED\r\n");
-    int i=0;
-    while(incomingDataInInterFace[i]!='\0'){
-        incomingDataInInterFace[i]=tolower(incomingDataInInterFace[i]);
-        i++;
+    if(!_powerSaveFlag) {
+#if pushDebugData
+        serialPorts[debug]->puts("Vehicle is in Power mode\r\n");
+#endif
+        return;
     }
-    char temp[2]= {incomingDataInInterFace[0],incomingDataInInterFace[1]};
-    if(strcmp(temp,"at")!=0) {
-        serialPorts[debug].puts("Not Local Command\r\n");
-        return 0;
+#if pushDebugData
+    serialPorts[debug]->puts("Power save On\r\n");
+#endif
+    if(currentOBDProtocol==protkLine14230) {
+        kLineUART->attach(NULL);
     }
-    if(strcmp(incomingDataInInterFace,"atz")==0) {
-        LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
-        LPC_SWM->PINASSIGN0 = 0xffffffffUL;
+    serialPorts[debug]->attach(NULL);
+    serialPorts[interFace]->attach(NULL);
+    timeCounter.detach();
+    *canDisableKey=1;
+
+//    LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
+//    LPC_SWM->PINASSIGN0 = 0xffffffffUL;
     LPC_SWM->PINASSIGN1 = 0xffffffffUL;
     LPC_SWM->PINASSIGN2 = 0xffffffffUL;
     LPC_SWM->PINASSIGN3 = 0xffffffffUL;
     LPC_SWM->PINASSIGN4 = 0xffffffffUL;
     LPC_SWM->PINASSIGN5 = 0xffffffffUL;
     LPC_SWM->PINASSIGN8 = 0xffffffffUL;
-        LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
+//    LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
+    clearInterfaceCommand();
+    serialPorts[interFace]->attach(&readInterface);
+    InterruptIn in2(P0_22);
+    InterruptIn in3(P0_27);
+    in2.rise(&flip);
+    in3.rise(&flip);
+    while(1) {
+//        serialPorts[debug]->printf("Waiting for interupt..\r\n");
+        __WFI();
+        int waitTime=3000;
+        while(waitTime>0) {
+            if(interfaceCmdReceived) {
+                break;
+            }
+            wait_ms(50);
+            waitTime=(waitTime-50);
+        }
+        if(interfaceCmdReceived) {
+//            serialPorts[debug]->printf("%s\r\n",incomingDataInInterFace);
+            if(checkForLocalInterfaceCommand()) {
+                clearInterfaceCommand();
+            } else {
+                serialPorts[interFace]->printf("?\r\n");
+            }
+            clearInterfaceCommand();
+        } else {
+            break;
+        }
+    }
+    NVIC_SystemReset();
+#if pushDebugData
+    serialPorts[debug]->puts("PowerSaveOff\r\n");
+#endif
+}
+int checkForLocalInterfaceCommand()
+{
+    serialPorts[debug]->puts("CommandDETECTED\r\n");
+    int i=0;
+    while(incomingDataInInterFace[i]!='\0') {
+        incomingDataInInterFace[i]=tolower(incomingDataInInterFace[i]);
+        i++;
+    }
+    char temp[2]= {incomingDataInInterFace[0],incomingDataInInterFace[1]};
+    if(strcmp(temp,"at")!=0) {
+        serialPorts[debug]->puts("Not Local Command\r\n");
+        return 0;
+    }
+    if(strcmp(incomingDataInInterFace,"atz")==0) {
         NVIC_SystemReset();
     }
     if(strcmp(incomingDataInInterFace,"atrv")==0) {
-        serialPorts[interFace].puts("12.1V\r\n");
+        serialPorts[interFace]->puts("12.1V\r\n");
         clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
         return 1;
     }
-    serialPorts[interFace].puts("?\r\n");
+    serialPorts[interFace]->puts("?\r\n");
     return 2;
 }
 void sendResponseToInterface()
 {
-    serialPorts[interFace].puts(incomingDataInOBD);
-    serialPorts[interFace].puts("\r\n");
+    serialPorts[interFace]->puts(incomingDataInOBD);
+    serialPorts[interFace]->puts("\r\n");
 }
 
 void clearInterfaceCommand()
@@ -380,7 +498,7 @@
 int initializeKLine()
 {
 #if sendDebugMessage
-    serialPorts[debug].puts("K-Line Check\r\n");
+    serialPorts[debug]->puts("K-Line Check\r\n");
 #endif
     *klineWakeup=1;
     wait_ms(300);
@@ -390,6 +508,7 @@
     wait_ms(25);
     delete klineWakeup;
     kLineUART=new Serial(P0_28,P0_27);
+    clearOBDCommand();
     kLineUART->baud(10400);
     kLineUART->attach(&readKLine);
     uint8_t crc=0;
@@ -402,9 +521,11 @@
     kLineUART->putc(0xF1);
     kLineUART->putc(0x81);
     kLineUART->putc(crc);
-    wait(1);
-    //have to check K-line Response
-    currentOBDProtocol=protkLine14230; // have to remove this line
+    wait(2);
+    if(incomingDataInOBD[5]!='\0') {
+        currentOBDProtocol=protkLine14230;
+    }
+    kLineUART->attach(NULL);
     return 0;
 }
 
@@ -448,6 +569,9 @@
 {
     isVehicleCommandProcessing=1;
     setKLineMsg(_cmd);
+    clearOBDCommand();
+    flushBuffer();
+    kLineUART->attach(&readKLine);
     for(kLineCommandCount=0; kLineCommandCount<6; kLineCommandCount++) {
         if(kLineCmd[kLineCommandCount]=='\0') {
             break;
@@ -455,9 +579,56 @@
         kLineUART->putc(kLineCmd[kLineCommandCount]);
     }
     wait_ms(500);
+    serialPorts[debug]->printf("Got Data\r\n");
     //have to check given and received commands are same.
-    //if it is same we have to read the response else just fush buffer and send the command again
+    //if it is same we have to read the response else just flush buffer and send the command again
+    for(int i=0; i<strlen(incomingDataInOBD); i++) {
+        serialPorts[debug]->printf("%02X",incomingDataInOBD[i]);
+    }
+    serialPorts[debug]->printf("\r\n");
+    kLineUART->attach(NULL);
+    strcpy(incomingDataInOBD,(incomingDataInOBD+kLineCommandCount)); //cut request from hole response
+    for(int i=0; i<strlen(incomingDataInOBD); i++) {
+        serialPorts[debug]->printf("%02X",incomingDataInOBD[i]);
+    }
+    serialPorts[debug]->printf("\r\n");
+    serialPorts[debug]->printf("%02X\r\n",incomingDataInOBD[0]);
+    int len=(incomingDataInOBD[0]%16);//convert first char to integer and find kength of the response
+    serialPorts[debug]->printf("Response Length : %d\r\n",len);
+    serialPorts[debug]->printf("Response Length : %d\r\n",strlen(incomingDataInOBD));
+    strcpy(incomingDataInOBD,(incomingDataInOBD+3)); //again cut first three byte
+    serialPorts[debug]->printf("Response Length : %d\r\n",strlen(incomingDataInOBD));
+    int obdDataLength=strlen(incomingDataInOBD);
+    int count=0;
+    while(count<len) {
+        if(_powerSaveFlag) {
+            serialPorts[interFace]->printf("+TUAWAKE\r\n");
+            _powerSaveFlag=0;
+            break;
+        }
+        if(obdDataLength>0) {
+            serialPorts[interFace]->printf("%02X",incomingDataInOBD[count]);
+            serialPorts[debug]->printf("%02X",incomingDataInOBD[count]);
+        } else {
+            serialPorts[interFace]->printf("%02X",0x00);
+            serialPorts[debug]->printf("%02X",0x00);
+        }
+        count++;
+        obdDataLength--;
+    }
+    if(len==0) {
+        if(!_powerSaveFlag) {
+            serialPorts[interFace]->printf("?");
+            serialPorts[debug]->printf("?");
+        }
+    }
+    if(!_powerSaveFlag) {
+        serialPorts[interFace]->printf("\r\n");
+        serialPorts[debug]->printf("\r\n");
+        serialPorts[debug]->printf("Done\r\n");
+    }
     clearInterfaceCommand();
+    clearOBDCommand();
     isVehicleCommandProcessing=0;
 }
 
@@ -469,14 +640,57 @@
 }
 void kLinekeepalive()
 {
+    serialPorts[debug]->printf("KeepAlive ENT\r\n");
     kLineUART->putc(0xc1);
     kLineUART->putc(0x33);
     kLineUART->putc(0xf1);
     kLineUART->putc(0x3e);
     kLineUART->putc(0x23);
+    serialPorts[debug]->printf("EXT\r\n");
 }
-char tolower(unsigned char ch) {
+char tolower(unsigned char ch)
+{
     if (ch >= 'A' && ch <= 'Z')
         ch = 'a' + (ch - 'A');
     return ch;
- }
\ No newline at end of file
+}
+
+int checkCarStatus()
+{
+//    LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
+    LPC_SWM->PINASSIGN0 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN1 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN2 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN3 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN4 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN5 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN8 = 0xffffffffUL;
+//    LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
+    DigitalOut kLine(P0_27);
+    DigitalOut canLine(P0_22);
+    if(kLine.read()) {
+        return 1;
+    }
+    if(canLine.read()) {
+        return 2;
+    }
+    return 0;
+}
+
+void initPins()
+{
+//    LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
+    LPC_SWM->PINASSIGN0 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN1 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN2 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN3 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN4 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN5 = 0xffffffffUL;
+    LPC_SWM->PINASSIGN8 = 0xffffffffUL;
+//    LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
+    serialPorts[0] = new Serial(P0_11,P0_13);
+    serialPorts[1] = new Serial(P0_15,P0_14);
+    klineWakeup = new DigitalOut(P0_28);
+    canBus = new CAN(P0_22, P0_23);
+    canDisableKey=new DigitalOut(P0_0,0);
+}
\ No newline at end of file