mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
api/CAN.h@147:30b64687e01f, 2016-09-16 (annotated)
- Committer:
- <>
- Date:
- Fri Sep 16 16:24:25 2016 +0100
- Revision:
- 147:30b64687e01f
- Parent:
- 144:ef7eb2e8f9f7
This updates the lib to the mbed lib v126
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | */ |
<> | 144:ef7eb2e8f9f7 | 16 | #ifndef MBED_CAN_H |
<> | 144:ef7eb2e8f9f7 | 17 | #define MBED_CAN_H |
<> | 144:ef7eb2e8f9f7 | 18 | |
<> | 144:ef7eb2e8f9f7 | 19 | #include "platform.h" |
<> | 144:ef7eb2e8f9f7 | 20 | |
<> | 144:ef7eb2e8f9f7 | 21 | #if DEVICE_CAN |
<> | 144:ef7eb2e8f9f7 | 22 | |
<> | 144:ef7eb2e8f9f7 | 23 | #include "can_api.h" |
<> | 144:ef7eb2e8f9f7 | 24 | #include "can_helper.h" |
<> | 144:ef7eb2e8f9f7 | 25 | #include "Callback.h" |
<> | 144:ef7eb2e8f9f7 | 26 | #include "PlatformMutex.h" |
<> | 144:ef7eb2e8f9f7 | 27 | |
<> | 144:ef7eb2e8f9f7 | 28 | namespace mbed { |
<> | 144:ef7eb2e8f9f7 | 29 | |
<> | 144:ef7eb2e8f9f7 | 30 | /** CANMessage class |
<> | 144:ef7eb2e8f9f7 | 31 | * |
<> | 144:ef7eb2e8f9f7 | 32 | * @Note Synchronization level: Thread safe |
<> | 144:ef7eb2e8f9f7 | 33 | */ |
<> | 144:ef7eb2e8f9f7 | 34 | class CANMessage : public CAN_Message { |
<> | 144:ef7eb2e8f9f7 | 35 | |
<> | 144:ef7eb2e8f9f7 | 36 | public: |
<> | 144:ef7eb2e8f9f7 | 37 | /** Creates empty CAN message. |
<> | 144:ef7eb2e8f9f7 | 38 | */ |
<> | 144:ef7eb2e8f9f7 | 39 | CANMessage() : CAN_Message() { |
<> | 144:ef7eb2e8f9f7 | 40 | len = 8; |
<> | 144:ef7eb2e8f9f7 | 41 | type = CANData; |
<> | 144:ef7eb2e8f9f7 | 42 | format = CANStandard; |
<> | 144:ef7eb2e8f9f7 | 43 | id = 0; |
<> | 144:ef7eb2e8f9f7 | 44 | memset(data, 0, 8); |
<> | 144:ef7eb2e8f9f7 | 45 | } |
<> | 144:ef7eb2e8f9f7 | 46 | |
<> | 144:ef7eb2e8f9f7 | 47 | /** Creates CAN message with specific content. |
<> | 144:ef7eb2e8f9f7 | 48 | */ |
<> | 144:ef7eb2e8f9f7 | 49 | CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { |
<> | 144:ef7eb2e8f9f7 | 50 | len = _len & 0xF; |
<> | 144:ef7eb2e8f9f7 | 51 | type = _type; |
<> | 144:ef7eb2e8f9f7 | 52 | format = _format; |
<> | 144:ef7eb2e8f9f7 | 53 | id = _id; |
<> | 144:ef7eb2e8f9f7 | 54 | memcpy(data, _data, _len); |
<> | 144:ef7eb2e8f9f7 | 55 | } |
<> | 144:ef7eb2e8f9f7 | 56 | |
<> | 144:ef7eb2e8f9f7 | 57 | /** Creates CAN remote message. |
<> | 144:ef7eb2e8f9f7 | 58 | */ |
<> | 144:ef7eb2e8f9f7 | 59 | CANMessage(int _id, CANFormat _format = CANStandard) { |
<> | 144:ef7eb2e8f9f7 | 60 | len = 0; |
<> | 144:ef7eb2e8f9f7 | 61 | type = CANRemote; |
<> | 144:ef7eb2e8f9f7 | 62 | format = _format; |
<> | 144:ef7eb2e8f9f7 | 63 | id = _id; |
<> | 144:ef7eb2e8f9f7 | 64 | memset(data, 0, 8); |
<> | 144:ef7eb2e8f9f7 | 65 | } |
<> | 144:ef7eb2e8f9f7 | 66 | }; |
<> | 144:ef7eb2e8f9f7 | 67 | |
<> | 144:ef7eb2e8f9f7 | 68 | /** A can bus client, used for communicating with can devices |
<> | 144:ef7eb2e8f9f7 | 69 | */ |
<> | 144:ef7eb2e8f9f7 | 70 | class CAN { |
<> | 144:ef7eb2e8f9f7 | 71 | |
<> | 144:ef7eb2e8f9f7 | 72 | public: |
<> | 144:ef7eb2e8f9f7 | 73 | /** Creates an CAN interface connected to specific pins. |
<> | 144:ef7eb2e8f9f7 | 74 | * |
<> | 144:ef7eb2e8f9f7 | 75 | * @param rd read from transmitter |
<> | 144:ef7eb2e8f9f7 | 76 | * @param td transmit to transmitter |
<> | 144:ef7eb2e8f9f7 | 77 | * |
<> | 144:ef7eb2e8f9f7 | 78 | * Example: |
<> | 144:ef7eb2e8f9f7 | 79 | * @code |
<> | 144:ef7eb2e8f9f7 | 80 | * #include "mbed.h" |
<> | 144:ef7eb2e8f9f7 | 81 | * |
<> | 144:ef7eb2e8f9f7 | 82 | * Ticker ticker; |
<> | 144:ef7eb2e8f9f7 | 83 | * DigitalOut led1(LED1); |
<> | 144:ef7eb2e8f9f7 | 84 | * DigitalOut led2(LED2); |
<> | 144:ef7eb2e8f9f7 | 85 | * CAN can1(p9, p10); |
<> | 144:ef7eb2e8f9f7 | 86 | * CAN can2(p30, p29); |
<> | 144:ef7eb2e8f9f7 | 87 | * |
<> | 144:ef7eb2e8f9f7 | 88 | * char counter = 0; |
<> | 144:ef7eb2e8f9f7 | 89 | * |
<> | 144:ef7eb2e8f9f7 | 90 | * void send() { |
<> | 144:ef7eb2e8f9f7 | 91 | * if(can1.write(CANMessage(1337, &counter, 1))) { |
<> | 144:ef7eb2e8f9f7 | 92 | * printf("Message sent: %d\n", counter); |
<> | 144:ef7eb2e8f9f7 | 93 | * counter++; |
<> | 144:ef7eb2e8f9f7 | 94 | * } |
<> | 144:ef7eb2e8f9f7 | 95 | * led1 = !led1; |
<> | 144:ef7eb2e8f9f7 | 96 | * } |
<> | 144:ef7eb2e8f9f7 | 97 | * |
<> | 144:ef7eb2e8f9f7 | 98 | * int main() { |
<> | 144:ef7eb2e8f9f7 | 99 | * ticker.attach(&send, 1); |
<> | 144:ef7eb2e8f9f7 | 100 | * CANMessage msg; |
<> | 144:ef7eb2e8f9f7 | 101 | * while(1) { |
<> | 144:ef7eb2e8f9f7 | 102 | * if(can2.read(msg)) { |
<> | 144:ef7eb2e8f9f7 | 103 | * printf("Message received: %d\n\n", msg.data[0]); |
<> | 144:ef7eb2e8f9f7 | 104 | * led2 = !led2; |
<> | 144:ef7eb2e8f9f7 | 105 | * } |
<> | 144:ef7eb2e8f9f7 | 106 | * wait(0.2); |
<> | 144:ef7eb2e8f9f7 | 107 | * } |
<> | 144:ef7eb2e8f9f7 | 108 | * } |
<> | 144:ef7eb2e8f9f7 | 109 | * @endcode |
<> | 144:ef7eb2e8f9f7 | 110 | */ |
<> | 144:ef7eb2e8f9f7 | 111 | CAN(PinName rd, PinName td); |
<> | 144:ef7eb2e8f9f7 | 112 | virtual ~CAN(); |
<> | 144:ef7eb2e8f9f7 | 113 | |
<> | 144:ef7eb2e8f9f7 | 114 | /** Set the frequency of the CAN interface |
<> | 144:ef7eb2e8f9f7 | 115 | * |
<> | 144:ef7eb2e8f9f7 | 116 | * @param hz The bus frequency in hertz |
<> | 144:ef7eb2e8f9f7 | 117 | * |
<> | 144:ef7eb2e8f9f7 | 118 | * @returns |
<> | 144:ef7eb2e8f9f7 | 119 | * 1 if successful, |
<> | 144:ef7eb2e8f9f7 | 120 | * 0 otherwise |
<> | 144:ef7eb2e8f9f7 | 121 | */ |
<> | 144:ef7eb2e8f9f7 | 122 | int frequency(int hz); |
<> | 144:ef7eb2e8f9f7 | 123 | |
<> | 144:ef7eb2e8f9f7 | 124 | /** Write a CANMessage to the bus. |
<> | 144:ef7eb2e8f9f7 | 125 | * |
<> | 144:ef7eb2e8f9f7 | 126 | * @param msg The CANMessage to write. |
<> | 144:ef7eb2e8f9f7 | 127 | * |
<> | 144:ef7eb2e8f9f7 | 128 | * @returns |
<> | 144:ef7eb2e8f9f7 | 129 | * 0 if write failed, |
<> | 144:ef7eb2e8f9f7 | 130 | * 1 if write was successful |
<> | 144:ef7eb2e8f9f7 | 131 | */ |
<> | 144:ef7eb2e8f9f7 | 132 | int write(CANMessage msg); |
<> | 144:ef7eb2e8f9f7 | 133 | |
<> | 144:ef7eb2e8f9f7 | 134 | /** Read a CANMessage from the bus. |
<> | 144:ef7eb2e8f9f7 | 135 | * |
<> | 144:ef7eb2e8f9f7 | 136 | * @param msg A CANMessage to read to. |
<> | 144:ef7eb2e8f9f7 | 137 | * @param handle message filter handle (0 for any message) |
<> | 144:ef7eb2e8f9f7 | 138 | * |
<> | 144:ef7eb2e8f9f7 | 139 | * @returns |
<> | 144:ef7eb2e8f9f7 | 140 | * 0 if no message arrived, |
<> | 144:ef7eb2e8f9f7 | 141 | * 1 if message arrived |
<> | 144:ef7eb2e8f9f7 | 142 | */ |
<> | 144:ef7eb2e8f9f7 | 143 | int read(CANMessage &msg, int handle = 0); |
<> | 144:ef7eb2e8f9f7 | 144 | |
<> | 144:ef7eb2e8f9f7 | 145 | /** Reset CAN interface. |
<> | 144:ef7eb2e8f9f7 | 146 | * |
<> | 144:ef7eb2e8f9f7 | 147 | * To use after error overflow. |
<> | 144:ef7eb2e8f9f7 | 148 | */ |
<> | 144:ef7eb2e8f9f7 | 149 | void reset(); |
<> | 144:ef7eb2e8f9f7 | 150 | |
<> | 144:ef7eb2e8f9f7 | 151 | /** Puts or removes the CAN interface into silent monitoring mode |
<> | 144:ef7eb2e8f9f7 | 152 | * |
<> | 144:ef7eb2e8f9f7 | 153 | * @param silent boolean indicating whether to go into silent mode or not |
<> | 144:ef7eb2e8f9f7 | 154 | */ |
<> | 144:ef7eb2e8f9f7 | 155 | void monitor(bool silent); |
<> | 144:ef7eb2e8f9f7 | 156 | |
<> | 144:ef7eb2e8f9f7 | 157 | enum Mode { |
<> | 144:ef7eb2e8f9f7 | 158 | Reset = 0, |
<> | 144:ef7eb2e8f9f7 | 159 | Normal, |
<> | 144:ef7eb2e8f9f7 | 160 | Silent, |
<> | 144:ef7eb2e8f9f7 | 161 | LocalTest, |
<> | 144:ef7eb2e8f9f7 | 162 | GlobalTest, |
<> | 144:ef7eb2e8f9f7 | 163 | SilentTest |
<> | 144:ef7eb2e8f9f7 | 164 | }; |
<> | 144:ef7eb2e8f9f7 | 165 | |
<> | 144:ef7eb2e8f9f7 | 166 | /** Change CAN operation to the specified mode |
<> | 144:ef7eb2e8f9f7 | 167 | * |
<> | 144:ef7eb2e8f9f7 | 168 | * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest) |
<> | 144:ef7eb2e8f9f7 | 169 | * |
<> | 144:ef7eb2e8f9f7 | 170 | * @returns |
<> | 144:ef7eb2e8f9f7 | 171 | * 0 if mode change failed or unsupported, |
<> | 144:ef7eb2e8f9f7 | 172 | * 1 if mode change was successful |
<> | 144:ef7eb2e8f9f7 | 173 | */ |
<> | 144:ef7eb2e8f9f7 | 174 | int mode(Mode mode); |
<> | 144:ef7eb2e8f9f7 | 175 | |
<> | 144:ef7eb2e8f9f7 | 176 | /** Filter out incomming messages |
<> | 144:ef7eb2e8f9f7 | 177 | * |
<> | 144:ef7eb2e8f9f7 | 178 | * @param id the id to filter on |
<> | 144:ef7eb2e8f9f7 | 179 | * @param mask the mask applied to the id |
<> | 144:ef7eb2e8f9f7 | 180 | * @param format format to filter on (Default CANAny) |
<> | 144:ef7eb2e8f9f7 | 181 | * @param handle message filter handle (Optional) |
<> | 144:ef7eb2e8f9f7 | 182 | * |
<> | 144:ef7eb2e8f9f7 | 183 | * @returns |
<> | 144:ef7eb2e8f9f7 | 184 | * 0 if filter change failed or unsupported, |
<> | 144:ef7eb2e8f9f7 | 185 | * new filter handle if successful |
<> | 144:ef7eb2e8f9f7 | 186 | */ |
<> | 144:ef7eb2e8f9f7 | 187 | int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0); |
<> | 144:ef7eb2e8f9f7 | 188 | |
<> | 144:ef7eb2e8f9f7 | 189 | /** Returns number of read errors to detect read overflow errors. |
<> | 144:ef7eb2e8f9f7 | 190 | */ |
<> | 144:ef7eb2e8f9f7 | 191 | unsigned char rderror(); |
<> | 144:ef7eb2e8f9f7 | 192 | |
<> | 144:ef7eb2e8f9f7 | 193 | /** Returns number of write errors to detect write overflow errors. |
<> | 144:ef7eb2e8f9f7 | 194 | */ |
<> | 144:ef7eb2e8f9f7 | 195 | unsigned char tderror(); |
<> | 144:ef7eb2e8f9f7 | 196 | |
<> | 144:ef7eb2e8f9f7 | 197 | enum IrqType { |
<> | 144:ef7eb2e8f9f7 | 198 | RxIrq = 0, |
<> | 144:ef7eb2e8f9f7 | 199 | TxIrq, |
<> | 144:ef7eb2e8f9f7 | 200 | EwIrq, |
<> | 144:ef7eb2e8f9f7 | 201 | DoIrq, |
<> | 144:ef7eb2e8f9f7 | 202 | WuIrq, |
<> | 144:ef7eb2e8f9f7 | 203 | EpIrq, |
<> | 144:ef7eb2e8f9f7 | 204 | AlIrq, |
<> | 144:ef7eb2e8f9f7 | 205 | BeIrq, |
<> | 147:30b64687e01f | 206 | IdIrq, |
<> | 147:30b64687e01f | 207 | |
<> | 147:30b64687e01f | 208 | IrqCnt |
<> | 144:ef7eb2e8f9f7 | 209 | }; |
<> | 144:ef7eb2e8f9f7 | 210 | |
<> | 144:ef7eb2e8f9f7 | 211 | /** Attach a function to call whenever a CAN frame received interrupt is |
<> | 144:ef7eb2e8f9f7 | 212 | * generated. |
<> | 144:ef7eb2e8f9f7 | 213 | * |
<> | 144:ef7eb2e8f9f7 | 214 | * @param func A pointer to a void function, or 0 to set as none |
<> | 144:ef7eb2e8f9f7 | 215 | * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error) |
<> | 144:ef7eb2e8f9f7 | 216 | */ |
<> | 144:ef7eb2e8f9f7 | 217 | void attach(Callback<void()> func, IrqType type=RxIrq); |
<> | 144:ef7eb2e8f9f7 | 218 | |
<> | 144:ef7eb2e8f9f7 | 219 | /** Attach a member function to call whenever a CAN frame received interrupt |
<> | 144:ef7eb2e8f9f7 | 220 | * is generated. |
<> | 144:ef7eb2e8f9f7 | 221 | * |
<> | 144:ef7eb2e8f9f7 | 222 | * @param obj pointer to the object to call the member function on |
<> | 144:ef7eb2e8f9f7 | 223 | * @param method pointer to the member function to be called |
<> | 144:ef7eb2e8f9f7 | 224 | * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) |
<> | 144:ef7eb2e8f9f7 | 225 | */ |
<> | 144:ef7eb2e8f9f7 | 226 | template<typename T> |
<> | 144:ef7eb2e8f9f7 | 227 | void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) { |
<> | 144:ef7eb2e8f9f7 | 228 | // Underlying call thread safe |
<> | 144:ef7eb2e8f9f7 | 229 | attach(Callback<void()>(obj, method), type); |
<> | 144:ef7eb2e8f9f7 | 230 | } |
<> | 144:ef7eb2e8f9f7 | 231 | |
<> | 144:ef7eb2e8f9f7 | 232 | /** Attach a member function to call whenever a CAN frame received interrupt |
<> | 144:ef7eb2e8f9f7 | 233 | * is generated. |
<> | 144:ef7eb2e8f9f7 | 234 | * |
<> | 144:ef7eb2e8f9f7 | 235 | * @param obj pointer to the object to call the member function on |
<> | 144:ef7eb2e8f9f7 | 236 | * @param method pointer to the member function to be called |
<> | 144:ef7eb2e8f9f7 | 237 | * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) |
<> | 144:ef7eb2e8f9f7 | 238 | */ |
<> | 144:ef7eb2e8f9f7 | 239 | template<typename T> |
<> | 144:ef7eb2e8f9f7 | 240 | void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) { |
<> | 144:ef7eb2e8f9f7 | 241 | // Underlying call thread safe |
<> | 144:ef7eb2e8f9f7 | 242 | attach(Callback<void()>(obj, method), type); |
<> | 144:ef7eb2e8f9f7 | 243 | } |
<> | 144:ef7eb2e8f9f7 | 244 | |
<> | 144:ef7eb2e8f9f7 | 245 | static void _irq_handler(uint32_t id, CanIrqType type); |
<> | 144:ef7eb2e8f9f7 | 246 | |
<> | 144:ef7eb2e8f9f7 | 247 | protected: |
<> | 144:ef7eb2e8f9f7 | 248 | virtual void lock(); |
<> | 144:ef7eb2e8f9f7 | 249 | virtual void unlock(); |
<> | 144:ef7eb2e8f9f7 | 250 | can_t _can; |
<> | 147:30b64687e01f | 251 | Callback<void()> _irq[IrqCnt]; |
<> | 144:ef7eb2e8f9f7 | 252 | PlatformMutex _mutex; |
<> | 144:ef7eb2e8f9f7 | 253 | }; |
<> | 144:ef7eb2e8f9f7 | 254 | |
<> | 144:ef7eb2e8f9f7 | 255 | } // namespace mbed |
<> | 144:ef7eb2e8f9f7 | 256 | |
<> | 144:ef7eb2e8f9f7 | 257 | #endif |
<> | 144:ef7eb2e8f9f7 | 258 | |
<> | 144:ef7eb2e8f9f7 | 259 | #endif // MBED_CAN_H |