Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of OBD21121 by
Diff: main.cpp
- Revision:
- 4:3e1e2b161403
- Parent:
- 2:d58ef72b4ab0
- Child:
- 5:53d84cbf9890
--- a/main.cpp Sat Feb 25 07:07:34 2017 +0000 +++ b/main.cpp Mon Feb 27 07:45:55 2017 +0000 @@ -13,15 +13,17 @@ #define sendDebugMessage 1 #define pushDebugData 1 -const int canSpeeds[3]={CANSPEED_500,CANSPEED_250,CANSPEED_125}; +const int canSpeeds[3]= {CANSPEED_125,CANSPEED_250,CANSPEED_500}; DigitalOut myled(LED2); -Serial serialPorts[3] = {Serial(P0_26,P0_6),Serial(P0_22,P0_23),Serial(USBTX,USBRX)}; +DigitalOut canDriverPowerSave(P0_4); +InterruptIn canActivity(P0_5); +RawSerial serialPorts[3] = {RawSerial(P0_26,P0_6),RawSerial(P0_22,P0_23),RawSerial(USBTX,USBRX)}; //Serial pc(P0_9,P0_29); CAN canBus(P0_24, P0_25); -//AnalogIn batteryVoltage(P0_20); +AnalogIn batteryVoltage(P0_6); //DigitalOut indicatorLEDs[3] = {P0_22,P0_23,P0_6}; @@ -77,17 +79,21 @@ int lastOBDCmdRcvdTime=0; int lastInterfaceCmdRcvdTime=0; +void activityOnCan() +{ +} + void readInterface() { char c; while(serialPorts[interFace].readable()) { lastInterfaceCmdRcvdTime=0; c = serialPorts[interFace].getc(); - if(incomingDataInInterFaceCounter<incomingDataInInterFaceSize){ + if(incomingDataInInterFaceCounter<incomingDataInInterFaceSize) { incomingDataInInterFace[incomingDataInInterFaceCounter] = c; } incomingDataInInterFaceCounter++; - + if(c=='\n') { interfaceCmdReceived=1; } @@ -100,7 +106,7 @@ while(serialPorts[kLine].readable()) { lastOBDCmdRcvdTime=0; c = serialPorts[kLine].getc(); - if(incomingDataInOBDCounter<incomingDataInOBDSize){ + if(incomingDataInOBDCounter<incomingDataInOBDSize) { incomingDataInOBD[incomingDataInOBDCounter]=c; } incomingDataInOBDCounter++; @@ -113,11 +119,12 @@ -void canReader(){ +void canReader() +{ if (canBus.read(can_MsgRx)) { - //Setting + //Setting lastOBDCmdRcvdTime=0; - + if ((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == ENGINE_RPM)) { serialPorts[debug].puts("HRHK"); for (int i = 0; i < (int)can_MsgRx.len; i++) { @@ -127,102 +134,106 @@ } } -void timeCounterFunction(){ +void timeCounterFunction() +{ lastInterfaceCmdRcvdTime++; lastOBDCmdRcvdTime++; //IF timout is over certain amount just shutdown - - + + } -int main() { +int main() +{ - serialPorts[debug].puts("f2114\r\n"); + serialPorts[debug].puts("f2117\r\n"); //Initialization incomingDataInInterFace=(char *)malloc(incomingDataInInterFaceSize*sizeof(char)); incomingDataInOBD=(char *)malloc(incomingDataInOBDSize*sizeof(char)); clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize); - clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize); - + clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize); + + + canDriverPowerSave=0; + timeCounter.attach(&timeCounterFunction, 1.0); serialPorts[interFace].attach(&readInterface); serialPorts[interFace].puts("ELM327 v1.0\r\n"); - + while(1) { - if(!currentOBDProtocol){ - #if sendDebugMessage - serialPorts[debug].puts("Going to figure protocol\r\n"); - #endif + if(!currentOBDProtocol) { +#if sendDebugMessage + serialPorts[debug].puts("Going to figure protocol\r\n"); +#endif serialPorts[kLine].attach(NULL); canBus.attach(NULL); serialPorts[interFace].puts("Searching...\r\n"); serialPorts[debug].puts("Searching...\r\n"); checkCommunicationProtocol(); - if(currentOBDProtocol==protkLine){ + if(currentOBDProtocol==protkLine) { serialPorts[kLine].attach(&readKLine); - } - if(currentOBDProtocol==protCan){ + } + if(currentOBDProtocol==protCan) { canBus.attach(&canReader); - } + } //??Shall we do somehitng when receive a specific commnad - + //?? Do this !Unknown only once. - if(!currentOBDProtocol){ + if(!currentOBDProtocol) { serialPorts[interFace].puts("!UnknownProtocol\r\n"); - #if sendDebugMessage - serialPorts[debug].puts("!Unknownprotocol\r\n"); - #endif +#if sendDebugMessage + serialPorts[debug].puts("!Unknownprotocol\r\n"); +#endif //Goto Sleep? } } - if(interfaceCmdReceived){ + if(interfaceCmdReceived) { checkForLocalInterfaceCommand(); } - if(interfaceCmdReceived){ + if(interfaceCmdReceived) { OBDTimer.reset(); OBDCmdReceived=0; sendCommand(); OBDTimer.start(); - while(OBDTimer.read_ms() < waitTimeForOBDComm){ - if(OBDCmdReceived){ + while(OBDTimer.read_ms() < waitTimeForOBDComm) { + if(OBDCmdReceived) { sendResponseToInterface(); break; } } OBDTimer.stop(); - if(!OBDCmdReceived){ + if(!OBDCmdReceived) { serialPorts[interFace].puts("?\r\n"); } OBDCmdReceived=0; interfaceCmdReceived=0; - //issueRegularCommands(); + //issueRegularCommands(); } myled=0; sleepProcess(); - if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ){ + if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) { powerSaveMode(); } myled=1; - - } } -void checkCommunicationProtocol(){ - #if sendDebugMessage - serialPorts[debug].puts("check Communication protocal ENT\r\n"); - #endif +void checkCommunicationProtocol() +{ +#if sendDebugMessage + serialPorts[debug].puts("check Communication protocal ENT\r\n"); +#endif int i=0; - do{ - #if sendDebugMessage + do { +#if sendDebugMessage serialPorts[debug].puts("CanBusCheck\r\n"); - #endif +#endif canBus.reset(); clearCanMessage(); canBus.frequency(canSpeeds[i]); @@ -230,27 +241,27 @@ OBDTimer.start(); OBDCmdReceived=0; if (canBus.write(CANMessage(PID_REQUEST, can_msg, 8))) { - #if sendDebugMessage +#if sendDebugMessage serialPorts[debug].printf("OBDTIMER:%d\r\n",OBDTimer.read_ms()); - #endif - while(OBDTimer.read_ms() < waitTimeForOBDComm){ - if(OBDCmdReceived){ +#endif + while(OBDTimer.read_ms() < waitTimeForOBDComm) { + if(OBDCmdReceived) { currentOBDProtocol=protCan; - canFrequency=i; - #if sendDebugMessage - serialPorts[debug].puts("check Communication protocal SET\r\n"); - #endif - return; + canFrequency=i; +#if sendDebugMessage + serialPorts[debug].puts("check Communication protocal SET\r\n"); +#endif + return; } } } wait_ms(200); - i++; - }while(i<3); + i++; + } while(i<3); OBDTimer.stop(); - #if sendDebugMessage +#if sendDebugMessage serialPorts[debug].puts("check Communication protocal EXT\r\n"); - #endif +#endif } void clearMemoryLocation( char *base, int _size) @@ -259,92 +270,120 @@ base[forCount]='\0'; } } -void clearCanMessage(){ - can_msg[0] = 0x02; - can_msg[1] = 0x01; - can_msg[2] = 0; - can_msg[3] = 0; - can_msg[4] = 0; - can_msg[5] = 0; - can_msg[6] = 0; - can_msg[7] = 0; +void clearCanMessage() +{ + can_msg[0] = 0x02; + can_msg[1] = 0x01; + can_msg[2] = 0; + can_msg[3] = 0; + can_msg[4] = 0; + can_msg[5] = 0; + can_msg[6] = 0; + can_msg[7] = 0; } -void sendCommand(){ +void sendCommand() +{ } -void sleepProcess() { - +void sleepProcess() +{ + #if pushDebugData serialPorts[debug].puts("SPe\r\n"); #endif OBDTimer.reset(); OBDTimer.start(); - while(OBDTimer.read_ms()<sleepTime && !interfaceCmdReceived){ + while(OBDTimer.read_ms()<sleepTime && !interfaceCmdReceived) { wait_ms(50); } OBDTimer.stop(); } -void powerSaveMode(){ +void powerSaveMode() +{ + //Putting CAn Driver on low power + //Detect can Activity + if(currentOBDProtocol==protCan) { + canDriverPowerSave=1; + } + #if pushDebugData serialPorts[debug].puts("PowerSaveOn\r\n"); #endif //Remove pin allocaitons - + + //Detect can Activity On Acnbus + if(currentOBDProtocol==protCan) { + canActivity.rise(&activityOnCan); + canActivity.fall(&activityOnCan); + } + + wait(20); - + if(currentOBDProtocol==protCan) { + canDriverPowerSave=0; + } + //Make pin allocaitons + + //Detect can Activity On Acnbus + if(currentOBDProtocol==protCan) { + canActivity.rise(NULL); + canActivity.fall(NULL); + } + lastOBDCmdRcvdTime=0; lastInterfaceCmdRcvdTime=0; - + #if pushDebugData serialPorts[debug].puts("PowerSaveOff\r\n"); #endif } -void tprintf( char* str, int num){ +void tprintf( char* str, int num) +{ int i2aCounter=0; - if (num == 0) - { + if (num == 0) { str[i2aCounter++] = '0'; str[i2aCounter] = '\0'; return; } int tempNum=num; - while(tempNum!=0){ + while(tempNum!=0) { tempNum/=10; i2aCounter++; } str[i2aCounter]='\0'; i2aCounter--; - while(i2aCounter>=0){ + while(i2aCounter>=0) { str[i2aCounter--] = (num % 10) + '0'; num = num/10; } return; } -void checkForLocalInterfaceCommand(){ - if(strstr(incomingDataInInterFace,"AT")==0){ - return; - } - if(strstr(incomingDataInInterFace,"ATZ")>0){ - NVIC_SystemReset(); - } - if(strstr(incomingDataInInterFace,"ATRV")>0){ - serialPorts[interFace].puts("Gointoreport bat vol\r\n"); - interfaceCmdReceived=0; - return; - } +void checkForLocalInterfaceCommand() +{ +// if(strstr(incomingDataInInterFace,"AT")==0){ +// return; +// } +// if(strstr(incomingDataInInterFace,"ATZ")>0){ +// NVIC_SystemReset(); +// } +// if(strstr(incomingDataInInterFace,"ATRV")>0){ +// serialPorts[interFace].puts("Gointoreport bat vol\r\n"); +// interfaceCmdReceived=0; +// return; +// } serialPorts[interFace].puts("?\r\n"); - -} -void sendResponseToInterface(){ } +void sendResponseToInterface() +{ +}