Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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