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