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_DIGITALOUT_H
AnnaBridge 189:f392fc9709a3 18 #define MBED_DIGITALOUT_H
AnnaBridge 189:f392fc9709a3 19
AnnaBridge 189:f392fc9709a3 20 #include "platform/platform.h"
AnnaBridge 189:f392fc9709a3 21 #include "hal/gpio_api.h"
AnnaBridge 189:f392fc9709a3 22 #include "platform/mbed_critical.h"
AnnaBridge 189:f392fc9709a3 23
AnnaBridge 189:f392fc9709a3 24 namespace mbed {
AnnaBridge 189:f392fc9709a3 25 /** \addtogroup drivers */
AnnaBridge 189:f392fc9709a3 26
AnnaBridge 189:f392fc9709a3 27 /** A digital output, used for setting the state of a pin
AnnaBridge 189:f392fc9709a3 28 *
AnnaBridge 189:f392fc9709a3 29 * @note Synchronization level: Interrupt safe
AnnaBridge 189:f392fc9709a3 30 *
AnnaBridge 189:f392fc9709a3 31 * Example:
AnnaBridge 189:f392fc9709a3 32 * @code
AnnaBridge 189:f392fc9709a3 33 * // Toggle a LED
AnnaBridge 189:f392fc9709a3 34 * #include "mbed.h"
AnnaBridge 189:f392fc9709a3 35 *
AnnaBridge 189:f392fc9709a3 36 * DigitalOut led(LED1);
AnnaBridge 189:f392fc9709a3 37 *
AnnaBridge 189:f392fc9709a3 38 * int main() {
AnnaBridge 189:f392fc9709a3 39 * while(1) {
AnnaBridge 189:f392fc9709a3 40 * led = !led;
AnnaBridge 189:f392fc9709a3 41 * wait(0.2);
AnnaBridge 189:f392fc9709a3 42 * }
AnnaBridge 189:f392fc9709a3 43 * }
AnnaBridge 189:f392fc9709a3 44 * @endcode
AnnaBridge 189:f392fc9709a3 45 * @ingroup drivers
AnnaBridge 189:f392fc9709a3 46 */
AnnaBridge 189:f392fc9709a3 47 class DigitalOut {
AnnaBridge 189:f392fc9709a3 48
AnnaBridge 189:f392fc9709a3 49 public:
AnnaBridge 189:f392fc9709a3 50 /** Create a DigitalOut connected to the specified pin
AnnaBridge 189:f392fc9709a3 51 *
AnnaBridge 189:f392fc9709a3 52 * @param pin DigitalOut pin to connect to
AnnaBridge 189:f392fc9709a3 53 */
AnnaBridge 189:f392fc9709a3 54 DigitalOut(PinName pin) : gpio()
AnnaBridge 189:f392fc9709a3 55 {
AnnaBridge 189:f392fc9709a3 56 // No lock needed in the constructor
AnnaBridge 189:f392fc9709a3 57 gpio_init_out(&gpio, pin);
AnnaBridge 189:f392fc9709a3 58 }
AnnaBridge 189:f392fc9709a3 59
AnnaBridge 189:f392fc9709a3 60 /** Create a DigitalOut connected to the specified pin
AnnaBridge 189:f392fc9709a3 61 *
AnnaBridge 189:f392fc9709a3 62 * @param pin DigitalOut pin to connect to
AnnaBridge 189:f392fc9709a3 63 * @param value the initial pin value
AnnaBridge 189:f392fc9709a3 64 */
AnnaBridge 189:f392fc9709a3 65 DigitalOut(PinName pin, int value) : gpio()
AnnaBridge 189:f392fc9709a3 66 {
AnnaBridge 189:f392fc9709a3 67 // No lock needed in the constructor
AnnaBridge 189:f392fc9709a3 68 gpio_init_out_ex(&gpio, pin, value);
AnnaBridge 189:f392fc9709a3 69 }
AnnaBridge 189:f392fc9709a3 70
AnnaBridge 189:f392fc9709a3 71 /** Set the output, specified as 0 or 1 (int)
AnnaBridge 189:f392fc9709a3 72 *
AnnaBridge 189:f392fc9709a3 73 * @param value An integer specifying the pin output value,
AnnaBridge 189:f392fc9709a3 74 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
AnnaBridge 189:f392fc9709a3 75 */
AnnaBridge 189:f392fc9709a3 76 void write(int value)
AnnaBridge 189:f392fc9709a3 77 {
AnnaBridge 189:f392fc9709a3 78 // Thread safe / atomic HAL call
AnnaBridge 189:f392fc9709a3 79 gpio_write(&gpio, value);
AnnaBridge 189:f392fc9709a3 80 }
AnnaBridge 189:f392fc9709a3 81
AnnaBridge 189:f392fc9709a3 82 /** Return the output setting, represented as 0 or 1 (int)
AnnaBridge 189:f392fc9709a3 83 *
AnnaBridge 189:f392fc9709a3 84 * @returns
AnnaBridge 189:f392fc9709a3 85 * an integer representing the output setting of the pin,
AnnaBridge 189:f392fc9709a3 86 * 0 for logical 0, 1 for logical 1
AnnaBridge 189:f392fc9709a3 87 */
AnnaBridge 189:f392fc9709a3 88 int read()
AnnaBridge 189:f392fc9709a3 89 {
AnnaBridge 189:f392fc9709a3 90 // Thread safe / atomic HAL call
AnnaBridge 189:f392fc9709a3 91 return gpio_read(&gpio);
AnnaBridge 189:f392fc9709a3 92 }
AnnaBridge 189:f392fc9709a3 93
AnnaBridge 189:f392fc9709a3 94 /** Return the output setting, represented as 0 or 1 (int)
AnnaBridge 189:f392fc9709a3 95 *
AnnaBridge 189:f392fc9709a3 96 * @returns
AnnaBridge 189:f392fc9709a3 97 * Non zero value if pin is connected to uc GPIO
AnnaBridge 189:f392fc9709a3 98 * 0 if gpio object was initialized with NC
AnnaBridge 189:f392fc9709a3 99 */
AnnaBridge 189:f392fc9709a3 100 int is_connected()
AnnaBridge 189:f392fc9709a3 101 {
AnnaBridge 189:f392fc9709a3 102 // Thread safe / atomic HAL call
AnnaBridge 189:f392fc9709a3 103 return gpio_is_connected(&gpio);
AnnaBridge 189:f392fc9709a3 104 }
AnnaBridge 189:f392fc9709a3 105
AnnaBridge 189:f392fc9709a3 106 /** A shorthand for write()
AnnaBridge 189:f392fc9709a3 107 * \sa DigitalOut::write()
AnnaBridge 189:f392fc9709a3 108 * @code
AnnaBridge 189:f392fc9709a3 109 * DigitalIn button(BUTTON1);
AnnaBridge 189:f392fc9709a3 110 * DigitalOut led(LED1);
AnnaBridge 189:f392fc9709a3 111 * led = button; // Equivalent to led.write(button.read())
AnnaBridge 189:f392fc9709a3 112 * @endcode
AnnaBridge 189:f392fc9709a3 113 */
AnnaBridge 189:f392fc9709a3 114 DigitalOut &operator= (int value)
AnnaBridge 189:f392fc9709a3 115 {
AnnaBridge 189:f392fc9709a3 116 // Underlying write is thread safe
AnnaBridge 189:f392fc9709a3 117 write(value);
AnnaBridge 189:f392fc9709a3 118 return *this;
AnnaBridge 189:f392fc9709a3 119 }
AnnaBridge 189:f392fc9709a3 120
AnnaBridge 189:f392fc9709a3 121 /** A shorthand for write() using the assignment operator which copies the
AnnaBridge 189:f392fc9709a3 122 * state from the DigitalOut argument.
AnnaBridge 189:f392fc9709a3 123 * \sa DigitalOut::write()
AnnaBridge 189:f392fc9709a3 124 */
AnnaBridge 189:f392fc9709a3 125 DigitalOut &operator= (DigitalOut &rhs)
AnnaBridge 189:f392fc9709a3 126 {
AnnaBridge 189:f392fc9709a3 127 core_util_critical_section_enter();
AnnaBridge 189:f392fc9709a3 128 write(rhs.read());
AnnaBridge 189:f392fc9709a3 129 core_util_critical_section_exit();
AnnaBridge 189:f392fc9709a3 130 return *this;
AnnaBridge 189:f392fc9709a3 131 }
AnnaBridge 189:f392fc9709a3 132
AnnaBridge 189:f392fc9709a3 133 /** A shorthand for read()
AnnaBridge 189:f392fc9709a3 134 * \sa DigitalOut::read()
AnnaBridge 189:f392fc9709a3 135 * @code
AnnaBridge 189:f392fc9709a3 136 * DigitalIn button(BUTTON1);
AnnaBridge 189:f392fc9709a3 137 * DigitalOut led(LED1);
AnnaBridge 189:f392fc9709a3 138 * led = button; // Equivalent to led.write(button.read())
AnnaBridge 189:f392fc9709a3 139 * @endcode
AnnaBridge 189:f392fc9709a3 140 */
AnnaBridge 189:f392fc9709a3 141 operator int()
AnnaBridge 189:f392fc9709a3 142 {
AnnaBridge 189:f392fc9709a3 143 // Underlying call is thread safe
AnnaBridge 189:f392fc9709a3 144 return read();
AnnaBridge 189:f392fc9709a3 145 }
AnnaBridge 189:f392fc9709a3 146
AnnaBridge 189:f392fc9709a3 147 protected:
AnnaBridge 189:f392fc9709a3 148 #if !defined(DOXYGEN_ONLY)
AnnaBridge 189:f392fc9709a3 149 gpio_t gpio;
AnnaBridge 189:f392fc9709a3 150 #endif //!defined(DOXYGEN_ONLY)
AnnaBridge 189:f392fc9709a3 151 };
AnnaBridge 189:f392fc9709a3 152
AnnaBridge 189:f392fc9709a3 153 } // namespace mbed
AnnaBridge 189:f392fc9709a3 154
AnnaBridge 189:f392fc9709a3 155 #endif