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