f211

Dependencies:   mbed-dev1

Fork of OBD21121 by Surendar S

Committer:
takeuz
Date:
Tue Jun 13 07:46:28 2017 +0000
Revision:
11:853d12fe7ec0
Parent:
10:fe1b976a3f9c
Child:
12:91c72461d70c
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 11:853d12fe7ec0 58 const int powerSaveTime=30;
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 11:853d12fe7ec0 157 wait_ms(800);
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 11:853d12fe7ec0 169 if(currentOBDProtocol==0/* && isPoweredupNow*/) {
takeuz 11:853d12fe7ec0 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 11:853d12fe7ec0 201 while(isVehicleCommandProcessing){}
takeuz 10:fe1b976a3f9c 202 processUserCommand(incomingDataInInterFace);
takeuz 8:cc3cb620c3bd 203 } else if(currentOBDProtocol==protkLine14230) {
takeuz 11:853d12fe7ec0 204 while(isVehicleCommandProcessing){}
takeuz 7:42cb4b01909c 205 flushBuffer();
takeuz 8:cc3cb620c3bd 206 /*kLineUART->attach(NULL);
takeuz 8:cc3cb620c3bd 207 kLineUART->attach(&readKLine);*/
takeuz 7:42cb4b01909c 208 sendCommandToVehicle(incomingDataInInterFace);
takeuz 7:42cb4b01909c 209 wait_ms(500);
takeuz 10:fe1b976a3f9c 210 serialPorts[debug]->printf("\r\n");
takeuz 8:cc3cb620c3bd 211 } else {
takeuz 8:cc3cb620c3bd 212 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 213 serialPorts[interFace]->puts("?\r\n");
takeuz 8:cc3cb620c3bd 214 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 215 serialPorts[debug]->puts("?\r\n");
takeuz 8:cc3cb620c3bd 216 #endif
takeuz 2:d58ef72b4ab0 217 }
takeuz 8:cc3cb620c3bd 218 } else if(interfaceCmdReceived) {
takeuz 7:42cb4b01909c 219 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 220 serialPorts[interFace]->puts("?\r\n");
takeuz 7:42cb4b01909c 221 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 222 serialPorts[debug]->puts("?\r\n");
takeuz 7:42cb4b01909c 223 #endif
takeuz 2:d58ef72b4ab0 224 }
takeuz 10:fe1b976a3f9c 225 // sleepProcess();
takeuz 11:853d12fe7ec0 226 if(lastOBDCmdRcvdTime> powerSaveTime /*&& lastInterfaceCmdRcvdTime> powerSaveTime */) {
takeuz 10:fe1b976a3f9c 227 powerSaveMode();
takeuz 10:fe1b976a3f9c 228 } else {
takeuz 10:fe1b976a3f9c 229 sleepProcess();
takeuz 10:fe1b976a3f9c 230 }
takeuz 8:cc3cb620c3bd 231 /*if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) {
takeuz 2:d58ef72b4ab0 232 powerSaveMode();
takeuz 7:42cb4b01909c 233 } else {
takeuz 7:42cb4b01909c 234 sleepProcess();
takeuz 7:42cb4b01909c 235 }*/
takeuz 0:ccd2474129f2 236 }
takeuz 0:ccd2474129f2 237 }
takeuz 4:3e1e2b161403 238 void checkCommunicationProtocol()
takeuz 4:3e1e2b161403 239 {
takeuz 8:cc3cb620c3bd 240 /* #if sendDebugMessage
takeuz 8:cc3cb620c3bd 241 serialPorts[debug].puts("check Communication protocal ENT\r\n");
takeuz 8:cc3cb620c3bd 242 #endif*/
takeuz 2:d58ef72b4ab0 243 int i=0;
takeuz 4:3e1e2b161403 244 do {
takeuz 4:3e1e2b161403 245 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 246 serialPorts[debug]->puts("CanBusCheck\r\n");
takeuz 4:3e1e2b161403 247 #endif
takeuz 10:fe1b976a3f9c 248 canBus->reset();
takeuz 10:fe1b976a3f9c 249 canBus->frequency(canSpeeds[i]);
takeuz 10:fe1b976a3f9c 250 canBus->attach(NULL);
takeuz 10:fe1b976a3f9c 251 canBus->attach(&canReader);
takeuz 2:d58ef72b4ab0 252 OBDTimer.reset();
takeuz 2:d58ef72b4ab0 253 OBDTimer.start();
takeuz 2:d58ef72b4ab0 254 OBDCmdReceived=0;
takeuz 7:42cb4b01909c 255 setCanMessage("010D");
takeuz 10:fe1b976a3f9c 256 if (canBus->write(CANMessage(PID_REQUEST, can_msg, 8))) {
takeuz 4:3e1e2b161403 257 while(OBDTimer.read_ms() < waitTimeForOBDComm) {
takeuz 4:3e1e2b161403 258 if(OBDCmdReceived) {
takeuz 2:d58ef72b4ab0 259 currentOBDProtocol=protCan;
takeuz 4:3e1e2b161403 260 canFrequency=i;
takeuz 8:cc3cb620c3bd 261 break;
takeuz 2:d58ef72b4ab0 262 }
takeuz 2:d58ef72b4ab0 263 }
takeuz 2:d58ef72b4ab0 264 }
takeuz 2:d58ef72b4ab0 265 wait_ms(200);
takeuz 4:3e1e2b161403 266 i++;
takeuz 8:cc3cb620c3bd 267 } while(i<3 && canFrequency==0);
takeuz 2:d58ef72b4ab0 268 OBDTimer.stop();
takeuz 10:fe1b976a3f9c 269 canBus->attach(NULL);
takeuz 8:cc3cb620c3bd 270 if(!currentOBDProtocol) {
takeuz 7:42cb4b01909c 271 //Kline interface check
takeuz 7:42cb4b01909c 272 initializeKLine();
takeuz 7:42cb4b01909c 273 }
takeuz 4:3e1e2b161403 274 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 275 serialPorts[debug]->puts("check Communication protocal EXT\r\n");
takeuz 4:3e1e2b161403 276 #endif
takeuz 2:d58ef72b4ab0 277 }
takeuz 2:d58ef72b4ab0 278 void clearMemoryLocation( char *base, int _size)
takeuz 2:d58ef72b4ab0 279 {
takeuz 2:d58ef72b4ab0 280 for(int forCount=0; forCount<_size; forCount++) {
takeuz 2:d58ef72b4ab0 281 base[forCount]='\0';
takeuz 2:d58ef72b4ab0 282 }
takeuz 2:d58ef72b4ab0 283 }
takeuz 7:42cb4b01909c 284 void setCanMessage(char* _cmd)
takeuz 4:3e1e2b161403 285 {
takeuz 10:fe1b976a3f9c 286 serialPorts[debug]->puts("user Command :");
takeuz 10:fe1b976a3f9c 287 serialPorts[debug]->puts(_cmd);
takeuz 10:fe1b976a3f9c 288 serialPorts[debug]->puts("\r\n");
takeuz 8:cc3cb620c3bd 289 int len=findStringLength(_cmd)/2;
takeuz 7:42cb4b01909c 290 char _mode[2]="";
takeuz 7:42cb4b01909c 291 for(int i=0; i<2; i++) {
takeuz 7:42cb4b01909c 292 _mode[i]=_cmd[i];
takeuz 7:42cb4b01909c 293 }
takeuz 7:42cb4b01909c 294 int mode=strtol(_mode,NULL, 16);
takeuz 7:42cb4b01909c 295 cmd=strtol((_cmd+2),NULL, 16);
takeuz 8:cc3cb620c3bd 296 can_msg[0] = len;
takeuz 7:42cb4b01909c 297 can_msg[1] = mode;
takeuz 7:42cb4b01909c 298 can_msg[2] = cmd;
takeuz 4:3e1e2b161403 299 can_msg[3] = 0;
takeuz 4:3e1e2b161403 300 can_msg[4] = 0;
takeuz 4:3e1e2b161403 301 can_msg[5] = 0;
takeuz 4:3e1e2b161403 302 can_msg[6] = 0;
takeuz 4:3e1e2b161403 303 can_msg[7] = 0;
takeuz 2:d58ef72b4ab0 304 }
takeuz 10:fe1b976a3f9c 305 void processUserCommand(char *_incomingDataInInterFace)
takeuz 4:3e1e2b161403 306 {
takeuz 11:853d12fe7ec0 307 isVehicleCommandProcessing=1;
takeuz 7:42cb4b01909c 308 OBDTimer.reset();
takeuz 10:fe1b976a3f9c 309 setCanMessage(_incomingDataInInterFace);
takeuz 7:42cb4b01909c 310 OBDTimer.start();
takeuz 7:42cb4b01909c 311 OBDCmdReceived=0;
takeuz 10:fe1b976a3f9c 312 clearOBDCommand();
takeuz 10:fe1b976a3f9c 313 canBus->attach(NULL);
takeuz 10:fe1b976a3f9c 314 canBus->attach(&canReader);
takeuz 10:fe1b976a3f9c 315 if (canBus->write(CANMessage(PID_REQUEST, can_msg, 8))) {
takeuz 7:42cb4b01909c 316 while(OBDTimer.read_ms() < waitTimeForOBDComm) {
takeuz 7:42cb4b01909c 317 if(OBDCmdReceived) {
takeuz 7:42cb4b01909c 318 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 319 if(_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 320 serialPorts[interFace]->printf("+TUAWAKE\r\n");
takeuz 10:fe1b976a3f9c 321 _powerSaveFlag=0;
takeuz 10:fe1b976a3f9c 322 break;
takeuz 10:fe1b976a3f9c 323 }
takeuz 10:fe1b976a3f9c 324 int len=incomingDataInOBD[0];
takeuz 10:fe1b976a3f9c 325 for(int i=1; i<=len; i++) {
takeuz 10:fe1b976a3f9c 326 if(incomingDataInOBD[i]!=0x00) {
takeuz 10:fe1b976a3f9c 327 serialPorts[interFace]->printf("%02X",incomingDataInOBD[i]);
takeuz 10:fe1b976a3f9c 328 } else {
takeuz 10:fe1b976a3f9c 329 serialPorts[interFace]->printf("%02X",00);
takeuz 10:fe1b976a3f9c 330 }
takeuz 10:fe1b976a3f9c 331 }
takeuz 7:42cb4b01909c 332 break;
takeuz 7:42cb4b01909c 333 }
takeuz 7:42cb4b01909c 334 }
takeuz 7:42cb4b01909c 335 }
takeuz 7:42cb4b01909c 336 OBDTimer.stop();
takeuz 7:42cb4b01909c 337 if(!OBDCmdReceived) {
takeuz 7:42cb4b01909c 338 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 339 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 340 serialPorts[interFace]->puts("?");
takeuz 10:fe1b976a3f9c 341 }
takeuz 7:42cb4b01909c 342 }
takeuz 10:fe1b976a3f9c 343 clearOBDCommand();
takeuz 10:fe1b976a3f9c 344 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 345 serialPorts[interFace]->puts("\r\n");
takeuz 10:fe1b976a3f9c 346 }
takeuz 11:853d12fe7ec0 347 isVehicleCommandProcessing=0;
takeuz 2:d58ef72b4ab0 348 }
takeuz 4:3e1e2b161403 349 void sleepProcess()
takeuz 4:3e1e2b161403 350 {
takeuz 4:3e1e2b161403 351
takeuz 2:d58ef72b4ab0 352 #if pushDebugData
takeuz 10:fe1b976a3f9c 353 serialPorts[debug]->puts("SPe\r\n");
takeuz 2:d58ef72b4ab0 354 #endif
takeuz 2:d58ef72b4ab0 355 OBDTimer.reset();
takeuz 2:d58ef72b4ab0 356 OBDTimer.start();
takeuz 4:3e1e2b161403 357 while(OBDTimer.read_ms()<sleepTime && !interfaceCmdReceived) {
takeuz 2:d58ef72b4ab0 358 wait_ms(50);
takeuz 10:fe1b976a3f9c 359 KlineReadCount=KlineReadCount+50;
takeuz 10:fe1b976a3f9c 360 if(currentOBDProtocol==protkLine14230 && KlineReadCount>=3000) {
takeuz 10:fe1b976a3f9c 361 KlineReadCount=0;
takeuz 11:853d12fe7ec0 362 if(isVehicleCommandProcessing) {
takeuz 10:fe1b976a3f9c 363 continue;
takeuz 10:fe1b976a3f9c 364 }
takeuz 10:fe1b976a3f9c 365 kLinekeepalive();
takeuz 10:fe1b976a3f9c 366 }
takeuz 2:d58ef72b4ab0 367 }
takeuz 2:d58ef72b4ab0 368 OBDTimer.stop();
takeuz 2:d58ef72b4ab0 369 }
takeuz 2:d58ef72b4ab0 370
takeuz 4:3e1e2b161403 371 void powerSaveMode()
takeuz 4:3e1e2b161403 372 {
takeuz 2:d58ef72b4ab0 373 #if pushDebugData
takeuz 10:fe1b976a3f9c 374 serialPorts[debug]->puts("Ignition test\r\n");
takeuz 2:d58ef72b4ab0 375 #endif
takeuz 10:fe1b976a3f9c 376 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 377 clearOBDCommand();
takeuz 10:fe1b976a3f9c 378 strcpy(incomingDataInInterFace,"010D");
takeuz 10:fe1b976a3f9c 379 _powerSaveFlag=1;
takeuz 10:fe1b976a3f9c 380 if(currentOBDProtocol==protCan) {
takeuz 11:853d12fe7ec0 381 while(isVehicleCommandProcessing){}
takeuz 10:fe1b976a3f9c 382 processUserCommand(incomingDataInInterFace);
takeuz 10:fe1b976a3f9c 383 } else if(currentOBDProtocol==protkLine14230) {
takeuz 10:fe1b976a3f9c 384 while(isVehicleCommandProcessing);
takeuz 10:fe1b976a3f9c 385 flushBuffer();
takeuz 10:fe1b976a3f9c 386 sendCommandToVehicle(incomingDataInInterFace);
takeuz 10:fe1b976a3f9c 387 wait_ms(500);
takeuz 10:fe1b976a3f9c 388 serialPorts[debug]->printf("\r\n");
takeuz 10:fe1b976a3f9c 389 }
takeuz 2:d58ef72b4ab0 390 #if pushDebugData
takeuz 10:fe1b976a3f9c 391 serialPorts[debug]->puts("Ignition test done\r\n");
takeuz 2:d58ef72b4ab0 392 #endif
takeuz 10:fe1b976a3f9c 393 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 394 #if pushDebugData
takeuz 10:fe1b976a3f9c 395 serialPorts[debug]->puts("Vehicle is in Power mode\r\n");
takeuz 10:fe1b976a3f9c 396 #endif
takeuz 10:fe1b976a3f9c 397 return;
takeuz 8:cc3cb620c3bd 398 }
takeuz 10:fe1b976a3f9c 399 #if pushDebugData
takeuz 10:fe1b976a3f9c 400 serialPorts[debug]->puts("Power save On\r\n");
takeuz 10:fe1b976a3f9c 401 #endif
takeuz 10:fe1b976a3f9c 402 if(currentOBDProtocol==protkLine14230) {
takeuz 10:fe1b976a3f9c 403 kLineUART->attach(NULL);
takeuz 5:53d84cbf9890 404 }
takeuz 10:fe1b976a3f9c 405 serialPorts[debug]->attach(NULL);
takeuz 10:fe1b976a3f9c 406 serialPorts[interFace]->attach(NULL);
takeuz 10:fe1b976a3f9c 407 timeCounter.detach();
takeuz 10:fe1b976a3f9c 408 *canDisableKey=1;
takeuz 10:fe1b976a3f9c 409
takeuz 10:fe1b976a3f9c 410 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
takeuz 10:fe1b976a3f9c 411 // LPC_SWM->PINASSIGN0 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 412 LPC_SWM->PINASSIGN1 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 413 LPC_SWM->PINASSIGN2 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 414 LPC_SWM->PINASSIGN3 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 415 LPC_SWM->PINASSIGN4 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 416 LPC_SWM->PINASSIGN5 = 0xffffffffUL;
takeuz 9:a3bdc9a71466 417 LPC_SWM->PINASSIGN8 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 418 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
takeuz 10:fe1b976a3f9c 419 clearInterfaceCommand();
takeuz 11:853d12fe7ec0 420 // serialPorts[interFace]->attach(&readInterface); // it will not work
takeuz 10:fe1b976a3f9c 421 InterruptIn in2(P0_22);
takeuz 10:fe1b976a3f9c 422 InterruptIn in3(P0_27);
takeuz 10:fe1b976a3f9c 423 in2.rise(&flip);
takeuz 10:fe1b976a3f9c 424 in3.rise(&flip);
takeuz 10:fe1b976a3f9c 425 while(1) {
takeuz 10:fe1b976a3f9c 426 // serialPorts[debug]->printf("Waiting for interupt..\r\n");
takeuz 10:fe1b976a3f9c 427 __WFI();
takeuz 10:fe1b976a3f9c 428 int waitTime=3000;
takeuz 10:fe1b976a3f9c 429 while(waitTime>0) {
takeuz 10:fe1b976a3f9c 430 if(interfaceCmdReceived) {
takeuz 10:fe1b976a3f9c 431 break;
takeuz 10:fe1b976a3f9c 432 }
takeuz 10:fe1b976a3f9c 433 wait_ms(50);
takeuz 10:fe1b976a3f9c 434 waitTime=(waitTime-50);
takeuz 10:fe1b976a3f9c 435 }
takeuz 10:fe1b976a3f9c 436 if(interfaceCmdReceived) {
takeuz 10:fe1b976a3f9c 437 // serialPorts[debug]->printf("%s\r\n",incomingDataInInterFace);
takeuz 10:fe1b976a3f9c 438 if(checkForLocalInterfaceCommand()) {
takeuz 10:fe1b976a3f9c 439 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 440 } else {
takeuz 11:853d12fe7ec0 441 serialPorts[interFace]->printf("?\r\n"); // here ahave to send "TTUF" // sleep command
takeuz 10:fe1b976a3f9c 442 }
takeuz 10:fe1b976a3f9c 443 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 444 } else {
takeuz 10:fe1b976a3f9c 445 break;
takeuz 10:fe1b976a3f9c 446 }
takeuz 10:fe1b976a3f9c 447 }
takeuz 10:fe1b976a3f9c 448 NVIC_SystemReset();
takeuz 10:fe1b976a3f9c 449 #if pushDebugData
takeuz 10:fe1b976a3f9c 450 serialPorts[debug]->puts("PowerSaveOff\r\n");
takeuz 10:fe1b976a3f9c 451 #endif
takeuz 10:fe1b976a3f9c 452 }
takeuz 10:fe1b976a3f9c 453 int checkForLocalInterfaceCommand()
takeuz 10:fe1b976a3f9c 454 {
takeuz 10:fe1b976a3f9c 455 serialPorts[debug]->puts("CommandDETECTED\r\n");
takeuz 10:fe1b976a3f9c 456 int i=0;
takeuz 10:fe1b976a3f9c 457 while(incomingDataInInterFace[i]!='\0') {
takeuz 10:fe1b976a3f9c 458 incomingDataInInterFace[i]=tolower(incomingDataInInterFace[i]);
takeuz 10:fe1b976a3f9c 459 i++;
takeuz 10:fe1b976a3f9c 460 }
takeuz 10:fe1b976a3f9c 461 char temp[2]= {incomingDataInInterFace[0],incomingDataInInterFace[1]};
takeuz 10:fe1b976a3f9c 462 if(strcmp(temp,"at")!=0) {
takeuz 10:fe1b976a3f9c 463 serialPorts[debug]->puts("Not Local Command\r\n");
takeuz 10:fe1b976a3f9c 464 return 0;
takeuz 10:fe1b976a3f9c 465 }
takeuz 10:fe1b976a3f9c 466 if(strcmp(incomingDataInInterFace,"atz")==0) {
takeuz 6:741524d110fe 467 NVIC_SystemReset();
takeuz 6:741524d110fe 468 }
takeuz 8:cc3cb620c3bd 469 if(strcmp(incomingDataInInterFace,"atrv")==0) {
takeuz 10:fe1b976a3f9c 470 serialPorts[interFace]->puts("12.1V\r\n");
takeuz 7:42cb4b01909c 471 clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
takeuz 7:42cb4b01909c 472 return 1;
takeuz 6:741524d110fe 473 }
takeuz 10:fe1b976a3f9c 474 serialPorts[interFace]->puts("?\r\n");
takeuz 7:42cb4b01909c 475 return 2;
takeuz 2:d58ef72b4ab0 476 }
takeuz 4:3e1e2b161403 477 void sendResponseToInterface()
takeuz 4:3e1e2b161403 478 {
takeuz 10:fe1b976a3f9c 479 serialPorts[interFace]->puts(incomingDataInOBD);
takeuz 10:fe1b976a3f9c 480 serialPorts[interFace]->puts("\r\n");
takeuz 7:42cb4b01909c 481 }
takeuz 7:42cb4b01909c 482
takeuz 7:42cb4b01909c 483 void clearInterfaceCommand()
takeuz 7:42cb4b01909c 484 {
takeuz 7:42cb4b01909c 485 incomingDataInInterFaceCounter=0;
takeuz 7:42cb4b01909c 486 interfaceCmdReceived=0;
takeuz 7:42cb4b01909c 487 clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
takeuz 7:42cb4b01909c 488 }
takeuz 7:42cb4b01909c 489
takeuz 7:42cb4b01909c 490 void clearOBDCommand()
takeuz 7:42cb4b01909c 491 {
takeuz 7:42cb4b01909c 492 clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
takeuz 8:cc3cb620c3bd 493 incomingDataInOBDCounter=0;
takeuz 8:cc3cb620c3bd 494 OBDCmdReceived=0;
takeuz 8:cc3cb620c3bd 495 }
takeuz 8:cc3cb620c3bd 496 int findStringLength(char *_cmd)
takeuz 8:cc3cb620c3bd 497 {
takeuz 8:cc3cb620c3bd 498 int i=0;
takeuz 8:cc3cb620c3bd 499 for(i=0; _cmd[i]!='\0'; ++i);
takeuz 8:cc3cb620c3bd 500 return i;
takeuz 8:cc3cb620c3bd 501 }
takeuz 8:cc3cb620c3bd 502 int initializeKLine()
takeuz 8:cc3cb620c3bd 503 {
takeuz 8:cc3cb620c3bd 504 #if sendDebugMessage
takeuz 10:fe1b976a3f9c 505 serialPorts[debug]->puts("K-Line Check\r\n");
takeuz 8:cc3cb620c3bd 506 #endif
takeuz 8:cc3cb620c3bd 507 *klineWakeup=1;
takeuz 8:cc3cb620c3bd 508 wait_ms(300);
takeuz 8:cc3cb620c3bd 509 *klineWakeup=0;
takeuz 8:cc3cb620c3bd 510 wait_ms(25);
takeuz 8:cc3cb620c3bd 511 *klineWakeup=1;
takeuz 8:cc3cb620c3bd 512 wait_ms(25);
takeuz 8:cc3cb620c3bd 513 delete klineWakeup;
takeuz 8:cc3cb620c3bd 514 kLineUART=new Serial(P0_28,P0_27);
takeuz 10:fe1b976a3f9c 515 clearOBDCommand();
takeuz 8:cc3cb620c3bd 516 kLineUART->baud(10400);
takeuz 8:cc3cb620c3bd 517 kLineUART->attach(&readKLine);
takeuz 8:cc3cb620c3bd 518 uint8_t crc=0;
takeuz 8:cc3cb620c3bd 519 crc=crc+0xC1;
takeuz 8:cc3cb620c3bd 520 crc=crc+0x33;
takeuz 8:cc3cb620c3bd 521 crc=crc+0xF1;
takeuz 8:cc3cb620c3bd 522 crc=crc+0x81;
takeuz 8:cc3cb620c3bd 523 kLineUART->putc(0xC1);
takeuz 8:cc3cb620c3bd 524 kLineUART->putc(0x33);
takeuz 8:cc3cb620c3bd 525 kLineUART->putc(0xF1);
takeuz 8:cc3cb620c3bd 526 kLineUART->putc(0x81);
takeuz 8:cc3cb620c3bd 527 kLineUART->putc(crc);
takeuz 10:fe1b976a3f9c 528 wait(2);
takeuz 10:fe1b976a3f9c 529 if(incomingDataInOBD[5]!='\0') {
takeuz 10:fe1b976a3f9c 530 currentOBDProtocol=protkLine14230;
takeuz 10:fe1b976a3f9c 531 }
takeuz 10:fe1b976a3f9c 532 kLineUART->attach(NULL);
takeuz 8:cc3cb620c3bd 533 return 0;
takeuz 7:42cb4b01909c 534 }
takeuz 7:42cb4b01909c 535
takeuz 8:cc3cb620c3bd 536 void setKLineMsg(char *_cmd)
takeuz 8:cc3cb620c3bd 537 {
takeuz 8:cc3cb620c3bd 538 for(int i=3; i<6; i++) {
takeuz 7:42cb4b01909c 539 kLineCmd[i]='\0';
takeuz 7:42cb4b01909c 540 }
takeuz 7:42cb4b01909c 541 int len=findStringLength(_cmd)/2;
takeuz 7:42cb4b01909c 542 char _mode[2]="";
takeuz 7:42cb4b01909c 543 uint8_t crc=0;
takeuz 8:cc3cb620c3bd 544 if(len==1) {
takeuz 7:42cb4b01909c 545 crc=crc+0xC1;
takeuz 7:42cb4b01909c 546 kLineCmd[0]=0xC1;
takeuz 7:42cb4b01909c 547 crc=crc+0x33;
takeuz 7:42cb4b01909c 548 kLineCmd[1]=(0x33);
takeuz 7:42cb4b01909c 549 crc=crc+0xF1;
takeuz 7:42cb4b01909c 550 kLineCmd[2]=(0xF1);
takeuz 7:42cb4b01909c 551 crc=crc+strtol((_cmd),NULL, 16);
takeuz 7:42cb4b01909c 552 kLineCmd[3]=(strtol((_cmd),NULL, 16));
takeuz 7:42cb4b01909c 553 kLineCmd[4]=(crc);
takeuz 8:cc3cb620c3bd 554 } else if(len==2) {
takeuz 7:42cb4b01909c 555 crc=crc+0xC2;
takeuz 7:42cb4b01909c 556 kLineCmd[0]=(0xC2);
takeuz 7:42cb4b01909c 557 crc=crc+0x33;
takeuz 7:42cb4b01909c 558 kLineCmd[1]=(0x33);
takeuz 7:42cb4b01909c 559 crc=crc+0xF1;
takeuz 7:42cb4b01909c 560 kLineCmd[2]=(0xF1);
takeuz 7:42cb4b01909c 561 for(int i=0; i<2; i++) {
takeuz 7:42cb4b01909c 562 _mode[i]=_cmd[i];
takeuz 7:42cb4b01909c 563 }
takeuz 7:42cb4b01909c 564 crc=crc+strtol(_mode,NULL, 16);
takeuz 7:42cb4b01909c 565 kLineCmd[3]=(strtol(_mode,NULL, 16));
takeuz 7:42cb4b01909c 566 crc=crc+(strtol((_cmd+2),NULL, 16));
takeuz 7:42cb4b01909c 567 kLineCmd[4]=(strtol((_cmd+2),NULL, 16));
takeuz 7:42cb4b01909c 568 kLineCmd[5]=(crc);
takeuz 7:42cb4b01909c 569 }
takeuz 7:42cb4b01909c 570 }
takeuz 8:cc3cb620c3bd 571
takeuz 8:cc3cb620c3bd 572 void sendCommandToVehicle(char *_cmd)
takeuz 8:cc3cb620c3bd 573 {
takeuz 7:42cb4b01909c 574 isVehicleCommandProcessing=1;
takeuz 7:42cb4b01909c 575 setKLineMsg(_cmd);
takeuz 10:fe1b976a3f9c 576 clearOBDCommand();
takeuz 10:fe1b976a3f9c 577 flushBuffer();
takeuz 10:fe1b976a3f9c 578 kLineUART->attach(&readKLine);
takeuz 8:cc3cb620c3bd 579 for(kLineCommandCount=0; kLineCommandCount<6; kLineCommandCount++) {
takeuz 8:cc3cb620c3bd 580 if(kLineCmd[kLineCommandCount]=='\0') {
takeuz 7:42cb4b01909c 581 break;
takeuz 7:42cb4b01909c 582 }
takeuz 7:42cb4b01909c 583 kLineUART->putc(kLineCmd[kLineCommandCount]);
takeuz 7:42cb4b01909c 584 }
takeuz 11:853d12fe7ec0 585 wait_ms(1000);
takeuz 10:fe1b976a3f9c 586 serialPorts[debug]->printf("Got Data\r\n");
takeuz 7:42cb4b01909c 587 //have to check given and received commands are same.
takeuz 10:fe1b976a3f9c 588 //if it is same we have to read the response else just flush buffer and send the command again
takeuz 10:fe1b976a3f9c 589 for(int i=0; i<strlen(incomingDataInOBD); i++) {
takeuz 10:fe1b976a3f9c 590 serialPorts[debug]->printf("%02X",incomingDataInOBD[i]);
takeuz 10:fe1b976a3f9c 591 }
takeuz 10:fe1b976a3f9c 592 serialPorts[debug]->printf("\r\n");
takeuz 10:fe1b976a3f9c 593 kLineUART->attach(NULL);
takeuz 10:fe1b976a3f9c 594 strcpy(incomingDataInOBD,(incomingDataInOBD+kLineCommandCount)); //cut request from hole response
takeuz 10:fe1b976a3f9c 595 for(int i=0; i<strlen(incomingDataInOBD); i++) {
takeuz 10:fe1b976a3f9c 596 serialPorts[debug]->printf("%02X",incomingDataInOBD[i]);
takeuz 10:fe1b976a3f9c 597 }
takeuz 10:fe1b976a3f9c 598 serialPorts[debug]->printf("\r\n");
takeuz 10:fe1b976a3f9c 599 serialPorts[debug]->printf("%02X\r\n",incomingDataInOBD[0]);
takeuz 10:fe1b976a3f9c 600 int len=(incomingDataInOBD[0]%16);//convert first char to integer and find kength of the response
takeuz 10:fe1b976a3f9c 601 serialPorts[debug]->printf("Response Length : %d\r\n",len);
takeuz 10:fe1b976a3f9c 602 serialPorts[debug]->printf("Response Length : %d\r\n",strlen(incomingDataInOBD));
takeuz 10:fe1b976a3f9c 603 strcpy(incomingDataInOBD,(incomingDataInOBD+3)); //again cut first three byte
takeuz 10:fe1b976a3f9c 604 serialPorts[debug]->printf("Response Length : %d\r\n",strlen(incomingDataInOBD));
takeuz 10:fe1b976a3f9c 605 int obdDataLength=strlen(incomingDataInOBD);
takeuz 10:fe1b976a3f9c 606 int count=0;
takeuz 10:fe1b976a3f9c 607 while(count<len) {
takeuz 10:fe1b976a3f9c 608 if(_powerSaveFlag) {
takeuz 11:853d12fe7ec0 609 serialPorts[interFace]->printf("+TUAWAKE");
takeuz 10:fe1b976a3f9c 610 _powerSaveFlag=0;
takeuz 10:fe1b976a3f9c 611 break;
takeuz 10:fe1b976a3f9c 612 }
takeuz 11:853d12fe7ec0 613 if(obdDataLength>0) { // have to recheck this condition
takeuz 10:fe1b976a3f9c 614 serialPorts[interFace]->printf("%02X",incomingDataInOBD[count]);
takeuz 10:fe1b976a3f9c 615 serialPorts[debug]->printf("%02X",incomingDataInOBD[count]);
takeuz 10:fe1b976a3f9c 616 } else {
takeuz 10:fe1b976a3f9c 617 serialPorts[interFace]->printf("%02X",0x00);
takeuz 10:fe1b976a3f9c 618 serialPorts[debug]->printf("%02X",0x00);
takeuz 10:fe1b976a3f9c 619 }
takeuz 10:fe1b976a3f9c 620 count++;
takeuz 10:fe1b976a3f9c 621 obdDataLength--;
takeuz 10:fe1b976a3f9c 622 }
takeuz 11:853d12fe7ec0 623
takeuz 11:853d12fe7ec0 624
takeuz 11:853d12fe7ec0 625 //????? suren: Have to recheck the following lines
takeuz 10:fe1b976a3f9c 626 if(len==0) {
takeuz 10:fe1b976a3f9c 627 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 628 serialPorts[interFace]->printf("?");
takeuz 10:fe1b976a3f9c 629 serialPorts[debug]->printf("?");
takeuz 10:fe1b976a3f9c 630 }
takeuz 10:fe1b976a3f9c 631 }
takeuz 10:fe1b976a3f9c 632 if(!_powerSaveFlag) {
takeuz 10:fe1b976a3f9c 633 serialPorts[interFace]->printf("\r\n");
takeuz 10:fe1b976a3f9c 634 serialPorts[debug]->printf("\r\n");
takeuz 10:fe1b976a3f9c 635 serialPorts[debug]->printf("Done\r\n");
takeuz 10:fe1b976a3f9c 636 }
takeuz 11:853d12fe7ec0 637 wait_ms(300);
takeuz 8:cc3cb620c3bd 638 clearInterfaceCommand();
takeuz 10:fe1b976a3f9c 639 clearOBDCommand();
takeuz 7:42cb4b01909c 640 isVehicleCommandProcessing=0;
takeuz 7:42cb4b01909c 641 }
takeuz 7:42cb4b01909c 642
takeuz 8:cc3cb620c3bd 643 void flushBuffer()
takeuz 8:cc3cb620c3bd 644 {
takeuz 8:cc3cb620c3bd 645 while(kLineUART->readable()) {
takeuz 7:42cb4b01909c 646 kLineUART->getc();
takeuz 7:42cb4b01909c 647 }
takeuz 7:42cb4b01909c 648 }
takeuz 8:cc3cb620c3bd 649 void kLinekeepalive()
takeuz 8:cc3cb620c3bd 650 {
takeuz 10:fe1b976a3f9c 651 serialPorts[debug]->printf("KeepAlive ENT\r\n");
takeuz 8:cc3cb620c3bd 652 kLineUART->putc(0xc1);
takeuz 8:cc3cb620c3bd 653 kLineUART->putc(0x33);
takeuz 8:cc3cb620c3bd 654 kLineUART->putc(0xf1);
takeuz 8:cc3cb620c3bd 655 kLineUART->putc(0x3e);
takeuz 8:cc3cb620c3bd 656 kLineUART->putc(0x23);
takeuz 10:fe1b976a3f9c 657 serialPorts[debug]->printf("EXT\r\n");
takeuz 8:cc3cb620c3bd 658 }
takeuz 10:fe1b976a3f9c 659 char tolower(unsigned char ch)
takeuz 10:fe1b976a3f9c 660 {
takeuz 8:cc3cb620c3bd 661 if (ch >= 'A' && ch <= 'Z')
takeuz 8:cc3cb620c3bd 662 ch = 'a' + (ch - 'A');
takeuz 8:cc3cb620c3bd 663 return ch;
takeuz 10:fe1b976a3f9c 664 }
takeuz 10:fe1b976a3f9c 665
takeuz 10:fe1b976a3f9c 666 int checkCarStatus()
takeuz 10:fe1b976a3f9c 667 {
takeuz 10:fe1b976a3f9c 668 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
takeuz 10:fe1b976a3f9c 669 LPC_SWM->PINASSIGN0 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 670 LPC_SWM->PINASSIGN1 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 671 LPC_SWM->PINASSIGN2 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 672 LPC_SWM->PINASSIGN3 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 673 LPC_SWM->PINASSIGN4 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 674 LPC_SWM->PINASSIGN5 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 675 LPC_SWM->PINASSIGN8 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 676 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
takeuz 10:fe1b976a3f9c 677 DigitalOut kLine(P0_27);
takeuz 10:fe1b976a3f9c 678 DigitalOut canLine(P0_22);
takeuz 10:fe1b976a3f9c 679 if(kLine.read()) {
takeuz 10:fe1b976a3f9c 680 return 1;
takeuz 10:fe1b976a3f9c 681 }
takeuz 10:fe1b976a3f9c 682 if(canLine.read()) {
takeuz 10:fe1b976a3f9c 683 return 2;
takeuz 10:fe1b976a3f9c 684 }
takeuz 10:fe1b976a3f9c 685 return 0;
takeuz 10:fe1b976a3f9c 686 }
takeuz 10:fe1b976a3f9c 687
takeuz 10:fe1b976a3f9c 688 void initPins()
takeuz 10:fe1b976a3f9c 689 {
takeuz 10:fe1b976a3f9c 690 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<7);
takeuz 10:fe1b976a3f9c 691 LPC_SWM->PINASSIGN0 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 692 LPC_SWM->PINASSIGN1 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 693 LPC_SWM->PINASSIGN2 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 694 LPC_SWM->PINASSIGN3 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 695 LPC_SWM->PINASSIGN4 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 696 LPC_SWM->PINASSIGN5 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 697 LPC_SWM->PINASSIGN8 = 0xffffffffUL;
takeuz 10:fe1b976a3f9c 698 // LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
takeuz 10:fe1b976a3f9c 699 serialPorts[0] = new Serial(P0_11,P0_13);
takeuz 10:fe1b976a3f9c 700 serialPorts[1] = new Serial(P0_15,P0_14);
takeuz 11:853d12fe7ec0 701 /*serialPorts[0] = new Serial(P0_16,P0_12);
takeuz 11:853d12fe7ec0 702 serialPorts[1] = new Serial(USBTX,USBRX);*/
takeuz 10:fe1b976a3f9c 703 klineWakeup = new DigitalOut(P0_28);
takeuz 10:fe1b976a3f9c 704 canBus = new CAN(P0_22, P0_23);
takeuz 10:fe1b976a3f9c 705 canDisableKey=new DigitalOut(P0_0,0);
takeuz 10:fe1b976a3f9c 706 }