initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 /* mbed Microcontroller Library
yihui 0:638edba3adf6 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:638edba3adf6 3 *
yihui 0:638edba3adf6 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:638edba3adf6 5 * you may not use this file except in compliance with the License.
yihui 0:638edba3adf6 6 * You may obtain a copy of the License at
yihui 0:638edba3adf6 7 *
yihui 0:638edba3adf6 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:638edba3adf6 9 *
yihui 0:638edba3adf6 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:638edba3adf6 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:638edba3adf6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:638edba3adf6 13 * See the License for the specific language governing permissions and
yihui 0:638edba3adf6 14 * limitations under the License.
yihui 0:638edba3adf6 15 */
yihui 0:638edba3adf6 16 #ifndef MBED_INTERRUPTIN_H
yihui 0:638edba3adf6 17 #define MBED_INTERRUPTIN_H
yihui 0:638edba3adf6 18
yihui 0:638edba3adf6 19 #include "platform.h"
yihui 0:638edba3adf6 20
yihui 0:638edba3adf6 21 #if DEVICE_INTERRUPTIN
yihui 0:638edba3adf6 22
yihui 0:638edba3adf6 23 #include "gpio_api.h"
yihui 0:638edba3adf6 24 #include "gpio_irq_api.h"
yihui 0:638edba3adf6 25 #include "FunctionPointer.h"
yihui 0:638edba3adf6 26
yihui 0:638edba3adf6 27 namespace mbed {
yihui 0:638edba3adf6 28
yihui 0:638edba3adf6 29 /** A digital interrupt input, used to call a function on a rising or falling edge
yihui 0:638edba3adf6 30 *
yihui 0:638edba3adf6 31 * Example:
yihui 0:638edba3adf6 32 * @code
yihui 0:638edba3adf6 33 * // Flash an LED while waiting for events
yihui 0:638edba3adf6 34 *
yihui 0:638edba3adf6 35 * #include "mbed.h"
yihui 0:638edba3adf6 36 *
yihui 0:638edba3adf6 37 * InterruptIn event(p16);
yihui 0:638edba3adf6 38 * DigitalOut led(LED1);
yihui 0:638edba3adf6 39 *
yihui 0:638edba3adf6 40 * void trigger() {
yihui 0:638edba3adf6 41 * printf("triggered!\n");
yihui 0:638edba3adf6 42 * }
yihui 0:638edba3adf6 43 *
yihui 0:638edba3adf6 44 * int main() {
yihui 0:638edba3adf6 45 * event.rise(&trigger);
yihui 0:638edba3adf6 46 * while(1) {
yihui 0:638edba3adf6 47 * led = !led;
yihui 0:638edba3adf6 48 * wait(0.25);
yihui 0:638edba3adf6 49 * }
yihui 0:638edba3adf6 50 * }
yihui 0:638edba3adf6 51 * @endcode
yihui 0:638edba3adf6 52 */
yihui 0:638edba3adf6 53 class InterruptIn {
yihui 0:638edba3adf6 54
yihui 0:638edba3adf6 55 public:
yihui 0:638edba3adf6 56
yihui 0:638edba3adf6 57 /** Create an InterruptIn connected to the specified pin
yihui 0:638edba3adf6 58 *
yihui 0:638edba3adf6 59 * @param pin InterruptIn pin to connect to
yihui 0:638edba3adf6 60 * @param name (optional) A string to identify the object
yihui 0:638edba3adf6 61 */
yihui 0:638edba3adf6 62 InterruptIn(PinName pin);
yihui 0:638edba3adf6 63 virtual ~InterruptIn();
yihui 0:638edba3adf6 64
yihui 0:638edba3adf6 65 int read();
yihui 0:638edba3adf6 66 #ifdef MBED_OPERATORS
yihui 0:638edba3adf6 67 operator int();
yihui 0:638edba3adf6 68
yihui 0:638edba3adf6 69 #endif
yihui 0:638edba3adf6 70
yihui 0:638edba3adf6 71 /** Attach a function to call when a rising edge occurs on the input
yihui 0:638edba3adf6 72 *
yihui 0:638edba3adf6 73 * @param fptr A pointer to a void function, or 0 to set as none
yihui 0:638edba3adf6 74 */
yihui 0:638edba3adf6 75 void rise(void (*fptr)(void));
yihui 0:638edba3adf6 76
yihui 0:638edba3adf6 77 /** Attach a member function to call when a rising edge occurs on the input
yihui 0:638edba3adf6 78 *
yihui 0:638edba3adf6 79 * @param tptr pointer to the object to call the member function on
yihui 0:638edba3adf6 80 * @param mptr pointer to the member function to be called
yihui 0:638edba3adf6 81 */
yihui 0:638edba3adf6 82 template<typename T>
yihui 0:638edba3adf6 83 void rise(T* tptr, void (T::*mptr)(void)) {
yihui 0:638edba3adf6 84 _rise.attach(tptr, mptr);
yihui 0:638edba3adf6 85 gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
yihui 0:638edba3adf6 86 }
yihui 0:638edba3adf6 87
yihui 0:638edba3adf6 88 /** Attach a function to call when a falling edge occurs on the input
yihui 0:638edba3adf6 89 *
yihui 0:638edba3adf6 90 * @param fptr A pointer to a void function, or 0 to set as none
yihui 0:638edba3adf6 91 */
yihui 0:638edba3adf6 92 void fall(void (*fptr)(void));
yihui 0:638edba3adf6 93
yihui 0:638edba3adf6 94 /** Attach a member function to call when a falling edge occurs on the input
yihui 0:638edba3adf6 95 *
yihui 0:638edba3adf6 96 * @param tptr pointer to the object to call the member function on
yihui 0:638edba3adf6 97 * @param mptr pointer to the member function to be called
yihui 0:638edba3adf6 98 */
yihui 0:638edba3adf6 99 template<typename T>
yihui 0:638edba3adf6 100 void fall(T* tptr, void (T::*mptr)(void)) {
yihui 0:638edba3adf6 101 _fall.attach(tptr, mptr);
yihui 0:638edba3adf6 102 gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
yihui 0:638edba3adf6 103 }
yihui 0:638edba3adf6 104
yihui 0:638edba3adf6 105 /** Set the input pin mode
yihui 0:638edba3adf6 106 *
yihui 0:638edba3adf6 107 * @param mode PullUp, PullDown, PullNone
yihui 0:638edba3adf6 108 */
yihui 0:638edba3adf6 109 void mode(PinMode pull);
yihui 0:638edba3adf6 110
yihui 0:638edba3adf6 111 /** Enable IRQ. This method depends on hw implementation, might enable one
yihui 0:638edba3adf6 112 * port interrupts. For further information, check gpio_irq_enable().
yihui 0:638edba3adf6 113 */
yihui 0:638edba3adf6 114 void enable_irq();
yihui 0:638edba3adf6 115
yihui 0:638edba3adf6 116 /** Disable IRQ. This method depends on hw implementation, might disable one
yihui 0:638edba3adf6 117 * port interrupts. For further information, check gpio_irq_disable().
yihui 0:638edba3adf6 118 */
yihui 0:638edba3adf6 119 void disable_irq();
yihui 0:638edba3adf6 120
yihui 0:638edba3adf6 121 static void _irq_handler(uint32_t id, gpio_irq_event event);
yihui 0:638edba3adf6 122
yihui 0:638edba3adf6 123 protected:
yihui 0:638edba3adf6 124 gpio_t gpio;
yihui 0:638edba3adf6 125 gpio_irq_t gpio_irq;
yihui 0:638edba3adf6 126
yihui 0:638edba3adf6 127 FunctionPointer _rise;
yihui 0:638edba3adf6 128 FunctionPointer _fall;
yihui 0:638edba3adf6 129 };
yihui 0:638edba3adf6 130
yihui 0:638edba3adf6 131 } // namespace mbed
yihui 0:638edba3adf6 132
yihui 0:638edba3adf6 133 #endif
yihui 0:638edba3adf6 134
yihui 0:638edba3adf6 135 #endif