mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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