inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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