Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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