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