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