1 /* 2 * GPIO driver definitions. 3 * (See below for additional copyright and licensing notices.) 4 * 5 * Copyright (C) 2017, 2018, 2023 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 Function_irq, 84 }; 85 86 virtual void request(unsigned pin) = 0; 87 virtual void free(unsigned pin) = 0; 88 89 /** 90 * \brief Request number of pins from GPIO chip 91 * \return Number of pins of this GPIO chip 92 */ 93 virtual unsigned nr_pins() const = 0; 94 95 /** 96 * \brief Generic (platform independent) setup for a pin. 97 * \param pin the pin number to configure. 98 * \param mode the mode for the pin (see Fix_mode). 99 * \param value the value if the pin is configured as output. 100 */ 101 virtual void setup(unsigned pin, unsigned mode, int value = 0) = 0; 102 103 /** 104 * \brief Generic (platform independet) function to set a pin's pull up/down mode 105 * \param pin The pin number to configure. 106 * \param mode The pull up/down mode (see Pull_mode). 107 */ 108 virtual void config_pull(unsigned pin, unsigned mode) = 0; 109 110 /** 111 * \brief Set platform specific pad configuration. 112 * \param pin the pin to configure. 113 * \param func a platform specific sub-function of a pad to be configured 114 * \param value a platform specific value for the given sub-function. 115 */ 116 virtual void config_pad(unsigned pin, unsigned func, unsigned value) = 0; 117 118 /** 119 * \brief Get Platform specific pad configuration. 120 * \param pin the pin to configure. 121 * \param func a platform specific sub-function of a pad to be configured 122 * \retparam value a platform specific value for the given sub-function. 123 */ 124 virtual void config_get(unsigned pin, unsigned func, unsigned *value) = 0; 125 126 /** 127 * \brief Get platform specific pad configuration. 128 * \param pin the pin to configure. 129 * \param func a platform specific sub-function of a pad to be configured 130 * \param value a platform specific value for the given sub-function. 131 */ 132 virtual void config_pad_get(unsigned pin, unsigned *func, unsigned *value) = 0; 133 134 /** 135 * \brief Get the value of the given pin (generic API). 136 * \param pin the pin to read the value from. 137 */ 138 virtual int get(unsigned pin) = 0; 139 140 /** 141 * \brief Set the value of the given pin (generic API). 142 * \pre The pin has to be configured as output before using this function, 143 * otherwise this call will be ignored. 144 * \param pin the pin to write the value to. 145 * \param value the value to program for output (0 or 1). 146 */ 147 virtual void set(unsigned pin, int value) = 0; 148 149 /** 150 * \brief Setup multiple pins (generic API) 151 * \param mask the pins to actually set up. 152 * \param mode the mode for the pins (see Fix_mode). 153 * \param outvalue the value if configured as output. 154 */ 155 virtual void multi_setup(Pin_slice const &mask, unsigned mode, unsigned outvalue = 0) = 0; 156 157 /** 158 * \brief Configure multiple pads at once (platform specific API). 159 * \param mask the pads to configure. 160 * \param func the platform-specific sub-function to configure. 161 * \param value the platform-specific value to set for the sub-function. 162 * \see config_pad() 163 */ 164 virtual void multi_config_pad(Pin_slice const &mask, unsigned func, unsigned value) = 0; 165 166 /** 167 * \brief Set the value for multiple output pins at once. 168 * \param mask the pins that shall actually be set to the given values. 169 * \param data the bit-wise value for each pin to set (according to mask). 170 */ 171 virtual void multi_set(Pin_slice const &mask, unsigned data) = 0; 172 173 /** 174 * \brief Get the value for all pins at once. 175 */ 176 virtual unsigned multi_get(unsigned offset) = 0; 177 178 179 void output(unsigned pin, int value) 180 { setup(pin, Output, value); } 181 182 void input(unsigned pin) 183 { setup(pin, Input); } 184 185 void multi_output(Pin_slice const &mask, unsigned data) 186 { multi_setup(mask, mask.mask, data); } 187 188 void multi_input(Pin_slice const &mask) 189 { multi_setup(mask, ~mask.mask); } 190 }; 191 192 } 193 194 #else 195 196 typedef enum { 197 false = 0, 198 true = 1 199 } bool; 200 201 /// Modes used for setup() 202 enum Fix_mode 203 { 204 Fix_input = L4VBUS_GPIO_SETUP_INPUT, ///< Configure as input pin 205 Fix_output = L4VBUS_GPIO_SETUP_OUTPUT, ///< Configure as output pin 206 Fix_irq = L4VBUS_GPIO_SETUP_IRQ, ///< Configure as IRQ source 207 }; 208 209 /// Modes used for pull up / pull down (config_pull()) 210 enum Pull_mode 211 { 212 Pull_none = L4VBUS_GPIO_PIN_PULL_NONE, ///< No pull up or pull down resistors 213 Pull_up = L4VBUS_GPIO_PIN_PULL_UP, ///< Enable pull up resistor 214 Pull_down = L4VBUS_GPIO_PIN_PULL_DOWN, ///< Enable pull down resistor 215 }; 216 217 // Generic function options 218 enum Function_levels 219 { 220 Function_gpio, 221 Function_alt, 222 Function_irq, 223 }; 224 225 #endif /* __cplusplus */