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:
Mon Apr 08 11:31:12 2013 +0000
Revision:
9:4af02032daeb
Parent:
8:bc97fa5d306e
Fixed bug where transmission of multi-frame messages wasn't recognising flow control to continue resulting in code waiting around doing nada

Who changed what in which revision?

UserRevisionLine numberNew contents of line
foxdie 0:9266fbfbef88 1 /*
foxdie 0:9266fbfbef88 2 GMLAN.h - Header 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 2:1a2cb289f24d 20 #include "GMLAN_29bit.h"
foxdie 4:486fec88517e 21 #include "GMLAN_11bit.h"
foxdie 5:d0b067be6d44 22 #include <vector>
foxdie 2:1a2cb289f24d 23
foxdie 0:9266fbfbef88 24 #ifndef GMLAN_H
foxdie 0:9266fbfbef88 25 #define GMLAN_H
foxdie 0:9266fbfbef88 26
foxdie 0:9266fbfbef88 27 /* Baud rates of various services */
foxdie 0:9266fbfbef88 28 #define GMLAN_BAUD_LS_NORMAL 33333
foxdie 0:9266fbfbef88 29 #define GMLAN_BAUD_LS_FAST 83333
foxdie 0:9266fbfbef88 30 #define GMLAN_BAUD_MS 95200
foxdie 0:9266fbfbef88 31 #define GMLAN_BAUD_HS 500000
foxdie 0:9266fbfbef88 32
foxdie 0:9266fbfbef88 33 class CANHeader {
foxdie 3:09fdfec053cd 34 /*
foxdie 3:09fdfec053cd 35 CANHeader was designed solely for 29-bit frames but supports 11-bit too by just setting the ArbID
foxdie 3:09fdfec053cd 36
foxdie 3:09fdfec053cd 37 Example 29-bit header packet from Steering Wheel Switches:
foxdie 3:09fdfec053cd 38
foxdie 3:09fdfec053cd 39 Hexadecimal: 0x10 0x0D 0x00 0x60
foxdie 3:09fdfec053cd 40 Binary: 00010000 00001101 00000000 01100000
foxdie 3:09fdfec053cd 41 Priority: ---
foxdie 3:09fdfec053cd 42 Arbitration: -- -------- ---
foxdie 3:09fdfec053cd 43 Sending ECU: ----- --------
foxdie 3:09fdfec053cd 44
foxdie 3:09fdfec053cd 45 Example 11-bit header packet from Head Unit:
foxdie 3:09fdfec053cd 46
foxdie 3:09fdfec053cd 47 Hexadecimal: 0x02 0x44
foxdie 3:09fdfec053cd 48 Binary: 00000010 01000100
foxdie 3:09fdfec053cd 49 Identifier: --- --------
foxdie 3:09fdfec053cd 50
foxdie 3:09fdfec053cd 51 */
foxdie 0:9266fbfbef88 52
foxdie 0:9266fbfbef88 53 private:
foxdie 0:9266fbfbef88 54 int priorityID, arbitrationID, senderID;
foxdie 0:9266fbfbef88 55
foxdie 0:9266fbfbef88 56 public:
foxdie 1:9dfa8ee351a3 57 // Main function
foxdie 1:9dfa8ee351a3 58 CANHeader() { }
foxdie 3:09fdfec053cd 59
foxdie 0:9266fbfbef88 60 // Methods for getting / setting priority, both integers
foxdie 0:9266fbfbef88 61 int priority(void) { return priorityID; }
foxdie 0:9266fbfbef88 62 void priority(int _priority) { priorityID = _priority; }
foxdie 3:09fdfec053cd 63
foxdie 0:9266fbfbef88 64 // Method for getting / setting arbitration id aka arbid, both integers
foxdie 0:9266fbfbef88 65 int arbitration(void) { return arbitrationID; }
foxdie 0:9266fbfbef88 66 void arbitration(int _arbitration) { arbitrationID = _arbitration; }
foxdie 3:09fdfec053cd 67
foxdie 0:9266fbfbef88 68 // Method for getting / setting sender id, both integers
foxdie 0:9266fbfbef88 69 int sender(void) { return senderID; }
foxdie 0:9266fbfbef88 70 void sender(int _sender) { senderID = _sender; }
foxdie 0:9266fbfbef88 71
foxdie 3:09fdfec053cd 72 // Function to decode either an 11-bit or 29-bit header packet and store values in respective variables
foxdie 0:9266fbfbef88 73 void decode(int _header);
foxdie 3:09fdfec053cd 74
foxdie 3:09fdfec053cd 75 // Function to encode stored values as 29-bit header and return header packet as int
foxdie 3:09fdfec053cd 76 int encode29bit(void);
foxdie 4:486fec88517e 77
foxdie 4:486fec88517e 78 // Function to encode stored values as 11-bit header and return header packet as int
foxdie 4:486fec88517e 79 int encode11bit(void);
foxdie 0:9266fbfbef88 80 };
foxdie 0:9266fbfbef88 81
foxdie 5:d0b067be6d44 82 class GMLAN_Message {
foxdie 5:d0b067be6d44 83 /*
foxdie 5:d0b067be6d44 84 Wrapper for CANMessage that automatically parses settings
foxdie 5:d0b067be6d44 85 */
foxdie 5:d0b067be6d44 86 private:
foxdie 5:d0b067be6d44 87 vector<char> data;
foxdie 5:d0b067be6d44 88 int priority, arbitration, sender;
foxdie 5:d0b067be6d44 89
foxdie 5:d0b067be6d44 90 public:
foxdie 5:d0b067be6d44 91 // Main function
foxdie 5:d0b067be6d44 92 GMLAN_Message(int _priority = -1, int _arbitration = -1, int _sender = -1,
foxdie 5:d0b067be6d44 93 int _b0 = -1, int _b1 = -1, int _b2 = -1, int _b3 = -1, int _b4 = -1, int _b5 = -1, int _b6 = -1, int _b7 = -1);
foxdie 5:d0b067be6d44 94
foxdie 5:d0b067be6d44 95 // Return result
foxdie 5:d0b067be6d44 96 CANMessage generate(void);
foxdie 5:d0b067be6d44 97 };
foxdie 5:d0b067be6d44 98
foxdie 6:32592425aa57 99 class GMLAN_11Bit_Request {
foxdie 6:32592425aa57 100 /*
foxdie 6:32592425aa57 101 Class to allow easier handling of sending and receiving 11-bit messages
foxdie 6:32592425aa57 102 */
foxdie 6:32592425aa57 103 private:
foxdie 6:32592425aa57 104 vector<char> request_data, response_data;
foxdie 6:32592425aa57 105 int id, request_state;
foxdie 6:32592425aa57 106 int tx_frame_counter, tx_bytes;
foxdie 6:32592425aa57 107 int rx_frame_counter, rx_bytes;
foxdie 8:bc97fa5d306e 108 bool await_response, handle_flowcontrol;
foxdie 6:32592425aa57 109 char frame_padding [8];
foxdie 6:32592425aa57 110
foxdie 6:32592425aa57 111 public:
foxdie 6:32592425aa57 112 // (Main function) Create message and send it
foxdie 8:bc97fa5d306e 113 GMLAN_11Bit_Request(int _id, vector<char> _request, bool _await_response = true, bool _handle_flowcontrol = true);
foxdie 6:32592425aa57 114
foxdie 6:32592425aa57 115 // Process each frame to transmit and flow control frame if needed
foxdie 6:32592425aa57 116 CANMessage getNextFrame(void);
foxdie 6:32592425aa57 117 CANMessage getFlowControl(void);
foxdie 6:32592425aa57 118 // Process each received frame
foxdie 6:32592425aa57 119 void processFrame(CANMessage msg);
foxdie 6:32592425aa57 120
foxdie 6:32592425aa57 121 // Handle starting and flow control
foxdie 6:32592425aa57 122 void start(void) { request_state = GMLAN_STATE_SEND_DATA; }
foxdie 6:32592425aa57 123 void continueFlow(void) { request_state = GMLAN_STATE_SEND_DATA; }
foxdie 6:32592425aa57 124
foxdie 6:32592425aa57 125 // Return request_state to confirm status
foxdie 6:32592425aa57 126 int getState(void) { return request_state; }
foxdie 6:32592425aa57 127 // Return ID
foxdie 6:32592425aa57 128 int getID(void) { return id; }
foxdie 7:5ec65e6e8095 129 // Return rx_bytes
foxdie 7:5ec65e6e8095 130 int getRXcount(void) { return rx_bytes; }
foxdie 6:32592425aa57 131 // Return response
foxdie 6:32592425aa57 132 vector<char> getResponse(void) { return response_data; }
foxdie 6:32592425aa57 133 };
foxdie 6:32592425aa57 134
foxdie 0:9266fbfbef88 135 #endif