From Ben Katz mbed-dev library. Removed unnecessary target files to reduce the overall size by a factor of 10 to make it easier to import into the online IDE.
Dependents: motor_driver motor_driver_screaming_fix
drivers/InterruptIn.cpp@0:083111ae2a11, 2020-11-26 (annotated)
- Committer:
- saloutos
- Date:
- Thu Nov 26 04:08:56 2020 +0000
- Revision:
- 0:083111ae2a11
first commit of leaned mbed dev lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
saloutos | 0:083111ae2a11 | 1 | /* mbed Microcontroller Library |
saloutos | 0:083111ae2a11 | 2 | * Copyright (c) 2006-2013 ARM Limited |
saloutos | 0:083111ae2a11 | 3 | * |
saloutos | 0:083111ae2a11 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
saloutos | 0:083111ae2a11 | 5 | * you may not use this file except in compliance with the License. |
saloutos | 0:083111ae2a11 | 6 | * You may obtain a copy of the License at |
saloutos | 0:083111ae2a11 | 7 | * |
saloutos | 0:083111ae2a11 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
saloutos | 0:083111ae2a11 | 9 | * |
saloutos | 0:083111ae2a11 | 10 | * Unless required by applicable law or agreed to in writing, software |
saloutos | 0:083111ae2a11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
saloutos | 0:083111ae2a11 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
saloutos | 0:083111ae2a11 | 13 | * See the License for the specific language governing permissions and |
saloutos | 0:083111ae2a11 | 14 | * limitations under the License. |
saloutos | 0:083111ae2a11 | 15 | */ |
saloutos | 0:083111ae2a11 | 16 | #include "drivers/InterruptIn.h" |
saloutos | 0:083111ae2a11 | 17 | |
saloutos | 0:083111ae2a11 | 18 | #if DEVICE_INTERRUPTIN |
saloutos | 0:083111ae2a11 | 19 | |
saloutos | 0:083111ae2a11 | 20 | namespace mbed { |
saloutos | 0:083111ae2a11 | 21 | |
saloutos | 0:083111ae2a11 | 22 | InterruptIn::InterruptIn(PinName pin) : gpio(), |
saloutos | 0:083111ae2a11 | 23 | gpio_irq(), |
saloutos | 0:083111ae2a11 | 24 | _rise(NULL), |
saloutos | 0:083111ae2a11 | 25 | _fall(NULL) { |
saloutos | 0:083111ae2a11 | 26 | // No lock needed in the constructor |
saloutos | 0:083111ae2a11 | 27 | |
saloutos | 0:083111ae2a11 | 28 | gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); |
saloutos | 0:083111ae2a11 | 29 | gpio_init_in(&gpio, pin); |
saloutos | 0:083111ae2a11 | 30 | } |
saloutos | 0:083111ae2a11 | 31 | |
saloutos | 0:083111ae2a11 | 32 | InterruptIn::~InterruptIn() { |
saloutos | 0:083111ae2a11 | 33 | // No lock needed in the destructor |
saloutos | 0:083111ae2a11 | 34 | gpio_irq_free(&gpio_irq); |
saloutos | 0:083111ae2a11 | 35 | } |
saloutos | 0:083111ae2a11 | 36 | |
saloutos | 0:083111ae2a11 | 37 | int InterruptIn::read() { |
saloutos | 0:083111ae2a11 | 38 | // Read only |
saloutos | 0:083111ae2a11 | 39 | return gpio_read(&gpio); |
saloutos | 0:083111ae2a11 | 40 | } |
saloutos | 0:083111ae2a11 | 41 | |
saloutos | 0:083111ae2a11 | 42 | void InterruptIn::mode(PinMode pull) { |
saloutos | 0:083111ae2a11 | 43 | core_util_critical_section_enter(); |
saloutos | 0:083111ae2a11 | 44 | gpio_mode(&gpio, pull); |
saloutos | 0:083111ae2a11 | 45 | core_util_critical_section_exit(); |
saloutos | 0:083111ae2a11 | 46 | } |
saloutos | 0:083111ae2a11 | 47 | |
saloutos | 0:083111ae2a11 | 48 | void InterruptIn::rise(Callback<void()> func) { |
saloutos | 0:083111ae2a11 | 49 | core_util_critical_section_enter(); |
saloutos | 0:083111ae2a11 | 50 | if (func) { |
saloutos | 0:083111ae2a11 | 51 | _rise = func; |
saloutos | 0:083111ae2a11 | 52 | gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
saloutos | 0:083111ae2a11 | 53 | } else { |
saloutos | 0:083111ae2a11 | 54 | _rise = NULL; |
saloutos | 0:083111ae2a11 | 55 | gpio_irq_set(&gpio_irq, IRQ_RISE, 0); |
saloutos | 0:083111ae2a11 | 56 | } |
saloutos | 0:083111ae2a11 | 57 | core_util_critical_section_exit(); |
saloutos | 0:083111ae2a11 | 58 | } |
saloutos | 0:083111ae2a11 | 59 | |
saloutos | 0:083111ae2a11 | 60 | void InterruptIn::fall(Callback<void()> func) { |
saloutos | 0:083111ae2a11 | 61 | core_util_critical_section_enter(); |
saloutos | 0:083111ae2a11 | 62 | if (func) { |
saloutos | 0:083111ae2a11 | 63 | _fall = func; |
saloutos | 0:083111ae2a11 | 64 | gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
saloutos | 0:083111ae2a11 | 65 | } else { |
saloutos | 0:083111ae2a11 | 66 | _fall = NULL; |
saloutos | 0:083111ae2a11 | 67 | gpio_irq_set(&gpio_irq, IRQ_FALL, 0); |
saloutos | 0:083111ae2a11 | 68 | } |
saloutos | 0:083111ae2a11 | 69 | core_util_critical_section_exit(); |
saloutos | 0:083111ae2a11 | 70 | } |
saloutos | 0:083111ae2a11 | 71 | |
saloutos | 0:083111ae2a11 | 72 | void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) { |
saloutos | 0:083111ae2a11 | 73 | InterruptIn *handler = (InterruptIn*)id; |
saloutos | 0:083111ae2a11 | 74 | switch (event) { |
saloutos | 0:083111ae2a11 | 75 | case IRQ_RISE: |
saloutos | 0:083111ae2a11 | 76 | if (handler->_rise) { |
saloutos | 0:083111ae2a11 | 77 | handler->_rise(); |
saloutos | 0:083111ae2a11 | 78 | } |
saloutos | 0:083111ae2a11 | 79 | break; |
saloutos | 0:083111ae2a11 | 80 | case IRQ_FALL: |
saloutos | 0:083111ae2a11 | 81 | if (handler->_fall) { |
saloutos | 0:083111ae2a11 | 82 | handler->_fall(); |
saloutos | 0:083111ae2a11 | 83 | } |
saloutos | 0:083111ae2a11 | 84 | break; |
saloutos | 0:083111ae2a11 | 85 | case IRQ_NONE: break; |
saloutos | 0:083111ae2a11 | 86 | } |
saloutos | 0:083111ae2a11 | 87 | } |
saloutos | 0:083111ae2a11 | 88 | |
saloutos | 0:083111ae2a11 | 89 | void InterruptIn::enable_irq() { |
saloutos | 0:083111ae2a11 | 90 | core_util_critical_section_enter(); |
saloutos | 0:083111ae2a11 | 91 | gpio_irq_enable(&gpio_irq); |
saloutos | 0:083111ae2a11 | 92 | core_util_critical_section_exit(); |
saloutos | 0:083111ae2a11 | 93 | } |
saloutos | 0:083111ae2a11 | 94 | |
saloutos | 0:083111ae2a11 | 95 | void InterruptIn::disable_irq() { |
saloutos | 0:083111ae2a11 | 96 | core_util_critical_section_enter(); |
saloutos | 0:083111ae2a11 | 97 | gpio_irq_disable(&gpio_irq); |
saloutos | 0:083111ae2a11 | 98 | core_util_critical_section_exit(); |
saloutos | 0:083111ae2a11 | 99 | } |
saloutos | 0:083111ae2a11 | 100 | |
saloutos | 0:083111ae2a11 | 101 | InterruptIn::operator int() { |
saloutos | 0:083111ae2a11 | 102 | // Underlying call is atomic |
saloutos | 0:083111ae2a11 | 103 | return read(); |
saloutos | 0:083111ae2a11 | 104 | } |
saloutos | 0:083111ae2a11 | 105 | |
saloutos | 0:083111ae2a11 | 106 | } // namespace mbed |
saloutos | 0:083111ae2a11 | 107 | |
saloutos | 0:083111ae2a11 | 108 | #endif |