mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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