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