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