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:
<>
Date:
Mon Jan 16 12:05:23 2017 +0000
Revision:
134:ad3be0349dc5
Parent:
128:9bcdf88f62b0
Child:
145:64910690c574
Release 134 of the mbed library

Ports for Upcoming Targets


Fixes and Changes

3488: Dev stm i2c v2 unitary functions https://github.com/ARMmbed/mbed-os/pull/3488
3492: Fix #3463 CAN read() return value https://github.com/ARMmbed/mbed-os/pull/3492
3503: [LPC15xx] Ensure that PWM=1 is resolved correctly https://github.com/ARMmbed/mbed-os/pull/3503
3504: [LPC15xx] CAN implementation improvements https://github.com/ARMmbed/mbed-os/pull/3504
3539: NUCLEO_F412ZG - Add support of TRNG peripheral https://github.com/ARMmbed/mbed-os/pull/3539
3540: STM: SPI: Initialize Rx in spi_master_write https://github.com/ARMmbed/mbed-os/pull/3540
3438: K64F: Add support for SERIAL ASYNCH API https://github.com/ARMmbed/mbed-os/pull/3438
3519: MCUXpresso: Fix ENET driver to enable interrupts after interrupt handler is set https://github.com/ARMmbed/mbed-os/pull/3519
3544: STM32L4 deepsleep improvement https://github.com/ARMmbed/mbed-os/pull/3544
3546: NUCLEO-F412ZG - Add CAN peripheral https://github.com/ARMmbed/mbed-os/pull/3546
3551: Fix I2C driver for RZ/A1H https://github.com/ARMmbed/mbed-os/pull/3551
3558: K64F UART Asynch API: Fix synchronization issue https://github.com/ARMmbed/mbed-os/pull/3558
3563: LPC4088 - Fix vector checksum https://github.com/ARMmbed/mbed-os/pull/3563
3567: Dev stm32 F0 v1.7.0 https://github.com/ARMmbed/mbed-os/pull/3567
3577: Fixes linking errors when building with debug profile https://github.com/ARMmbed/mbed-os/pull/3577

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