Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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