Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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