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