help :(

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_DIGITALOUT_H
annieluo2 0:d6c9b09b4042 17 #define MBED_DIGITALOUT_H
annieluo2 0:d6c9b09b4042 18
annieluo2 0:d6c9b09b4042 19 #include "platform/platform.h"
annieluo2 0:d6c9b09b4042 20 #include "hal/gpio_api.h"
annieluo2 0:d6c9b09b4042 21 #include "platform/critical.h"
annieluo2 0:d6c9b09b4042 22
annieluo2 0:d6c9b09b4042 23 namespace mbed {
annieluo2 0:d6c9b09b4042 24 /** \addtogroup drivers */
annieluo2 0:d6c9b09b4042 25 /** @{*/
annieluo2 0:d6c9b09b4042 26
annieluo2 0:d6c9b09b4042 27 /** A digital output, used for setting the state of a pin
annieluo2 0:d6c9b09b4042 28 *
annieluo2 0:d6c9b09b4042 29 * @Note Synchronization level: Interrupt safe
annieluo2 0:d6c9b09b4042 30 *
annieluo2 0:d6c9b09b4042 31 * Example:
annieluo2 0:d6c9b09b4042 32 * @code
annieluo2 0:d6c9b09b4042 33 * // Toggle a LED
annieluo2 0:d6c9b09b4042 34 * #include "mbed.h"
annieluo2 0:d6c9b09b4042 35 *
annieluo2 0:d6c9b09b4042 36 * DigitalOut led(LED1);
annieluo2 0:d6c9b09b4042 37 *
annieluo2 0:d6c9b09b4042 38 * int main() {
annieluo2 0:d6c9b09b4042 39 * while(1) {
annieluo2 0:d6c9b09b4042 40 * led = !led;
annieluo2 0:d6c9b09b4042 41 * wait(0.2);
annieluo2 0:d6c9b09b4042 42 * }
annieluo2 0:d6c9b09b4042 43 * }
annieluo2 0:d6c9b09b4042 44 * @endcode
annieluo2 0:d6c9b09b4042 45 */
annieluo2 0:d6c9b09b4042 46 class DigitalOut {
annieluo2 0:d6c9b09b4042 47
annieluo2 0:d6c9b09b4042 48 public:
annieluo2 0:d6c9b09b4042 49 /** Create a DigitalOut connected to the specified pin
annieluo2 0:d6c9b09b4042 50 *
annieluo2 0:d6c9b09b4042 51 * @param pin DigitalOut pin to connect to
annieluo2 0:d6c9b09b4042 52 */
annieluo2 0:d6c9b09b4042 53 DigitalOut(PinName pin) : gpio() {
annieluo2 0:d6c9b09b4042 54 // No lock needed in the constructor
annieluo2 0:d6c9b09b4042 55 gpio_init_out(&gpio, pin);
annieluo2 0:d6c9b09b4042 56 }
annieluo2 0:d6c9b09b4042 57
annieluo2 0:d6c9b09b4042 58 /** Create a DigitalOut connected to the specified pin
annieluo2 0:d6c9b09b4042 59 *
annieluo2 0:d6c9b09b4042 60 * @param pin DigitalOut pin to connect to
annieluo2 0:d6c9b09b4042 61 * @param value the initial pin value
annieluo2 0:d6c9b09b4042 62 */
annieluo2 0:d6c9b09b4042 63 DigitalOut(PinName pin, int value) : gpio() {
annieluo2 0:d6c9b09b4042 64 // No lock needed in the constructor
annieluo2 0:d6c9b09b4042 65 gpio_init_out_ex(&gpio, pin, value);
annieluo2 0:d6c9b09b4042 66 }
annieluo2 0:d6c9b09b4042 67
annieluo2 0:d6c9b09b4042 68 /** Set the output, specified as 0 or 1 (int)
annieluo2 0:d6c9b09b4042 69 *
annieluo2 0:d6c9b09b4042 70 * @param value An integer specifying the pin output value,
annieluo2 0:d6c9b09b4042 71 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
annieluo2 0:d6c9b09b4042 72 */
annieluo2 0:d6c9b09b4042 73 void write(int value) {
annieluo2 0:d6c9b09b4042 74 // Thread safe / atomic HAL call
annieluo2 0:d6c9b09b4042 75 gpio_write(&gpio, value);
annieluo2 0:d6c9b09b4042 76 }
annieluo2 0:d6c9b09b4042 77
annieluo2 0:d6c9b09b4042 78 /** Return the output setting, represented as 0 or 1 (int)
annieluo2 0:d6c9b09b4042 79 *
annieluo2 0:d6c9b09b4042 80 * @returns
annieluo2 0:d6c9b09b4042 81 * an integer representing the output setting of the pin,
annieluo2 0:d6c9b09b4042 82 * 0 for logical 0, 1 for logical 1
annieluo2 0:d6c9b09b4042 83 */
annieluo2 0:d6c9b09b4042 84 int read() {
annieluo2 0:d6c9b09b4042 85 // Thread safe / atomic HAL call
annieluo2 0:d6c9b09b4042 86 return gpio_read(&gpio);
annieluo2 0:d6c9b09b4042 87 }
annieluo2 0:d6c9b09b4042 88
annieluo2 0:d6c9b09b4042 89 /** Return the output setting, represented as 0 or 1 (int)
annieluo2 0:d6c9b09b4042 90 *
annieluo2 0:d6c9b09b4042 91 * @returns
annieluo2 0:d6c9b09b4042 92 * Non zero value if pin is connected to uc GPIO
annieluo2 0:d6c9b09b4042 93 * 0 if gpio object was initialized with NC
annieluo2 0:d6c9b09b4042 94 */
annieluo2 0:d6c9b09b4042 95 int is_connected() {
annieluo2 0:d6c9b09b4042 96 // Thread safe / atomic HAL call
annieluo2 0:d6c9b09b4042 97 return gpio_is_connected(&gpio);
annieluo2 0:d6c9b09b4042 98 }
annieluo2 0:d6c9b09b4042 99
annieluo2 0:d6c9b09b4042 100 /** A shorthand for write()
annieluo2 0:d6c9b09b4042 101 */
annieluo2 0:d6c9b09b4042 102 DigitalOut& operator= (int value) {
annieluo2 0:d6c9b09b4042 103 // Underlying write is thread safe
annieluo2 0:d6c9b09b4042 104 write(value);
annieluo2 0:d6c9b09b4042 105 return *this;
annieluo2 0:d6c9b09b4042 106 }
annieluo2 0:d6c9b09b4042 107
annieluo2 0:d6c9b09b4042 108 DigitalOut& operator= (DigitalOut& rhs) {
annieluo2 0:d6c9b09b4042 109 core_util_critical_section_enter();
annieluo2 0:d6c9b09b4042 110 write(rhs.read());
annieluo2 0:d6c9b09b4042 111 core_util_critical_section_exit();
annieluo2 0:d6c9b09b4042 112 return *this;
annieluo2 0:d6c9b09b4042 113 }
annieluo2 0:d6c9b09b4042 114
annieluo2 0:d6c9b09b4042 115 /** A shorthand for read()
annieluo2 0:d6c9b09b4042 116 */
annieluo2 0:d6c9b09b4042 117 operator int() {
annieluo2 0:d6c9b09b4042 118 // Underlying call is thread safe
annieluo2 0:d6c9b09b4042 119 return read();
annieluo2 0:d6c9b09b4042 120 }
annieluo2 0:d6c9b09b4042 121
annieluo2 0:d6c9b09b4042 122 protected:
annieluo2 0:d6c9b09b4042 123 gpio_t gpio;
annieluo2 0:d6c9b09b4042 124 };
annieluo2 0:d6c9b09b4042 125
annieluo2 0:d6c9b09b4042 126 } // namespace mbed
annieluo2 0:d6c9b09b4042 127
annieluo2 0:d6c9b09b4042 128 #endif
annieluo2 0:d6c9b09b4042 129
annieluo2 0:d6c9b09b4042 130 /** @}*/