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