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