001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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