4545
Fork of mbed-dev by
drivers/InterruptIn.cpp@166:33361e55dd8c, 2017-08-23 (annotated)
- Committer:
- iftaziz
- Date:
- Wed Aug 23 10:32:38 2017 +0000
- Revision:
- 166:33361e55dd8c
- Parent:
- 160:d5399cc887bb
r1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include "drivers/InterruptIn.h" |
<> | 149:156823d33999 | 17 | |
<> | 149:156823d33999 | 18 | #if DEVICE_INTERRUPTIN |
<> | 149:156823d33999 | 19 | |
<> | 149:156823d33999 | 20 | namespace mbed { |
<> | 149:156823d33999 | 21 | |
<> | 149:156823d33999 | 22 | static void donothing() {} |
<> | 149:156823d33999 | 23 | |
<> | 149:156823d33999 | 24 | InterruptIn::InterruptIn(PinName pin) : gpio(), |
<> | 149:156823d33999 | 25 | gpio_irq(), |
<> | 149:156823d33999 | 26 | _rise(), |
<> | 149:156823d33999 | 27 | _fall() { |
<> | 149:156823d33999 | 28 | // No lock needed in the constructor |
<> | 149:156823d33999 | 29 | |
<> | 160:d5399cc887bb | 30 | _rise = donothing; |
<> | 160:d5399cc887bb | 31 | _fall = donothing; |
<> | 149:156823d33999 | 32 | |
<> | 149:156823d33999 | 33 | gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); |
<> | 149:156823d33999 | 34 | gpio_init_in(&gpio, pin); |
<> | 149:156823d33999 | 35 | } |
<> | 149:156823d33999 | 36 | |
<> | 149:156823d33999 | 37 | InterruptIn::~InterruptIn() { |
<> | 149:156823d33999 | 38 | // No lock needed in the destructor |
<> | 149:156823d33999 | 39 | gpio_irq_free(&gpio_irq); |
<> | 149:156823d33999 | 40 | } |
<> | 149:156823d33999 | 41 | |
<> | 149:156823d33999 | 42 | int InterruptIn::read() { |
<> | 149:156823d33999 | 43 | // Read only |
<> | 149:156823d33999 | 44 | return gpio_read(&gpio); |
<> | 149:156823d33999 | 45 | } |
<> | 149:156823d33999 | 46 | |
<> | 149:156823d33999 | 47 | void InterruptIn::mode(PinMode pull) { |
<> | 149:156823d33999 | 48 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 49 | gpio_mode(&gpio, pull); |
<> | 149:156823d33999 | 50 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 51 | } |
<> | 149:156823d33999 | 52 | |
<> | 149:156823d33999 | 53 | void InterruptIn::rise(Callback<void()> func) { |
<> | 149:156823d33999 | 54 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 55 | if (func) { |
<> | 160:d5399cc887bb | 56 | _rise = func; |
<> | 149:156823d33999 | 57 | gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
<> | 149:156823d33999 | 58 | } else { |
<> | 160:d5399cc887bb | 59 | _rise = donothing; |
<> | 149:156823d33999 | 60 | gpio_irq_set(&gpio_irq, IRQ_RISE, 0); |
<> | 149:156823d33999 | 61 | } |
<> | 149:156823d33999 | 62 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 63 | } |
<> | 149:156823d33999 | 64 | |
<> | 149:156823d33999 | 65 | void InterruptIn::fall(Callback<void()> func) { |
<> | 149:156823d33999 | 66 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 67 | if (func) { |
<> | 160:d5399cc887bb | 68 | _fall = func; |
<> | 149:156823d33999 | 69 | gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
<> | 149:156823d33999 | 70 | } else { |
<> | 160:d5399cc887bb | 71 | _fall = donothing; |
<> | 149:156823d33999 | 72 | gpio_irq_set(&gpio_irq, IRQ_FALL, 0); |
<> | 149:156823d33999 | 73 | } |
<> | 149:156823d33999 | 74 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 75 | } |
<> | 149:156823d33999 | 76 | |
<> | 149:156823d33999 | 77 | void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) { |
<> | 149:156823d33999 | 78 | InterruptIn *handler = (InterruptIn*)id; |
<> | 149:156823d33999 | 79 | switch (event) { |
<> | 160:d5399cc887bb | 80 | case IRQ_RISE: handler->_rise(); break; |
<> | 160:d5399cc887bb | 81 | case IRQ_FALL: handler->_fall(); break; |
<> | 149:156823d33999 | 82 | case IRQ_NONE: break; |
<> | 149:156823d33999 | 83 | } |
<> | 149:156823d33999 | 84 | } |
<> | 149:156823d33999 | 85 | |
<> | 149:156823d33999 | 86 | void InterruptIn::enable_irq() { |
<> | 149:156823d33999 | 87 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 88 | gpio_irq_enable(&gpio_irq); |
<> | 149:156823d33999 | 89 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 90 | } |
<> | 149:156823d33999 | 91 | |
<> | 149:156823d33999 | 92 | void InterruptIn::disable_irq() { |
<> | 149:156823d33999 | 93 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 94 | gpio_irq_disable(&gpio_irq); |
<> | 149:156823d33999 | 95 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 96 | } |
<> | 149:156823d33999 | 97 | |
<> | 149:156823d33999 | 98 | InterruptIn::operator int() { |
<> | 149:156823d33999 | 99 | // Underlying call is atomic |
<> | 149:156823d33999 | 100 | return read(); |
<> | 149:156823d33999 | 101 | } |
<> | 149:156823d33999 | 102 | |
<> | 149:156823d33999 | 103 | } // namespace mbed |
<> | 149:156823d33999 | 104 | |
<> | 149:156823d33999 | 105 | #endif |