mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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