Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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