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
mbed library release version 165

Who changed what in which revision?

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