mbed library sources for TON Board V1.2
Fork of mbed-dev by
drivers/DigitalIn.h@168:1dc8c8bdd03d, 2017-06-29 (annotated)
- Committer:
- krebyy
- Date:
- Thu Jun 29 20:34:04 2017 +0000
- Revision:
- 168:1dc8c8bdd03d
- Parent:
- 167:e84263d55307
First commit - mbed for TON Board V1.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #ifndef MBED_DIGITALIN_H |
<> | 149:156823d33999 | 17 | #define MBED_DIGITALIN_H |
<> | 149:156823d33999 | 18 | |
<> | 149:156823d33999 | 19 | #include "platform/platform.h" |
<> | 149:156823d33999 | 20 | |
<> | 149:156823d33999 | 21 | #include "hal/gpio_api.h" |
<> | 160:d5399cc887bb | 22 | #include "platform/mbed_critical.h" |
<> | 149:156823d33999 | 23 | |
<> | 149:156823d33999 | 24 | namespace mbed { |
<> | 149:156823d33999 | 25 | /** \addtogroup drivers */ |
<> | 149:156823d33999 | 26 | |
<> | 149:156823d33999 | 27 | /** A digital input, used for reading the state of a pin |
<> | 149:156823d33999 | 28 | * |
AnnaBridge | 167:e84263d55307 | 29 | * @note Synchronization level: Interrupt safe |
<> | 149:156823d33999 | 30 | * |
<> | 149:156823d33999 | 31 | * Example: |
<> | 149:156823d33999 | 32 | * @code |
<> | 149:156823d33999 | 33 | * // Flash an LED while a DigitalIn is true |
<> | 149:156823d33999 | 34 | * |
<> | 149:156823d33999 | 35 | * #include "mbed.h" |
<> | 149:156823d33999 | 36 | * |
<> | 149:156823d33999 | 37 | * DigitalIn enable(p5); |
<> | 149:156823d33999 | 38 | * DigitalOut led(LED1); |
<> | 149:156823d33999 | 39 | * |
<> | 149:156823d33999 | 40 | * int main() { |
<> | 149:156823d33999 | 41 | * while(1) { |
<> | 149:156823d33999 | 42 | * if(enable) { |
<> | 149:156823d33999 | 43 | * led = !led; |
<> | 149:156823d33999 | 44 | * } |
<> | 149:156823d33999 | 45 | * wait(0.25); |
<> | 149:156823d33999 | 46 | * } |
<> | 149:156823d33999 | 47 | * } |
<> | 149:156823d33999 | 48 | * @endcode |
AnnaBridge | 167:e84263d55307 | 49 | * @ingroup drivers |
<> | 149:156823d33999 | 50 | */ |
<> | 149:156823d33999 | 51 | class DigitalIn { |
<> | 149:156823d33999 | 52 | |
<> | 149:156823d33999 | 53 | public: |
<> | 149:156823d33999 | 54 | /** Create a DigitalIn connected to the specified pin |
<> | 149:156823d33999 | 55 | * |
<> | 149:156823d33999 | 56 | * @param pin DigitalIn pin to connect to |
<> | 149:156823d33999 | 57 | */ |
<> | 149:156823d33999 | 58 | DigitalIn(PinName pin) : gpio() { |
<> | 149:156823d33999 | 59 | // No lock needed in the constructor |
<> | 149:156823d33999 | 60 | gpio_init_in(&gpio, pin); |
<> | 149:156823d33999 | 61 | } |
<> | 149:156823d33999 | 62 | |
<> | 149:156823d33999 | 63 | /** Create a DigitalIn connected to the specified pin |
<> | 149:156823d33999 | 64 | * |
<> | 149:156823d33999 | 65 | * @param pin DigitalIn pin to connect to |
<> | 149:156823d33999 | 66 | * @param mode the initial mode of the pin |
<> | 149:156823d33999 | 67 | */ |
<> | 149:156823d33999 | 68 | DigitalIn(PinName pin, PinMode mode) : gpio() { |
<> | 149:156823d33999 | 69 | // No lock needed in the constructor |
<> | 149:156823d33999 | 70 | gpio_init_in_ex(&gpio, pin, mode); |
<> | 149:156823d33999 | 71 | } |
<> | 149:156823d33999 | 72 | /** Read the input, represented as 0 or 1 (int) |
<> | 149:156823d33999 | 73 | * |
<> | 149:156823d33999 | 74 | * @returns |
<> | 149:156823d33999 | 75 | * An integer representing the state of the input pin, |
<> | 149:156823d33999 | 76 | * 0 for logical 0, 1 for logical 1 |
<> | 149:156823d33999 | 77 | */ |
<> | 149:156823d33999 | 78 | int read() { |
<> | 149:156823d33999 | 79 | // Thread safe / atomic HAL call |
<> | 149:156823d33999 | 80 | return gpio_read(&gpio); |
<> | 149:156823d33999 | 81 | } |
<> | 149:156823d33999 | 82 | |
<> | 149:156823d33999 | 83 | /** Set the input pin mode |
<> | 149:156823d33999 | 84 | * |
AnnaBridge | 167:e84263d55307 | 85 | * @param pull PullUp, PullDown, PullNone, OpenDrain |
<> | 149:156823d33999 | 86 | */ |
<> | 149:156823d33999 | 87 | void mode(PinMode pull) { |
<> | 149:156823d33999 | 88 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 89 | gpio_mode(&gpio, pull); |
<> | 149:156823d33999 | 90 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 91 | } |
<> | 149:156823d33999 | 92 | |
<> | 149:156823d33999 | 93 | /** Return the output setting, represented as 0 or 1 (int) |
<> | 149:156823d33999 | 94 | * |
<> | 149:156823d33999 | 95 | * @returns |
<> | 149:156823d33999 | 96 | * Non zero value if pin is connected to uc GPIO |
<> | 149:156823d33999 | 97 | * 0 if gpio object was initialized with NC |
<> | 149:156823d33999 | 98 | */ |
<> | 149:156823d33999 | 99 | int is_connected() { |
<> | 149:156823d33999 | 100 | // Thread safe / atomic HAL call |
<> | 149:156823d33999 | 101 | return gpio_is_connected(&gpio); |
<> | 149:156823d33999 | 102 | } |
<> | 149:156823d33999 | 103 | |
<> | 149:156823d33999 | 104 | /** An operator shorthand for read() |
AnnaBridge | 167:e84263d55307 | 105 | * \sa DigitalIn::read() |
<> | 149:156823d33999 | 106 | */ |
<> | 149:156823d33999 | 107 | operator int() { |
<> | 149:156823d33999 | 108 | // Underlying read is thread safe |
<> | 149:156823d33999 | 109 | return read(); |
<> | 149:156823d33999 | 110 | } |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | protected: |
<> | 149:156823d33999 | 113 | gpio_t gpio; |
<> | 149:156823d33999 | 114 | }; |
<> | 149:156823d33999 | 115 | |
<> | 149:156823d33999 | 116 | } // namespace mbed |
<> | 149:156823d33999 | 117 | |
<> | 149:156823d33999 | 118 | #endif |