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