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