mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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