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