mbed library sources. Supersedes mbed-src.
Dependents: LPCXpresso1769_blinky
Fork of mbed-dev by
DigitalInOut.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_DIGITALINOUT_H 00017 #define MBED_DIGITALINOUT_H 00018 00019 #include "platform.h" 00020 00021 #include "gpio_api.h" 00022 #include "critical.h" 00023 00024 namespace mbed { 00025 00026 /** A digital input/output, used for setting or reading a bi-directional pin 00027 * 00028 * @Note Synchronization level: Interrupt safe 00029 */ 00030 class DigitalInOut { 00031 00032 public: 00033 /** Create a DigitalInOut connected to the specified pin 00034 * 00035 * @param pin DigitalInOut pin to connect to 00036 */ 00037 DigitalInOut(PinName pin) : gpio() { 00038 // No lock needed in the constructor 00039 gpio_init_in(&gpio, pin); 00040 } 00041 00042 /** Create a DigitalInOut connected to the specified pin 00043 * 00044 * @param pin DigitalInOut pin to connect to 00045 * @param direction the initial direction of the pin 00046 * @param mode the initial mode of the pin 00047 * @param value the initial value of the pin if is an output 00048 */ 00049 DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() { 00050 // No lock needed in the constructor 00051 gpio_init_inout(&gpio, pin, direction, mode, value); 00052 } 00053 00054 /** Set the output, specified as 0 or 1 (int) 00055 * 00056 * @param value An integer specifying the pin output value, 00057 * 0 for logical 0, 1 (or any other non-zero value) for logical 1 00058 */ 00059 void write(int value) { 00060 // Thread safe / atomic HAL call 00061 gpio_write(&gpio, value); 00062 } 00063 00064 /** Return the output setting, represented as 0 or 1 (int) 00065 * 00066 * @returns 00067 * an integer representing the output setting of the pin if it is an output, 00068 * or read the input if set as an input 00069 */ 00070 int read() { 00071 // Thread safe / atomic HAL call 00072 return gpio_read(&gpio); 00073 } 00074 00075 /** Set as an output 00076 */ 00077 void output() { 00078 core_util_critical_section_enter(); 00079 gpio_dir(&gpio, PIN_OUTPUT); 00080 core_util_critical_section_exit(); 00081 } 00082 00083 /** Set as an input 00084 */ 00085 void input() { 00086 core_util_critical_section_enter(); 00087 gpio_dir(&gpio, PIN_INPUT); 00088 core_util_critical_section_exit(); 00089 } 00090 00091 /** Set the input pin mode 00092 * 00093 * @param mode PullUp, PullDown, PullNone, OpenDrain 00094 */ 00095 void mode(PinMode pull) { 00096 core_util_critical_section_enter(); 00097 gpio_mode(&gpio, pull); 00098 core_util_critical_section_exit(); 00099 } 00100 00101 /** Return the output setting, represented as 0 or 1 (int) 00102 * 00103 * @returns 00104 * Non zero value if pin is connected to uc GPIO 00105 * 0 if gpio object was initialized with NC 00106 */ 00107 int is_connected() { 00108 // Thread safe / atomic HAL call 00109 return gpio_is_connected(&gpio); 00110 } 00111 00112 /** A shorthand for write() 00113 */ 00114 DigitalInOut& operator= (int value) { 00115 // Underlying write is thread safe 00116 write(value); 00117 return *this; 00118 } 00119 00120 DigitalInOut& operator= (DigitalInOut& rhs) { 00121 core_util_critical_section_enter(); 00122 write(rhs.read()); 00123 core_util_critical_section_exit(); 00124 return *this; 00125 } 00126 00127 /** A shorthand for read() 00128 */ 00129 operator int() { 00130 // Underlying call is thread safe 00131 return read(); 00132 } 00133 00134 protected: 00135 gpio_t gpio; 00136 }; 00137 00138 } // namespace mbed 00139 00140 #endif
Generated on Tue Jul 12 2022 16:04:25 by 1.7.2