A working demonstration on how to read GMLAN packets using an mbed and a compatible CAN transceiver such as (and tested with) the CAN-Bus demo board. The SparkFun CAN Shield should also work perfectly too, as should just about every MCP2551-based solution (but this code should be portable to other transceivers). Please note to get this to work, you must tie CAN_L to ground and connect CAN_H to the single wire CAN.

Dependencies:   mbed

GMLAN Sniffer

Introduction

This project makes use of CAN on an LPC1768 to communicate with General Motors GMLAN network via a compatible transceiver. GMLAN is a single wire CAN Bus that operates at 33.333 kbit with 29-bit header packets and is commonly used for transmission of non-critical information between such nodes on automobiles as the following;

  • Dash cluster
  • Entertainment system (head unit)
  • Satellite Navigation (OnStar for example)
  • HVAC controls
  • Immobiliser / alarm / other security systems
  • Door locks and door status
  • Power Windows
  • Interior / exterior lighting
  • Reversing sensors

The list is extensive, for a better idea of what can be done, view the ARBIDs tab on the GMLAN Bible Google Docs spreadsheet (tabs are at the bottom of the page).

Hardware setup

This is just one example of how you can interface with GMLAN using an SKPang CAN-Bus Breakout Board (based around an MCP2551 CAN Transceiver), using these instructions I assembled the following on my trusty prototyping board;

/media/uploads/foxdie/_scaled_mbed-canbus-layout.jpg

(Click to open high resolution version)

Committer:
foxdie
Date:
Thu Dec 06 11:10:52 2012 +0000
Revision:
2:a285b70981c6
Parent:
1:3daf1c9b3315
Child:
3:ce56713e4201
Cleaned up obsolete code and comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
foxdie 0:f08f63171361 1 #include "mbed.h"
foxdie 0:f08f63171361 2
foxdie 0:f08f63171361 3 DigitalOut rx_led(LED1);
foxdie 0:f08f63171361 4 DigitalOut tx_led(LED4);
foxdie 0:f08f63171361 5 Serial pc(USBTX, USBRX);
foxdie 0:f08f63171361 6 CAN gmlan(p30, p29);
foxdie 0:f08f63171361 7
foxdie 0:f08f63171361 8 void clearAndHome()
foxdie 0:f08f63171361 9 {
foxdie 0:f08f63171361 10 pc.printf("%c", 27); // ESC
foxdie 0:f08f63171361 11 pc.printf("[2J"); // clear screen
foxdie 0:f08f63171361 12 pc.printf("%c", 27); // ESC
foxdie 0:f08f63171361 13 pc.printf("[H"); // cursor to home
foxdie 0:f08f63171361 14 }
foxdie 0:f08f63171361 15
foxdie 1:3daf1c9b3315 16 void processMessage()
foxdie 1:3daf1c9b3315 17 {
foxdie 1:3daf1c9b3315 18 time_t seconds = time(NULL);
foxdie 1:3daf1c9b3315 19 rx_led = 1;
foxdie 1:3daf1c9b3315 20 CANMessage rxmsg;
foxdie 1:3daf1c9b3315 21 gmlan.read(rxmsg);
foxdie 1:3daf1c9b3315 22 pc.printf("[%10d]\t[%d]\t[0x%08X]\t", seconds, rxmsg.len, rxmsg.id);
foxdie 1:3daf1c9b3315 23 for (unsigned int i = 0; i < rxmsg.len; i++)
foxdie 1:3daf1c9b3315 24 pc.printf("%02X ", rxmsg.data[i]);
foxdie 1:3daf1c9b3315 25 pc.printf("\r\n");
foxdie 1:3daf1c9b3315 26 rx_led = 0;
foxdie 1:3daf1c9b3315 27 }
foxdie 1:3daf1c9b3315 28
foxdie 0:f08f63171361 29 int main() {
foxdie 2:a285b70981c6 30 // Reset uptime timer and set serial baud rate to 115kbit
foxdie 1:3daf1c9b3315 31 set_time(0);
foxdie 0:f08f63171361 32 pc.baud(115200);
foxdie 0:f08f63171361 33
foxdie 2:a285b70981c6 34 // Set CANBUS to 33.3kbit and put into monitor mode (does not ACK packets, aka stealth mode)
foxdie 2:a285b70981c6 35 int baudrate = 33333;
foxdie 2:a285b70981c6 36 gmlan.frequency(baudrate);
foxdie 0:f08f63171361 37 gmlan.monitor(true);
foxdie 0:f08f63171361 38
foxdie 2:a285b70981c6 39 // Clear serial terminal and start capturing packets
foxdie 0:f08f63171361 40 clearAndHome();
foxdie 0:f08f63171361 41 pc.printf("Starting packet capture at %i bps\r\n", baudrate);
foxdie 0:f08f63171361 42
foxdie 1:3daf1c9b3315 43 gmlan.attach(&processMessage);
foxdie 1:3daf1c9b3315 44
foxdie 0:f08f63171361 45 while(1) {
foxdie 2:a285b70981c6 46 // Sleep for 20ms repeatedly, all messages are handled by an interrupt, this prevents keeping the mbed at full load
foxdie 1:3daf1c9b3315 47 wait(0.02);
foxdie 0:f08f63171361 48 }
foxdie 0:f08f63171361 49 }