mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
drivers/DigitalIn.h@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New 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_DIGITALIN_H |
elessair | 0:f269e3021894 | 17 | #define MBED_DIGITALIN_H |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #include "platform/platform.h" |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #include "hal/gpio_api.h" |
elessair | 0:f269e3021894 | 22 | #include "platform/critical.h" |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | namespace mbed { |
elessair | 0:f269e3021894 | 25 | /** \addtogroup drivers */ |
elessair | 0:f269e3021894 | 26 | /** @{*/ |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | /** A digital input, used for reading the state of a pin |
elessair | 0:f269e3021894 | 29 | * |
elessair | 0:f269e3021894 | 30 | * @Note Synchronization level: Interrupt safe |
elessair | 0:f269e3021894 | 31 | * |
elessair | 0:f269e3021894 | 32 | * Example: |
elessair | 0:f269e3021894 | 33 | * @code |
elessair | 0:f269e3021894 | 34 | * // Flash an LED while a DigitalIn is true |
elessair | 0:f269e3021894 | 35 | * |
elessair | 0:f269e3021894 | 36 | * #include "mbed.h" |
elessair | 0:f269e3021894 | 37 | * |
elessair | 0:f269e3021894 | 38 | * DigitalIn enable(p5); |
elessair | 0:f269e3021894 | 39 | * DigitalOut led(LED1); |
elessair | 0:f269e3021894 | 40 | * |
elessair | 0:f269e3021894 | 41 | * int main() { |
elessair | 0:f269e3021894 | 42 | * while(1) { |
elessair | 0:f269e3021894 | 43 | * if(enable) { |
elessair | 0:f269e3021894 | 44 | * led = !led; |
elessair | 0:f269e3021894 | 45 | * } |
elessair | 0:f269e3021894 | 46 | * wait(0.25); |
elessair | 0:f269e3021894 | 47 | * } |
elessair | 0:f269e3021894 | 48 | * } |
elessair | 0:f269e3021894 | 49 | * @endcode |
elessair | 0:f269e3021894 | 50 | */ |
elessair | 0:f269e3021894 | 51 | class DigitalIn { |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | public: |
elessair | 0:f269e3021894 | 54 | /** Create a DigitalIn connected to the specified pin |
elessair | 0:f269e3021894 | 55 | * |
elessair | 0:f269e3021894 | 56 | * @param pin DigitalIn pin to connect to |
elessair | 0:f269e3021894 | 57 | */ |
elessair | 0:f269e3021894 | 58 | DigitalIn(PinName pin) : gpio() { |
elessair | 0:f269e3021894 | 59 | // No lock needed in the constructor |
elessair | 0:f269e3021894 | 60 | gpio_init_in(&gpio, pin); |
elessair | 0:f269e3021894 | 61 | } |
elessair | 0:f269e3021894 | 62 | |
elessair | 0:f269e3021894 | 63 | /** Create a DigitalIn connected to the specified pin |
elessair | 0:f269e3021894 | 64 | * |
elessair | 0:f269e3021894 | 65 | * @param pin DigitalIn pin to connect to |
elessair | 0:f269e3021894 | 66 | * @param mode the initial mode of the pin |
elessair | 0:f269e3021894 | 67 | */ |
elessair | 0:f269e3021894 | 68 | DigitalIn(PinName pin, PinMode mode) : gpio() { |
elessair | 0:f269e3021894 | 69 | // No lock needed in the constructor |
elessair | 0:f269e3021894 | 70 | gpio_init_in_ex(&gpio, pin, mode); |
elessair | 0:f269e3021894 | 71 | } |
elessair | 0:f269e3021894 | 72 | /** Read the input, represented as 0 or 1 (int) |
elessair | 0:f269e3021894 | 73 | * |
elessair | 0:f269e3021894 | 74 | * @returns |
elessair | 0:f269e3021894 | 75 | * An integer representing the state of the input pin, |
elessair | 0:f269e3021894 | 76 | * 0 for logical 0, 1 for logical 1 |
elessair | 0:f269e3021894 | 77 | */ |
elessair | 0:f269e3021894 | 78 | int read() { |
elessair | 0:f269e3021894 | 79 | // Thread safe / atomic HAL call |
elessair | 0:f269e3021894 | 80 | return gpio_read(&gpio); |
elessair | 0:f269e3021894 | 81 | } |
elessair | 0:f269e3021894 | 82 | |
elessair | 0:f269e3021894 | 83 | /** Set the input pin mode |
elessair | 0:f269e3021894 | 84 | * |
elessair | 0:f269e3021894 | 85 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
elessair | 0:f269e3021894 | 86 | */ |
elessair | 0:f269e3021894 | 87 | void mode(PinMode pull) { |
elessair | 0:f269e3021894 | 88 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 89 | gpio_mode(&gpio, pull); |
elessair | 0:f269e3021894 | 90 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 91 | } |
elessair | 0:f269e3021894 | 92 | |
elessair | 0:f269e3021894 | 93 | /** Return the output setting, represented as 0 or 1 (int) |
elessair | 0:f269e3021894 | 94 | * |
elessair | 0:f269e3021894 | 95 | * @returns |
elessair | 0:f269e3021894 | 96 | * Non zero value if pin is connected to uc GPIO |
elessair | 0:f269e3021894 | 97 | * 0 if gpio object was initialized with NC |
elessair | 0:f269e3021894 | 98 | */ |
elessair | 0:f269e3021894 | 99 | int is_connected() { |
elessair | 0:f269e3021894 | 100 | // Thread safe / atomic HAL call |
elessair | 0:f269e3021894 | 101 | return gpio_is_connected(&gpio); |
elessair | 0:f269e3021894 | 102 | } |
elessair | 0:f269e3021894 | 103 | |
elessair | 0:f269e3021894 | 104 | /** An operator shorthand for read() |
elessair | 0:f269e3021894 | 105 | */ |
elessair | 0:f269e3021894 | 106 | operator int() { |
elessair | 0:f269e3021894 | 107 | // Underlying read is thread safe |
elessair | 0:f269e3021894 | 108 | return read(); |
elessair | 0:f269e3021894 | 109 | } |
elessair | 0:f269e3021894 | 110 | |
elessair | 0:f269e3021894 | 111 | protected: |
elessair | 0:f269e3021894 | 112 | gpio_t gpio; |
elessair | 0:f269e3021894 | 113 | }; |
elessair | 0:f269e3021894 | 114 | |
elessair | 0:f269e3021894 | 115 | } // namespace mbed |
elessair | 0:f269e3021894 | 116 | |
elessair | 0:f269e3021894 | 117 | #endif |
elessair | 0:f269e3021894 | 118 | |
elessair | 0:f269e3021894 | 119 | /** @}*/ |