mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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