Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/drivers/InterruptIn.cpp@0:2f0e1e23c242, 2018-04-13 (annotated)
- Committer:
- glebiuskv
- Date:
- Fri Apr 13 08:53:46 2018 +0000
- Revision:
- 0:2f0e1e23c242
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
glebiuskv | 0:2f0e1e23c242 | 1 | /* mbed Microcontroller Library |
glebiuskv | 0:2f0e1e23c242 | 2 | * Copyright (c) 2006-2013 ARM Limited |
glebiuskv | 0:2f0e1e23c242 | 3 | * |
glebiuskv | 0:2f0e1e23c242 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
glebiuskv | 0:2f0e1e23c242 | 5 | * you may not use this file except in compliance with the License. |
glebiuskv | 0:2f0e1e23c242 | 6 | * You may obtain a copy of the License at |
glebiuskv | 0:2f0e1e23c242 | 7 | * |
glebiuskv | 0:2f0e1e23c242 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
glebiuskv | 0:2f0e1e23c242 | 9 | * |
glebiuskv | 0:2f0e1e23c242 | 10 | * Unless required by applicable law or agreed to in writing, software |
glebiuskv | 0:2f0e1e23c242 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
glebiuskv | 0:2f0e1e23c242 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
glebiuskv | 0:2f0e1e23c242 | 13 | * See the License for the specific language governing permissions and |
glebiuskv | 0:2f0e1e23c242 | 14 | * limitations under the License. |
glebiuskv | 0:2f0e1e23c242 | 15 | */ |
glebiuskv | 0:2f0e1e23c242 | 16 | #include "drivers/InterruptIn.h" |
glebiuskv | 0:2f0e1e23c242 | 17 | |
glebiuskv | 0:2f0e1e23c242 | 18 | #if DEVICE_INTERRUPTIN |
glebiuskv | 0:2f0e1e23c242 | 19 | |
glebiuskv | 0:2f0e1e23c242 | 20 | namespace mbed { |
glebiuskv | 0:2f0e1e23c242 | 21 | |
glebiuskv | 0:2f0e1e23c242 | 22 | // Note: This single-parameter constructor exists to maintain binary |
glebiuskv | 0:2f0e1e23c242 | 23 | // compatibility. |
glebiuskv | 0:2f0e1e23c242 | 24 | // If not for that, we could simplify by having only the 2-param |
glebiuskv | 0:2f0e1e23c242 | 25 | // constructor, with a default value for the PinMode. |
glebiuskv | 0:2f0e1e23c242 | 26 | InterruptIn::InterruptIn(PinName pin) : gpio(), |
glebiuskv | 0:2f0e1e23c242 | 27 | gpio_irq(), |
glebiuskv | 0:2f0e1e23c242 | 28 | _rise(NULL), |
glebiuskv | 0:2f0e1e23c242 | 29 | _fall(NULL) { |
glebiuskv | 0:2f0e1e23c242 | 30 | // No lock needed in the constructor |
glebiuskv | 0:2f0e1e23c242 | 31 | irq_init(pin); |
glebiuskv | 0:2f0e1e23c242 | 32 | gpio_init_in(&gpio, pin); |
glebiuskv | 0:2f0e1e23c242 | 33 | } |
glebiuskv | 0:2f0e1e23c242 | 34 | |
glebiuskv | 0:2f0e1e23c242 | 35 | InterruptIn::InterruptIn(PinName pin, PinMode mode) : |
glebiuskv | 0:2f0e1e23c242 | 36 | gpio(), |
glebiuskv | 0:2f0e1e23c242 | 37 | gpio_irq(), |
glebiuskv | 0:2f0e1e23c242 | 38 | _rise(NULL), |
glebiuskv | 0:2f0e1e23c242 | 39 | _fall(NULL) { |
glebiuskv | 0:2f0e1e23c242 | 40 | // No lock needed in the constructor |
glebiuskv | 0:2f0e1e23c242 | 41 | irq_init(pin); |
glebiuskv | 0:2f0e1e23c242 | 42 | gpio_init_in_ex(&gpio, pin, mode); |
glebiuskv | 0:2f0e1e23c242 | 43 | } |
glebiuskv | 0:2f0e1e23c242 | 44 | |
glebiuskv | 0:2f0e1e23c242 | 45 | void InterruptIn::irq_init(PinName pin) { |
glebiuskv | 0:2f0e1e23c242 | 46 | gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); |
glebiuskv | 0:2f0e1e23c242 | 47 | } |
glebiuskv | 0:2f0e1e23c242 | 48 | |
glebiuskv | 0:2f0e1e23c242 | 49 | InterruptIn::~InterruptIn() { |
glebiuskv | 0:2f0e1e23c242 | 50 | // No lock needed in the destructor |
glebiuskv | 0:2f0e1e23c242 | 51 | gpio_irq_free(&gpio_irq); |
glebiuskv | 0:2f0e1e23c242 | 52 | } |
glebiuskv | 0:2f0e1e23c242 | 53 | |
glebiuskv | 0:2f0e1e23c242 | 54 | int InterruptIn::read() { |
glebiuskv | 0:2f0e1e23c242 | 55 | // Read only |
glebiuskv | 0:2f0e1e23c242 | 56 | return gpio_read(&gpio); |
glebiuskv | 0:2f0e1e23c242 | 57 | } |
glebiuskv | 0:2f0e1e23c242 | 58 | |
glebiuskv | 0:2f0e1e23c242 | 59 | void InterruptIn::mode(PinMode pull) { |
glebiuskv | 0:2f0e1e23c242 | 60 | core_util_critical_section_enter(); |
glebiuskv | 0:2f0e1e23c242 | 61 | gpio_mode(&gpio, pull); |
glebiuskv | 0:2f0e1e23c242 | 62 | core_util_critical_section_exit(); |
glebiuskv | 0:2f0e1e23c242 | 63 | } |
glebiuskv | 0:2f0e1e23c242 | 64 | |
glebiuskv | 0:2f0e1e23c242 | 65 | void InterruptIn::rise(Callback<void()> func) { |
glebiuskv | 0:2f0e1e23c242 | 66 | core_util_critical_section_enter(); |
glebiuskv | 0:2f0e1e23c242 | 67 | if (func) { |
glebiuskv | 0:2f0e1e23c242 | 68 | _rise = func; |
glebiuskv | 0:2f0e1e23c242 | 69 | gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
glebiuskv | 0:2f0e1e23c242 | 70 | } else { |
glebiuskv | 0:2f0e1e23c242 | 71 | _rise = NULL; |
glebiuskv | 0:2f0e1e23c242 | 72 | gpio_irq_set(&gpio_irq, IRQ_RISE, 0); |
glebiuskv | 0:2f0e1e23c242 | 73 | } |
glebiuskv | 0:2f0e1e23c242 | 74 | core_util_critical_section_exit(); |
glebiuskv | 0:2f0e1e23c242 | 75 | } |
glebiuskv | 0:2f0e1e23c242 | 76 | |
glebiuskv | 0:2f0e1e23c242 | 77 | void InterruptIn::fall(Callback<void()> func) { |
glebiuskv | 0:2f0e1e23c242 | 78 | core_util_critical_section_enter(); |
glebiuskv | 0:2f0e1e23c242 | 79 | if (func) { |
glebiuskv | 0:2f0e1e23c242 | 80 | _fall = func; |
glebiuskv | 0:2f0e1e23c242 | 81 | gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
glebiuskv | 0:2f0e1e23c242 | 82 | } else { |
glebiuskv | 0:2f0e1e23c242 | 83 | _fall = NULL; |
glebiuskv | 0:2f0e1e23c242 | 84 | gpio_irq_set(&gpio_irq, IRQ_FALL, 0); |
glebiuskv | 0:2f0e1e23c242 | 85 | } |
glebiuskv | 0:2f0e1e23c242 | 86 | core_util_critical_section_exit(); |
glebiuskv | 0:2f0e1e23c242 | 87 | } |
glebiuskv | 0:2f0e1e23c242 | 88 | |
glebiuskv | 0:2f0e1e23c242 | 89 | void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) { |
glebiuskv | 0:2f0e1e23c242 | 90 | InterruptIn *handler = (InterruptIn*)id; |
glebiuskv | 0:2f0e1e23c242 | 91 | switch (event) { |
glebiuskv | 0:2f0e1e23c242 | 92 | case IRQ_RISE: |
glebiuskv | 0:2f0e1e23c242 | 93 | if (handler->_rise) { |
glebiuskv | 0:2f0e1e23c242 | 94 | handler->_rise(); |
glebiuskv | 0:2f0e1e23c242 | 95 | } |
glebiuskv | 0:2f0e1e23c242 | 96 | break; |
glebiuskv | 0:2f0e1e23c242 | 97 | case IRQ_FALL: |
glebiuskv | 0:2f0e1e23c242 | 98 | if (handler->_fall) { |
glebiuskv | 0:2f0e1e23c242 | 99 | handler->_fall(); |
glebiuskv | 0:2f0e1e23c242 | 100 | } |
glebiuskv | 0:2f0e1e23c242 | 101 | break; |
glebiuskv | 0:2f0e1e23c242 | 102 | case IRQ_NONE: break; |
glebiuskv | 0:2f0e1e23c242 | 103 | } |
glebiuskv | 0:2f0e1e23c242 | 104 | } |
glebiuskv | 0:2f0e1e23c242 | 105 | |
glebiuskv | 0:2f0e1e23c242 | 106 | void InterruptIn::enable_irq() { |
glebiuskv | 0:2f0e1e23c242 | 107 | core_util_critical_section_enter(); |
glebiuskv | 0:2f0e1e23c242 | 108 | gpio_irq_enable(&gpio_irq); |
glebiuskv | 0:2f0e1e23c242 | 109 | core_util_critical_section_exit(); |
glebiuskv | 0:2f0e1e23c242 | 110 | } |
glebiuskv | 0:2f0e1e23c242 | 111 | |
glebiuskv | 0:2f0e1e23c242 | 112 | void InterruptIn::disable_irq() { |
glebiuskv | 0:2f0e1e23c242 | 113 | core_util_critical_section_enter(); |
glebiuskv | 0:2f0e1e23c242 | 114 | gpio_irq_disable(&gpio_irq); |
glebiuskv | 0:2f0e1e23c242 | 115 | core_util_critical_section_exit(); |
glebiuskv | 0:2f0e1e23c242 | 116 | } |
glebiuskv | 0:2f0e1e23c242 | 117 | |
glebiuskv | 0:2f0e1e23c242 | 118 | InterruptIn::operator int() { |
glebiuskv | 0:2f0e1e23c242 | 119 | // Underlying call is atomic |
glebiuskv | 0:2f0e1e23c242 | 120 | return read(); |
glebiuskv | 0:2f0e1e23c242 | 121 | } |
glebiuskv | 0:2f0e1e23c242 | 122 | |
glebiuskv | 0:2f0e1e23c242 | 123 | } // namespace mbed |
glebiuskv | 0:2f0e1e23c242 | 124 | |
glebiuskv | 0:2f0e1e23c242 | 125 | #endif |