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