mbed
Fork of mbed-dev by
drivers/InterruptIn.cpp@182:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 182:57724642e740
- Parent:
- 176:af195413fb11
mbed-dev library. Release version 159.
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 | InterruptIn::InterruptIn(PinName pin) : gpio(), |
<> | 149:156823d33999 | 23 | gpio_irq(), |
AnnaBridge | 176:af195413fb11 | 24 | _rise(NULL), |
AnnaBridge | 176:af195413fb11 | 25 | _fall(NULL) { |
<> | 149:156823d33999 | 26 | // No lock needed in the constructor |
<> | 149:156823d33999 | 27 | |
<> | 149:156823d33999 | 28 | gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); |
<> | 149:156823d33999 | 29 | gpio_init_in(&gpio, pin); |
<> | 149:156823d33999 | 30 | } |
<> | 149:156823d33999 | 31 | |
<> | 149:156823d33999 | 32 | InterruptIn::~InterruptIn() { |
<> | 149:156823d33999 | 33 | // No lock needed in the destructor |
<> | 149:156823d33999 | 34 | gpio_irq_free(&gpio_irq); |
<> | 149:156823d33999 | 35 | } |
<> | 149:156823d33999 | 36 | |
<> | 149:156823d33999 | 37 | int InterruptIn::read() { |
<> | 149:156823d33999 | 38 | // Read only |
<> | 149:156823d33999 | 39 | return gpio_read(&gpio); |
<> | 149:156823d33999 | 40 | } |
<> | 149:156823d33999 | 41 | |
<> | 149:156823d33999 | 42 | void InterruptIn::mode(PinMode pull) { |
<> | 149:156823d33999 | 43 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 44 | gpio_mode(&gpio, pull); |
<> | 149:156823d33999 | 45 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 46 | } |
<> | 149:156823d33999 | 47 | |
<> | 149:156823d33999 | 48 | void InterruptIn::rise(Callback<void()> func) { |
<> | 149:156823d33999 | 49 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 50 | if (func) { |
<> | 160:d5399cc887bb | 51 | _rise = func; |
<> | 149:156823d33999 | 52 | gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
<> | 149:156823d33999 | 53 | } else { |
AnnaBridge | 176:af195413fb11 | 54 | _rise = NULL; |
<> | 149:156823d33999 | 55 | gpio_irq_set(&gpio_irq, IRQ_RISE, 0); |
<> | 149:156823d33999 | 56 | } |
<> | 149:156823d33999 | 57 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 58 | } |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | void InterruptIn::fall(Callback<void()> func) { |
<> | 149:156823d33999 | 61 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 62 | if (func) { |
<> | 160:d5399cc887bb | 63 | _fall = func; |
<> | 149:156823d33999 | 64 | gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
<> | 149:156823d33999 | 65 | } else { |
AnnaBridge | 176:af195413fb11 | 66 | _fall = NULL; |
<> | 149:156823d33999 | 67 | gpio_irq_set(&gpio_irq, IRQ_FALL, 0); |
<> | 149:156823d33999 | 68 | } |
<> | 149:156823d33999 | 69 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 70 | } |
<> | 149:156823d33999 | 71 | |
<> | 149:156823d33999 | 72 | void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) { |
<> | 149:156823d33999 | 73 | InterruptIn *handler = (InterruptIn*)id; |
<> | 149:156823d33999 | 74 | switch (event) { |
AnnaBridge | 176:af195413fb11 | 75 | case IRQ_RISE: |
AnnaBridge | 176:af195413fb11 | 76 | if (handler->_rise) { |
AnnaBridge | 176:af195413fb11 | 77 | handler->_rise(); |
AnnaBridge | 176:af195413fb11 | 78 | } |
AnnaBridge | 176:af195413fb11 | 79 | break; |
AnnaBridge | 176:af195413fb11 | 80 | case IRQ_FALL: |
AnnaBridge | 176:af195413fb11 | 81 | if (handler->_fall) { |
AnnaBridge | 176:af195413fb11 | 82 | handler->_fall(); |
AnnaBridge | 176:af195413fb11 | 83 | } |
AnnaBridge | 176:af195413fb11 | 84 | break; |
<> | 149:156823d33999 | 85 | case IRQ_NONE: break; |
<> | 149:156823d33999 | 86 | } |
<> | 149:156823d33999 | 87 | } |
<> | 149:156823d33999 | 88 | |
<> | 149:156823d33999 | 89 | void InterruptIn::enable_irq() { |
<> | 149:156823d33999 | 90 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 91 | gpio_irq_enable(&gpio_irq); |
<> | 149:156823d33999 | 92 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 93 | } |
<> | 149:156823d33999 | 94 | |
<> | 149:156823d33999 | 95 | void InterruptIn::disable_irq() { |
<> | 149:156823d33999 | 96 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 97 | gpio_irq_disable(&gpio_irq); |
<> | 149:156823d33999 | 98 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 99 | } |
<> | 149:156823d33999 | 100 | |
<> | 149:156823d33999 | 101 | InterruptIn::operator int() { |
<> | 149:156823d33999 | 102 | // Underlying call is atomic |
<> | 149:156823d33999 | 103 | return read(); |
<> | 149:156823d33999 | 104 | } |
<> | 149:156823d33999 | 105 | |
<> | 149:156823d33999 | 106 | } // namespace mbed |
<> | 149:156823d33999 | 107 | |
<> | 149:156823d33999 | 108 | #endif |