The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Kojto
Date:
Wed Jul 19 16:46:19 2017 +0100
Revision:
147:a97add6d7e64
Parent:
146:22da6e220af6
Child:
152:235179ab3f27
Release 147 of the mbed library.

Who changed what in which revision?

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