takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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