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