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
mbed library release version 165

Who changed what in which revision?

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