Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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