mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers InterruptIn.cpp Source File

InterruptIn.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "drivers/InterruptIn.h"
00018 
00019 #if DEVICE_INTERRUPTIN
00020 
00021 namespace mbed {
00022 
00023 // Note: This single-parameter constructor exists to maintain binary
00024 //       compatibility.
00025 //       If not for that, we could simplify by having only the 2-param
00026 //       constructor, with a default value for the PinMode.
00027 InterruptIn::InterruptIn(PinName pin) : gpio(),
00028     gpio_irq(),
00029     _rise(NULL),
00030     _fall(NULL)
00031 {
00032     // No lock needed in the constructor
00033     irq_init(pin);
00034     gpio_init_in(&gpio, pin);
00035 }
00036 
00037 InterruptIn::InterruptIn(PinName pin, PinMode mode) :
00038     gpio(),
00039     gpio_irq(),
00040     _rise(NULL),
00041     _fall(NULL)
00042 {
00043     // No lock needed in the constructor
00044     irq_init(pin);
00045     gpio_init_in_ex(&gpio, pin, mode);
00046 }
00047 
00048 void InterruptIn::irq_init(PinName pin)
00049 {
00050     gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
00051 }
00052 
00053 InterruptIn::~InterruptIn()
00054 {
00055     // No lock needed in the destructor
00056     gpio_irq_free(&gpio_irq);
00057 }
00058 
00059 int InterruptIn::read()
00060 {
00061     // Read only
00062     return gpio_read(&gpio);
00063 }
00064 
00065 void InterruptIn::mode(PinMode pull)
00066 {
00067     core_util_critical_section_enter();
00068     gpio_mode(&gpio, pull);
00069     core_util_critical_section_exit();
00070 }
00071 
00072 void InterruptIn::rise(Callback<void()> func)
00073 {
00074     core_util_critical_section_enter();
00075     if (func) {
00076         _rise = func;
00077         gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
00078     } else {
00079         _rise = NULL;
00080         gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
00081     }
00082     core_util_critical_section_exit();
00083 }
00084 
00085 void InterruptIn::fall(Callback<void()> func)
00086 {
00087     core_util_critical_section_enter();
00088     if (func) {
00089         _fall = func;
00090         gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
00091     } else {
00092         _fall = NULL;
00093         gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
00094     }
00095     core_util_critical_section_exit();
00096 }
00097 
00098 void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event)
00099 {
00100     InterruptIn *handler = (InterruptIn *)id;
00101     switch (event) {
00102         case IRQ_RISE:
00103             if (handler->_rise) {
00104                 handler->_rise();
00105             }
00106             break;
00107         case IRQ_FALL:
00108             if (handler->_fall) {
00109                 handler->_fall();
00110             }
00111             break;
00112         case IRQ_NONE:
00113             break;
00114     }
00115 }
00116 
00117 void InterruptIn::enable_irq()
00118 {
00119     core_util_critical_section_enter();
00120     gpio_irq_enable(&gpio_irq);
00121     core_util_critical_section_exit();
00122 }
00123 
00124 void InterruptIn::disable_irq()
00125 {
00126     core_util_critical_section_enter();
00127     gpio_irq_disable(&gpio_irq);
00128     core_util_critical_section_exit();
00129 }
00130 
00131 InterruptIn::operator int()
00132 {
00133     // Underlying call is atomic
00134     return read();
00135 }
00136 
00137 } // namespace mbed
00138 
00139 #endif