This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

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 "InterruptIn.h"
00017 
00018 #if DEVICE_INTERRUPTIN
00019 
00020 namespace mbed {
00021 
00022 InterruptIn::InterruptIn(PinName pin) : gpio(),
00023                                         gpio_irq(),
00024                                         _rise(),
00025                                         _fall() {
00026     // No lock needed in the constructor
00027     gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
00028     gpio_init_in(&gpio, pin);
00029 }
00030 
00031 InterruptIn::~InterruptIn() {
00032     // No lock needed in the destructor
00033     gpio_irq_free(&gpio_irq);
00034 }
00035 
00036 int InterruptIn::read() {
00037     // Read only
00038     return gpio_read(&gpio);
00039 }
00040 
00041 void InterruptIn::mode(PinMode pull) {
00042     core_util_critical_section_enter();
00043     gpio_mode(&gpio, pull);
00044     core_util_critical_section_exit();
00045 }
00046 
00047 void InterruptIn::rise(Callback<void()> func) {
00048     core_util_critical_section_enter();
00049     if (func) {
00050         _rise.attach(func);
00051         gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
00052     } else {
00053         _rise.attach(NULL);
00054         gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
00055     }
00056     core_util_critical_section_exit();
00057 }
00058 
00059 void InterruptIn::fall(Callback<void()> func) {
00060     core_util_critical_section_enter();
00061     if (func) {
00062         _fall.attach(func);
00063         gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
00064     } else {
00065         _fall.attach(NULL);
00066         gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
00067     }
00068     core_util_critical_section_exit();
00069 }
00070 
00071 void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {
00072     InterruptIn *handler = (InterruptIn*)id;
00073     switch (event) {
00074         case IRQ_RISE: handler->_rise.call(); break;
00075         case IRQ_FALL: handler->_fall.call(); break;
00076         case IRQ_NONE: break;
00077     }
00078 }
00079 
00080 void InterruptIn::enable_irq() {
00081     core_util_critical_section_enter();
00082     gpio_irq_enable(&gpio_irq);
00083     core_util_critical_section_exit();
00084 }
00085 
00086 void InterruptIn::disable_irq() {
00087     core_util_critical_section_enter();
00088     gpio_irq_disable(&gpio_irq);
00089     core_util_critical_section_exit();
00090 }
00091 
00092 InterruptIn::operator int() {
00093     // Underlying call is atomic
00094     return read();
00095 }
00096 
00097 } // namespace mbed
00098 
00099 #endif