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 22:25:51 2012 +0000
Revision:
3:ce56713e4201
Parent:
2:a285b70981c6
Child:
4:e40a05b32518
Removed unneeded code, tested and works.

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