Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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