L4Re - L4 Runtime Environment
vbus_gpio
1 // vi:ft=cpp
2 /*
3  * (c) 2011 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
4  * economic rights: Technische Universit├Ąt Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10 #pragma once
11 
12 #include <l4/vbus/vbus>
13 #include <l4/vbus/vbus_gpio.h>
14 
15 namespace L4vbus {
16 
17 /**
18  * \brief A GPIO pin
19  * \ingroup l4vbus_gpio_module
20  *
21  */
22 class Gpio_pin : public Device
23 {
24 public:
25  Gpio_pin(Device const &dev, unsigned pin)
26  : Device(dev), _pin(pin)
27  {}
28 
29  /**
30  * \brief Read value of GPIO input pin
31  *
32  * \return Value of GPIO pin (usually 0 or 1), negative error code otherwise.
33  */
34  int get() const
35  {
36  return l4vbus_gpio_get(_bus.cap(), _dev, _pin);
37  }
38 
39  /**
40  * \brief Set GPIO output pin
41  *
42  * \param value Value to write to the GPIO pin (usually 0 or 1)
43  * \return 0 if OK, error code otherwise
44  */
45  int set(int value) const
46  {
47  return l4vbus_gpio_set(_bus.cap(), _dev, _pin, value);
48  }
49 
50  /**
51  * \brief Configure the function of a GPIO pin
52  *
53  * \param mode GPIO function, see #L4vbus_gpio_generic_func for generic
54  * functions. Hardware specific functions must be provided in
55  * the lower 8 bits.
56  * \param value Optional value to set the GPIO pin to if it is configured
57  * as an output pin
58  * \return 0 if OK, error code otherwise
59  */
60  int setup(unsigned mode, unsigned value) const
61  {
62  return l4vbus_gpio_setup(_bus.cap(), _dev, _pin, mode, value);
63  }
64 
65  /**
66  * \brief Generic function to set pull up/down mode
67  *
68  * \param mode mode for pull up/down resistors, see #L4vbus_gpio_pull_modes
69  * \return 0 if OK, error code otherwise
70  */
71  int config_pull(unsigned mode) const
72  {
73  return l4vbus_gpio_config_pull(_bus.cap(), _dev, _pin, mode);
74  }
75 
76  /**
77  * \brief Hardware specific configuration function
78  *
79  * \param func Hardware specific configuration register, usually offset to
80  * the GPIO chip's base address
81  * \param value Value which is written into the hardware specific
82  * configuration register for the specified pin
83  * \return 0 if OK, error code otherwise
84  */
85  int config_pad(unsigned func, unsigned value) const
86  {
87  return l4vbus_gpio_config_pad(_bus.cap(), _dev, _pin, func, value);
88  }
89 
90  /**
91  * \brief Read hardware specific configuration
92  *
93  * \param func Hardware specific configuration register to read from.
94  * Usually this is an offset to the GPIO chip's base address.
95  * \param[out] value The configuration value.
96  * \return 0 if OK, error code otherwise
97  */
98  int config_get(unsigned func, unsigned *value) const
99  {
100  return l4vbus_gpio_config_get(_bus.cap(), _dev, _pin, func, value);
101  }
102 
103  /**
104  * \brief Create IRQ for GPIO pin
105  *
106  * \return IRQ number if OK, negative error code otherwise
107  */
108  int to_irq() const
109  {
110  return l4vbus_gpio_to_irq(_bus.cap(), _dev, _pin);
111  }
112 
113  /**
114  * \brief Get pin number
115  *
116  * \return GPIO pin number
117  */
118  unsigned pin() const { return _pin; }
119 
120 protected:
121  Gpio_pin() {}
122  unsigned _pin;
123 };
124 
125 /**
126  * \brief A Gpio_module groups multiple GPIO pins together
127  * \ingroup l4vbus_gpio_module
128  */
129 class Gpio_module : public Device
130 {
131 public:
132  Gpio_module(Device dev)
133  : Device(dev)
134  {}
135 
136  /**
137  * \brief A slice of the pins provided by this module.
138  *
139  * Data type to specify a selection of pins for the 'multi'
140  * methods.
141  */
142  struct Pin_slice
143  {
144  Pin_slice(unsigned offset, unsigned mask) : offset(offset), mask(mask) {}
145  unsigned offset, mask;
146  };
147 
148  /**
149  * \brief Configure function of multiple GPIO pins at once
150  *
151  * \param mask Mask of GPIO pins to configure. A bit set to 1 configures
152  * this pin. A maximum of 32 pins can be configured at once.
153  * The real number depends on the hardware and the driver
154  * implementation.
155  * \param mode GPIO function, see #L4vbus_gpio_generic_func for generic
156  * functions. Hardware specific functions must be provided in
157  * the lower 8 bits.
158  * \param value Optional value to set the GPIO pins to if they are
159  * configured as output pins
160  * \return 0 if OK, error code otherwise
161  */
162  int setup(Pin_slice const &mask, unsigned mode, unsigned value) const
163  {
164  return l4vbus_gpio_multi_setup(_bus.cap(), _dev, mask.offset, mask.mask,
165  mode, value);
166  }
167 
168  /**
169  * \brief Hardware specific configuration function for multiple GPIO pins
170  *
171  * \param mask Mask of GPIO pins to configure. A bit set to 1 configures
172  * this pin. A maximum of 32 pins can be configured at once.
173  * The real number depends on the hardware and the driver
174  * implementation.
175  * \param func Hardware specific configuration register, usually offset to
176  * the GPIO chip's base address.
177  * \param value Value which is written into the hardware specific
178  * configuration register for the specified pins
179  * \return 0 if OK, error code otherwise
180  */
181  int config_pad(Pin_slice const &mask, unsigned func, unsigned value) const
182  {
183  return l4vbus_gpio_multi_config_pad(_bus.cap(), _dev, mask.offset,
184  mask.mask, func, value);
185  }
186 
187  /**
188  * \brief Read values of multiple GPIO pins at once
189  *
190  * \param offset Pin corresponding to the LSB in \a data.
191  * Note: allowed may be hardware specific.
192  * \param[out] data Each bit returns the value (0 or 1) for the corresponding
193  * GPIO pin. The value of pins that are not accessible is
194  * undefined.
195  * \return 0 if OK, error code otherwise
196  */
197  int get(unsigned offset, unsigned *data) const
198  {
199  return l4vbus_gpio_multi_get(_bus.cap(), _dev, offset, data);
200  }
201 
202  /**
203  * \brief Set multiple GPIO output pins at once
204  *
205  * \param mask Mask of GPIO pins to set. A bit set to 1 selects this pin.
206  * A maximum of 32 pins can be set at once. The real number
207  * depends on the hardware and the driver implementation.
208  * \param data Each bit corresponds to the GPIO pin in \a mask. The value
209  * of each bit is written to the GPIO pin if its bit in \a mask
210  * is set.
211  * \return 0 if OK, error code otherwise
212  */
213  int set(Pin_slice const &mask, unsigned data)
214  {
215  return l4vbus_gpio_multi_set(_bus.cap(), _dev, mask.offset,
216  mask.mask, data);
217  }
218 
219  /**
220  * \brief Get Gpio_pin for a specific pin of this Gpio_module
221  *
222  * \param pin GPIO pin number to get Gpio_pin for.
223  * \return Gpio_pin
224  */
225  Gpio_pin pin(unsigned pin) const
226  {
227  return Gpio_pin(*this, pin);
228  }
229 
230 protected:
231  Gpio_module() {}
232 };
233 
234 }