mbed library for NZ32-SC151

Committer:
modtronix
Date:
Fri Jul 24 21:01:44 2015 +1000
Revision:
1:71204b8406f2
Current mbed v103 (594)

Who changed what in which revision?

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