gpio  1.4.2
General Purpose I/O Package
gpio.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: gpio
4 //
5 // Library: libgpio
6 //
7 // File: gpio.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-04-08 17:22:10 -0600 (Wed, 08 Apr 2015) $
12  * $Rev: 3913 $
13  *
14  * \brief GPIO interface declarations and defines.
15  *
16  * The memory mapped interface is base on:
17  * \author Markham Thomas
18  * (https://github.com/mlinuxguy/odpygpio)\n
19  *
20  * \author Robin Knight (robin.knight@roadnarrows.com)
21  *
22  * \copyright
23  * \h_copy 2015-2017. RoadNarrows LLC.\n
24  * http://www.roadnarrows.com\n
25  * All Rights Reserved
26  */
27 /*
28  * @EulaBegin@
29 // Permission is hereby granted, without written agreement and without
30 // license or royalty fees, to use, copy, modify, and distribute this
31 // software and its documentation for any purpose, provided that
32 // (1) The above copyright notice and the following two paragraphs
33 // appear in all copies of the source code and (2) redistributions
34 // including binaries reproduces these notices in the supporting
35 // documentation. Substantial modifications to this software may be
36 // copyrighted by their authors and need not follow the licensing terms
37 // described here, provided that the new terms are clearly indicated in
38 // all files where they apply.
39 //
40 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45 // THE POSSIBILITY OF SUCH DAMAGE.
46 //
47 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  * @EulaEnd@
53  */
54 ////////////////////////////////////////////////////////////////////////////////
55 
56 #ifndef _GPIO_H
57 #define _GPIO_H
58 
59 #include <sys/types.h>
60 
61 #include "rnr/rnrconfig.h"
62 
63 C_DECLS_BEGIN
64 
65 //
66 // GPIO direction
67 //
68 #define GPIO_DIR_IN 0 ///< input
69 #define GPIO_DIR_OUT 1 ///< output
70 #define GPIO_DIR_IN_STR "in" ///< input string
71 #define GPIO_DIR_OUT_STR "out" ///< output string
72 
73 //
74 // GPIO edge to make select trigger
75 //
76 #define GPIO_EDGE_NONE 0 ///< no edge
77 #define GPIO_EDGE_RISING 1 ///< rising edge
78 #define GPIO_EDGE_FALLING 2 ///< falling edge
79 #define GPIO_EDGE_BOTH 3 ///< both edges
80 #define GPIO_EDGE_NONE_STR "none" ///< no edge string
81 #define GPIO_EDGE_RISING_STR "rising" ///< rising edge string
82 #define GPIO_EDGE_FALLING_STR "falling" ///< falling edge string
83 #define GPIO_EDGE_BOTH_STR "both" ///< both edges string
84 
85 //
86 // GPIO pull ups
87 //
88 #define GPIO_PULL_DS 0 ///< disable pullup/down
89 #define GPIO_PULL_UP 1 ///< enable pullup
90 #define GPIO_PULL_DN 2 ///< enable pulldown
91 #define GPIO_PULL_DS_STR "disabled" ///< disable pullup/down string
92 #define GPIO_PULL_UP_STR "up" ///< enable pullup string
93 #define GPIO_PULL_DN_STR "down" ///< enable pulldown string
94 
95 /*!
96  * \brief GPIO info structure.
97  */
98 typedef struct
99 {
100  int gpio; ///< sysfs exported gpio number
101  int pin; ///< external header pin number
102  int dir; ///< gpio direction
103  int edge; ///< gpio edge type trigger
104  int pull; ///< pull state
105  int value; ///< current value
106 } gpio_info_t;
107 
108 
109 //-----------------------------------------------------------------------------
110 // Prototypes
111 //-----------------------------------------------------------------------------
112 
113 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
114 // GPIO access methods using the sysfs system.
115 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
116 
117 /*!
118  * \brief Make GPIO directory name.
119  *
120  * Director name: /sys/class/gpio/gpio<gpio>
121  *
122  * The base directory holds key special files such as <em>value</em>,
123  * <em>direction</em>, and <em>edge</em>.
124  *
125  * Method: sysfs
126  *
127  * \param gpio The sysfs exported GPIO number.
128  * \param [out] buf Buffer to hold directory name.
129  * \param size Size of buffer in bytes.
130  */
131 extern void gpioMakeDirname(int gpio, char buf[], size_t size);
132 
133 /*!
134  * \brief Export (create) a GPIO interface.
135  *
136  * The call requires root privaleges.
137  *
138  * Method: sysfs
139  *
140  * \param gpio The sysfs exported GPIO number.
141  *
142  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
143  */
144 extern int gpioExport(int gpio);
145 
146 /*!
147  * \brief Unexport (delete) a GPIO interface.
148  *
149  * The call requires root privaleges.
150  *
151  * Method: sysfs
152  *
153  * \param gpio The sysfs exported GPIO number.
154  *
155  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
156  */
157 extern int gpioUnexport(int gpio);
158 
159 /*!
160  * \brief Set GPIO signal direction.
161  *
162  * Method: sysfs
163  *
164  * \param gpio The sysfs exported GPIO number.
165  * \param dir Direction. One of: GPIO_DIR_IN(0) GPIO_DIR_OUT(1)
166  *
167  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
168  */
169 extern int gpioSetDirection(int gpio, int dir);
170 
171 /*!
172  * \brief Set GPIO edge trigger type.
173  *
174  * Method: sysfs
175  *
176  * \param gpio The sysfs exported GPIO number.
177  * \param edge Edge. One of: GPIO_EDGE_NONE(0) GPIO_EDGE_RISING(1)
178  * GPIO_EDGE_FALLING(2) GPIO_EDGE_BOTH(3)
179  *
180  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
181  */
182 extern int gpioSetEdge(int gpio, int edge);
183 
184 /*!
185  * \brief Set GPIO pull.
186  *
187  * Methodsys mmap
188  *
189  * \param gpio The sysfs exported GPIO number.
190  * \param pull Pull type. One of:
191  * GPIO_PULL_DS(0) to disabled
192  * GPIO_PULL_UP(1) to pull-up
193  * GPIO_PULL_DN(2) to pull-down
194  *
195  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
196  */
197 extern int gpioSetPull(int gpio, int pull);
198 
199 /*!
200  * \brief Safely probe GPIO parameters.
201  *
202  * Method: sysfs
203  *
204  * \param gpio The sysfs exported GPIO number.
205  * \param [out] p GPIO info.
206  *
207  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
208  */
209 extern int gpioProbe(int gpio, gpio_info_t *p);
210 
211 /*!
212  * \brief Read GPIO pin's current value.
213  *
214  * Method: sysfs
215  *
216  * \param gpio The sysfs exported GPIO number.
217  *
218  * \return
219  * On success the pin value 0 or 1 is returned.
220  * Otherwise RC_ERROR(-1) is returned.
221  */
222 extern int gpioRead(int gpio);
223 
224 /*!
225  * \brief Write GPIO value.
226  *
227  * Method: sysfs
228  *
229  * \param gpio The sysfs exported GPIO number.
230  * \param value GPIO pin value. One of: 0 (low) or 1 (high).
231  *
232  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
233  */
234 extern int gpioWrite(int gpio, int value);
235 
236 /*!
237  * \brief Notify on GPIO input value change.
238  *
239  * This function blocks until 1) value has changed, 2) a timeout has occurred,
240  * or 3) an error was encoutered.
241  *
242  * Method: sysfs
243  *
244  * \param fd Open GPIO pin file descriptor.
245  * \param timeout Wait timeout. A value of 0.0 is no timeout.
246  *
247  * \return
248  * On success the pin value 0 or 1 is returned.
249  * Otherwise RC_ERROR(-1) is returned. Use errno to determine error.
250  */
251 extern int gpioNotify(int fd, double timeout);
252 
253 /*!
254  * \brief Open GPIO pin.
255  *
256  * Leaving the file descriptor open allows for quick reads or writes. It is
257  * the responsibility of the user to close the file descriptor.
258  *
259  * Method: sysfs
260  *
261  * \param gpio The sysfs exported GPIO number.
262  *
263  * \return On success the open file descriptor is returned.
264  * Otherwise RC_ERROR(-1) is returned.
265  */
266 extern int gpioOpen(int gpio);
267 
268 /*!
269  * \brief Close GPIO pin.
270  *
271  * Method: sysfs
272  *
273  * \param fd Open GPIO pin file descriptor.
274  *
275  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
276  */
277 extern int gpioClose(int fd);
278 
279 /*!
280  * \brief Quick read GPIO pin's current value.
281  *
282  * Method: sysfs
283  *
284  * \param fd Open GPIO pin file descriptor.
285  *
286  * \return
287  * On success the pin value 0 or 1 is returned.
288  * Otherwise RC_ERROR(-1) is returned.
289  */
290 extern int gpioQuickRead(int fd);
291 
292 /*!
293  * \brief Quick write GPIO pin value.
294  *
295  * Method: sysfs
296  *
297  * \param fd Open GPIO pin file descriptor.
298  * \param value GPIO pin value. One of: 0 (low) or 1 (high).
299  *
300  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
301  */
302 extern int gpioQuickWrite(int fd, int value);
303 
304 /*!
305  * \brief Bit-bang bits out a GPIO pin.
306  *
307  * Bits from pattern[0] ... pattern[N] sequentially drive the GPIO output
308  * pin. If bit == 0 then the pin is driven low, if bit == 1 then the pin
309  * is driven high.
310  *
311  * The pattern buffer must be at least (bitCount + 7) / 8 bytes long. The bits
312  * in pattern[i] are read out in big-endian order b7 b6 ...
313  *
314  * Between bit output, an inter-bit delay of usecIbd is enforced.
315  *
316  * Method: sysfs
317  *
318  * \param fd Open GPIO pin file descriptor.
319  * \param pattern Bit pattern buffer to bang. The bytes are big-endian.
320  * \param bitCount Count of bits to bang.
321  * \param usecIbd Inter-bit delay in microseconds. A 0 value means a fast
322  * as the architecture can bang.
323  *
324  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
325  */
326 extern int gpioBitBang(int fd,
327  byte_t pattern[],
328  size_t bitCount,
329  unsigned int usecIbd);
330 
331 
332 #ifdef MMAP_GPIO
333 
334 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
335 // GPIO access methods using memory mapped I/O.
336 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
337 
338 /*!
339  * \brief GPIO info structure.
340  */
341 typedef struct
342 {
343  int gpio; ///< sysfs exported gpio number
344  int pin; ///< external header pin number
345  off_t base; ///< memory mapped base address
346  int channel; ///< memory mapped gpio channel offset
347  int bit; //<< memory mapped gpio bit
348  int dir; ///< gpio direction
349  int edge; ///< gpio edge type trigger
350  int pull; ///< pull state
351  int value; ///< current value
352 } mmap_gpio_info_t;
353 
354 /*!
355  * \brief Find the external header pin number from system exported GPIO number.
356  *
357  * \param gpio The sysfs exported GPIO number.
358  *
359  * \return On success returns the associated header pin number.
360  * On error, RC_ERROR(-1) is returned.
361  */
362 extern int gpioExportedToPin(int gpio);
363 
364 /*!
365  * \brief Find the system exported GPIO number from external header pin number.
366  *
367  * \param pin External CON10 header pin number.
368  *
369  * \return On success returns the associated exported GPIO number.
370  * On error, RC_ERROR(-1) is returned.
371  */
372 extern int gpioPinToExported(int pin);
373 
374 /*!
375  * \brief Map the memory block of GPIO.
376  *
377  * This call must be called prior to accessing the GPIO via memory.
378  *
379  * Method: mmap
380  *
381  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
382  */
383 extern int mmapGpioMap();
384 
385 /*!
386  * \brief Unmap the memory block of GPIO.
387  *
388  * Should be called when finished with memory mapped GPIO.
389  *
390  * Method: mmap
391  *
392  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
393  */
394 extern int mmapGpioUnmap();
395 
396 /*!
397  * \brief Set GPIO signal direction.
398  *
399  * Method: mmap
400  *
401  * \param gpio The sysfs exported GPIO number.
402  * \param dir Direction. One of: GPIO_DIR_IN(0) GPIO_DIR_OUT(1)
403  *
404  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
405  */
406 extern int mmapGpioSetDirection(int gpio, int dir);
407 
408 /*!
409  * \brief Set GPIO pull.
410  *
411  * Method: mmap
412  *
413  * \param gpio The sysfs exported GPIO number.
414  * \param pull Pull type. One of:
415  * GPIO_PULL_DS(0) to disabled
416  * GPIO_PULL_UP(1) to pull-up
417  * GPIO_PULL_DN(2) to pull-down
418  *
419  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
420  */
421 extern int mmapGpioSetPull(int gpio, int pull);
422 
423 /*!
424  * \brief Safely probe GPIO parameters.
425  *
426  * Method: mmap
427  *
428  * \param gpio The sysfs exported GPIO number.
429  * \param [out] p GPIO info.
430  *
431  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
432  */
433 extern int mmapGpioProbe(int gpio, mmap_gpio_info_t *p);
434 
435 /*!
436  * \brief Read GPIO pin's current value.
437  *
438  * Method: mmap
439  *
440  * \param gpio The sysfs exported GPIO number.
441  *
442  * \return
443  * On success the pin value 0 or 1 is returned.
444  * Otherwise RC_ERROR(-1) is returned.
445  */
446 extern int mmapGpioRead(int gpio);
447 
448 /*!
449  * \brief Write GPIO pin value.
450  *
451  * \note Expect it to take around 800 clocks after setting a output bit for it
452  * to show.
453  *
454  * Method: mmap
455  *
456  * \param gpio The sysfs exported GPIO number.
457  * \param value GPIO pin value. One of: 0 (low) or 1 (high).
458  *
459  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
460  */
461 extern int mmapGpioWrite(int gpio, int value);
462 
463 /*!
464  * \brief Toggle GPIO count high-low (low-high) times.
465  *
466  * Method: mmap
467  *
468  * \param gpio The sysfs exported GPIO number.
469  * \param count Count toggles.
470  *
471  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
472  */
473 extern int mmapGpioToggle(int gpio, int count);
474 
475 /*!
476  * \brief Bit-bang bits out a GPIO pin.
477  *
478  * Bits from pattern[0] ... pattern[N] sequentially drive the GPIO output
479  * pin. If bit == 0 then the pin is driven low, if bit == 1 then the pin
480  * is driven high.
481  *
482  * The pattern buffer must be at least (bitCount + 7) / 8 bytes long. The bits
483  * in pattern[i] are read out in big-endian order b7 b6 ...
484  *
485  * Between bit output, an inter-bit delay of usecIbd is enforced.
486  *
487  * Method: mmap
488  *
489  * \param gpio The sysfs exported GPIO number.
490  * \param pattern Bit pattern buffer to bang. The bytes are big-endian.
491  * \param bitCount Count of bits to bang.
492  * \param usecIbd Inter-bit delay in microseconds. A 0 value means a fast
493  * as the architecture can bang.
494  *
495  * \return On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.
496  */
497 extern int mmapGpioBitBang(int gpio,
498  byte_t pattern[],
499  size_t bitCount,
500  unsigned int usecIbd);
501 
502 #endif // MMAP_GPIO
503 
504 
505 C_DECLS_END
506 
507 #endif // _GPIO_H
int gpioQuickWrite(int fd, int value)
Quick write GPIO pin value.
Definition: gpio.c:538
int gpioOpen(int gpio)
Open GPIO pin.
Definition: gpio.c:492
int pull
pull state
Definition: gpio.h:104
int gpio
sysfs exported gpio number
Definition: gpio.h:100
int gpioRead(int gpio)
Read GPIO pin&#39;s current value.
Definition: gpio.c:388
int gpioProbe(int gpio, gpio_info_t *p)
Safely probe GPIO parameters.
Definition: gpio.c:346
int gpioWrite(int gpio, int value)
Write GPIO value.
Definition: gpio.c:414
int gpioNotify(int fd, double timeout)
Notify on GPIO input value change.
Definition: gpio.c:442
int gpioQuickRead(int fd)
Quick read GPIO pin&#39;s current value.
Definition: gpio.c:520
int edge
gpio edge type trigger
Definition: gpio.h:103
int gpioSetEdge(int gpio, int edge)
Set GPIO edge trigger type.
Definition: gpio.c:292
int gpioBitBang(int fd, byte_t pattern[], size_t bitCount, unsigned int usecIbd)
Bit-bang bits out a GPIO pin.
Definition: gpio.c:558
int gpioUnexport(int gpio)
Unexport (delete) a GPIO interface.
Definition: gpio.c:221
int gpioClose(int fd)
Close GPIO pin.
Definition: gpio.c:510
GPIO info structure.
Definition: gpio.h:98
int dir
gpio direction
Definition: gpio.h:102
int gpioSetPull(int gpio, int pull)
Set GPIO pull.
Definition: gpio.c:340
int pin
external header pin number
Definition: gpio.h:101
int gpioSetDirection(int gpio, int dir)
Set GPIO signal direction.
Definition: gpio.c:250
void gpioMakeDirname(int gpio, char buf[], size_t size)
Make GPIO directory name.
Definition: gpio.c:587
int gpioExport(int gpio)
Export (create) a GPIO interface.
Definition: gpio.c:192
int value
current value
Definition: gpio.h:105