CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Charles David Young 0:aa13e1c335cd 1 /* mbed Microcontroller Library
Charles David Young 0:aa13e1c335cd 2 * Copyright (c) 2006-2013 ARM Limited
Charles David Young 0:aa13e1c335cd 3 *
Charles David Young 0:aa13e1c335cd 4 * Licensed under the Apache License, Version 2.0 (the "License");
Charles David Young 0:aa13e1c335cd 5 * you may not use this file except in compliance with the License.
Charles David Young 0:aa13e1c335cd 6 * You may obtain a copy of the License at
Charles David Young 0:aa13e1c335cd 7 *
Charles David Young 0:aa13e1c335cd 8 * http://www.apache.org/licenses/LICENSE-2.0
Charles David Young 0:aa13e1c335cd 9 *
Charles David Young 0:aa13e1c335cd 10 * Unless required by applicable law or agreed to in writing, software
Charles David Young 0:aa13e1c335cd 11 * distributed under the License is distributed on an "AS IS" BASIS,
Charles David Young 0:aa13e1c335cd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Charles David Young 0:aa13e1c335cd 13 * See the License for the specific language governing permissions and
Charles David Young 0:aa13e1c335cd 14 * limitations under the License.
Charles David Young 0:aa13e1c335cd 15 */
Charles David Young 0:aa13e1c335cd 16 #ifndef MBED_INTERRUPTIN_H
Charles David Young 0:aa13e1c335cd 17 #define MBED_INTERRUPTIN_H
Charles David Young 0:aa13e1c335cd 18
Charles David Young 0:aa13e1c335cd 19 #include "platform/platform.h"
Charles David Young 0:aa13e1c335cd 20
Charles David Young 0:aa13e1c335cd 21 #if DEVICE_INTERRUPTIN
Charles David Young 0:aa13e1c335cd 22
Charles David Young 0:aa13e1c335cd 23 #include "hal/gpio_api.h"
Charles David Young 0:aa13e1c335cd 24 #include "hal/gpio_irq_api.h"
Charles David Young 0:aa13e1c335cd 25 #include "platform/Callback.h"
Charles David Young 0:aa13e1c335cd 26 #include "platform/critical.h"
Charles David Young 0:aa13e1c335cd 27 #include "platform/toolchain.h"
Charles David Young 0:aa13e1c335cd 28
Charles David Young 0:aa13e1c335cd 29 namespace mbed {
Charles David Young 0:aa13e1c335cd 30 /** \addtogroup drivers */
Charles David Young 0:aa13e1c335cd 31 /** @{*/
Charles David Young 0:aa13e1c335cd 32
Charles David Young 0:aa13e1c335cd 33 /** A digital interrupt input, used to call a function on a rising or falling edge
Charles David Young 0:aa13e1c335cd 34 *
Charles David Young 0:aa13e1c335cd 35 * @Note Synchronization level: Interrupt safe
Charles David Young 0:aa13e1c335cd 36 *
Charles David Young 0:aa13e1c335cd 37 * Example:
Charles David Young 0:aa13e1c335cd 38 * @code
Charles David Young 0:aa13e1c335cd 39 * // Flash an LED while waiting for events
Charles David Young 0:aa13e1c335cd 40 *
Charles David Young 0:aa13e1c335cd 41 * #include "mbed.h"
Charles David Young 0:aa13e1c335cd 42 *
Charles David Young 0:aa13e1c335cd 43 * InterruptIn event(p16);
Charles David Young 0:aa13e1c335cd 44 * DigitalOut led(LED1);
Charles David Young 0:aa13e1c335cd 45 *
Charles David Young 0:aa13e1c335cd 46 * void trigger() {
Charles David Young 0:aa13e1c335cd 47 * printf("triggered!\n");
Charles David Young 0:aa13e1c335cd 48 * }
Charles David Young 0:aa13e1c335cd 49 *
Charles David Young 0:aa13e1c335cd 50 * int main() {
Charles David Young 0:aa13e1c335cd 51 * event.rise(&trigger);
Charles David Young 0:aa13e1c335cd 52 * while(1) {
Charles David Young 0:aa13e1c335cd 53 * led = !led;
Charles David Young 0:aa13e1c335cd 54 * wait(0.25);
Charles David Young 0:aa13e1c335cd 55 * }
Charles David Young 0:aa13e1c335cd 56 * }
Charles David Young 0:aa13e1c335cd 57 * @endcode
Charles David Young 0:aa13e1c335cd 58 */
Charles David Young 0:aa13e1c335cd 59 class InterruptIn {
Charles David Young 0:aa13e1c335cd 60
Charles David Young 0:aa13e1c335cd 61 public:
Charles David Young 0:aa13e1c335cd 62
Charles David Young 0:aa13e1c335cd 63 /** Create an InterruptIn connected to the specified pin
Charles David Young 0:aa13e1c335cd 64 *
Charles David Young 0:aa13e1c335cd 65 * @param pin InterruptIn pin to connect to
Charles David Young 0:aa13e1c335cd 66 * @param name (optional) A string to identify the object
Charles David Young 0:aa13e1c335cd 67 */
Charles David Young 0:aa13e1c335cd 68 InterruptIn(PinName pin);
Charles David Young 0:aa13e1c335cd 69 virtual ~InterruptIn();
Charles David Young 0:aa13e1c335cd 70
Charles David Young 0:aa13e1c335cd 71 /** Read the input, represented as 0 or 1 (int)
Charles David Young 0:aa13e1c335cd 72 *
Charles David Young 0:aa13e1c335cd 73 * @returns
Charles David Young 0:aa13e1c335cd 74 * An integer representing the state of the input pin,
Charles David Young 0:aa13e1c335cd 75 * 0 for logical 0, 1 for logical 1
Charles David Young 0:aa13e1c335cd 76 */
Charles David Young 0:aa13e1c335cd 77 int read();
Charles David Young 0:aa13e1c335cd 78
Charles David Young 0:aa13e1c335cd 79 /** An operator shorthand for read()
Charles David Young 0:aa13e1c335cd 80 */
Charles David Young 0:aa13e1c335cd 81 operator int();
Charles David Young 0:aa13e1c335cd 82
Charles David Young 0:aa13e1c335cd 83
Charles David Young 0:aa13e1c335cd 84 /** Attach a function to call when a rising edge occurs on the input
Charles David Young 0:aa13e1c335cd 85 *
Charles David Young 0:aa13e1c335cd 86 * @param func A pointer to a void function, or 0 to set as none
Charles David Young 0:aa13e1c335cd 87 */
Charles David Young 0:aa13e1c335cd 88 void rise(Callback<void()> func);
Charles David Young 0:aa13e1c335cd 89
Charles David Young 0:aa13e1c335cd 90 /** Attach a member function to call when a rising edge occurs on the input
Charles David Young 0:aa13e1c335cd 91 *
Charles David Young 0:aa13e1c335cd 92 * @param obj pointer to the object to call the member function on
Charles David Young 0:aa13e1c335cd 93 * @param method pointer to the member function to be called
Charles David Young 0:aa13e1c335cd 94 * @deprecated
Charles David Young 0:aa13e1c335cd 95 * The rise function does not support cv-qualifiers. Replaced by
Charles David Young 0:aa13e1c335cd 96 * rise(callback(obj, method)).
Charles David Young 0:aa13e1c335cd 97 */
Charles David Young 0:aa13e1c335cd 98 template<typename T, typename M>
Charles David Young 0:aa13e1c335cd 99 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Charles David Young 0:aa13e1c335cd 100 "The rise function does not support cv-qualifiers. Replaced by "
Charles David Young 0:aa13e1c335cd 101 "rise(callback(obj, method)).")
Charles David Young 0:aa13e1c335cd 102 void rise(T *obj, M method) {
Charles David Young 0:aa13e1c335cd 103 core_util_critical_section_enter();
Charles David Young 0:aa13e1c335cd 104 rise(callback(obj, method));
Charles David Young 0:aa13e1c335cd 105 core_util_critical_section_exit();
Charles David Young 0:aa13e1c335cd 106 }
Charles David Young 0:aa13e1c335cd 107
Charles David Young 0:aa13e1c335cd 108 /** Attach a function to call when a falling edge occurs on the input
Charles David Young 0:aa13e1c335cd 109 *
Charles David Young 0:aa13e1c335cd 110 * @param func A pointer to a void function, or 0 to set as none
Charles David Young 0:aa13e1c335cd 111 */
Charles David Young 0:aa13e1c335cd 112 void fall(Callback<void()> func);
Charles David Young 0:aa13e1c335cd 113
Charles David Young 0:aa13e1c335cd 114 /** Attach a member function to call when a falling edge occurs on the input
Charles David Young 0:aa13e1c335cd 115 *
Charles David Young 0:aa13e1c335cd 116 * @param obj pointer to the object to call the member function on
Charles David Young 0:aa13e1c335cd 117 * @param method pointer to the member function to be called
Charles David Young 0:aa13e1c335cd 118 * @deprecated
Charles David Young 0:aa13e1c335cd 119 * The rise function does not support cv-qualifiers. Replaced by
Charles David Young 0:aa13e1c335cd 120 * rise(callback(obj, method)).
Charles David Young 0:aa13e1c335cd 121 */
Charles David Young 0:aa13e1c335cd 122 template<typename T, typename M>
Charles David Young 0:aa13e1c335cd 123 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Charles David Young 0:aa13e1c335cd 124 "The fall function does not support cv-qualifiers. Replaced by "
Charles David Young 0:aa13e1c335cd 125 "fall(callback(obj, method)).")
Charles David Young 0:aa13e1c335cd 126 void fall(T *obj, M method) {
Charles David Young 0:aa13e1c335cd 127 core_util_critical_section_enter();
Charles David Young 0:aa13e1c335cd 128 fall(callback(obj, method));
Charles David Young 0:aa13e1c335cd 129 core_util_critical_section_exit();
Charles David Young 0:aa13e1c335cd 130 }
Charles David Young 0:aa13e1c335cd 131
Charles David Young 0:aa13e1c335cd 132 /** Set the input pin mode
Charles David Young 0:aa13e1c335cd 133 *
Charles David Young 0:aa13e1c335cd 134 * @param mode PullUp, PullDown, PullNone
Charles David Young 0:aa13e1c335cd 135 */
Charles David Young 0:aa13e1c335cd 136 void mode(PinMode pull);
Charles David Young 0:aa13e1c335cd 137
Charles David Young 0:aa13e1c335cd 138 /** Enable IRQ. This method depends on hw implementation, might enable one
Charles David Young 0:aa13e1c335cd 139 * port interrupts. For further information, check gpio_irq_enable().
Charles David Young 0:aa13e1c335cd 140 */
Charles David Young 0:aa13e1c335cd 141 void enable_irq();
Charles David Young 0:aa13e1c335cd 142
Charles David Young 0:aa13e1c335cd 143 /** Disable IRQ. This method depends on hw implementation, might disable one
Charles David Young 0:aa13e1c335cd 144 * port interrupts. For further information, check gpio_irq_disable().
Charles David Young 0:aa13e1c335cd 145 */
Charles David Young 0:aa13e1c335cd 146 void disable_irq();
Charles David Young 0:aa13e1c335cd 147
Charles David Young 0:aa13e1c335cd 148 static void _irq_handler(uint32_t id, gpio_irq_event event);
Charles David Young 0:aa13e1c335cd 149
Charles David Young 0:aa13e1c335cd 150 protected:
Charles David Young 0:aa13e1c335cd 151 gpio_t gpio;
Charles David Young 0:aa13e1c335cd 152 gpio_irq_t gpio_irq;
Charles David Young 0:aa13e1c335cd 153
Charles David Young 0:aa13e1c335cd 154 Callback<void()> _rise;
Charles David Young 0:aa13e1c335cd 155 Callback<void()> _fall;
Charles David Young 0:aa13e1c335cd 156 };
Charles David Young 0:aa13e1c335cd 157
Charles David Young 0:aa13e1c335cd 158 } // namespace mbed
Charles David Young 0:aa13e1c335cd 159
Charles David Young 0:aa13e1c335cd 160 #endif
Charles David Young 0:aa13e1c335cd 161
Charles David Young 0:aa13e1c335cd 162 #endif
Charles David Young 0:aa13e1c335cd 163
Charles David Young 0:aa13e1c335cd 164 /** @}*/