Rtos API example

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