Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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