Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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