Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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