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