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