]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/common/InterruptIn.cpp
Merge commit 'fdc38ef3f92af7adeeb4de49550d8838c8a39b5c'
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / common / InterruptIn.cpp
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "InterruptIn.h"
17
18 #if DEVICE_INTERRUPTIN
19
20 namespace mbed {
21
22 InterruptIn::InterruptIn(PinName pin) : gpio(),
23                                         gpio_irq(),
24                                         _rise(),
25                                         _fall() {
26     gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
27     gpio_init_in(&gpio, pin);
28 }
29
30 InterruptIn::~InterruptIn() {
31     gpio_irq_free(&gpio_irq);
32 }
33
34 int InterruptIn::read() {
35     return gpio_read(&gpio);
36 }
37
38 void InterruptIn::mode(PinMode pull) {
39     gpio_mode(&gpio, pull);
40 }
41
42 void InterruptIn::rise(void (*fptr)(void)) {
43     if (fptr) {
44         _rise.attach(fptr);
45         gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
46     } else {
47         gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
48     }
49 }
50
51 void InterruptIn::fall(void (*fptr)(void)) {
52     if (fptr) {
53         _fall.attach(fptr);
54         gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
55     } else {
56         gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
57     }
58 }
59
60 void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {
61     InterruptIn *handler = (InterruptIn*)id;
62     switch (event) {
63         case IRQ_RISE: handler->_rise.call(); break;
64         case IRQ_FALL: handler->_fall.call(); break;
65         case IRQ_NONE: break;
66     }
67 }
68
69 void InterruptIn::enable_irq() {
70     gpio_irq_enable(&gpio_irq);
71 }
72
73 void InterruptIn::disable_irq() {
74     gpio_irq_disable(&gpio_irq);
75 }
76
77 #ifdef MBED_OPERATORS
78 InterruptIn::operator int() {
79     return read();
80 }
81 #endif
82
83 } // namespace mbed
84
85 #endif