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