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