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_INTERRUPTIN_H
Bethory 0:6ad07c9019fd 17 #define MBED_INTERRUPTIN_H
Bethory 0:6ad07c9019fd 18
Bethory 0:6ad07c9019fd 19 #include "platform/platform.h"
Bethory 0:6ad07c9019fd 20
Bethory 0:6ad07c9019fd 21 #if defined (DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
Bethory 0:6ad07c9019fd 22
Bethory 0:6ad07c9019fd 23 #include "hal/gpio_api.h"
Bethory 0:6ad07c9019fd 24 #include "hal/gpio_irq_api.h"
Bethory 0:6ad07c9019fd 25 #include "platform/Callback.h"
Bethory 0:6ad07c9019fd 26 #include "platform/mbed_critical.h"
Bethory 0:6ad07c9019fd 27 #include "platform/mbed_toolchain.h"
Bethory 0:6ad07c9019fd 28 #include "platform/NonCopyable.h"
Bethory 0:6ad07c9019fd 29
Bethory 0:6ad07c9019fd 30 namespace mbed {
Bethory 0:6ad07c9019fd 31 /** \addtogroup drivers */
Bethory 0:6ad07c9019fd 32
Bethory 0:6ad07c9019fd 33 /** A digital interrupt input, used to call a function on a rising or falling edge
Bethory 0:6ad07c9019fd 34 *
Bethory 0:6ad07c9019fd 35 * @note Synchronization level: Interrupt safe
Bethory 0:6ad07c9019fd 36 *
Bethory 0:6ad07c9019fd 37 * Example:
Bethory 0:6ad07c9019fd 38 * @code
Bethory 0:6ad07c9019fd 39 * // Flash an LED while waiting for events
Bethory 0:6ad07c9019fd 40 *
Bethory 0:6ad07c9019fd 41 * #include "mbed.h"
Bethory 0:6ad07c9019fd 42 *
Bethory 0:6ad07c9019fd 43 * InterruptIn event(p16);
Bethory 0:6ad07c9019fd 44 * DigitalOut led(LED1);
Bethory 0:6ad07c9019fd 45 *
Bethory 0:6ad07c9019fd 46 * void trigger() {
Bethory 0:6ad07c9019fd 47 * printf("triggered!\n");
Bethory 0:6ad07c9019fd 48 * }
Bethory 0:6ad07c9019fd 49 *
Bethory 0:6ad07c9019fd 50 * int main() {
Bethory 0:6ad07c9019fd 51 * event.rise(&trigger);
Bethory 0:6ad07c9019fd 52 * while(1) {
Bethory 0:6ad07c9019fd 53 * led = !led;
Bethory 0:6ad07c9019fd 54 * wait(0.25);
Bethory 0:6ad07c9019fd 55 * }
Bethory 0:6ad07c9019fd 56 * }
Bethory 0:6ad07c9019fd 57 * @endcode
Bethory 0:6ad07c9019fd 58 * @ingroup drivers
Bethory 0:6ad07c9019fd 59 */
Bethory 0:6ad07c9019fd 60 class InterruptIn : private NonCopyable<InterruptIn> {
Bethory 0:6ad07c9019fd 61
Bethory 0:6ad07c9019fd 62 public:
Bethory 0:6ad07c9019fd 63
Bethory 0:6ad07c9019fd 64 /** Create an InterruptIn connected to the specified pin
Bethory 0:6ad07c9019fd 65 *
Bethory 0:6ad07c9019fd 66 * @param pin InterruptIn pin to connect to
Bethory 0:6ad07c9019fd 67 */
Bethory 0:6ad07c9019fd 68 InterruptIn(PinName pin);
Bethory 0:6ad07c9019fd 69
Bethory 0:6ad07c9019fd 70 /** Create an InterruptIn connected to the specified pin,
Bethory 0:6ad07c9019fd 71 * and the pin configured to the specified mode.
Bethory 0:6ad07c9019fd 72 *
Bethory 0:6ad07c9019fd 73 * @param pin InterruptIn pin to connect to
Bethory 0:6ad07c9019fd 74 * @param mode The mode to set the pin to (PullUp/PullDown/etc.)
Bethory 0:6ad07c9019fd 75 */
Bethory 0:6ad07c9019fd 76 InterruptIn(PinName pin, PinMode mode);
Bethory 0:6ad07c9019fd 77
Bethory 0:6ad07c9019fd 78 virtual ~InterruptIn();
Bethory 0:6ad07c9019fd 79
Bethory 0:6ad07c9019fd 80 /** Read the input, represented as 0 or 1 (int)
Bethory 0:6ad07c9019fd 81 *
Bethory 0:6ad07c9019fd 82 * @returns
Bethory 0:6ad07c9019fd 83 * An integer representing the state of the input pin,
Bethory 0:6ad07c9019fd 84 * 0 for logical 0, 1 for logical 1
Bethory 0:6ad07c9019fd 85 */
Bethory 0:6ad07c9019fd 86 int read();
Bethory 0:6ad07c9019fd 87
Bethory 0:6ad07c9019fd 88 /** An operator shorthand for read()
Bethory 0:6ad07c9019fd 89 */
Bethory 0:6ad07c9019fd 90 operator int();
Bethory 0:6ad07c9019fd 91
Bethory 0:6ad07c9019fd 92
Bethory 0:6ad07c9019fd 93 /** Attach a function to call when a rising edge occurs on the input
Bethory 0:6ad07c9019fd 94 *
Bethory 0:6ad07c9019fd 95 * @param func A pointer to a void function, or 0 to set as none
Bethory 0:6ad07c9019fd 96 */
Bethory 0:6ad07c9019fd 97 void rise(Callback<void()> func);
Bethory 0:6ad07c9019fd 98
Bethory 0:6ad07c9019fd 99 /** Attach a member function to call when a rising edge occurs on the input
Bethory 0:6ad07c9019fd 100 *
Bethory 0:6ad07c9019fd 101 * @param obj pointer to the object to call the member function on
Bethory 0:6ad07c9019fd 102 * @param method pointer to the member function to be called
Bethory 0:6ad07c9019fd 103 * @deprecated
Bethory 0:6ad07c9019fd 104 * The rise function does not support cv-qualifiers. Replaced by
Bethory 0:6ad07c9019fd 105 * rise(callback(obj, method)).
Bethory 0:6ad07c9019fd 106 */
Bethory 0:6ad07c9019fd 107 template<typename T, typename M>
Bethory 0:6ad07c9019fd 108 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Bethory 0:6ad07c9019fd 109 "The rise function does not support cv-qualifiers. Replaced by "
Bethory 0:6ad07c9019fd 110 "rise(callback(obj, method)).")
Bethory 0:6ad07c9019fd 111 void rise(T *obj, M method) {
Bethory 0:6ad07c9019fd 112 core_util_critical_section_enter();
Bethory 0:6ad07c9019fd 113 rise(callback(obj, method));
Bethory 0:6ad07c9019fd 114 core_util_critical_section_exit();
Bethory 0:6ad07c9019fd 115 }
Bethory 0:6ad07c9019fd 116
Bethory 0:6ad07c9019fd 117 /** Attach a function to call when a falling edge occurs on the input
Bethory 0:6ad07c9019fd 118 *
Bethory 0:6ad07c9019fd 119 * @param func A pointer to a void function, or 0 to set as none
Bethory 0:6ad07c9019fd 120 */
Bethory 0:6ad07c9019fd 121 void fall(Callback<void()> func);
Bethory 0:6ad07c9019fd 122
Bethory 0:6ad07c9019fd 123 /** Attach a member function to call when a falling edge occurs on the input
Bethory 0:6ad07c9019fd 124 *
Bethory 0:6ad07c9019fd 125 * @param obj pointer to the object to call the member function on
Bethory 0:6ad07c9019fd 126 * @param method pointer to the member function to be called
Bethory 0:6ad07c9019fd 127 * @deprecated
Bethory 0:6ad07c9019fd 128 * The rise function does not support cv-qualifiers. Replaced by
Bethory 0:6ad07c9019fd 129 * rise(callback(obj, method)).
Bethory 0:6ad07c9019fd 130 */
Bethory 0:6ad07c9019fd 131 template<typename T, typename M>
Bethory 0:6ad07c9019fd 132 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Bethory 0:6ad07c9019fd 133 "The fall function does not support cv-qualifiers. Replaced by "
Bethory 0:6ad07c9019fd 134 "fall(callback(obj, method)).")
Bethory 0:6ad07c9019fd 135 void fall(T *obj, M method) {
Bethory 0:6ad07c9019fd 136 core_util_critical_section_enter();
Bethory 0:6ad07c9019fd 137 fall(callback(obj, method));
Bethory 0:6ad07c9019fd 138 core_util_critical_section_exit();
Bethory 0:6ad07c9019fd 139 }
Bethory 0:6ad07c9019fd 140
Bethory 0:6ad07c9019fd 141 /** Set the input pin mode
Bethory 0:6ad07c9019fd 142 *
Bethory 0:6ad07c9019fd 143 * @param pull PullUp, PullDown, PullNone
Bethory 0:6ad07c9019fd 144 */
Bethory 0:6ad07c9019fd 145 void mode(PinMode pull);
Bethory 0:6ad07c9019fd 146
Bethory 0:6ad07c9019fd 147 /** Enable IRQ. This method depends on hw implementation, might enable one
Bethory 0:6ad07c9019fd 148 * port interrupts. For further information, check gpio_irq_enable().
Bethory 0:6ad07c9019fd 149 */
Bethory 0:6ad07c9019fd 150 void enable_irq();
Bethory 0:6ad07c9019fd 151
Bethory 0:6ad07c9019fd 152 /** Disable IRQ. This method depends on hw implementation, might disable one
Bethory 0:6ad07c9019fd 153 * port interrupts. For further information, check gpio_irq_disable().
Bethory 0:6ad07c9019fd 154 */
Bethory 0:6ad07c9019fd 155 void disable_irq();
Bethory 0:6ad07c9019fd 156
Bethory 0:6ad07c9019fd 157 static void _irq_handler(uint32_t id, gpio_irq_event event);
Bethory 0:6ad07c9019fd 158
Bethory 0:6ad07c9019fd 159 protected:
Bethory 0:6ad07c9019fd 160 gpio_t gpio;
Bethory 0:6ad07c9019fd 161 gpio_irq_t gpio_irq;
Bethory 0:6ad07c9019fd 162
Bethory 0:6ad07c9019fd 163 Callback<void()> _rise;
Bethory 0:6ad07c9019fd 164 Callback<void()> _fall;
Bethory 0:6ad07c9019fd 165
Bethory 0:6ad07c9019fd 166 void irq_init(PinName pin);
Bethory 0:6ad07c9019fd 167 };
Bethory 0:6ad07c9019fd 168
Bethory 0:6ad07c9019fd 169 } // namespace mbed
Bethory 0:6ad07c9019fd 170
Bethory 0:6ad07c9019fd 171 #endif
Bethory 0:6ad07c9019fd 172
Bethory 0:6ad07c9019fd 173 #endif