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