,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

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