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