version_2.0
Dependents: cc3000_ping_demo_try_2
Fork of mbed by
CAN.h@43:e2ed12d17f06, 2012-10-26 (annotated)
- Committer:
- emilmont
- Date:
- Fri Oct 26 17:40:46 2012 +0100
- Revision:
- 43:e2ed12d17f06
- Parent:
- 33:5364839841bd
- Child:
- 44:24d45a770a51
Update documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rolf.meyer@arm.com | 11:1c1ebd0324fa | 1 | /* mbed Microcontroller Library - can |
emilmont | 27:7110ebee3484 | 2 | * Copyright (c) 2009-2011 ARM Limited. All rights reserved. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 3 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 4 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 5 | #ifndef MBED_CAN_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 6 | #define MBED_CAN_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 7 | |
emilmont | 27:7110ebee3484 | 8 | #include "device.h" |
emilmont | 27:7110ebee3484 | 9 | |
emilmont | 27:7110ebee3484 | 10 | #if DEVICE_CAN |
emilmont | 27:7110ebee3484 | 11 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 12 | #include "Base.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 13 | #include "platform.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 14 | #include "PinNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 15 | #include "PeripheralNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 16 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 17 | #include "can_helper.h" |
simon | 22:9114680c05da | 18 | #include "FunctionPointer.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 19 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 20 | #include <string.h> |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 21 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 22 | namespace mbed { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 23 | |
emilmont | 43:e2ed12d17f06 | 24 | /** CANMessage class |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 25 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 26 | class CANMessage : public CAN_Message { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 27 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 28 | public: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 29 | |
emilmont | 43:e2ed12d17f06 | 30 | /** Creates empty CAN message. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 31 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 32 | CANMessage() { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 33 | len = 8; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 34 | type = CANData; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 35 | format = CANStandard; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 36 | id = 0; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 37 | memset(data, 0, 8); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 38 | } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 39 | |
emilmont | 43:e2ed12d17f06 | 40 | /** Creates CAN message with specific content. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 41 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 42 | CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 43 | len = _len & 0xF; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 44 | type = _type; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 45 | format = _format; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 46 | id = _id; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 47 | memcpy(data, _data, _len); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 48 | } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 49 | |
emilmont | 43:e2ed12d17f06 | 50 | /** Creates CAN remote message. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 51 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 52 | CANMessage(int _id, CANFormat _format = CANStandard) { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 53 | len = 0; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 54 | type = CANRemote; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 55 | format = _format; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 56 | id = _id; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 57 | memset(data, 0, 8); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 58 | } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 59 | #if 0 // Inhereted from CAN_Message, for documentation only |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 60 | |
emilmont | 43:e2ed12d17f06 | 61 | /** The message id. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 62 | * |
emilmont | 43:e2ed12d17f06 | 63 | * - If format is CANStandard it must be an 11 bit long id. |
emilmont | 43:e2ed12d17f06 | 64 | * - If format is CANExtended it must be an 29 bit long id. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 65 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 66 | unsigned int id; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 67 | |
emilmont | 43:e2ed12d17f06 | 68 | /** Space for 8 byte payload. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 69 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 70 | * If type is CANData data can store up to 8 byte data. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 71 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 72 | unsigned char data[8]; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 73 | |
emilmont | 43:e2ed12d17f06 | 74 | /** Length of data in bytes. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 75 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 76 | * If type is CANData data can store up to 8 byte data. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 77 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 78 | unsigned char len; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 79 | |
emilmont | 43:e2ed12d17f06 | 80 | /** Defines if the message has standard or extended format. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 81 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 82 | * Defines the type of message id: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 83 | * Default is CANStandard which implies 11 bit id. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 84 | * CANExtended means 29 bit message id. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 85 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 86 | CANFormat format; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 87 | |
emilmont | 43:e2ed12d17f06 | 88 | /** Defines the type of a message. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 89 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 90 | * The message type can rather be CANData for a message with data (default). |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 91 | * Or CANRemote for a request of a specific CAN message. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 92 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 93 | CANType type; // 0 - DATA FRAME, 1 - REMOTE FRAME |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 94 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 95 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 96 | |
emilmont | 43:e2ed12d17f06 | 97 | /** A can bus client, used for communicating with can devices |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 98 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 99 | class CAN : public Base { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 100 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 101 | public: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 102 | |
emilmont | 43:e2ed12d17f06 | 103 | /** Creates an CAN interface connected to specific pins. |
emilmont | 43:e2ed12d17f06 | 104 | * |
emilmont | 43:e2ed12d17f06 | 105 | * @param rd read from transmitter |
emilmont | 43:e2ed12d17f06 | 106 | * @param td transmit to transmitter |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 107 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 108 | * Example: |
emilmont | 43:e2ed12d17f06 | 109 | * @code |
emilmont | 43:e2ed12d17f06 | 110 | * #include "mbed.h" |
emilmont | 43:e2ed12d17f06 | 111 | * |
emilmont | 43:e2ed12d17f06 | 112 | * Ticker ticker; |
emilmont | 43:e2ed12d17f06 | 113 | * DigitalOut led1(LED1); |
emilmont | 43:e2ed12d17f06 | 114 | * DigitalOut led2(LED2); |
emilmont | 43:e2ed12d17f06 | 115 | * CAN can1(p9, p10); |
emilmont | 43:e2ed12d17f06 | 116 | * CAN can2(p30, p29); |
emilmont | 43:e2ed12d17f06 | 117 | * |
emilmont | 43:e2ed12d17f06 | 118 | * char counter = 0; |
emilmont | 43:e2ed12d17f06 | 119 | * |
emilmont | 43:e2ed12d17f06 | 120 | * void send() { |
emilmont | 43:e2ed12d17f06 | 121 | * if(can1.write(CANMessage(1337, &counter, 1))) { |
emilmont | 43:e2ed12d17f06 | 122 | * printf("Message sent: %d\n", counter); |
emilmont | 43:e2ed12d17f06 | 123 | * counter++; |
emilmont | 43:e2ed12d17f06 | 124 | * } |
emilmont | 43:e2ed12d17f06 | 125 | * led1 = !led1; |
emilmont | 43:e2ed12d17f06 | 126 | * } |
emilmont | 43:e2ed12d17f06 | 127 | * |
emilmont | 43:e2ed12d17f06 | 128 | * int main() { |
emilmont | 43:e2ed12d17f06 | 129 | * ticker.attach(&send, 1); |
emilmont | 43:e2ed12d17f06 | 130 | * CANMessage msg; |
emilmont | 43:e2ed12d17f06 | 131 | * while(1) { |
emilmont | 43:e2ed12d17f06 | 132 | * if(can2.read(msg)) { |
emilmont | 43:e2ed12d17f06 | 133 | * printf("Message received: %d\n\n", msg.data[0]); |
emilmont | 43:e2ed12d17f06 | 134 | * led2 = !led2; |
emilmont | 43:e2ed12d17f06 | 135 | * } |
emilmont | 43:e2ed12d17f06 | 136 | * wait(0.2); |
emilmont | 43:e2ed12d17f06 | 137 | * } |
emilmont | 43:e2ed12d17f06 | 138 | * } |
emilmont | 43:e2ed12d17f06 | 139 | * @endcode |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 140 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 141 | CAN(PinName rd, PinName td); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 142 | virtual ~CAN(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 143 | |
emilmont | 43:e2ed12d17f06 | 144 | /** Set the frequency of the CAN interface |
emilmont | 43:e2ed12d17f06 | 145 | * |
emilmont | 43:e2ed12d17f06 | 146 | * @param hz The bus frequency in hertz |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 147 | * |
emilmont | 43:e2ed12d17f06 | 148 | * @returns |
emilmont | 43:e2ed12d17f06 | 149 | * 1 if successful, |
emilmont | 43:e2ed12d17f06 | 150 | * 0 otherwise |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 151 | */ |
simon | 21:3944f1e2fa4f | 152 | int frequency(int hz); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 153 | |
emilmont | 43:e2ed12d17f06 | 154 | /** Write a CANMessage to the bus. |
emilmont | 43:e2ed12d17f06 | 155 | * |
emilmont | 43:e2ed12d17f06 | 156 | * @param msg The CANMessage to write. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 157 | * |
emilmont | 43:e2ed12d17f06 | 158 | * @returns |
emilmont | 43:e2ed12d17f06 | 159 | * 0 if write failed, |
emilmont | 43:e2ed12d17f06 | 160 | * 1 if write was successful |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 161 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 162 | int write(CANMessage msg); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 163 | |
emilmont | 43:e2ed12d17f06 | 164 | /** Read a CANMessage from the bus. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 165 | * |
emilmont | 43:e2ed12d17f06 | 166 | * @param msg A CANMessage to read to. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 167 | * |
emilmont | 43:e2ed12d17f06 | 168 | * @returns |
emilmont | 43:e2ed12d17f06 | 169 | * 0 if no message arrived, |
emilmont | 43:e2ed12d17f06 | 170 | * 1 if message arrived |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 171 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 172 | int read(CANMessage &msg); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 173 | |
emilmont | 43:e2ed12d17f06 | 174 | /** Reset CAN interface. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 175 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 176 | * To use after error overflow. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 177 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 178 | void reset(); |
simon | 22:9114680c05da | 179 | |
emilmont | 43:e2ed12d17f06 | 180 | /** Puts or removes the CAN interface into silent monitoring mode |
simon | 22:9114680c05da | 181 | * |
emilmont | 43:e2ed12d17f06 | 182 | * @param silent boolean indicating whether to go into silent mode or not |
simon | 22:9114680c05da | 183 | */ |
simon | 22:9114680c05da | 184 | void monitor(bool silent); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 185 | |
emilmont | 43:e2ed12d17f06 | 186 | /** Returns number of read errors to detect read overflow errors. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 187 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 188 | unsigned char rderror(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 189 | |
emilmont | 43:e2ed12d17f06 | 190 | /** Returns number of write errors to detect write overflow errors. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 191 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 192 | unsigned char tderror(); |
simon | 22:9114680c05da | 193 | |
emilmont | 43:e2ed12d17f06 | 194 | /** Attach a function to call whenever a CAN frame received interrupt is |
simon | 22:9114680c05da | 195 | * generated. |
simon | 22:9114680c05da | 196 | * |
emilmont | 43:e2ed12d17f06 | 197 | * @param fptr A pointer to a void function, or 0 to set as none |
simon | 22:9114680c05da | 198 | */ |
simon | 22:9114680c05da | 199 | void attach(void (*fptr)(void)); |
simon | 22:9114680c05da | 200 | |
emilmont | 43:e2ed12d17f06 | 201 | /** Attach a member function to call whenever a CAN frame received interrupt |
simon | 22:9114680c05da | 202 | * is generated. |
simon | 22:9114680c05da | 203 | * |
emilmont | 43:e2ed12d17f06 | 204 | * @param tptr pointer to the object to call the member function on |
emilmont | 43:e2ed12d17f06 | 205 | * @param mptr pointer to the member function to be called |
simon | 22:9114680c05da | 206 | */ |
simon | 22:9114680c05da | 207 | template<typename T> |
emilmont | 33:5364839841bd | 208 | void attach(T* tptr, void (T::*mptr)(void)) { |
emilmont | 33:5364839841bd | 209 | if((mptr != NULL) && (tptr != NULL)) { |
emilmont | 33:5364839841bd | 210 | _rxirq.attach(tptr, mptr); |
emilmont | 33:5364839841bd | 211 | setup_interrupt(); |
emilmont | 33:5364839841bd | 212 | } else { |
emilmont | 33:5364839841bd | 213 | remove_interrupt(); |
emilmont | 33:5364839841bd | 214 | } |
emilmont | 33:5364839841bd | 215 | } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 216 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 217 | private: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 218 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 219 | CANName _id; |
simon | 22:9114680c05da | 220 | FunctionPointer _rxirq; |
simon | 22:9114680c05da | 221 | |
simon | 22:9114680c05da | 222 | void setup_interrupt(void); |
simon | 22:9114680c05da | 223 | void remove_interrupt(void); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 224 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 225 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 226 | } // namespace mbed |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 227 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 228 | #endif // MBED_CAN_H |
emilmont | 27:7110ebee3484 | 229 | |
emilmont | 27:7110ebee3484 | 230 | #endif |