Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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