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