,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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