dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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