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 #ifndef MBED_INTERRUPTIN_H
marcozecchini 0:9fca2b23d0ba 17 #define MBED_INTERRUPTIN_H
marcozecchini 0:9fca2b23d0ba 18
marcozecchini 0:9fca2b23d0ba 19 #include "platform/platform.h"
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21 #if defined (DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
marcozecchini 0:9fca2b23d0ba 22
marcozecchini 0:9fca2b23d0ba 23 #include "hal/gpio_api.h"
marcozecchini 0:9fca2b23d0ba 24 #include "hal/gpio_irq_api.h"
marcozecchini 0:9fca2b23d0ba 25 #include "platform/Callback.h"
marcozecchini 0:9fca2b23d0ba 26 #include "platform/mbed_critical.h"
marcozecchini 0:9fca2b23d0ba 27 #include "platform/mbed_toolchain.h"
marcozecchini 0:9fca2b23d0ba 28 #include "platform/NonCopyable.h"
marcozecchini 0:9fca2b23d0ba 29
marcozecchini 0:9fca2b23d0ba 30 namespace mbed {
marcozecchini 0:9fca2b23d0ba 31 /** \addtogroup drivers */
marcozecchini 0:9fca2b23d0ba 32
marcozecchini 0:9fca2b23d0ba 33 /** A digital interrupt input, used to call a function on a rising or falling edge
marcozecchini 0:9fca2b23d0ba 34 *
marcozecchini 0:9fca2b23d0ba 35 * @note Synchronization level: Interrupt safe
marcozecchini 0:9fca2b23d0ba 36 *
marcozecchini 0:9fca2b23d0ba 37 * Example:
marcozecchini 0:9fca2b23d0ba 38 * @code
marcozecchini 0:9fca2b23d0ba 39 * // Flash an LED while waiting for events
marcozecchini 0:9fca2b23d0ba 40 *
marcozecchini 0:9fca2b23d0ba 41 * #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 42 *
marcozecchini 0:9fca2b23d0ba 43 * InterruptIn event(p16);
marcozecchini 0:9fca2b23d0ba 44 * DigitalOut led(LED1);
marcozecchini 0:9fca2b23d0ba 45 *
marcozecchini 0:9fca2b23d0ba 46 * void trigger() {
marcozecchini 0:9fca2b23d0ba 47 * printf("triggered!\n");
marcozecchini 0:9fca2b23d0ba 48 * }
marcozecchini 0:9fca2b23d0ba 49 *
marcozecchini 0:9fca2b23d0ba 50 * int main() {
marcozecchini 0:9fca2b23d0ba 51 * event.rise(&trigger);
marcozecchini 0:9fca2b23d0ba 52 * while(1) {
marcozecchini 0:9fca2b23d0ba 53 * led = !led;
marcozecchini 0:9fca2b23d0ba 54 * wait(0.25);
marcozecchini 0:9fca2b23d0ba 55 * }
marcozecchini 0:9fca2b23d0ba 56 * }
marcozecchini 0:9fca2b23d0ba 57 * @endcode
marcozecchini 0:9fca2b23d0ba 58 * @ingroup drivers
marcozecchini 0:9fca2b23d0ba 59 */
marcozecchini 0:9fca2b23d0ba 60 class InterruptIn : private NonCopyable<InterruptIn> {
marcozecchini 0:9fca2b23d0ba 61
marcozecchini 0:9fca2b23d0ba 62 public:
marcozecchini 0:9fca2b23d0ba 63
marcozecchini 0:9fca2b23d0ba 64 /** Create an InterruptIn connected to the specified pin
marcozecchini 0:9fca2b23d0ba 65 *
marcozecchini 0:9fca2b23d0ba 66 * @param pin InterruptIn pin to connect to
marcozecchini 0:9fca2b23d0ba 67 */
marcozecchini 0:9fca2b23d0ba 68 InterruptIn(PinName pin);
marcozecchini 0:9fca2b23d0ba 69 virtual ~InterruptIn();
marcozecchini 0:9fca2b23d0ba 70
marcozecchini 0:9fca2b23d0ba 71 /** Read the input, represented as 0 or 1 (int)
marcozecchini 0:9fca2b23d0ba 72 *
marcozecchini 0:9fca2b23d0ba 73 * @returns
marcozecchini 0:9fca2b23d0ba 74 * An integer representing the state of the input pin,
marcozecchini 0:9fca2b23d0ba 75 * 0 for logical 0, 1 for logical 1
marcozecchini 0:9fca2b23d0ba 76 */
marcozecchini 0:9fca2b23d0ba 77 int read();
marcozecchini 0:9fca2b23d0ba 78
marcozecchini 0:9fca2b23d0ba 79 /** An operator shorthand for read()
marcozecchini 0:9fca2b23d0ba 80 */
marcozecchini 0:9fca2b23d0ba 81 operator int();
marcozecchini 0:9fca2b23d0ba 82
marcozecchini 0:9fca2b23d0ba 83
marcozecchini 0:9fca2b23d0ba 84 /** Attach a function to call when a rising edge occurs on the input
marcozecchini 0:9fca2b23d0ba 85 *
marcozecchini 0:9fca2b23d0ba 86 * @param func A pointer to a void function, or 0 to set as none
marcozecchini 0:9fca2b23d0ba 87 */
marcozecchini 0:9fca2b23d0ba 88 void rise(Callback<void()> func);
marcozecchini 0:9fca2b23d0ba 89
marcozecchini 0:9fca2b23d0ba 90 /** Attach a member function to call when a rising edge occurs on the input
marcozecchini 0:9fca2b23d0ba 91 *
marcozecchini 0:9fca2b23d0ba 92 * @param obj pointer to the object to call the member function on
marcozecchini 0:9fca2b23d0ba 93 * @param method pointer to the member function to be called
marcozecchini 0:9fca2b23d0ba 94 * @deprecated
marcozecchini 0:9fca2b23d0ba 95 * The rise function does not support cv-qualifiers. Replaced by
marcozecchini 0:9fca2b23d0ba 96 * rise(callback(obj, method)).
marcozecchini 0:9fca2b23d0ba 97 */
marcozecchini 0:9fca2b23d0ba 98 template<typename T, typename M>
marcozecchini 0:9fca2b23d0ba 99 MBED_DEPRECATED_SINCE("mbed-os-5.1",
marcozecchini 0:9fca2b23d0ba 100 "The rise function does not support cv-qualifiers. Replaced by "
marcozecchini 0:9fca2b23d0ba 101 "rise(callback(obj, method)).")
marcozecchini 0:9fca2b23d0ba 102 void rise(T *obj, M method) {
marcozecchini 0:9fca2b23d0ba 103 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 104 rise(callback(obj, method));
marcozecchini 0:9fca2b23d0ba 105 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 106 }
marcozecchini 0:9fca2b23d0ba 107
marcozecchini 0:9fca2b23d0ba 108 /** Attach a function to call when a falling edge occurs on the input
marcozecchini 0:9fca2b23d0ba 109 *
marcozecchini 0:9fca2b23d0ba 110 * @param func A pointer to a void function, or 0 to set as none
marcozecchini 0:9fca2b23d0ba 111 */
marcozecchini 0:9fca2b23d0ba 112 void fall(Callback<void()> func);
marcozecchini 0:9fca2b23d0ba 113
marcozecchini 0:9fca2b23d0ba 114 /** Attach a member function to call when a falling edge occurs on the input
marcozecchini 0:9fca2b23d0ba 115 *
marcozecchini 0:9fca2b23d0ba 116 * @param obj pointer to the object to call the member function on
marcozecchini 0:9fca2b23d0ba 117 * @param method pointer to the member function to be called
marcozecchini 0:9fca2b23d0ba 118 * @deprecated
marcozecchini 0:9fca2b23d0ba 119 * The rise function does not support cv-qualifiers. Replaced by
marcozecchini 0:9fca2b23d0ba 120 * rise(callback(obj, method)).
marcozecchini 0:9fca2b23d0ba 121 */
marcozecchini 0:9fca2b23d0ba 122 template<typename T, typename M>
marcozecchini 0:9fca2b23d0ba 123 MBED_DEPRECATED_SINCE("mbed-os-5.1",
marcozecchini 0:9fca2b23d0ba 124 "The fall function does not support cv-qualifiers. Replaced by "
marcozecchini 0:9fca2b23d0ba 125 "fall(callback(obj, method)).")
marcozecchini 0:9fca2b23d0ba 126 void fall(T *obj, M method) {
marcozecchini 0:9fca2b23d0ba 127 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 128 fall(callback(obj, method));
marcozecchini 0:9fca2b23d0ba 129 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 130 }
marcozecchini 0:9fca2b23d0ba 131
marcozecchini 0:9fca2b23d0ba 132 /** Set the input pin mode
marcozecchini 0:9fca2b23d0ba 133 *
marcozecchini 0:9fca2b23d0ba 134 * @param pull PullUp, PullDown, PullNone
marcozecchini 0:9fca2b23d0ba 135 */
marcozecchini 0:9fca2b23d0ba 136 void mode(PinMode pull);
marcozecchini 0:9fca2b23d0ba 137
marcozecchini 0:9fca2b23d0ba 138 /** Enable IRQ. This method depends on hw implementation, might enable one
marcozecchini 0:9fca2b23d0ba 139 * port interrupts. For further information, check gpio_irq_enable().
marcozecchini 0:9fca2b23d0ba 140 */
marcozecchini 0:9fca2b23d0ba 141 void enable_irq();
marcozecchini 0:9fca2b23d0ba 142
marcozecchini 0:9fca2b23d0ba 143 /** Disable IRQ. This method depends on hw implementation, might disable one
marcozecchini 0:9fca2b23d0ba 144 * port interrupts. For further information, check gpio_irq_disable().
marcozecchini 0:9fca2b23d0ba 145 */
marcozecchini 0:9fca2b23d0ba 146 void disable_irq();
marcozecchini 0:9fca2b23d0ba 147
marcozecchini 0:9fca2b23d0ba 148 static void _irq_handler(uint32_t id, gpio_irq_event event);
marcozecchini 0:9fca2b23d0ba 149
marcozecchini 0:9fca2b23d0ba 150 protected:
marcozecchini 0:9fca2b23d0ba 151 gpio_t gpio;
marcozecchini 0:9fca2b23d0ba 152 gpio_irq_t gpio_irq;
marcozecchini 0:9fca2b23d0ba 153
marcozecchini 0:9fca2b23d0ba 154 Callback<void()> _rise;
marcozecchini 0:9fca2b23d0ba 155 Callback<void()> _fall;
marcozecchini 0:9fca2b23d0ba 156 };
marcozecchini 0:9fca2b23d0ba 157
marcozecchini 0:9fca2b23d0ba 158 } // namespace mbed
marcozecchini 0:9fca2b23d0ba 159
marcozecchini 0:9fca2b23d0ba 160 #endif
marcozecchini 0:9fca2b23d0ba 161
marcozecchini 0:9fca2b23d0ba 162 #endif