Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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