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