this hurts

Dependencies:   FFT

Committer:
annieluo2
Date:
Wed Dec 02 18:02:03 2020 +0000
Revision:
0:d6c9b09b4042
boo

Who changed what in which revision?

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