mbed library sources

Committer:
ebrus
Date:
Wed Jul 27 18:35:32 2016 +0000
Revision:
0:0a673c671a56
4

Who changed what in which revision?

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