mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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