mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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