Knight KE / Mbed OS Game_Master
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     // No lock needed in the constructor
00031     irq_init(pin);
00032     gpio_init_in(&gpio, pin);
00033 }
00034 
00035 InterruptIn::InterruptIn(PinName pin, PinMode mode) :
00036                                         gpio(),
00037                                         gpio_irq(),
00038                                         _rise(NULL),
00039                                         _fall(NULL) {
00040     // No lock needed in the constructor
00041     irq_init(pin);
00042     gpio_init_in_ex(&gpio, pin, mode);
00043 }
00044 
00045 void InterruptIn::irq_init(PinName pin) {
00046    gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
00047 }
00048 
00049 InterruptIn::~InterruptIn() {
00050     // No lock needed in the destructor
00051     gpio_irq_free(&gpio_irq);
00052 }
00053 
00054 int InterruptIn::read() {
00055     // Read only
00056     return gpio_read(&gpio);
00057 }
00058 
00059 void InterruptIn::mode(PinMode pull) {
00060     core_util_critical_section_enter();
00061     gpio_mode(&gpio, pull);
00062     core_util_critical_section_exit();
00063 }
00064 
00065 void InterruptIn::rise(Callback<void()> func) {
00066     core_util_critical_section_enter();
00067     if (func) {
00068         _rise = func;
00069         gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
00070     } else {
00071         _rise = NULL;
00072         gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
00073     }
00074     core_util_critical_section_exit();
00075 }
00076 
00077 void InterruptIn::fall(Callback<void()> func) {
00078     core_util_critical_section_enter();
00079     if (func) {
00080         _fall = func;
00081         gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
00082     } else {
00083         _fall = NULL;
00084         gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
00085     }
00086     core_util_critical_section_exit();
00087 }
00088 
00089 void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {
00090     InterruptIn *handler = (InterruptIn*)id;
00091     switch (event) {
00092         case IRQ_RISE: 
00093             if (handler->_rise) {
00094                 handler->_rise();
00095             }
00096             break;
00097         case IRQ_FALL: 
00098             if (handler->_fall) {
00099                 handler->_fall(); 
00100             }
00101             break;
00102         case IRQ_NONE: break;
00103     }
00104 }
00105 
00106 void InterruptIn::enable_irq() {
00107     core_util_critical_section_enter();
00108     gpio_irq_enable(&gpio_irq);
00109     core_util_critical_section_exit();
00110 }
00111 
00112 void InterruptIn::disable_irq() {
00113     core_util_critical_section_enter();
00114     gpio_irq_disable(&gpio_irq);
00115     core_util_critical_section_exit();
00116 }
00117 
00118 InterruptIn::operator int() {
00119     // Underlying call is atomic
00120     return read();
00121 }
00122 
00123 } // namespace mbed
00124 
00125 #endif