paul@0 | 1 | /* |
paul@0 | 2 | * GPIO driver definitions. |
paul@0 | 3 | * (See below for additional copyright and licensing notices.) |
paul@0 | 4 | * |
paul@0 | 5 | * (c) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | * |
paul@0 | 7 | * This program is free software; you can redistribute it and/or |
paul@0 | 8 | * modify it under the terms of the GNU General Public License as |
paul@0 | 9 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 10 | * the License, or (at your option) any later version. |
paul@0 | 11 | * |
paul@0 | 12 | * This program is distributed in the hope that it will be useful, |
paul@0 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 15 | * GNU General Public License for more details. |
paul@0 | 16 | * |
paul@0 | 17 | * You should have received a copy of the GNU General Public License |
paul@0 | 18 | * along with this program; if not, write to the Free Software |
paul@0 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 20 | * Boston, MA 02110-1301, USA |
paul@0 | 21 | * |
paul@0 | 22 | * |
paul@0 | 23 | * Subject to other copyrights, being derived from the gpio headers in the io |
paul@0 | 24 | * package. |
paul@0 | 25 | * |
paul@0 | 26 | * This file is part of TUD:OS and distributed under the terms of the |
paul@0 | 27 | * GNU General Public License 2. |
paul@0 | 28 | * Please see the COPYING-GPL-2 file for details. |
paul@0 | 29 | */ |
paul@0 | 30 | |
paul@0 | 31 | #pragma once |
paul@0 | 32 | |
paul@0 | 33 | #include <l4/vbus/vbus_gpio.h> |
paul@0 | 34 | |
paul@0 | 35 | typedef struct Pin_slice |
paul@0 | 36 | { |
paul@0 | 37 | unsigned offset; |
paul@0 | 38 | unsigned mask; |
paul@0 | 39 | } Pin_slice; |
paul@0 | 40 | |
paul@0 | 41 | #ifdef __cplusplus |
paul@0 | 42 | |
paul@0 | 43 | namespace Hw { |
paul@0 | 44 | |
paul@0 | 45 | // IRQ control for each GPIO pin. |
paul@0 | 46 | |
paul@0 | 47 | class Gpio_irq_pin |
paul@0 | 48 | { |
paul@0 | 49 | public: |
paul@0 | 50 | virtual void do_mask() = 0; |
paul@0 | 51 | virtual void do_unmask() = 0; |
paul@0 | 52 | virtual bool do_set_mode(unsigned mode) = 0; |
paul@0 | 53 | virtual int clear() = 0; |
paul@0 | 54 | virtual bool enabled() = 0; |
paul@0 | 55 | }; |
paul@0 | 56 | |
paul@0 | 57 | // GPIO peripheral control. |
paul@0 | 58 | |
paul@0 | 59 | class Gpio_chip |
paul@0 | 60 | { |
paul@0 | 61 | public: |
paul@0 | 62 | /// Modes used for setup() |
paul@0 | 63 | enum Fix_mode |
paul@0 | 64 | { |
paul@0 | 65 | Input = L4VBUS_GPIO_SETUP_INPUT, ///< Configure as input pin |
paul@0 | 66 | Output = L4VBUS_GPIO_SETUP_OUTPUT, ///< Configure as output pin |
paul@0 | 67 | Irq = L4VBUS_GPIO_SETUP_IRQ, ///< Configure as IRQ source |
paul@0 | 68 | }; |
paul@0 | 69 | |
paul@0 | 70 | /// Modes used for pull up / pull down (config_pull()) |
paul@0 | 71 | enum Pull_mode |
paul@0 | 72 | { |
paul@0 | 73 | Pull_none = L4VBUS_GPIO_PIN_PULL_NONE, ///< No pull up or pull down resistors |
paul@0 | 74 | Pull_up = L4VBUS_GPIO_PIN_PULL_UP, ///< Enable pull up resistor |
paul@0 | 75 | Pull_down = L4VBUS_GPIO_PIN_PULL_DOWN, ///< Enable pull down resistor |
paul@0 | 76 | }; |
paul@0 | 77 | |
paul@0 | 78 | // Generic function options |
paul@0 | 79 | enum Function_levels |
paul@0 | 80 | { |
paul@0 | 81 | Function_gpio, |
paul@0 | 82 | Function_alt, |
paul@0 | 83 | }; |
paul@0 | 84 | |
paul@0 | 85 | virtual void request(unsigned pin) = 0; |
paul@0 | 86 | virtual void free(unsigned pin) = 0; |
paul@0 | 87 | |
paul@0 | 88 | /** |
paul@0 | 89 | * \brief Request number of pins from GPIO chip |
paul@0 | 90 | * \return Number of pins of this GPIO chip |
paul@0 | 91 | */ |
paul@0 | 92 | virtual unsigned nr_pins() const = 0; |
paul@0 | 93 | |
paul@0 | 94 | /** |
paul@0 | 95 | * \brief Generic (platform independent) setup for a pin. |
paul@0 | 96 | * \param pin the pin number to configure. |
paul@0 | 97 | * \param mode the mode for the pin (see Fix_mode). |
paul@0 | 98 | * \param value the value if the pin is configured as output. |
paul@0 | 99 | */ |
paul@0 | 100 | virtual void setup(unsigned pin, unsigned mode, int value = 0) = 0; |
paul@0 | 101 | |
paul@0 | 102 | /** |
paul@0 | 103 | * \brief Generic (platform independet) function to set a pin's pull up/down mode |
paul@0 | 104 | * \param pin The pin number to configure. |
paul@0 | 105 | * \param mode The pull up/down mode (see Pull_mode). |
paul@0 | 106 | */ |
paul@0 | 107 | virtual void config_pull(unsigned pin, unsigned mode) = 0; |
paul@0 | 108 | |
paul@0 | 109 | /** |
paul@0 | 110 | * \brief Set platform specific pad configuration. |
paul@0 | 111 | * \param pin the pin to configure. |
paul@0 | 112 | * \param func a platform specific sub-function of a pad to be configured |
paul@0 | 113 | * \param value a platform specific value for the given sub-function. |
paul@0 | 114 | */ |
paul@0 | 115 | virtual void config_pad(unsigned pin, unsigned func, unsigned value) = 0; |
paul@0 | 116 | |
paul@0 | 117 | /** |
paul@0 | 118 | * \brief Get Platform specific pad configuration. |
paul@0 | 119 | * \param pin the pin to configure. |
paul@0 | 120 | * \param func a platform specific sub-function of a pad to be configured |
paul@0 | 121 | * \retparam value a platform specific value for the given sub-function. |
paul@0 | 122 | */ |
paul@0 | 123 | virtual void config_get(unsigned pin, unsigned func, unsigned *value) = 0; |
paul@0 | 124 | |
paul@0 | 125 | /** |
paul@0 | 126 | * \brief Get the value of the given pin (generic API). |
paul@0 | 127 | * \param pin the pin to read the value from. |
paul@0 | 128 | */ |
paul@0 | 129 | virtual int get(unsigned pin) = 0; |
paul@0 | 130 | |
paul@0 | 131 | /** |
paul@0 | 132 | * \brief Set the value of the given pin (generic API). |
paul@0 | 133 | * \pre The pin has to be configured as output before using this function, |
paul@0 | 134 | * otherwise this call will be ignored. |
paul@0 | 135 | * \param pin the pin to write the value to. |
paul@0 | 136 | * \param value the value to program for output (0 or 1). |
paul@0 | 137 | */ |
paul@0 | 138 | virtual void set(unsigned pin, int value) = 0; |
paul@0 | 139 | |
paul@0 | 140 | /** |
paul@0 | 141 | * \brief Setup multiple pins (generic API) |
paul@0 | 142 | * \param mask the pins to actually set up. |
paul@0 | 143 | * \param mode the mode for the pins (see Fix_mode). |
paul@0 | 144 | * \param outvalue the value if configured as output. |
paul@0 | 145 | */ |
paul@0 | 146 | virtual void multi_setup(Pin_slice const &mask, unsigned mode, unsigned outvalue = 0) = 0; |
paul@0 | 147 | |
paul@0 | 148 | /** |
paul@0 | 149 | * \brief Configure multiple pads at once (platform specific API). |
paul@0 | 150 | * \param mask the pads to configure. |
paul@0 | 151 | * \param func the platform-specific sub-function to configure. |
paul@0 | 152 | * \param value the platform-specific value to set for the sub-function. |
paul@0 | 153 | * \see config_pad() |
paul@0 | 154 | */ |
paul@0 | 155 | virtual void multi_config_pad(Pin_slice const &mask, unsigned func, unsigned value) = 0; |
paul@0 | 156 | |
paul@0 | 157 | /** |
paul@0 | 158 | * \brief Set the value for multiple output pins at once. |
paul@0 | 159 | * \param mask the pins that shall actually be set to the given values. |
paul@0 | 160 | * \param data the bit-wise value for each pin to set (according to mask). |
paul@0 | 161 | */ |
paul@0 | 162 | virtual void multi_set(Pin_slice const &mask, unsigned data) = 0; |
paul@0 | 163 | |
paul@0 | 164 | /** |
paul@0 | 165 | * \brief Get the value for all pins at once. |
paul@0 | 166 | */ |
paul@0 | 167 | virtual unsigned multi_get(unsigned offset) = 0; |
paul@0 | 168 | |
paul@0 | 169 | |
paul@0 | 170 | void output(unsigned pin, int value) |
paul@0 | 171 | { setup(pin, Output, value); } |
paul@0 | 172 | |
paul@0 | 173 | void input(unsigned pin) |
paul@0 | 174 | { setup(pin, Input); } |
paul@0 | 175 | |
paul@0 | 176 | void multi_output(Pin_slice const &mask, unsigned data) |
paul@0 | 177 | { multi_setup(mask, mask.mask, data); } |
paul@0 | 178 | |
paul@0 | 179 | void multi_input(Pin_slice const &mask) |
paul@0 | 180 | { multi_setup(mask, ~mask.mask); } |
paul@0 | 181 | }; |
paul@0 | 182 | |
paul@0 | 183 | } |
paul@0 | 184 | |
paul@0 | 185 | #else |
paul@0 | 186 | |
paul@0 | 187 | typedef enum { |
paul@0 | 188 | false = 0, |
paul@0 | 189 | true = 1 |
paul@0 | 190 | } bool; |
paul@0 | 191 | |
paul@0 | 192 | /// Modes used for setup() |
paul@0 | 193 | enum Fix_mode |
paul@0 | 194 | { |
paul@0 | 195 | Fix_input = L4VBUS_GPIO_SETUP_INPUT, ///< Configure as input pin |
paul@0 | 196 | Fix_output = L4VBUS_GPIO_SETUP_OUTPUT, ///< Configure as output pin |
paul@0 | 197 | Fix_irq = L4VBUS_GPIO_SETUP_IRQ, ///< Configure as IRQ source |
paul@0 | 198 | }; |
paul@0 | 199 | |
paul@0 | 200 | /// Modes used for pull up / pull down (config_pull()) |
paul@0 | 201 | enum Pull_mode |
paul@0 | 202 | { |
paul@0 | 203 | Pull_none = L4VBUS_GPIO_PIN_PULL_NONE, ///< No pull up or pull down resistors |
paul@0 | 204 | Pull_up = L4VBUS_GPIO_PIN_PULL_UP, ///< Enable pull up resistor |
paul@0 | 205 | Pull_down = L4VBUS_GPIO_PIN_PULL_DOWN, ///< Enable pull down resistor |
paul@0 | 206 | }; |
paul@0 | 207 | |
paul@0 | 208 | // Generic function options |
paul@0 | 209 | enum Function_levels |
paul@0 | 210 | { |
paul@0 | 211 | Function_gpio, |
paul@0 | 212 | Function_alt, |
paul@0 | 213 | }; |
paul@0 | 214 | |
paul@0 | 215 | #endif /* __cplusplus */ |