
Interior CAN bus monitoring of Dodge/Chrysler vehicles
main.cpp@0:5f0cd7bd1389, 2011-01-31 (annotated)
- Committer:
- rtgree01
- Date:
- Mon Jan 31 05:10:57 2011 +0000
- Revision:
- 0:5f0cd7bd1389
- Child:
- 1:d8284a72815e
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 | Ticker ticker; |
rtgree01 | 0:5f0cd7bd1389 | 5 | DigitalOut led1(LED1); |
rtgree01 | 0:5f0cd7bd1389 | 6 | DigitalOut led2(LED2); |
rtgree01 | 0:5f0cd7bd1389 | 7 | DigitalOut led3(LED3); |
rtgree01 | 0:5f0cd7bd1389 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
rtgree01 | 0:5f0cd7bd1389 | 9 | |
rtgree01 | 0:5f0cd7bd1389 | 10 | |
rtgree01 | 0:5f0cd7bd1389 | 11 | // CAN_RS pin at Philips PCA82C250 can bus controller. |
rtgree01 | 0:5f0cd7bd1389 | 12 | // activate transceiver by pulling this pin to GND. |
rtgree01 | 0:5f0cd7bd1389 | 13 | // (Rise and fall slope controlled by resistor R_s) |
rtgree01 | 0:5f0cd7bd1389 | 14 | // (+5V result in tranceiver standby mode) |
rtgree01 | 0:5f0cd7bd1389 | 15 | // For further information see datasheet page 4 |
rtgree01 | 0:5f0cd7bd1389 | 16 | |
rtgree01 | 0:5f0cd7bd1389 | 17 | DigitalOut can_Pca82c250SlopePin(p28); |
rtgree01 | 0:5f0cd7bd1389 | 18 | |
rtgree01 | 0:5f0cd7bd1389 | 19 | // second can controller on these pins. Not used here. |
rtgree01 | 0:5f0cd7bd1389 | 20 | // CAN can1(p9, p10); |
rtgree01 | 0:5f0cd7bd1389 | 21 | // We use can on mbed pins 29(CAN_TXD) and 30(CAN_RXD). |
rtgree01 | 0:5f0cd7bd1389 | 22 | CAN can2(p30, p29); |
rtgree01 | 0:5f0cd7bd1389 | 23 | CAN can1(p9, p10); |
rtgree01 | 0:5f0cd7bd1389 | 24 | |
rtgree01 | 0:5f0cd7bd1389 | 25 | char counter = 0; |
rtgree01 | 0:5f0cd7bd1389 | 26 | bool sendme = false; |
rtgree01 | 0:5f0cd7bd1389 | 27 | CANMessage can_MsgTx; |
rtgree01 | 0:5f0cd7bd1389 | 28 | CANMessage can_MsgRx; |
rtgree01 | 0:5f0cd7bd1389 | 29 | char msg[14]; |
rtgree01 | 0:5f0cd7bd1389 | 30 | |
rtgree01 | 0:5f0cd7bd1389 | 31 | int main() |
rtgree01 | 0:5f0cd7bd1389 | 32 | { |
rtgree01 | 0:5f0cd7bd1389 | 33 | pc.baud(115200); |
rtgree01 | 0:5f0cd7bd1389 | 34 | // pc.printf("Starting CAN Monitor\r\n"); |
rtgree01 | 0:5f0cd7bd1389 | 35 | |
rtgree01 | 0:5f0cd7bd1389 | 36 | can1.frequency(500000); |
rtgree01 | 0:5f0cd7bd1389 | 37 | |
rtgree01 | 0:5f0cd7bd1389 | 38 | LPC_CAN2->BTR = 0x52001C; |
rtgree01 | 0:5f0cd7bd1389 | 39 | // pc.printf("CAN Freq = 0x%08x\r\n", LPC_CAN2->BTR); |
rtgree01 | 0:5f0cd7bd1389 | 40 | |
rtgree01 | 0:5f0cd7bd1389 | 41 | // activate external can transceiver |
rtgree01 | 0:5f0cd7bd1389 | 42 | can_Pca82c250SlopePin = 0; |
rtgree01 | 0:5f0cd7bd1389 | 43 | |
rtgree01 | 0:5f0cd7bd1389 | 44 | // every 500ms |
rtgree01 | 0:5f0cd7bd1389 | 45 | // ticker.attach(&send, 0.5); |
rtgree01 | 0:5f0cd7bd1389 | 46 | /// create message object for message reception |
rtgree01 | 0:5f0cd7bd1389 | 47 | |
rtgree01 | 0:5f0cd7bd1389 | 48 | while (1) |
rtgree01 | 0:5f0cd7bd1389 | 49 | { |
rtgree01 | 0:5f0cd7bd1389 | 50 | // send received messages to the pc via serial line (9k6, 8n1) |
rtgree01 | 0:5f0cd7bd1389 | 51 | if (can2.read(can_MsgRx)) |
rtgree01 | 0:5f0cd7bd1389 | 52 | { |
rtgree01 | 0:5f0cd7bd1389 | 53 | // sync |
rtgree01 | 0:5f0cd7bd1389 | 54 | pc.putc(0xff); |
rtgree01 | 0:5f0cd7bd1389 | 55 | pc.putc(0x00); |
rtgree01 | 0:5f0cd7bd1389 | 56 | pc.putc(0xff); |
rtgree01 | 0:5f0cd7bd1389 | 57 | |
rtgree01 | 0:5f0cd7bd1389 | 58 | // data |
rtgree01 | 0:5f0cd7bd1389 | 59 | pc.putc((can_MsgRx.id & 0xff00) >> 8); |
rtgree01 | 0:5f0cd7bd1389 | 60 | pc.putc((can_MsgRx.id & 0x00ff)); |
rtgree01 | 0:5f0cd7bd1389 | 61 | pc.putc(can_MsgRx.len); |
rtgree01 | 0:5f0cd7bd1389 | 62 | for (int i = 0; i < can_MsgRx.len; i++) |
rtgree01 | 0:5f0cd7bd1389 | 63 | { |
rtgree01 | 0:5f0cd7bd1389 | 64 | pc.putc(can_MsgRx.data[i]); |
rtgree01 | 0:5f0cd7bd1389 | 65 | } |
rtgree01 | 0:5f0cd7bd1389 | 66 | |
rtgree01 | 0:5f0cd7bd1389 | 67 | // any incoming message: toggle led2 |
rtgree01 | 0:5f0cd7bd1389 | 68 | led2 = !led2; |
rtgree01 | 0:5f0cd7bd1389 | 69 | } |
rtgree01 | 0:5f0cd7bd1389 | 70 | |
rtgree01 | 0:5f0cd7bd1389 | 71 | if (can1.read(can_MsgRx)) |
rtgree01 | 0:5f0cd7bd1389 | 72 | { |
rtgree01 | 0:5f0cd7bd1389 | 73 | // sync |
rtgree01 | 0:5f0cd7bd1389 | 74 | pc.putc(0xff); |
rtgree01 | 0:5f0cd7bd1389 | 75 | pc.putc(0x00); |
rtgree01 | 0:5f0cd7bd1389 | 76 | pc.putc(0xff); |
rtgree01 | 0:5f0cd7bd1389 | 77 | |
rtgree01 | 0:5f0cd7bd1389 | 78 | // data |
rtgree01 | 0:5f0cd7bd1389 | 79 | pc.putc((can_MsgRx.id & 0xff00) >> 8); |
rtgree01 | 0:5f0cd7bd1389 | 80 | pc.putc((can_MsgRx.id & 0x00ff)); |
rtgree01 | 0:5f0cd7bd1389 | 81 | pc.putc(can_MsgRx.len); |
rtgree01 | 0:5f0cd7bd1389 | 82 | for (int i = 0; i < can_MsgRx.len; i++) |
rtgree01 | 0:5f0cd7bd1389 | 83 | { |
rtgree01 | 0:5f0cd7bd1389 | 84 | pc.putc(can_MsgRx.data[i]); |
rtgree01 | 0:5f0cd7bd1389 | 85 | } |
rtgree01 | 0:5f0cd7bd1389 | 86 | |
rtgree01 | 0:5f0cd7bd1389 | 87 | // any incoming message: toggle led2 |
rtgree01 | 0:5f0cd7bd1389 | 88 | led3 = !led3; |
rtgree01 | 0:5f0cd7bd1389 | 89 | } |
rtgree01 | 0:5f0cd7bd1389 | 90 | |
rtgree01 | 0:5f0cd7bd1389 | 91 | while (pc.readable()) |
rtgree01 | 0:5f0cd7bd1389 | 92 | { |
rtgree01 | 0:5f0cd7bd1389 | 93 | msg[counter] = pc.getc(); |
rtgree01 | 0:5f0cd7bd1389 | 94 | counter++; |
rtgree01 | 0:5f0cd7bd1389 | 95 | |
rtgree01 | 0:5f0cd7bd1389 | 96 | if (counter == 14) |
rtgree01 | 0:5f0cd7bd1389 | 97 | { |
rtgree01 | 0:5f0cd7bd1389 | 98 | break; |
rtgree01 | 0:5f0cd7bd1389 | 99 | } |
rtgree01 | 0:5f0cd7bd1389 | 100 | |
rtgree01 | 0:5f0cd7bd1389 | 101 | if (counter == 3) |
rtgree01 | 0:5f0cd7bd1389 | 102 | { |
rtgree01 | 0:5f0cd7bd1389 | 103 | if ((msg[0] != 0xff) || (msg[1] != 0x00) || (msg[2] != 0xff)) |
rtgree01 | 0:5f0cd7bd1389 | 104 | { |
rtgree01 | 0:5f0cd7bd1389 | 105 | counter = 0; |
rtgree01 | 0:5f0cd7bd1389 | 106 | msg[0] = msg[1]; |
rtgree01 | 0:5f0cd7bd1389 | 107 | msg[1] = msg[2]; |
rtgree01 | 0:5f0cd7bd1389 | 108 | } |
rtgree01 | 0:5f0cd7bd1389 | 109 | } |
rtgree01 | 0:5f0cd7bd1389 | 110 | } |
rtgree01 | 0:5f0cd7bd1389 | 111 | |
rtgree01 | 0:5f0cd7bd1389 | 112 | if (counter > 13) |
rtgree01 | 0:5f0cd7bd1389 | 113 | { |
rtgree01 | 0:5f0cd7bd1389 | 114 | counter = 0; |
rtgree01 | 0:5f0cd7bd1389 | 115 | sendme = false; |
rtgree01 | 0:5f0cd7bd1389 | 116 | |
rtgree01 | 0:5f0cd7bd1389 | 117 | if ((msg[0] == 0xff) && (msg[1] == 0x00) && (msg[2] == 0xff)) |
rtgree01 | 0:5f0cd7bd1389 | 118 | { |
rtgree01 | 0:5f0cd7bd1389 | 119 | char temp[14]; |
rtgree01 | 0:5f0cd7bd1389 | 120 | memcpy(temp, msg, 14); |
rtgree01 | 0:5f0cd7bd1389 | 121 | |
rtgree01 | 0:5f0cd7bd1389 | 122 | memset(msg, 0, 14); |
rtgree01 | 0:5f0cd7bd1389 | 123 | |
rtgree01 | 0:5f0cd7bd1389 | 124 | can_MsgTx.id = (((short)temp[3]) << 8) + temp[4]; |
rtgree01 | 0:5f0cd7bd1389 | 125 | can_MsgTx.len = temp[5]; |
rtgree01 | 0:5f0cd7bd1389 | 126 | for (int i = 0; i < 8; i++) |
rtgree01 | 0:5f0cd7bd1389 | 127 | { |
rtgree01 | 0:5f0cd7bd1389 | 128 | can_MsgTx.data[i] = temp[6 + i]; |
rtgree01 | 0:5f0cd7bd1389 | 129 | } |
rtgree01 | 0:5f0cd7bd1389 | 130 | |
rtgree01 | 0:5f0cd7bd1389 | 131 | //send the message |
rtgree01 | 0:5f0cd7bd1389 | 132 | led1 = !led1; |
rtgree01 | 0:5f0cd7bd1389 | 133 | can2.write(can_MsgTx); |
rtgree01 | 0:5f0cd7bd1389 | 134 | } |
rtgree01 | 0:5f0cd7bd1389 | 135 | } |
rtgree01 | 0:5f0cd7bd1389 | 136 | } |
rtgree01 | 0:5f0cd7bd1389 | 137 | } |