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