Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
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) { 00023 gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); 00024 gpio_init(&gpio, pin, PIN_INPUT); 00025 } 00026 00027 InterruptIn::~InterruptIn() { 00028 gpio_irq_free(&gpio_irq); 00029 } 00030 00031 int InterruptIn::read() { 00032 return gpio_read(&gpio); 00033 } 00034 00035 void InterruptIn::mode(PinMode pull) { 00036 gpio_mode(&gpio, pull); 00037 } 00038 00039 pFunctionPointer_t InterruptIn::rise(void (*fptr)(void)) { 00040 pFunctionPointer_t pf = NULL; 00041 _rise.clear(); 00042 if (fptr) { 00043 pf = _rise.add(fptr); 00044 gpio_irq_set(&gpio_irq, IRQ_RISE, 1); 00045 } else { 00046 gpio_irq_set(&gpio_irq, IRQ_RISE, 0); 00047 } 00048 return pf; 00049 } 00050 00051 pFunctionPointer_t InterruptIn::rise_add_common(void (*fptr)(void), bool front) { 00052 if (NULL == fptr) 00053 return NULL; 00054 pFunctionPointer_t pf = front ? _rise.add_front(fptr) : _rise.add(fptr); 00055 gpio_irq_set(&gpio_irq, IRQ_RISE, 1); 00056 return pf; 00057 } 00058 00059 bool InterruptIn::rise_remove(pFunctionPointer_t pf) { 00060 bool res = _rise.remove(pf); 00061 if (res && _rise.size() == 0) 00062 gpio_irq_set(&gpio_irq, IRQ_RISE, 0); 00063 return res; 00064 } 00065 00066 pFunctionPointer_t InterruptIn::fall(void (*fptr)(void)) { 00067 pFunctionPointer_t pf = NULL; 00068 _fall.clear(); 00069 if (fptr) { 00070 pf = _fall.add(fptr); 00071 gpio_irq_set(&gpio_irq, IRQ_FALL, 1); 00072 } else { 00073 gpio_irq_set(&gpio_irq, IRQ_FALL, 0); 00074 } 00075 return pf; 00076 } 00077 00078 pFunctionPointer_t InterruptIn::fall_add_common(void (*fptr)(void), bool front) { 00079 if (NULL == fptr) 00080 return NULL; 00081 pFunctionPointer_t pf = front ? _fall.add_front(fptr) : _fall.add(fptr); 00082 gpio_irq_set(&gpio_irq, IRQ_FALL, 1); 00083 return pf; 00084 } 00085 00086 bool InterruptIn::fall_remove(pFunctionPointer_t pf) { 00087 bool res = _fall.remove(pf); 00088 if (res && _fall.size() == 0) 00089 gpio_irq_set(&gpio_irq, IRQ_FALL, 0); 00090 return res; 00091 } 00092 00093 void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) { 00094 InterruptIn *handler = (InterruptIn*)id; 00095 switch (event) { 00096 case IRQ_RISE: handler->_rise.call(); break; 00097 case IRQ_FALL: handler->_fall.call(); break; 00098 case IRQ_NONE: break; 00099 } 00100 } 00101 00102 #ifdef MBED_OPERATORS 00103 InterruptIn::operator int() { 00104 return read(); 00105 } 00106 #endif 00107 00108 } // namespace mbed 00109 00110 #endif
Generated on Tue Jul 12 2022 20:46:37 by
1.7.2
