Fork of the official mbed C/C SDK provides the software platform and libraries to build your applications for RenBED.
Dependents: 1-RenBuggyTimed RenBED_RGB RenBED_RGB_PWM RenBED_RGB
Fork of mbed by
CAN.h@54:71b101360fb9, 2013-01-08 (annotated)
- Committer:
- emilmont
- Date:
- Tue Jan 08 12:46:36 2013 +0000
- Revision:
- 54:71b101360fb9
- Parent:
- 44:24d45a770a51
- Child:
- 55:d722ed6a4237
Support "stream as file" use case
Unify binary libraries
Update copyright
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 44:24d45a770a51 | 1 | /* mbed Microcontroller Library |
emilmont | 54:71b101360fb9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
emilmont | 44:24d45a770a51 | 3 | * |
emilmont | 44:24d45a770a51 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
emilmont | 44:24d45a770a51 | 5 | * of this software and associated documentation files (the "Software"), to deal |
emilmont | 44:24d45a770a51 | 6 | * in the Software without restriction, including without limitation the rights |
emilmont | 44:24d45a770a51 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
emilmont | 44:24d45a770a51 | 8 | * copies of the Software, and to permit persons to whom the Software is |
emilmont | 44:24d45a770a51 | 9 | * furnished to do so, subject to the following conditions: |
emilmont | 44:24d45a770a51 | 10 | * |
emilmont | 44:24d45a770a51 | 11 | * The above copyright notice and this permission notice shall be included in |
emilmont | 44:24d45a770a51 | 12 | * all copies or substantial portions of the Software. |
emilmont | 44:24d45a770a51 | 13 | * |
emilmont | 44:24d45a770a51 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
emilmont | 44:24d45a770a51 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
emilmont | 44:24d45a770a51 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
emilmont | 44:24d45a770a51 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
emilmont | 44:24d45a770a51 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 44:24d45a770a51 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
emilmont | 44:24d45a770a51 | 20 | * SOFTWARE. |
emilmont | 44:24d45a770a51 | 21 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 22 | #ifndef MBED_CAN_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 23 | #define MBED_CAN_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 24 | |
emilmont | 44:24d45a770a51 | 25 | #include "platform.h" |
emilmont | 27:7110ebee3484 | 26 | |
emilmont | 27:7110ebee3484 | 27 | #if DEVICE_CAN |
emilmont | 27:7110ebee3484 | 28 | |
emilmont | 44:24d45a770a51 | 29 | #include "can_api.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 30 | #include "can_helper.h" |
simon | 22:9114680c05da | 31 | #include "FunctionPointer.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 32 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 33 | namespace mbed { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 34 | |
emilmont | 43:e2ed12d17f06 | 35 | /** CANMessage class |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 36 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 37 | class CANMessage : public CAN_Message { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 38 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 39 | public: |
emilmont | 43:e2ed12d17f06 | 40 | /** Creates empty CAN message. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 41 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 42 | CANMessage() { |
emilmont | 44:24d45a770a51 | 43 | len = 8; |
emilmont | 44:24d45a770a51 | 44 | type = CANData; |
emilmont | 44:24d45a770a51 | 45 | format = CANStandard; |
emilmont | 44:24d45a770a51 | 46 | id = 0; |
emilmont | 44:24d45a770a51 | 47 | memset(data, 0, 8); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 48 | } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 49 | |
emilmont | 43:e2ed12d17f06 | 50 | /** Creates CAN message with specific content. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 51 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 52 | CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 53 | len = _len & 0xF; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 54 | type = _type; |
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 | memcpy(data, _data, _len); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 58 | } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 59 | |
emilmont | 43:e2ed12d17f06 | 60 | /** Creates CAN remote message. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 61 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 62 | CANMessage(int _id, CANFormat _format = CANStandard) { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 63 | len = 0; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 64 | type = CANRemote; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 65 | format = _format; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 66 | id = _id; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 67 | memset(data, 0, 8); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 68 | } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 69 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 70 | |
emilmont | 43:e2ed12d17f06 | 71 | /** A can bus client, used for communicating with can devices |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 72 | */ |
emilmont | 44:24d45a770a51 | 73 | class CAN { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 74 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 75 | public: |
emilmont | 43:e2ed12d17f06 | 76 | /** Creates an CAN interface connected to specific pins. |
emilmont | 43:e2ed12d17f06 | 77 | * |
emilmont | 43:e2ed12d17f06 | 78 | * @param rd read from transmitter |
emilmont | 43:e2ed12d17f06 | 79 | * @param td transmit to transmitter |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 80 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 81 | * Example: |
emilmont | 43:e2ed12d17f06 | 82 | * @code |
emilmont | 43:e2ed12d17f06 | 83 | * #include "mbed.h" |
emilmont | 43:e2ed12d17f06 | 84 | * |
emilmont | 43:e2ed12d17f06 | 85 | * Ticker ticker; |
emilmont | 43:e2ed12d17f06 | 86 | * DigitalOut led1(LED1); |
emilmont | 43:e2ed12d17f06 | 87 | * DigitalOut led2(LED2); |
emilmont | 43:e2ed12d17f06 | 88 | * CAN can1(p9, p10); |
emilmont | 43:e2ed12d17f06 | 89 | * CAN can2(p30, p29); |
emilmont | 43:e2ed12d17f06 | 90 | * |
emilmont | 43:e2ed12d17f06 | 91 | * char counter = 0; |
emilmont | 43:e2ed12d17f06 | 92 | * |
emilmont | 43:e2ed12d17f06 | 93 | * void send() { |
emilmont | 43:e2ed12d17f06 | 94 | * if(can1.write(CANMessage(1337, &counter, 1))) { |
emilmont | 43:e2ed12d17f06 | 95 | * printf("Message sent: %d\n", counter); |
emilmont | 43:e2ed12d17f06 | 96 | * counter++; |
emilmont | 43:e2ed12d17f06 | 97 | * } |
emilmont | 43:e2ed12d17f06 | 98 | * led1 = !led1; |
emilmont | 43:e2ed12d17f06 | 99 | * } |
emilmont | 43:e2ed12d17f06 | 100 | * |
emilmont | 43:e2ed12d17f06 | 101 | * int main() { |
emilmont | 43:e2ed12d17f06 | 102 | * ticker.attach(&send, 1); |
emilmont | 43:e2ed12d17f06 | 103 | * CANMessage msg; |
emilmont | 43:e2ed12d17f06 | 104 | * while(1) { |
emilmont | 43:e2ed12d17f06 | 105 | * if(can2.read(msg)) { |
emilmont | 43:e2ed12d17f06 | 106 | * printf("Message received: %d\n\n", msg.data[0]); |
emilmont | 43:e2ed12d17f06 | 107 | * led2 = !led2; |
emilmont | 43:e2ed12d17f06 | 108 | * } |
emilmont | 43:e2ed12d17f06 | 109 | * wait(0.2); |
emilmont | 43:e2ed12d17f06 | 110 | * } |
emilmont | 43:e2ed12d17f06 | 111 | * } |
emilmont | 43:e2ed12d17f06 | 112 | * @endcode |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 113 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 114 | CAN(PinName rd, PinName td); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 115 | virtual ~CAN(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 116 | |
emilmont | 43:e2ed12d17f06 | 117 | /** Set the frequency of the CAN interface |
emilmont | 43:e2ed12d17f06 | 118 | * |
emilmont | 43:e2ed12d17f06 | 119 | * @param hz The bus frequency in hertz |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 120 | * |
emilmont | 43:e2ed12d17f06 | 121 | * @returns |
emilmont | 43:e2ed12d17f06 | 122 | * 1 if successful, |
emilmont | 43:e2ed12d17f06 | 123 | * 0 otherwise |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 124 | */ |
simon | 21:3944f1e2fa4f | 125 | int frequency(int hz); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 126 | |
emilmont | 43:e2ed12d17f06 | 127 | /** Write a CANMessage to the bus. |
emilmont | 43:e2ed12d17f06 | 128 | * |
emilmont | 43:e2ed12d17f06 | 129 | * @param msg The CANMessage to write. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 130 | * |
emilmont | 43:e2ed12d17f06 | 131 | * @returns |
emilmont | 43:e2ed12d17f06 | 132 | * 0 if write failed, |
emilmont | 43:e2ed12d17f06 | 133 | * 1 if write was successful |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 134 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 135 | int write(CANMessage msg); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 136 | |
emilmont | 43:e2ed12d17f06 | 137 | /** Read a CANMessage from the bus. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 138 | * |
emilmont | 43:e2ed12d17f06 | 139 | * @param msg A CANMessage to read to. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 140 | * |
emilmont | 43:e2ed12d17f06 | 141 | * @returns |
emilmont | 43:e2ed12d17f06 | 142 | * 0 if no message arrived, |
emilmont | 43:e2ed12d17f06 | 143 | * 1 if message arrived |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 144 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 145 | int read(CANMessage &msg); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 146 | |
emilmont | 43:e2ed12d17f06 | 147 | /** Reset CAN interface. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 148 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 149 | * To use after error overflow. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 150 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 151 | void reset(); |
emilmont | 44:24d45a770a51 | 152 | |
emilmont | 43:e2ed12d17f06 | 153 | /** Puts or removes the CAN interface into silent monitoring mode |
simon | 22:9114680c05da | 154 | * |
emilmont | 43:e2ed12d17f06 | 155 | * @param silent boolean indicating whether to go into silent mode or not |
simon | 22:9114680c05da | 156 | */ |
simon | 22:9114680c05da | 157 | void monitor(bool silent); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 158 | |
emilmont | 43:e2ed12d17f06 | 159 | /** Returns number of read errors to detect read overflow errors. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 160 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 161 | unsigned char rderror(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 162 | |
emilmont | 43:e2ed12d17f06 | 163 | /** Returns number of write errors to detect write overflow errors. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 164 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 165 | unsigned char tderror(); |
simon | 22:9114680c05da | 166 | |
emilmont | 43:e2ed12d17f06 | 167 | /** Attach a function to call whenever a CAN frame received interrupt is |
simon | 22:9114680c05da | 168 | * generated. |
simon | 22:9114680c05da | 169 | * |
emilmont | 43:e2ed12d17f06 | 170 | * @param fptr A pointer to a void function, or 0 to set as none |
simon | 22:9114680c05da | 171 | */ |
simon | 22:9114680c05da | 172 | void attach(void (*fptr)(void)); |
simon | 22:9114680c05da | 173 | |
emilmont | 43:e2ed12d17f06 | 174 | /** Attach a member function to call whenever a CAN frame received interrupt |
simon | 22:9114680c05da | 175 | * is generated. |
simon | 22:9114680c05da | 176 | * |
emilmont | 43:e2ed12d17f06 | 177 | * @param tptr pointer to the object to call the member function on |
emilmont | 43:e2ed12d17f06 | 178 | * @param mptr pointer to the member function to be called |
simon | 22:9114680c05da | 179 | */ |
simon | 22:9114680c05da | 180 | template<typename T> |
emilmont | 33:5364839841bd | 181 | void attach(T* tptr, void (T::*mptr)(void)) { |
emilmont | 33:5364839841bd | 182 | if((mptr != NULL) && (tptr != NULL)) { |
emilmont | 33:5364839841bd | 183 | _rxirq.attach(tptr, mptr); |
emilmont | 33:5364839841bd | 184 | setup_interrupt(); |
emilmont | 33:5364839841bd | 185 | } else { |
emilmont | 33:5364839841bd | 186 | remove_interrupt(); |
emilmont | 33:5364839841bd | 187 | } |
emilmont | 33:5364839841bd | 188 | } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 189 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 190 | private: |
emilmont | 44:24d45a770a51 | 191 | can_t _can; |
simon | 22:9114680c05da | 192 | FunctionPointer _rxirq; |
emilmont | 44:24d45a770a51 | 193 | |
simon | 22:9114680c05da | 194 | void setup_interrupt(void); |
simon | 22:9114680c05da | 195 | void remove_interrupt(void); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 196 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 197 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 198 | } // namespace mbed |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 199 | |
emilmont | 44:24d45a770a51 | 200 | #endif |
emilmont | 27:7110ebee3484 | 201 | |
emilmont | 44:24d45a770a51 | 202 | #endif // MBED_CAN_H |