mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
Child:
167:e84263d55307
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

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