Libraries to support working with GMLAN - General Motors CAN BUS network in most of their vehicles between 2007-present day. Please note this is a work in progress and not guaranteed to be correct, use at your own risk! Read commit logs / subscribe to see what has been added, it's a work in progress after all ;)

Committer:
foxdie
Date:
Tue Mar 19 10:03:21 2013 +0000
Revision:
5:d0b067be6d44
Parent:
4:486fec88517e
Child:
6:32592425aa57
Fixed a couple of syntax errors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
foxdie 0:9266fbfbef88 1 /*
foxdie 0:9266fbfbef88 2 GMLAN.cpp - Source file for GMLAN Library
foxdie 0:9266fbfbef88 3
foxdie 0:9266fbfbef88 4 GMLAN is a Controller Area Network Bus used in General Motors vehicles from
foxdie 0:9266fbfbef88 5 roughly 2007-onwards. Its purpose is to allow various Electronic Control Units
foxdie 0:9266fbfbef88 6 (aka ECUs) within a modern vehicle to share information and enact procedures.
foxdie 0:9266fbfbef88 7
foxdie 0:9266fbfbef88 8 An example of this would be communication between the HU (Head unit) and the
foxdie 0:9266fbfbef88 9 DIC (Dashboard Information Cluster), when you adjust the volume up / down, this
foxdie 0:9266fbfbef88 10 is reported to the cluster to be displayed.
foxdie 0:9266fbfbef88 11
foxdie 0:9266fbfbef88 12 It is the function of this library to "crack open" this world to allow anyone
foxdie 0:9266fbfbef88 13 with only as little as a few hours of C++ programming under their belt to get
foxdie 0:9266fbfbef88 14 started in what can sometimes seem a daunting world.
foxdie 0:9266fbfbef88 15
foxdie 0:9266fbfbef88 16 Jason Gaunt, 18th Feb 2013
foxdie 0:9266fbfbef88 17 */
foxdie 0:9266fbfbef88 18
foxdie 5:d0b067be6d44 19 #include "mbed.h"
foxdie 0:9266fbfbef88 20 #include "GMLAN.h"
foxdie 5:d0b067be6d44 21 #include <vector>
foxdie 0:9266fbfbef88 22
foxdie 0:9266fbfbef88 23 void CANHeader::decode(int _header) {
foxdie 3:09fdfec053cd 24 if (_header < 0x800)
foxdie 3:09fdfec053cd 25 {
foxdie 3:09fdfec053cd 26 // 11-bit header
foxdie 4:486fec88517e 27 arbitrationID = (_header >> 0) & 0x7FF;
foxdie 3:09fdfec053cd 28 } else {
foxdie 3:09fdfec053cd 29 // 29-bit header
foxdie 3:09fdfec053cd 30 priorityID = (_header >> 26) & 0x7;
foxdie 3:09fdfec053cd 31 arbitrationID = (_header >> 13) & 0x1FFF;
foxdie 3:09fdfec053cd 32 senderID = (_header >> 0) & 0x1FFF;
foxdie 3:09fdfec053cd 33 }
foxdie 0:9266fbfbef88 34 }
foxdie 3:09fdfec053cd 35 int CANHeader::encode29bit(void) {
foxdie 0:9266fbfbef88 36 long int buffer = 0;
foxdie 0:9266fbfbef88 37 buffer = (buffer << 3) | 0x0; // 3 bit padding
foxdie 0:9266fbfbef88 38 buffer = (buffer << 3) | priorityID;
foxdie 0:9266fbfbef88 39 buffer = (buffer << 13) | arbitrationID;
foxdie 0:9266fbfbef88 40 buffer = (buffer << 13) | senderID;
foxdie 0:9266fbfbef88 41 return buffer;
foxdie 4:486fec88517e 42 }
foxdie 4:486fec88517e 43 int CANHeader::encode11bit(void) {
foxdie 4:486fec88517e 44 short int buffer = 0;
foxdie 4:486fec88517e 45 buffer = (buffer << 5) | 0x0; // 5 bit padding
foxdie 4:486fec88517e 46 buffer = (buffer << 11) | arbitrationID;
foxdie 4:486fec88517e 47 return buffer;
foxdie 5:d0b067be6d44 48 }
foxdie 5:d0b067be6d44 49
foxdie 5:d0b067be6d44 50
foxdie 5:d0b067be6d44 51 GMLAN_Message::GMLAN_Message(int _priority, int _arbitration, int _sender,
foxdie 5:d0b067be6d44 52 int _b0, int _b1, int _b2, int _b3, int _b4, int _b5, int _b6, int _b7) {
foxdie 5:d0b067be6d44 53 priority = _priority;
foxdie 5:d0b067be6d44 54 arbitration = _arbitration;
foxdie 5:d0b067be6d44 55 sender = _sender;
foxdie 5:d0b067be6d44 56 if (_b0 != -1) data.push_back(_b0);
foxdie 5:d0b067be6d44 57 if (_b1 != -1) data.push_back(_b1);
foxdie 5:d0b067be6d44 58 if (_b2 != -1) data.push_back(_b2);
foxdie 5:d0b067be6d44 59 if (_b3 != -1) data.push_back(_b3);
foxdie 5:d0b067be6d44 60 if (_b4 != -1) data.push_back(_b4);
foxdie 5:d0b067be6d44 61 if (_b5 != -1) data.push_back(_b5);
foxdie 5:d0b067be6d44 62 if (_b6 != -1) data.push_back(_b6);
foxdie 5:d0b067be6d44 63 if (_b7 != -1) data.push_back(_b7);
foxdie 5:d0b067be6d44 64 }
foxdie 5:d0b067be6d44 65 CANMessage GMLAN_Message::generate(void) {
foxdie 5:d0b067be6d44 66 CANHeader hdr;
foxdie 5:d0b067be6d44 67 hdr.priority(priority);
foxdie 5:d0b067be6d44 68 hdr.arbitration(arbitration);
foxdie 5:d0b067be6d44 69 hdr.sender(sender);
foxdie 5:d0b067be6d44 70
foxdie 5:d0b067be6d44 71 char datatochars [data.size()];
foxdie 5:d0b067be6d44 72 for (int i = 0; i < data.size(); i++) datatochars[i] = data[i];
foxdie 5:d0b067be6d44 73
foxdie 5:d0b067be6d44 74 if (sender > 0x0)
foxdie 5:d0b067be6d44 75 return CANMessage(hdr.encode29bit(), datatochars, data.size(), CANData, CANExtended);
foxdie 5:d0b067be6d44 76 else
foxdie 5:d0b067be6d44 77 return CANMessage(arbitration, datatochars, data.size(), CANData, CANStandard);
foxdie 0:9266fbfbef88 78 }