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

Fork of mbed-dev by mbed official

Committer:
apluscw
Date:
Fri Jul 20 21:24:42 2018 +0000
Revision:
187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apluscw 187:92cbb9eec47b 1 /* mbed Microcontroller Library
apluscw 187:92cbb9eec47b 2 * Copyright (c) 2006-2013 ARM Limited
apluscw 187:92cbb9eec47b 3 *
apluscw 187:92cbb9eec47b 4 * Licensed under the Apache License, Version 2.0 (the "License");
apluscw 187:92cbb9eec47b 5 * you may not use this file except in compliance with the License.
apluscw 187:92cbb9eec47b 6 * You may obtain a copy of the License at
apluscw 187:92cbb9eec47b 7 *
apluscw 187:92cbb9eec47b 8 * http://www.apache.org/licenses/LICENSE-2.0
apluscw 187:92cbb9eec47b 9 *
apluscw 187:92cbb9eec47b 10 * Unless required by applicable law or agreed to in writing, software
apluscw 187:92cbb9eec47b 11 * distributed under the License is distributed on an "AS IS" BASIS,
apluscw 187:92cbb9eec47b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
apluscw 187:92cbb9eec47b 13 * See the License for the specific language governing permissions and
apluscw 187:92cbb9eec47b 14 * limitations under the License.
apluscw 187:92cbb9eec47b 15 */
apluscw 187:92cbb9eec47b 16 #include "InterruptIn.h"
apluscw 187:92cbb9eec47b 17
apluscw 187:92cbb9eec47b 18 #if DEVICE_INTERRUPTIN
apluscw 187:92cbb9eec47b 19
apluscw 187:92cbb9eec47b 20 namespace mbed {
apluscw 187:92cbb9eec47b 21
apluscw 187:92cbb9eec47b 22 InterruptIn::InterruptIn(PinName pin) : gpio(),
apluscw 187:92cbb9eec47b 23 gpio_irq(),
apluscw 187:92cbb9eec47b 24 _rise(),
apluscw 187:92cbb9eec47b 25 _fall() {
apluscw 187:92cbb9eec47b 26 // No lock needed in the constructor
apluscw 187:92cbb9eec47b 27 gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
apluscw 187:92cbb9eec47b 28 gpio_init_in(&gpio, pin);
apluscw 187:92cbb9eec47b 29 }
apluscw 187:92cbb9eec47b 30
apluscw 187:92cbb9eec47b 31 InterruptIn::~InterruptIn() {
apluscw 187:92cbb9eec47b 32 // No lock needed in the destructor
apluscw 187:92cbb9eec47b 33 gpio_irq_free(&gpio_irq);
apluscw 187:92cbb9eec47b 34 }
apluscw 187:92cbb9eec47b 35
apluscw 187:92cbb9eec47b 36 int InterruptIn::read() {
apluscw 187:92cbb9eec47b 37 // Read only
apluscw 187:92cbb9eec47b 38 return gpio_read(&gpio);
apluscw 187:92cbb9eec47b 39 }
apluscw 187:92cbb9eec47b 40
apluscw 187:92cbb9eec47b 41 void InterruptIn::mode(PinMode pull) {
apluscw 187:92cbb9eec47b 42 core_util_critical_section_enter();
apluscw 187:92cbb9eec47b 43 gpio_mode(&gpio, pull);
apluscw 187:92cbb9eec47b 44 core_util_critical_section_exit();
apluscw 187:92cbb9eec47b 45 }
apluscw 187:92cbb9eec47b 46
apluscw 187:92cbb9eec47b 47 void InterruptIn::rise(Callback<void()> func) {
apluscw 187:92cbb9eec47b 48 core_util_critical_section_enter();
apluscw 187:92cbb9eec47b 49 if (func) {
apluscw 187:92cbb9eec47b 50 _rise.attach(func);
apluscw 187:92cbb9eec47b 51 gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
apluscw 187:92cbb9eec47b 52 } else {
apluscw 187:92cbb9eec47b 53 _rise.attach(NULL);
apluscw 187:92cbb9eec47b 54 gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
apluscw 187:92cbb9eec47b 55 }
apluscw 187:92cbb9eec47b 56 core_util_critical_section_exit();
apluscw 187:92cbb9eec47b 57 }
apluscw 187:92cbb9eec47b 58
apluscw 187:92cbb9eec47b 59 void InterruptIn::fall(Callback<void()> func) {
apluscw 187:92cbb9eec47b 60 core_util_critical_section_enter();
apluscw 187:92cbb9eec47b 61 if (func) {
apluscw 187:92cbb9eec47b 62 _fall.attach(func);
apluscw 187:92cbb9eec47b 63 gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
apluscw 187:92cbb9eec47b 64 } else {
apluscw 187:92cbb9eec47b 65 _fall.attach(NULL);
apluscw 187:92cbb9eec47b 66 gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
apluscw 187:92cbb9eec47b 67 }
apluscw 187:92cbb9eec47b 68 core_util_critical_section_exit();
apluscw 187:92cbb9eec47b 69 }
apluscw 187:92cbb9eec47b 70
apluscw 187:92cbb9eec47b 71 void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {
apluscw 187:92cbb9eec47b 72 InterruptIn *handler = (InterruptIn*)id;
apluscw 187:92cbb9eec47b 73 switch (event) {
apluscw 187:92cbb9eec47b 74 case IRQ_RISE: handler->_rise.call(); break;
apluscw 187:92cbb9eec47b 75 case IRQ_FALL: handler->_fall.call(); break;
apluscw 187:92cbb9eec47b 76 case IRQ_NONE: break;
apluscw 187:92cbb9eec47b 77 }
apluscw 187:92cbb9eec47b 78 }
apluscw 187:92cbb9eec47b 79
apluscw 187:92cbb9eec47b 80 void InterruptIn::enable_irq() {
apluscw 187:92cbb9eec47b 81 core_util_critical_section_enter();
apluscw 187:92cbb9eec47b 82 gpio_irq_enable(&gpio_irq);
apluscw 187:92cbb9eec47b 83 core_util_critical_section_exit();
apluscw 187:92cbb9eec47b 84 }
apluscw 187:92cbb9eec47b 85
apluscw 187:92cbb9eec47b 86 void InterruptIn::disable_irq() {
apluscw 187:92cbb9eec47b 87 core_util_critical_section_enter();
apluscw 187:92cbb9eec47b 88 gpio_irq_disable(&gpio_irq);
apluscw 187:92cbb9eec47b 89 core_util_critical_section_exit();
apluscw 187:92cbb9eec47b 90 }
apluscw 187:92cbb9eec47b 91
apluscw 187:92cbb9eec47b 92 InterruptIn::operator int() {
apluscw 187:92cbb9eec47b 93 // Underlying call is atomic
apluscw 187:92cbb9eec47b 94 return read();
apluscw 187:92cbb9eec47b 95 }
apluscw 187:92cbb9eec47b 96
apluscw 187:92cbb9eec47b 97 } // namespace mbed
apluscw 187:92cbb9eec47b 98
apluscw 187:92cbb9eec47b 99 #endif