
Interior CAN bus monitoring of Dodge/Chrysler vehicles
main.cpp@1:d8284a72815e, 2011-02-01 (annotated)
- Committer:
- rtgree01
- Date:
- Tue Feb 01 20:21:48 2011 +0000
- Revision:
- 1:d8284a72815e
- Parent:
- 0:5f0cd7bd1389
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rtgree01 | 0:5f0cd7bd1389 | 1 | #include "mbed.h" |
rtgree01 | 0:5f0cd7bd1389 | 2 | #include "CAN.h" |
rtgree01 | 0:5f0cd7bd1389 | 3 | |
rtgree01 | 0:5f0cd7bd1389 | 4 | DigitalOut led1(LED1); |
rtgree01 | 0:5f0cd7bd1389 | 5 | DigitalOut led2(LED2); |
rtgree01 | 0:5f0cd7bd1389 | 6 | DigitalOut led3(LED3); |
rtgree01 | 1:d8284a72815e | 7 | |
rtgree01 | 1:d8284a72815e | 8 | Serial pc(USBTX, USBRX); // tx, rx default settings (9600 8N1) |
rtgree01 | 0:5f0cd7bd1389 | 9 | |
rtgree01 | 0:5f0cd7bd1389 | 10 | |
rtgree01 | 1:d8284a72815e | 11 | CAN can1(p9, p10); // CAN1 uses pins 9 and 10 (tx, rx) |
rtgree01 | 1:d8284a72815e | 12 | CAN can2(p30, p29); // CAN2 uses pins 30 and 29 (tx, rx) |
rtgree01 | 0:5f0cd7bd1389 | 13 | |
rtgree01 | 1:d8284a72815e | 14 | DigitalOut can1_SleepMode(p11); // Use pin 11 to control the sleep mode of can1 |
rtgree01 | 1:d8284a72815e | 15 | DigitalOut can2_SleepMode(p28); // Use pin 28 to control the sleep mode of can2 |
rtgree01 | 0:5f0cd7bd1389 | 16 | |
rtgree01 | 0:5f0cd7bd1389 | 17 | CANMessage can_MsgTx; |
rtgree01 | 0:5f0cd7bd1389 | 18 | CANMessage can_MsgRx; |
rtgree01 | 1:d8284a72815e | 19 | |
rtgree01 | 0:5f0cd7bd1389 | 20 | char msg[14]; |
rtgree01 | 1:d8284a72815e | 21 | char counter = 0; |
rtgree01 | 0:5f0cd7bd1389 | 22 | |
rtgree01 | 0:5f0cd7bd1389 | 23 | int main() |
rtgree01 | 0:5f0cd7bd1389 | 24 | { |
rtgree01 | 1:d8284a72815e | 25 | pc.baud(115200); // change serial interface to pc to 115200, 8N1 |
rtgree01 | 0:5f0cd7bd1389 | 26 | |
rtgree01 | 1:d8284a72815e | 27 | can1.frequency(500000); // many frequencies are supported |
rtgree01 | 0:5f0cd7bd1389 | 28 | |
rtgree01 | 1:d8284a72815e | 29 | can2.frequency(83000); // last time I checked 83000 was not, so.... |
rtgree01 | 1:d8284a72815e | 30 | LPC_CAN2->BTR = 0x52001C; // I had to manually change the "registers" that control the frequency |
rtgree01 | 1:d8284a72815e | 31 | // the registers are coded weird, so mbed tried to hide that |
rtgree01 | 1:d8284a72815e | 32 | // so usually you only need to issue the can2.frequency() command |
rtgree01 | 0:5f0cd7bd1389 | 33 | |
rtgree01 | 1:d8284a72815e | 34 | can1_SleepMode = 0; // Set the Sleep Mode to 0 or low or false... this makes sure the transceiver is on |
rtgree01 | 1:d8284a72815e | 35 | can2_SleepMode = 0; // Set the Sleep Mode to 0 or low or false... this makes sure the transceiver is on |
rtgree01 | 0:5f0cd7bd1389 | 36 | |
rtgree01 | 0:5f0cd7bd1389 | 37 | while (1) |
rtgree01 | 0:5f0cd7bd1389 | 38 | { |
rtgree01 | 1:d8284a72815e | 39 | // send received messages to the pc via serial line |
rtgree01 | 0:5f0cd7bd1389 | 40 | if (can2.read(can_MsgRx)) |
rtgree01 | 0:5f0cd7bd1389 | 41 | { |
rtgree01 | 1:d8284a72815e | 42 | // When sending a message to the PC, send a couple of sync bytes so that when the PC looks, it finds the start of a message |
rtgree01 | 1:d8284a72815e | 43 | // putc will write a character/byte to the pc, getc reads a character/byte from the pc |
rtgree01 | 0:5f0cd7bd1389 | 44 | pc.putc(0xff); |
rtgree01 | 0:5f0cd7bd1389 | 45 | pc.putc(0x00); |
rtgree01 | 0:5f0cd7bd1389 | 46 | pc.putc(0xff); |
rtgree01 | 0:5f0cd7bd1389 | 47 | |
rtgree01 | 1:d8284a72815e | 48 | // Now, start sending the data of the message |
rtgree01 | 1:d8284a72815e | 49 | |
rtgree01 | 1:d8284a72815e | 50 | // The id is 2 bytes long, so I have to break it up |
rtgree01 | 1:d8284a72815e | 51 | pc.putc((can_MsgRx.id & 0xff00) >> 8); // select the first byte... and write it |
rtgree01 | 1:d8284a72815e | 52 | pc.putc((can_MsgRx.id & 0x00ff)); // select the second byte... and write it |
rtgree01 | 1:d8284a72815e | 53 | pc.putc(can_MsgRx.len); // send the length of the can message |
rtgree01 | 0:5f0cd7bd1389 | 54 | for (int i = 0; i < can_MsgRx.len; i++) |
rtgree01 | 0:5f0cd7bd1389 | 55 | { |
rtgree01 | 1:d8284a72815e | 56 | pc.putc(can_MsgRx.data[i]); // send each of the bytes... from the first byte to the last byte. |
rtgree01 | 0:5f0cd7bd1389 | 57 | } |
rtgree01 | 0:5f0cd7bd1389 | 58 | |
rtgree01 | 0:5f0cd7bd1389 | 59 | // any incoming message: toggle led2 |
rtgree01 | 0:5f0cd7bd1389 | 60 | led2 = !led2; |
rtgree01 | 0:5f0cd7bd1389 | 61 | } |
rtgree01 | 0:5f0cd7bd1389 | 62 | |
rtgree01 | 1:d8284a72815e | 63 | // send received messages to the pc via serial line |
rtgree01 | 0:5f0cd7bd1389 | 64 | if (can1.read(can_MsgRx)) |
rtgree01 | 0:5f0cd7bd1389 | 65 | { |
rtgree01 | 1:d8284a72815e | 66 | // When sending a message to the PC, send a couple of sync bytes so that when the PC looks, it finds the start of a message |
rtgree01 | 1:d8284a72815e | 67 | // putc will write a character/byte to the pc, getc reads a character/byte from the pc |
rtgree01 | 0:5f0cd7bd1389 | 68 | pc.putc(0xff); |
rtgree01 | 0:5f0cd7bd1389 | 69 | pc.putc(0x00); |
rtgree01 | 0:5f0cd7bd1389 | 70 | pc.putc(0xff); |
rtgree01 | 0:5f0cd7bd1389 | 71 | |
rtgree01 | 1:d8284a72815e | 72 | // Now, start sending the data of the message |
rtgree01 | 1:d8284a72815e | 73 | |
rtgree01 | 1:d8284a72815e | 74 | // The id is 2 bytes long, so I have to break it up |
rtgree01 | 1:d8284a72815e | 75 | pc.putc((can_MsgRx.id & 0xff00) >> 8); // select the first byte... and write it |
rtgree01 | 1:d8284a72815e | 76 | pc.putc((can_MsgRx.id & 0x00ff)); // select the second byte... and write it |
rtgree01 | 1:d8284a72815e | 77 | pc.putc(can_MsgRx.len); // send the length of the can message |
rtgree01 | 0:5f0cd7bd1389 | 78 | for (int i = 0; i < can_MsgRx.len; i++) |
rtgree01 | 0:5f0cd7bd1389 | 79 | { |
rtgree01 | 1:d8284a72815e | 80 | pc.putc(can_MsgRx.data[i]); // send each of the bytes... from the first byte to the last byte. |
rtgree01 | 0:5f0cd7bd1389 | 81 | } |
rtgree01 | 0:5f0cd7bd1389 | 82 | |
rtgree01 | 1:d8284a72815e | 83 | // any incoming message: toggle led3 |
rtgree01 | 0:5f0cd7bd1389 | 84 | led3 = !led3; |
rtgree01 | 0:5f0cd7bd1389 | 85 | } |
rtgree01 | 0:5f0cd7bd1389 | 86 | |
rtgree01 | 1:d8284a72815e | 87 | // repeat this section of code if there is any bytes coming from the PC |
rtgree01 | 0:5f0cd7bd1389 | 88 | while (pc.readable()) |
rtgree01 | 0:5f0cd7bd1389 | 89 | { |
rtgree01 | 1:d8284a72815e | 90 | msg[counter] = pc.getc(); // read the byte from the pc, and put it into a buffer at the next available position |
rtgree01 | 1:d8284a72815e | 91 | counter++; // update the next available position |
rtgree01 | 0:5f0cd7bd1389 | 92 | |
rtgree01 | 1:d8284a72815e | 93 | if (counter == 14) // if we received a full message (14 bytes) from the PC, then break out of the loop |
rtgree01 | 0:5f0cd7bd1389 | 94 | { |
rtgree01 | 0:5f0cd7bd1389 | 95 | break; |
rtgree01 | 0:5f0cd7bd1389 | 96 | } |
rtgree01 | 0:5f0cd7bd1389 | 97 | |
rtgree01 | 1:d8284a72815e | 98 | if (counter == 3) // if we have received the first 3 bytes, check to see if they are the sync |
rtgree01 | 0:5f0cd7bd1389 | 99 | { |
rtgree01 | 0:5f0cd7bd1389 | 100 | if ((msg[0] != 0xff) || (msg[1] != 0x00) || (msg[2] != 0xff)) |
rtgree01 | 0:5f0cd7bd1389 | 101 | { |
rtgree01 | 1:d8284a72815e | 102 | counter = 2; // if they aren't the sync, skip the first byte by... |
rtgree01 | 1:d8284a72815e | 103 | msg[0] = msg[1]; // moving the second and third bytes into the first and second spots |
rtgree01 | 0:5f0cd7bd1389 | 104 | msg[1] = msg[2]; |
rtgree01 | 0:5f0cd7bd1389 | 105 | } |
rtgree01 | 0:5f0cd7bd1389 | 106 | } |
rtgree01 | 1:d8284a72815e | 107 | |
rtgree01 | 1:d8284a72815e | 108 | // Only stop repeating if all 14 bytes have been received from the pc, or if the pc isn't sending any more bytes |
rtgree01 | 0:5f0cd7bd1389 | 109 | } |
rtgree01 | 0:5f0cd7bd1389 | 110 | |
rtgree01 | 1:d8284a72815e | 111 | // So... if the pc sent a full message |
rtgree01 | 0:5f0cd7bd1389 | 112 | if (counter > 13) |
rtgree01 | 0:5f0cd7bd1389 | 113 | { |
rtgree01 | 1:d8284a72815e | 114 | counter = 0; // reset the buffer |
rtgree01 | 0:5f0cd7bd1389 | 115 | |
rtgree01 | 1:d8284a72815e | 116 | // doublecheck to see if the sync was in the pc message |
rtgree01 | 0:5f0cd7bd1389 | 117 | if ((msg[0] == 0xff) && (msg[1] == 0x00) && (msg[2] == 0xff)) |
rtgree01 | 0:5f0cd7bd1389 | 118 | { |
rtgree01 | 1:d8284a72815e | 119 | // Start filling in the data into the can tx message... |
rtgree01 | 1:d8284a72815e | 120 | can_MsgTx.id = (((short)msg[3]) << 8) + msg[4]; // make the id from the 2 bytes |
rtgree01 | 1:d8284a72815e | 121 | can_MsgTx.len = msg[5]; // set the size of the message |
rtgree01 | 1:d8284a72815e | 122 | for (int i = 0; i < 8; i++) // fill in all 8 bytes |
rtgree01 | 0:5f0cd7bd1389 | 123 | { |
rtgree01 | 1:d8284a72815e | 124 | can_MsgTx.data[i] = msg[6 + i]; // make sure to use the right bytes offset from the start |
rtgree01 | 0:5f0cd7bd1389 | 125 | } |
rtgree01 | 0:5f0cd7bd1389 | 126 | |
rtgree01 | 1:d8284a72815e | 127 | led1 = !led1; // toggle the led to show that we sending a can message on the second CAN transceiver |
rtgree01 | 1:d8284a72815e | 128 | can2.write(can_MsgTx); //send the message |
rtgree01 | 1:d8284a72815e | 129 | |
rtgree01 | 1:d8284a72815e | 130 | memset(msg, 0, 14); // clear out the pc interface's buffer |
rtgree01 | 0:5f0cd7bd1389 | 131 | } |
rtgree01 | 0:5f0cd7bd1389 | 132 | } |
rtgree01 | 0:5f0cd7bd1389 | 133 | } |
rtgree01 | 0:5f0cd7bd1389 | 134 | } |