5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
group-onsemi
Date:
Wed Jan 25 20:34:15 2017 +0000
Revision:
0:098463de4c5d
Initial commit

Who changed what in which revision?

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