f211

Dependencies:   mbed-dev1

Fork of OBD21121 by Surendar S

Committer:
takeuz
Date:
Mon May 08 10:44:36 2017 +0000
Revision:
10:fe1b976a3f9c
Parent:
9:a3bdc9a71466
Child:
11:853d12fe7ec0
obd211 updated version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takeuz 0:ccd2474129f2 1 #include "mbed.h"
takeuz 2:d58ef72b4ab0 2 #include "functions.h"
takeuz 2:d58ef72b4ab0 3
takeuz 2:d58ef72b4ab0 4 #define PID_REQUEST 0x7DF
takeuz 2:d58ef72b4ab0 5 #define PID_REPLY 0x7E8
takeuz 2:d58ef72b4ab0 6
takeuz 8:cc3cb620c3bd 7 #define CANSPEED_500 500000
takeuz 8:cc3cb620c3bd 8 #define CANSPEED_250 250000
takeuz 8:cc3cb620c3bd 9 #define CANSPEED_125 125000
takeuz 2:d58ef72b4ab0 10 #define sendDebugMessage 1
takeuz 2:d58ef72b4ab0 11 #define pushDebugData 1
takeuz 2:d58ef72b4ab0 12
takeuz 8:cc3cb620c3bd 13 const int canSpeeds[3]= {CANSPEED_125,CANSPEED_500,CANSPEED_250};
takeuz 10:fe1b976a3f9c 14 //Serial serialPorts[2] = {Serial(P0_11,P0_13),Serial(P0_15,P0_14)};
takeuz 10:fe1b976a3f9c 15 Serial *serialPorts[2] = {NULL,NULL};
takeuz 8:cc3cb620c3bd 16 Serial *kLineUART=NULL;
takeuz 10:fe1b976a3f9c 17 DigitalOut *klineWakeup=NULL;
takeuz 10:fe1b976a3f9c 18 DigitalOut *canDisableKey=NULL;
takeuz 10:fe1b976a3f9c 19 CAN *canBus=NULL;
takeuz 2:d58ef72b4ab0 20
takeuz 8:cc3cb620c3bd 21 Ticker timeCounter;
takeuz 8:cc3cb620c3bd 22 Timer OBDTimer;
takeuz 8:cc3cb620c3bd 23 CANMessage can_MsgRx;
takeuz 8:cc3cb620c3bd 24 char kLineCmd[6]= {'\0','\0','\0','\0','\0','\0'};
takeuz 7:42cb4b01909c 25
takeuz 8:cc3cb620c3bd 26 void clearOBDCommand();
takeuz 8:cc3cb620c3bd 27 void flushBuffer();
takeuz 10:fe1b976a3f9c 28 void processUserCommand(char *_incomingDataInInterFace);
takeuz 7:42cb4b01909c 29 void sendCommandToVehicle(char *_cmd);
takeuz 8:cc3cb620c3bd 30 void setKLineMsg(char *_cmd);
takeuz 8:cc3cb620c3bd 31 void flushBuffer();
takeuz 8:cc3cb620c3bd 32 void kLinekeepalive();
takeuz 8:cc3cb620c3bd 33 char tolower(unsigned char ch);
takeuz 7:42cb4b01909c 34 int findStringLength(char *_cmd);
takeuz 10:fe1b976a3f9c 35 int checkCarStatus();
takeuz 10:fe1b976a3f9c 36 void initPins();
takeuz 2:d58ef72b4ab0 37
takeuz 10:fe1b976a3f9c 38 int _powerSaveFlag=0;
takeuz 10:fe1b976a3f9c 39 int isKeepAliveCommand=0;
takeuz 8:cc3cb620c3bd 40 int isPoweredupNow=1;
takeuz 10:fe1b976a3f9c 41 int interFace=0;
takeuz 7:42cb4b01909c 42 int debug=1;
takeuz 2:d58ef72b4ab0 43 int currentOBDProtocol=0;
takeuz 2:d58ef72b4ab0 44 const int protCan=1;
takeuz 7:42cb4b01909c 45 const int protkLine9141=2;
takeuz 7:42cb4b01909c 46 const int protkLine14230=3;
takeuz 2:d58ef72b4ab0 47 int canFrequency;
takeuz 7:42cb4b01909c 48 int cmd;
takeuz 2:d58ef72b4ab0 49 char can_msg[8];
takeuz 2:d58ef72b4ab0 50 char *incomingDataInInterFace;
takeuz 2:d58ef72b4ab0 51 const int incomingDataInInterFaceSize=256;
takeuz 2:d58ef72b4ab0 52 int incomingDataInInterFaceCounter=0;
takeuz 8:cc3cb620c3bd 53 char * incomingDataInOBD;
takeuz 2:d58ef72b4ab0 54 const int incomingDataInOBDSize=256;
takeuz 2:d58ef72b4ab0 55 int incomingDataInOBDCounter=0;
takeuz 2:d58ef72b4ab0 56 const int waitTimeForOBDComm=2000;
takeuz 2:d58ef72b4ab0 57 const int sleepTime=25000;
takeuz 10:fe1b976a3f9c 58 const int powerSaveTime=180;
takeuz 2:d58ef72b4ab0 59 int OBDCmdReceived=0;
takeuz 2:d58ef72b4ab0 60 int interfaceCmdReceived=0;
takeuz 2:d58ef72b4ab0 61 int lastOBDCmdRcvdTime=0;
takeuz 2:d58ef72b4ab0 62 int lastInterfaceCmdRcvdTime=0;
takeuz 8:cc3cb620c3bd 63 int KlineReadCount=0;
takeuz 8:cc3cb620c3bd 64 int isVehicleCommandProcessing=0;
takeuz 8:cc3cb620c3bd 65 int kLineCommandCount=0;
takeuz 8:cc3cb620c3bd 66 char kByte=0;
takeuz 4:3e1e2b161403 67
takeuz 10:fe1b976a3f9c 68 void flip()
takeuz 10:fe1b976a3f9c 69 {
takeuz 10:fe1b976a3f9c 70 // led = !led;
takeuz 10:fe1b976a3f9c 71 }
takeuz 2:d58ef72b4ab0 72 void readInterface()
takeuz 2:d58ef72b4ab0 73 {
takeuz 2:d58ef72b4ab0 74 char c;
takeuz 10:fe1b976a3f9c 75 while(serialPorts[interFace]->readable()) {
takeuz 2:d58ef72b4ab0 76 lastInterfaceCmdRcvdTime=0;
takeuz 10:fe1b976a3f9c 77 c = serialPorts[interFace]->getc();
takeuz 7:42cb4b01909c 78
takeuz 10:fe1b976a3f9c 79 serialPorts[debug]->putc(c);
takeuz 8:cc3cb620c3bd 80
takeuz 6:741524d110fe 81 if(incomingDataInInterFaceCounter<incomingDataInInterFaceSize && c!='\r' && c!='\n') {
takeuz 2:d58ef72b4ab0 82 incomingDataInInterFace[incomingDataInInterFaceCounter] = c;
takeuz 2:d58ef72b4ab0 83 }
takeuz 2:d58ef72b4ab0 84 incomingDataInInterFaceCounter++;
takeuz 4:3e1e2b161403 85
takeuz 7:42cb4b01909c 86 if(c=='\r') {
takeuz 2:d58ef72b4ab0 87 interfaceCmdReceived=1;
takeuz 10:fe1b976a3f9c 88 serialPorts[debug]->puts("nl\r\n");
takeuz 2:d58ef72b4ab0 89 }
takeuz 2:d58ef72b4ab0 90
takeuz 2:d58ef72b4ab0 91 }
takeuz 2:d58ef72b4ab0 92 }
takeuz 2:d58ef72b4ab0 93
takeuz 4:3e1e2b161403 94 void canReader()
takeuz 4:3e1e2b161403 95 {
takeuz 10:fe1b976a3f9c 96 // serialPorts[debug]->puts("a");
takeuz 10:fe1b976a3f9c 97 if (canBus->read(can_MsgRx)) {
takeuz 2:d58ef72b4ab0 98 lastOBDCmdRcvdTime=0;
takeuz 8:cc3cb620c3bd 99 if ((can_MsgRx.id == PID_REPLY)) {
takeuz 10:fe1b976a3f9c 100 for (int i = 0; i < (int)can_MsgRx.len && currentOBDProtocol!=0; i++) {
takeuz 10:fe1b976a3f9c 101 char c;
takeuz 10:fe1b976a3f9c 102 // sprintf(c, "%02X",can_MsgRx.data[i]);
takeuz 10:fe1b976a3f9c 103 // serialPorts[interFace].puts(c);
takeuz 10:fe1b976a3f9c 104 if(incomingDataInOBDCounter< incomingDataInOBDSize) {
takeuz 10:fe1b976a3f9c 105 incomingDataInOBD[incomingDataInOBDCounter]=can_MsgRx.data[i];
takeuz 10:fe1b976a3f9c 106 incomingDataInOBDCounter++;
takeuz 10:fe1b976a3f9c 107 }
takeuz 10:fe1b976a3f9c 108 }
takeuz 7:42cb4b01909c 109 OBDCmdReceived=1;
takeuz 2:d58ef72b4ab0 110 }
takeuz 2:d58ef72b4ab0 111 }
takeuz 2:d58ef72b4ab0 112 }
takeuz 2:d58ef72b4ab0 113
takeuz 8:cc3cb620c3bd 114 void readKLine()
takeuz 8:cc3cb620c3bd 115 {
takeuz 8:cc3cb620c3bd 116 char c;
takeuz 8:cc3cb620c3bd 117 while(kLineUART->readable()) {
takeuz 8:cc3cb620c3bd 118 c=kLineUART->getc();
takeuz 10:fe1b976a3f9c 119 lastOBDCmdRcvdTime=0;
takeuz 10:fe1b976a3f9c 120 if(incomingDataInOBDCounter< incomingDataInOBDSize) {
takeuz 10:fe1b976a3f9c 121 incomingDataInOBD[incomingDataInOBDCounter]=c;
takeuz 10:fe1b976a3f9c 122 incomingDataInOBDCounter++;
takeuz 10:fe1b976a3f9c 123 }
takeuz 8:cc3cb620c3bd 124 }
takeuz 8:cc3cb620c3bd 125 }
takeuz 8:cc3cb620c3bd 126
takeuz 8:cc3cb620c3bd 127
takeuz 4:3e1e2b161403 128 void timeCounterFunction()
takeuz 4:3e1e2b161403 129 {
takeuz 2:d58ef72b4ab0 130 lastInterfaceCmdRcvdTime++;
takeuz 2:d58ef72b4ab0 131 lastOBDCmdRcvdTime++;
takeuz 10:fe1b976a3f9c 132 /*KlineReadCount++;
takeuz 8:cc3cb620c3bd 133 if(currentOBDProtocol==protkLine14230 && KlineReadCount>=3) {
takeuz 7:42cb4b01909c 134 KlineReadCount=0;
takeuz 8:cc3cb620c3bd 135 if(incomingDataInInterFaceCounter) {
takeuz 7:42cb4b01909c 136 return;
takeuz 7:42cb4b01909c 137 }
takeuz 8:cc3cb620c3bd 138 kLinekeepalive();
takeuz 10:fe1b976a3f9c 139 }*/
takeuz 8:cc3cb620c3bd 140
takeuz 2:d58ef72b4ab0 141 }
takeuz 0:ccd2474129f2 142
takeuz 4:3e1e2b161403 143 int main()
takeuz 4:3e1e2b161403 144 {
takeuz 10:fe1b976a3f9c 145 //Serial serialPorts[2] = {Serial(P0_11,P0_13),Serial(P0_15,P0_14)};
takeuz 10:fe1b976a3f9c 146
takeuz 10:fe1b976a3f9c 147 initPins();
takeuz 10:fe1b976a3f9c 148 serialPorts[interFace]->baud(9600);
takeuz 10:fe1b976a3f9c 149 serialPorts[debug]->puts("f2120\r\n");
takeuz 2:d58ef72b4ab0 150 incomingDataInInterFace=(char *)malloc(incomingDataInInterFaceSize*sizeof(char));
takeuz 2:d58ef72b4ab0 151 incomingDataInOBD=(char *)malloc(incomingDataInOBDSize*sizeof(char));
takeuz 10:fe1b976a3f9c 152
takeuz 8:cc3cb620c3bd 153 *klineWakeup=0;
takeuz 2:d58ef72b4ab0 154 clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
takeuz 4:3e1e2b161403 155 clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
takeuz 10:fe1b976a3f9c 156
takeuz 10:fe1b976a3f9c 157 wait_ms(1000);
takeuz 2:d58ef72b4ab0 158 timeCounter.attach(&timeCounterFunction, 1.0);
takeuz 10:fe1b976a3f9c 159 serialPorts[interFace]->attach(&readInterface);
takeuz 10:fe1b976a3f9c 160 serialPorts[interFace]->puts("+TU211 v1.0\r");
takeuz 10:fe1b976a3f9c 161 serialPorts[interFace]->puts("OK\r\n");
takeuz 10:fe1b976a3f9c 162 wait_ms(300);
takeuz 0:ccd2474129f2 163 while(1) {
takeuz 7:42cb4b01909c 164 if(interfaceCmdReceived && checkForLocalInterfaceCommand()) {
takeuz 10:fe1b976a3f9c 165 serialPorts[debug]->puts("Going to clear UART data\r\n");
takeuz 7:42cb4b01909c 166 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 167 serialPorts[debug]->puts("UART data cleared\r\n");
takeuz 7:42cb4b01909c 168 }
takeuz 8:cc3cb620c3bd 169 if(currentOBDProtocol==0 && isPoweredupNow) {
takeuz 8:cc3cb620c3bd 170 isPoweredupNow=0;
takeuz 4:3e1e2b161403 171 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 172 serialPorts[debug]->puts("Going to figure protocol\r\n");
takeuz 4:3e1e2b161403 173 #endif
takeuz 10:fe1b976a3f9c 174 serialPorts[debug]->puts("Searching...\r\n");
takeuz 2:d58ef72b4ab0 175 checkCommunicationProtocol();
takeuz 4:3e1e2b161403 176 if(!currentOBDProtocol) {
takeuz 4:3e1e2b161403 177 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 178 serialPorts[debug]->puts("!Unknownprotocol\r\n");
takeuz 4:3e1e2b161403 179 #endif
takeuz 7:42cb4b01909c 180 } else if(currentOBDProtocol==protCan) {
takeuz 10:fe1b976a3f9c 181 *canDisableKey=0;
takeuz 7:42cb4b01909c 182 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 183 serialPorts[debug]->puts("can Protocol\r\n");
takeuz 7:42cb4b01909c 184 #endif
takeuz 8:cc3cb620c3bd 185 } else if(currentOBDProtocol==protkLine14230) {
takeuz 10:fe1b976a3f9c 186 *canDisableKey=1;
takeuz 7:42cb4b01909c 187 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 188 serialPorts[debug]->puts("Kline Protocol\r\n");
takeuz 7:42cb4b01909c 189 #endif
takeuz 2:d58ef72b4ab0 190 }
takeuz 2:d58ef72b4ab0 191 }
takeuz 7:42cb4b01909c 192 if(interfaceCmdReceived && checkForLocalInterfaceCommand()) {
takeuz 10:fe1b976a3f9c 193 serialPorts[debug]->puts("Going to clear UART data\r\n");
takeuz 7:42cb4b01909c 194 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 195 serialPorts[debug]->puts("UART data cleared\r\n");
takeuz 2:d58ef72b4ab0 196 }
takeuz 7:42cb4b01909c 197 if(interfaceCmdReceived && currentOBDProtocol) {
takeuz 10:fe1b976a3f9c 198 serialPorts[debug]->puts("Going to process user command\r\n");
takeuz 7:42cb4b01909c 199 clearOBDCommand();
takeuz 8:cc3cb620c3bd 200 if(currentOBDProtocol==protCan) {
takeuz 10:fe1b976a3f9c 201 processUserCommand(incomingDataInInterFace);
takeuz 8:cc3cb620c3bd 202 } else if(currentOBDProtocol==protkLine14230) {
takeuz 7:42cb4b01909c 203 while(isVehicleCommandProcessing);
takeuz 7:42cb4b01909c 204 flushBuffer();
takeuz 8:cc3cb620c3bd 205 /*kLineUART->attach(NULL);
takeuz 8:cc3cb620c3bd 206 kLineUART->attach(&readKLine);*/
takeuz 7:42cb4b01909c 207 sendCommandToVehicle(incomingDataInInterFace);
takeuz 7:42cb4b01909c 208 wait_ms(500);
takeuz 10:fe1b976a3f9c 209 serialPorts[debug]->printf("\r\n");
takeuz 8:cc3cb620c3bd 210 } else {
takeuz 8:cc3cb620c3bd 211 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 212 serialPorts[interFace]->puts("?\r\n");
takeuz 8:cc3cb620c3bd 213 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 214 serialPorts[debug]->puts("?\r\n");
takeuz 8:cc3cb620c3bd 215 #endif
takeuz 2:d58ef72b4ab0 216 }
takeuz 8:cc3cb620c3bd 217 } else if(interfaceCmdReceived) {
takeuz 7:42cb4b01909c 218 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 219 serialPorts[interFace]->puts("?\r\n");
takeuz 7:42cb4b01909c 220 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 221 serialPorts[debug]->puts("?\r\n");
takeuz 7:42cb4b01909c 222 #endif
takeuz 2:d58ef72b4ab0 223 }
takeuz 10:fe1b976a3f9c 224 // sleepProcess();
takeuz 10:fe1b976a3f9c 225 if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) {
takeuz 10:fe1b976a3f9c 226 powerSaveMode();
takeuz 10:fe1b976a3f9c 227 } else {
takeuz 10:fe1b976a3f9c 228 sleepProcess();
takeuz 10:fe1b976a3f9c 229 }
takeuz 8:cc3cb620c3bd 230 /*if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) {
takeuz 2:d58ef72b4ab0 231 powerSaveMode();
takeuz 7:42cb4b01909c 232 } else {
takeuz 7:42cb4b01909c 233 sleepProcess();
takeuz 7:42cb4b01909c 234 }*/
takeuz 0:ccd2474129f2 235 }
takeuz 0:ccd2474129f2 236 }
takeuz 4:3e1e2b161403 237 void checkCommunicationProtocol()
takeuz 4:3e1e2b161403 238 {
takeuz 8:cc3cb620c3bd 239 /* #if sendDebugMessage
takeuz 8:cc3cb620c3bd 240 serialPorts[debug].puts("check Communication protocal ENT\r\n");
takeuz 8:cc3cb620c3bd 241 #endif*/
takeuz 2:d58ef72b4ab0 242 int i=0;
takeuz 4:3e1e2b161403 243 do {
takeuz 4:3e1e2b161403 244 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 245 serialPorts[debug]->puts("CanBusCheck\r\n");
takeuz 4:3e1e2b161403 246 #endif
takeuz 10:fe1b976a3f9c 247 canBus->reset();
takeuz 10:fe1b976a3f9c 248 canBus->frequency(canSpeeds[i]);
takeuz 10:fe1b976a3f9c 249 canBus->attach(NULL);
takeuz 10:fe1b976a3f9c 250 canBus->attach(&canReader);
takeuz 2:d58ef72b4ab0 251 OBDTimer.reset();
takeuz 2:d58ef72b4ab0 252 OBDTimer.start();
takeuz 2:d58ef72b4ab0 253 OBDCmdReceived=0;
takeuz 7:42cb4b01909c 254 setCanMessage("010D");
takeuz 10:fe1b976a3f9c 255 if (canBus->write(CANMessage(PID_REQUEST, can_msg, 8))) {
takeuz 4:3e1e2b161403 256 while(OBDTimer.read_ms() < waitTimeForOBDComm) {
takeuz 4:3e1e2b161403 257 if(OBDCmdReceived) {
takeuz 2:d58ef72b4ab0 258 currentOBDProtocol=protCan;
takeuz 4:3e1e2b161403 259 canFrequency=i;
takeuz 8:cc3cb620c3bd 260 break;
takeuz 2:d58ef72b4ab0 261 }
takeuz 2:d58ef72b4ab0 262 }
takeuz 2:d58ef72b4ab0 263 }
takeuz 2:d58ef72b4ab0 264 wait_ms(200);
takeuz 4:3e1e2b161403 265 i++;
takeuz 8:cc3cb620c3bd 266 } while(i<3 && canFrequency==0);
takeuz 2:d58ef72b4ab0 267 OBDTimer.stop();
takeuz 10:fe1b976a3f9c 268 canBus->attach(NULL);
takeuz 8:cc3cb620c3bd 269 if(!currentOBDProtocol) {
takeuz 7:42cb4b01909c 270 //Kline interface check
takeuz 7:42cb4b01909c 271 initializeKLine();
takeuz 7:42cb4b01909c 272 }
takeuz 4:3e1e2b161403 273 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 274 serialPorts[debug]->puts("check Communication protocal EXT\r\n");
takeuz 4:3e1e2b161403 275 #endif
takeuz 2:d58ef72b4ab0 276 }
takeuz 2:d58ef72b4ab0 277 void clearMemoryLocation( char *base, int _size)
takeuz 2:d58ef72b4ab0 278 {
takeuz 2:d58ef72b4ab0 279 for(int forCount=0; forCount<_size; forCount++) {
takeuz 2:d58ef72b4ab0 280 base[forCount]='\0';
takeuz 2:d58ef72b4ab0 281 }
takeuz 2:d58ef72b4ab0 282 }
takeuz 7:42cb4b01909c 283 void setCanMessage(char* _cmd)
takeuz 4:3e1e2b161403 284 {
takeuz 10:fe1b976a3f9c 285 serialPorts[debug]->puts("user Command :");
takeuz 10:fe1b976a3f9c 286 serialPorts[debug]->puts(_cmd);
takeuz 10:fe1b976a3f9c 287 serialPorts[debug]->puts("\r\n");
takeuz 8:cc3cb620c3bd 288 int len=findStringLength(_cmd)/2;
takeuz 7:42cb4b01909c 289 char _mode[2]="";
takeuz 7:42cb4b01909c 290 for(int i=0; i<2; i++) {
takeuz 7:42cb4b01909c 291 _mode[i]=_cmd[i];
takeuz 7:42cb4b01909c 292 }
takeuz 7:42cb4b01909c 293 int mode=strtol(_mode,NULL, 16);
takeuz 7:42cb4b01909c 294 cmd=strtol((_cmd+2),NULL, 16);
takeuz 8:cc3cb620c3bd 295 can_msg[0] = len;
takeuz 7:42cb4b01909c 296 can_msg[1] = mode;
takeuz 7:42cb4b01909c 297 can_msg[2] = cmd;
takeuz 4:3e1e2b161403 298 can_msg[3] = 0;
takeuz 4:3e1e2b161403 299 can_msg[4] = 0;
takeuz 4:3e1e2b161403 300 can_msg[5] = 0;
takeuz 4:3e1e2b161403 301 can_msg[6] = 0;
takeuz 4:3e1e2b161403 302 can_msg[7] = 0;
takeuz 2:d58ef72b4ab0 303 }
takeuz 10:fe1b976a3f9c 304 void processUserCommand(char *_incomingDataInInterFace)
takeuz 4:3e1e2b161403 305 {
takeuz 7:42cb4b01909c 306 OBDTimer.reset();
takeuz 10:fe1b976a3f9c 307 setCanMessage(_incomingDataInInterFace);
takeuz 7:42cb4b01909c 308 OBDTimer.start();
takeuz 7:42cb4b01909c 309 OBDCmdReceived=0;
takeuz 10:fe1b976a3f9c 310 clearOBDCommand();
takeuz 10:fe1b976a3f9c 311 canBus->attach(NULL);
takeuz 10:fe1b976a3f9c 312 canBus->attach(&canReader);
takeuz 10:fe1b976a3f9c 313 if (canBus->write(CANMessage(PID_REQUEST, can_msg, 8))) {
takeuz 7:42cb4b01909c 314 while(OBDTimer.read_ms() < waitTimeForOBDComm) {
takeuz 7:42cb4b01909c 315 if(OBDCmdReceived) {
takeuz 7:42cb4b01909c 316 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 317 if(_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 318 serialPorts[interFace]->printf("+TUAWAKE\r\n");
takeuz 10:fe1b976a3f9c 319 _powerSaveFlag=0;
takeuz 10:fe1b976a3f9c 320 break;
takeuz 10:fe1b976a3f9c 321 }
takeuz 10:fe1b976a3f9c 322 int len=incomingDataInOBD[0];
takeuz 10:fe1b976a3f9c 323 for(int i=1; i<=len; i++) {
takeuz 10:fe1b976a3f9c 324 if(incomingDataInOBD[i]!=0x00) {
takeuz 10:fe1b976a3f9c 325 serialPorts[interFace]->printf("%02X",incomingDataInOBD[i]);
takeuz 10:fe1b976a3f9c 326 } else {
takeuz 10:fe1b976a3f9c 327 serialPorts[interFace]->printf("%02X",00);
takeuz 10:fe1b976a3f9c 328 }
takeuz 10:fe1b976a3f9c 329 }
takeuz 7:42cb4b01909c 330 break;
takeuz 7:42cb4b01909c 331 }
takeuz 7:42cb4b01909c 332 }
takeuz 7:42cb4b01909c 333 }
takeuz 7:42cb4b01909c 334 OBDTimer.stop();
takeuz 7:42cb4b01909c 335 if(!OBDCmdReceived) {
takeuz 7:42cb4b01909c 336 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 337 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 338 serialPorts[interFace]->puts("?");
takeuz 10:fe1b976a3f9c 339 }
takeuz 7:42cb4b01909c 340 }
takeuz 10:fe1b976a3f9c 341 clearOBDCommand();
takeuz 10:fe1b976a3f9c 342 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 343 serialPorts[interFace]->puts("\r\n");
takeuz 10:fe1b976a3f9c 344 }
takeuz 2:d58ef72b4ab0 345 }
takeuz 4:3e1e2b161403 346 void sleepProcess()
takeuz 4:3e1e2b161403 347 {
takeuz 4:3e1e2b161403 348
takeuz 2:d58ef72b4ab0 349 #if pushDebugData
takeuz 10:fe1b976a3f9c 350 serialPorts[debug]->puts("SPe\r\n");
takeuz 2:d58ef72b4ab0 351 #endif
takeuz 2:d58ef72b4ab0 352 OBDTimer.reset();
takeuz 2:d58ef72b4ab0 353 OBDTimer.start();
takeuz 4:3e1e2b161403 354 while(OBDTimer.read_ms()<sleepTime && !interfaceCmdReceived) {
takeuz 2:d58ef72b4ab0 355 wait_ms(50);
takeuz 10:fe1b976a3f9c 356 KlineReadCount=KlineReadCount+50;
takeuz 10:fe1b976a3f9c 357 if(currentOBDProtocol==protkLine14230 && KlineReadCount>=3000) {
takeuz 10:fe1b976a3f9c 358 KlineReadCount=0;
takeuz 10:fe1b976a3f9c 359 if(incomingDataInInterFaceCounter) {
takeuz 10:fe1b976a3f9c 360 continue;
takeuz 10:fe1b976a3f9c 361 }
takeuz 10:fe1b976a3f9c 362 kLinekeepalive();
takeuz 10:fe1b976a3f9c 363 }
takeuz 2:d58ef72b4ab0 364 }
takeuz 2:d58ef72b4ab0 365 OBDTimer.stop();
takeuz 2:d58ef72b4ab0 366 }
takeuz 2:d58ef72b4ab0 367
takeuz 4:3e1e2b161403 368 void powerSaveMode()
takeuz 4:3e1e2b161403 369 {
takeuz 2:d58ef72b4ab0 370 #if pushDebugData
takeuz 10:fe1b976a3f9c 371 serialPorts[debug]->puts("Ignition test\r\n");
takeuz 2:d58ef72b4ab0 372 #endif
takeuz 10:fe1b976a3f9c 373 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 374 clearOBDCommand();
takeuz 10:fe1b976a3f9c 375 strcpy(incomingDataInInterFace,"010D");
takeuz 10:fe1b976a3f9c 376 _powerSaveFlag=1;
takeuz 10:fe1b976a3f9c 377 if(currentOBDProtocol==protCan) {
takeuz 10:fe1b976a3f9c 378 processUserCommand(incomingDataInInterFace);
takeuz 10:fe1b976a3f9c 379 } else if(currentOBDProtocol==protkLine14230) {
takeuz 10:fe1b976a3f9c 380 while(isVehicleCommandProcessing);
takeuz 10:fe1b976a3f9c 381 flushBuffer();
takeuz 10:fe1b976a3f9c 382 sendCommandToVehicle(incomingDataInInterFace);
takeuz 10:fe1b976a3f9c 383 wait_ms(500);
takeuz 10:fe1b976a3f9c 384 serialPorts[debug]->printf("\r\n");
takeuz 10:fe1b976a3f9c 385 }
takeuz 2:d58ef72b4ab0 386 #if pushDebugData
takeuz 10:fe1b976a3f9c 387 serialPorts[debug]->puts("Ignition test done\r\n");
takeuz 2:d58ef72b4ab0 388 #endif
takeuz 10:fe1b976a3f9c 389 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 390 #if pushDebugData
takeuz 10:fe1b976a3f9c 391 serialPorts[debug]->puts("Vehicle is in Power mode\r\n");
takeuz 10:fe1b976a3f9c 392 #endif
takeuz 10:fe1b976a3f9c 393 return;
takeuz 8:cc3cb620c3bd 394 }
takeuz 10:fe1b976a3f9c 395 #if pushDebugData
takeuz 10:fe1b976a3f9c 396 serialPorts[debug]->puts("Power save On\r\n");
takeuz 10:fe1b976a3f9c 397 #endif
takeuz 10:fe1b976a3f9c 398 if(currentOBDProtocol==protkLine14230) {
takeuz 10:fe1b976a3f9c 399 kLineUART->attach(NULL);
takeuz 5:53d84cbf9890 400 }
takeuz 10:fe1b976a3f9c 401 serialPorts[debug]->attach(NULL);
takeuz 10:fe1b976a3f9c 402 serialPorts[interFace]->attach(NULL);
takeuz 10:fe1b976a3f9c 403 timeCounter.detach();
takeuz 10:fe1b976a3f9c 404 *canDisableKey=1;
takeuz 10:fe1b976a3f9c 405
takeuz 10:fe1b976a3f9c 406 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
takeuz 10:fe1b976a3f9c 407 // LPC_SWM->PINASSIGN0 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 408 LPC_SWM->PINASSIGN1 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 409 LPC_SWM->PINASSIGN2 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 410 LPC_SWM->PINASSIGN3 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 411 LPC_SWM->PINASSIGN4 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 412 LPC_SWM->PINASSIGN5 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 413 LPC_SWM->PINASSIGN8 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 414 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
takeuz 10:fe1b976a3f9c 415 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 416 serialPorts[interFace]->attach(&readInterface);
takeuz 10:fe1b976a3f9c 417 InterruptIn in2(P0_22);
takeuz 10:fe1b976a3f9c 418 InterruptIn in3(P0_27);
takeuz 10:fe1b976a3f9c 419 in2.rise(&flip);
takeuz 10:fe1b976a3f9c 420 in3.rise(&flip);
takeuz 10:fe1b976a3f9c 421 while(1) {
takeuz 10:fe1b976a3f9c 422 // serialPorts[debug]->printf("Waiting for interupt..\r\n");
takeuz 10:fe1b976a3f9c 423 __WFI();
takeuz 10:fe1b976a3f9c 424 int waitTime=3000;
takeuz 10:fe1b976a3f9c 425 while(waitTime>0) {
takeuz 10:fe1b976a3f9c 426 if(interfaceCmdReceived) {
takeuz 10:fe1b976a3f9c 427 break;
takeuz 10:fe1b976a3f9c 428 }
takeuz 10:fe1b976a3f9c 429 wait_ms(50);
takeuz 10:fe1b976a3f9c 430 waitTime=(waitTime-50);
takeuz 10:fe1b976a3f9c 431 }
takeuz 10:fe1b976a3f9c 432 if(interfaceCmdReceived) {
takeuz 10:fe1b976a3f9c 433 // serialPorts[debug]->printf("%s\r\n",incomingDataInInterFace);
takeuz 10:fe1b976a3f9c 434 if(checkForLocalInterfaceCommand()) {
takeuz 10:fe1b976a3f9c 435 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 436 } else {
takeuz 10:fe1b976a3f9c 437 serialPorts[interFace]->printf("?\r\n");
takeuz 10:fe1b976a3f9c 438 }
takeuz 10:fe1b976a3f9c 439 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 440 } else {
takeuz 10:fe1b976a3f9c 441 break;
takeuz 10:fe1b976a3f9c 442 }
takeuz 10:fe1b976a3f9c 443 }
takeuz 10:fe1b976a3f9c 444 NVIC_SystemReset();
takeuz 10:fe1b976a3f9c 445 #if pushDebugData
takeuz 10:fe1b976a3f9c 446 serialPorts[debug]->puts("PowerSaveOff\r\n");
takeuz 10:fe1b976a3f9c 447 #endif
takeuz 10:fe1b976a3f9c 448 }
takeuz 10:fe1b976a3f9c 449 int checkForLocalInterfaceCommand()
takeuz 10:fe1b976a3f9c 450 {
takeuz 10:fe1b976a3f9c 451 serialPorts[debug]->puts("CommandDETECTED\r\n");
takeuz 10:fe1b976a3f9c 452 int i=0;
takeuz 10:fe1b976a3f9c 453 while(incomingDataInInterFace[i]!='\0') {
takeuz 10:fe1b976a3f9c 454 incomingDataInInterFace[i]=tolower(incomingDataInInterFace[i]);
takeuz 10:fe1b976a3f9c 455 i++;
takeuz 10:fe1b976a3f9c 456 }
takeuz 10:fe1b976a3f9c 457 char temp[2]= {incomingDataInInterFace[0],incomingDataInInterFace[1]};
takeuz 10:fe1b976a3f9c 458 if(strcmp(temp,"at")!=0) {
takeuz 10:fe1b976a3f9c 459 serialPorts[debug]->puts("Not Local Command\r\n");
takeuz 10:fe1b976a3f9c 460 return 0;
takeuz 10:fe1b976a3f9c 461 }
takeuz 10:fe1b976a3f9c 462 if(strcmp(incomingDataInInterFace,"atz")==0) {
takeuz 6:741524d110fe 463 NVIC_SystemReset();
takeuz 6:741524d110fe 464 }
takeuz 8:cc3cb620c3bd 465 if(strcmp(incomingDataInInterFace,"atrv")==0) {
takeuz 10:fe1b976a3f9c 466 serialPorts[interFace]->puts("12.1V\r\n");
takeuz 7:42cb4b01909c 467 clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
takeuz 7:42cb4b01909c 468 return 1;
takeuz 6:741524d110fe 469 }
takeuz 10:fe1b976a3f9c 470 serialPorts[interFace]->puts("?\r\n");
takeuz 7:42cb4b01909c 471 return 2;
takeuz 2:d58ef72b4ab0 472 }
takeuz 4:3e1e2b161403 473 void sendResponseToInterface()
takeuz 4:3e1e2b161403 474 {
takeuz 10:fe1b976a3f9c 475 serialPorts[interFace]->puts(incomingDataInOBD);
takeuz 10:fe1b976a3f9c 476 serialPorts[interFace]->puts("\r\n");
takeuz 7:42cb4b01909c 477 }
takeuz 7:42cb4b01909c 478
takeuz 7:42cb4b01909c 479 void clearInterfaceCommand()
takeuz 7:42cb4b01909c 480 {
takeuz 7:42cb4b01909c 481 incomingDataInInterFaceCounter=0;
takeuz 7:42cb4b01909c 482 interfaceCmdReceived=0;
takeuz 7:42cb4b01909c 483 clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
takeuz 7:42cb4b01909c 484 }
takeuz 7:42cb4b01909c 485
takeuz 7:42cb4b01909c 486 void clearOBDCommand()
takeuz 7:42cb4b01909c 487 {
takeuz 7:42cb4b01909c 488 clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
takeuz 8:cc3cb620c3bd 489 incomingDataInOBDCounter=0;
takeuz 8:cc3cb620c3bd 490 OBDCmdReceived=0;
takeuz 8:cc3cb620c3bd 491 }
takeuz 8:cc3cb620c3bd 492 int findStringLength(char *_cmd)
takeuz 8:cc3cb620c3bd 493 {
takeuz 8:cc3cb620c3bd 494 int i=0;
takeuz 8:cc3cb620c3bd 495 for(i=0; _cmd[i]!='\0'; ++i);
takeuz 8:cc3cb620c3bd 496 return i;
takeuz 8:cc3cb620c3bd 497 }
takeuz 8:cc3cb620c3bd 498 int initializeKLine()
takeuz 8:cc3cb620c3bd 499 {
takeuz 8:cc3cb620c3bd 500 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 501 serialPorts[debug]->puts("K-Line Check\r\n");
takeuz 8:cc3cb620c3bd 502 #endif
takeuz 8:cc3cb620c3bd 503 *klineWakeup=1;
takeuz 8:cc3cb620c3bd 504 wait_ms(300);
takeuz 8:cc3cb620c3bd 505 *klineWakeup=0;
takeuz 8:cc3cb620c3bd 506 wait_ms(25);
takeuz 8:cc3cb620c3bd 507 *klineWakeup=1;
takeuz 8:cc3cb620c3bd 508 wait_ms(25);
takeuz 8:cc3cb620c3bd 509 delete klineWakeup;
takeuz 8:cc3cb620c3bd 510 kLineUART=new Serial(P0_28,P0_27);
takeuz 10:fe1b976a3f9c 511 clearOBDCommand();
takeuz 8:cc3cb620c3bd 512 kLineUART->baud(10400);
takeuz 8:cc3cb620c3bd 513 kLineUART->attach(&readKLine);
takeuz 8:cc3cb620c3bd 514 uint8_t crc=0;
takeuz 8:cc3cb620c3bd 515 crc=crc+0xC1;
takeuz 8:cc3cb620c3bd 516 crc=crc+0x33;
takeuz 8:cc3cb620c3bd 517 crc=crc+0xF1;
takeuz 8:cc3cb620c3bd 518 crc=crc+0x81;
takeuz 8:cc3cb620c3bd 519 kLineUART->putc(0xC1);
takeuz 8:cc3cb620c3bd 520 kLineUART->putc(0x33);
takeuz 8:cc3cb620c3bd 521 kLineUART->putc(0xF1);
takeuz 8:cc3cb620c3bd 522 kLineUART->putc(0x81);
takeuz 8:cc3cb620c3bd 523 kLineUART->putc(crc);
takeuz 10:fe1b976a3f9c 524 wait(2);
takeuz 10:fe1b976a3f9c 525 if(incomingDataInOBD[5]!='\0') {
takeuz 10:fe1b976a3f9c 526 currentOBDProtocol=protkLine14230;
takeuz 10:fe1b976a3f9c 527 }
takeuz 10:fe1b976a3f9c 528 kLineUART->attach(NULL);
takeuz 8:cc3cb620c3bd 529 return 0;
takeuz 7:42cb4b01909c 530 }
takeuz 7:42cb4b01909c 531
takeuz 8:cc3cb620c3bd 532 void setKLineMsg(char *_cmd)
takeuz 8:cc3cb620c3bd 533 {
takeuz 8:cc3cb620c3bd 534 for(int i=3; i<6; i++) {
takeuz 7:42cb4b01909c 535 kLineCmd[i]='\0';
takeuz 7:42cb4b01909c 536 }
takeuz 7:42cb4b01909c 537 int len=findStringLength(_cmd)/2;
takeuz 7:42cb4b01909c 538 char _mode[2]="";
takeuz 7:42cb4b01909c 539 uint8_t crc=0;
takeuz 8:cc3cb620c3bd 540 if(len==1) {
takeuz 7:42cb4b01909c 541 crc=crc+0xC1;
takeuz 7:42cb4b01909c 542 kLineCmd[0]=0xC1;
takeuz 7:42cb4b01909c 543 crc=crc+0x33;
takeuz 7:42cb4b01909c 544 kLineCmd[1]=(0x33);
takeuz 7:42cb4b01909c 545 crc=crc+0xF1;
takeuz 7:42cb4b01909c 546 kLineCmd[2]=(0xF1);
takeuz 7:42cb4b01909c 547 crc=crc+strtol((_cmd),NULL, 16);
takeuz 7:42cb4b01909c 548 kLineCmd[3]=(strtol((_cmd),NULL, 16));
takeuz 7:42cb4b01909c 549 kLineCmd[4]=(crc);
takeuz 8:cc3cb620c3bd 550 } else if(len==2) {
takeuz 7:42cb4b01909c 551 crc=crc+0xC2;
takeuz 7:42cb4b01909c 552 kLineCmd[0]=(0xC2);
takeuz 7:42cb4b01909c 553 crc=crc+0x33;
takeuz 7:42cb4b01909c 554 kLineCmd[1]=(0x33);
takeuz 7:42cb4b01909c 555 crc=crc+0xF1;
takeuz 7:42cb4b01909c 556 kLineCmd[2]=(0xF1);
takeuz 7:42cb4b01909c 557 for(int i=0; i<2; i++) {
takeuz 7:42cb4b01909c 558 _mode[i]=_cmd[i];
takeuz 7:42cb4b01909c 559 }
takeuz 7:42cb4b01909c 560 crc=crc+strtol(_mode,NULL, 16);
takeuz 7:42cb4b01909c 561 kLineCmd[3]=(strtol(_mode,NULL, 16));
takeuz 7:42cb4b01909c 562 crc=crc+(strtol((_cmd+2),NULL, 16));
takeuz 7:42cb4b01909c 563 kLineCmd[4]=(strtol((_cmd+2),NULL, 16));
takeuz 7:42cb4b01909c 564 kLineCmd[5]=(crc);
takeuz 7:42cb4b01909c 565 }
takeuz 7:42cb4b01909c 566 }
takeuz 8:cc3cb620c3bd 567
takeuz 8:cc3cb620c3bd 568 void sendCommandToVehicle(char *_cmd)
takeuz 8:cc3cb620c3bd 569 {
takeuz 7:42cb4b01909c 570 isVehicleCommandProcessing=1;
takeuz 7:42cb4b01909c 571 setKLineMsg(_cmd);
takeuz 10:fe1b976a3f9c 572 clearOBDCommand();
takeuz 10:fe1b976a3f9c 573 flushBuffer();
takeuz 10:fe1b976a3f9c 574 kLineUART->attach(&readKLine);
takeuz 8:cc3cb620c3bd 575 for(kLineCommandCount=0; kLineCommandCount<6; kLineCommandCount++) {
takeuz 8:cc3cb620c3bd 576 if(kLineCmd[kLineCommandCount]=='\0') {
takeuz 7:42cb4b01909c 577 break;
takeuz 7:42cb4b01909c 578 }
takeuz 7:42cb4b01909c 579 kLineUART->putc(kLineCmd[kLineCommandCount]);
takeuz 7:42cb4b01909c 580 }
takeuz 8:cc3cb620c3bd 581 wait_ms(500);
takeuz 10:fe1b976a3f9c 582 serialPorts[debug]->printf("Got Data\r\n");
takeuz 7:42cb4b01909c 583 //have to check given and received commands are same.
takeuz 10:fe1b976a3f9c 584 //if it is same we have to read the response else just flush buffer and send the command again
takeuz 10:fe1b976a3f9c 585 for(int i=0; i<strlen(incomingDataInOBD); i++) {
takeuz 10:fe1b976a3f9c 586 serialPorts[debug]->printf("%02X",incomingDataInOBD[i]);
takeuz 10:fe1b976a3f9c 587 }
takeuz 10:fe1b976a3f9c 588 serialPorts[debug]->printf("\r\n");
takeuz 10:fe1b976a3f9c 589 kLineUART->attach(NULL);
takeuz 10:fe1b976a3f9c 590 strcpy(incomingDataInOBD,(incomingDataInOBD+kLineCommandCount)); //cut request from hole response
takeuz 10:fe1b976a3f9c 591 for(int i=0; i<strlen(incomingDataInOBD); i++) {
takeuz 10:fe1b976a3f9c 592 serialPorts[debug]->printf("%02X",incomingDataInOBD[i]);
takeuz 10:fe1b976a3f9c 593 }
takeuz 10:fe1b976a3f9c 594 serialPorts[debug]->printf("\r\n");
takeuz 10:fe1b976a3f9c 595 serialPorts[debug]->printf("%02X\r\n",incomingDataInOBD[0]);
takeuz 10:fe1b976a3f9c 596 int len=(incomingDataInOBD[0]%16);//convert first char to integer and find kength of the response
takeuz 10:fe1b976a3f9c 597 serialPorts[debug]->printf("Response Length : %d\r\n",len);
takeuz 10:fe1b976a3f9c 598 serialPorts[debug]->printf("Response Length : %d\r\n",strlen(incomingDataInOBD));
takeuz 10:fe1b976a3f9c 599 strcpy(incomingDataInOBD,(incomingDataInOBD+3)); //again cut first three byte
takeuz 10:fe1b976a3f9c 600 serialPorts[debug]->printf("Response Length : %d\r\n",strlen(incomingDataInOBD));
takeuz 10:fe1b976a3f9c 601 int obdDataLength=strlen(incomingDataInOBD);
takeuz 10:fe1b976a3f9c 602 int count=0;
takeuz 10:fe1b976a3f9c 603 while(count<len) {
takeuz 10:fe1b976a3f9c 604 if(_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 605 serialPorts[interFace]->printf("+TUAWAKE\r\n");
takeuz 10:fe1b976a3f9c 606 _powerSaveFlag=0;
takeuz 10:fe1b976a3f9c 607 break;
takeuz 10:fe1b976a3f9c 608 }
takeuz 10:fe1b976a3f9c 609 if(obdDataLength>0) {
takeuz 10:fe1b976a3f9c 610 serialPorts[interFace]->printf("%02X",incomingDataInOBD[count]);
takeuz 10:fe1b976a3f9c 611 serialPorts[debug]->printf("%02X",incomingDataInOBD[count]);
takeuz 10:fe1b976a3f9c 612 } else {
takeuz 10:fe1b976a3f9c 613 serialPorts[interFace]->printf("%02X",0x00);
takeuz 10:fe1b976a3f9c 614 serialPorts[debug]->printf("%02X",0x00);
takeuz 10:fe1b976a3f9c 615 }
takeuz 10:fe1b976a3f9c 616 count++;
takeuz 10:fe1b976a3f9c 617 obdDataLength--;
takeuz 10:fe1b976a3f9c 618 }
takeuz 10:fe1b976a3f9c 619 if(len==0) {
takeuz 10:fe1b976a3f9c 620 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 621 serialPorts[interFace]->printf("?");
takeuz 10:fe1b976a3f9c 622 serialPorts[debug]->printf("?");
takeuz 10:fe1b976a3f9c 623 }
takeuz 10:fe1b976a3f9c 624 }
takeuz 10:fe1b976a3f9c 625 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 626 serialPorts[interFace]->printf("\r\n");
takeuz 10:fe1b976a3f9c 627 serialPorts[debug]->printf("\r\n");
takeuz 10:fe1b976a3f9c 628 serialPorts[debug]->printf("Done\r\n");
takeuz 10:fe1b976a3f9c 629 }
takeuz 8:cc3cb620c3bd 630 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 631 clearOBDCommand();
takeuz 7:42cb4b01909c 632 isVehicleCommandProcessing=0;
takeuz 7:42cb4b01909c 633 }
takeuz 7:42cb4b01909c 634
takeuz 8:cc3cb620c3bd 635 void flushBuffer()
takeuz 8:cc3cb620c3bd 636 {
takeuz 8:cc3cb620c3bd 637 while(kLineUART->readable()) {
takeuz 7:42cb4b01909c 638 kLineUART->getc();
takeuz 7:42cb4b01909c 639 }
takeuz 7:42cb4b01909c 640 }
takeuz 8:cc3cb620c3bd 641 void kLinekeepalive()
takeuz 8:cc3cb620c3bd 642 {
takeuz 10:fe1b976a3f9c 643 serialPorts[debug]->printf("KeepAlive ENT\r\n");
takeuz 8:cc3cb620c3bd 644 kLineUART->putc(0xc1);
takeuz 8:cc3cb620c3bd 645 kLineUART->putc(0x33);
takeuz 8:cc3cb620c3bd 646 kLineUART->putc(0xf1);
takeuz 8:cc3cb620c3bd 647 kLineUART->putc(0x3e);
takeuz 8:cc3cb620c3bd 648 kLineUART->putc(0x23);
takeuz 10:fe1b976a3f9c 649 serialPorts[debug]->printf("EXT\r\n");
takeuz 8:cc3cb620c3bd 650 }
takeuz 10:fe1b976a3f9c 651 char tolower(unsigned char ch)
takeuz 10:fe1b976a3f9c 652 {
takeuz 8:cc3cb620c3bd 653 if (ch >= 'A' && ch <= 'Z')
takeuz 8:cc3cb620c3bd 654 ch = 'a' + (ch - 'A');
takeuz 8:cc3cb620c3bd 655 return ch;
takeuz 10:fe1b976a3f9c 656 }
takeuz 10:fe1b976a3f9c 657
takeuz 10:fe1b976a3f9c 658 int checkCarStatus()
takeuz 10:fe1b976a3f9c 659 {
takeuz 10:fe1b976a3f9c 660 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
takeuz 10:fe1b976a3f9c 661 LPC_SWM->PINASSIGN0 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 662 LPC_SWM->PINASSIGN1 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 663 LPC_SWM->PINASSIGN2 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 664 LPC_SWM->PINASSIGN3 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 665 LPC_SWM->PINASSIGN4 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 666 LPC_SWM->PINASSIGN5 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 667 LPC_SWM->PINASSIGN8 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 668 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
takeuz 10:fe1b976a3f9c 669 DigitalOut kLine(P0_27);
takeuz 10:fe1b976a3f9c 670 DigitalOut canLine(P0_22);
takeuz 10:fe1b976a3f9c 671 if(kLine.read()) {
takeuz 10:fe1b976a3f9c 672 return 1;
takeuz 10:fe1b976a3f9c 673 }
takeuz 10:fe1b976a3f9c 674 if(canLine.read()) {
takeuz 10:fe1b976a3f9c 675 return 2;
takeuz 10:fe1b976a3f9c 676 }
takeuz 10:fe1b976a3f9c 677 return 0;
takeuz 10:fe1b976a3f9c 678 }
takeuz 10:fe1b976a3f9c 679
takeuz 10:fe1b976a3f9c 680 void initPins()
takeuz 10:fe1b976a3f9c 681 {
takeuz 10:fe1b976a3f9c 682 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
takeuz 10:fe1b976a3f9c 683 LPC_SWM->PINASSIGN0 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 684 LPC_SWM->PINASSIGN1 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 685 LPC_SWM->PINASSIGN2 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 686 LPC_SWM->PINASSIGN3 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 687 LPC_SWM->PINASSIGN4 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 688 LPC_SWM->PINASSIGN5 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 689 LPC_SWM->PINASSIGN8 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 690 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
takeuz 10:fe1b976a3f9c 691 serialPorts[0] = new Serial(P0_11,P0_13);
takeuz 10:fe1b976a3f9c 692 serialPorts[1] = new Serial(P0_15,P0_14);
takeuz 10:fe1b976a3f9c 693 klineWakeup = new DigitalOut(P0_28);
takeuz 10:fe1b976a3f9c 694 canBus = new CAN(P0_22, P0_23);
takeuz 10:fe1b976a3f9c 695 canDisableKey=new DigitalOut(P0_0,0);
takeuz 10:fe1b976a3f9c 696 }