mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
pmcorreia
Date:
Tue Oct 09 14:42:37 2018 +0000
Revision:
187:fa51feb62426
Parent:
186:707f6e361f3e
Updated version to work with F446RE

Who changed what in which revision?

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