Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Tue Mar 09 13:10:40 2021 +0000
Revision:
3:6fe17b8a6d62
Parent:
0:4beb2ea291ec
SDBlockDevice added

Who changed what in which revision?

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