Open OBC / e36obd

Dependents:   obdtest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Bus.cpp Source File

Bus.cpp

00001 #include "Bus.h"
00002 
00003 Bus::Bus(PinName tx, PinName rx) :
00004     Serial(tx, rx)
00005 {
00006     txPin = tx;
00007     rxPin = rx;
00008 }
00009 
00010 //send data out to the bus and make sure the echo comes back ok
00011 int Bus::send(char* data, int length)
00012 {
00013     int count = 0;
00014     while(length--)
00015     {
00016         while(!writeable());
00017         putc(*data);
00018         
00019         //wait for the echo and verify it
00020         int timeout;
00021         for(timeout = 10000; timeout; timeout--)
00022         {
00023             if(!readable())
00024                 continue;
00025             char c = getc();
00026             if(c != *data)
00027                 dbg.printf("interface error/collision: %x != %x\r\n", c, *data);
00028             break;
00029         }
00030         if(!timeout)
00031             dbg.printf("echo timeout - check interface hardware\r\n");
00032             
00033         data++;
00034         count++;
00035         //might need to sleep between bytes for some modules (abs?)
00036     }
00037     return count;
00038 }
00039 
00040 //gets everything in the uart buffer
00041 int Bus::get(char* data, int length)
00042 {
00043     int count = 0;
00044     while(readable() && length--)
00045     {
00046         *data++ = getc();
00047         count++;
00048     }
00049     return count;
00050 }
00051 
00052 //verify that we can assert the bus
00053 //a failure here almost certainly points to a problem with the mbed-vehicle interface circuitry
00054 //this should pass regardless of whether the interface is connected to the vehicle bus
00055 bool Bus::test()
00056 {
00057     //write some data to the bus
00058     while(!writeable());
00059     putc(0x55);
00060     
00061     //wait for the echo and verify it
00062     int timeout;
00063     for(timeout = 10000; timeout; timeout--)
00064     {
00065         if(!readable())
00066             continue;
00067         char c = getc();
00068         if(c == 0x55)
00069             return 1;
00070     }
00071     return 0;
00072 }