gpio.h: Fixed a couple of typos in description
[contrib/mraa.git] / api / mraa / gpio.h
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #pragma once
26
27 /**
28  * @file
29  * @brief General Purpose IO
30  *
31  * Gpio is the General Purpose IO interface to libmraa. Its features depend on
32  * the board type used, it can use gpiolibs (exported via a kernel module
33  * through sysfs), or memory mapped IO via a /dev/uio device or /dev/mem
34  * depending again on the board configuration.
35  *
36  * @snippet gpio_read6.c Interesting
37  */
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 #ifdef SWIGPYTHON
44 #include <Python.h>
45 #endif
46
47 #include <stdio.h>
48 #include <pthread.h>
49
50 #include "common.h"
51
52 /**
53  * Opaque pointer definition to the internal struct _gpio
54  */
55 typedef struct _gpio* mraa_gpio_context;
56
57 /**
58  * Gpio Output modes
59  */
60 typedef enum {
61     MRAA_GPIO_STRONG = 0,   /**< Default. Strong high and low */
62     MRAA_GPIO_PULLUP = 1,   /**< Resistive High */
63     MRAA_GPIO_PULLDOWN = 2, /**< Resistive Low */
64     MRAA_GPIO_HIZ = 3       /**< High Z State */
65 } mraa_gpio_mode_t;
66
67 /**
68  * Gpio Direction options
69  */
70 typedef enum {
71     MRAA_GPIO_OUT = 0,      /**< Output. A Mode can also be set */
72     MRAA_GPIO_IN = 1,       /**< Input */
73     MRAA_GPIO_OUT_HIGH = 2, /**< Output. Init High */
74     MRAA_GPIO_OUT_LOW = 3   /**< Output. Init Low */
75 } mraa_gpio_dir_t;
76
77 /**
78  * Gpio Edge types for interupts
79  */
80 typedef enum {
81     MRAA_GPIO_EDGE_NONE = 0,   /**< No interrupt on Gpio */
82     MRAA_GPIO_EDGE_BOTH = 1,   /**< Interupt on rising & falling */
83     MRAA_GPIO_EDGE_RISING = 2, /**< Interupt on rising only */
84     MRAA_GPIO_EDGE_FALLING = 3 /**< Interupt on falling only */
85 } mraa_gpio_edge_t;
86
87 /**
88  * Initialise gpio_context, based on board number
89  *
90  *  @param pin Pin number read from the board, i.e IO3 is 3
91  *  @returns gpio context or NULL
92  */
93 mraa_gpio_context mraa_gpio_init(int pin);
94
95 /**
96  * Initialise gpio context without any mapping to a pin
97  *
98  * @param gpiopin gpio pin as listed in SYSFS
99  * @return gpio context or NULL
100  */
101 mraa_gpio_context mraa_gpio_init_raw(int gpiopin);
102
103 /**
104  * Set the edge mode on the gpio
105  *
106  * @param dev The Gpio context
107  * @param mode The edge mode to set the gpio into
108  * @return Result of operation
109  */
110 mraa_result_t mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode);
111
112 /**
113  * Set an interupt on pin
114  *
115  * @param dev The Gpio context
116  * @param edge The edge mode to set the gpio into
117  * @param fptr Function pointer to function to be called when interupt is
118  * triggered
119  * @param args Arguments passed to the interrupt handler (fptr)
120  * @return Result of operation
121  */
122 mraa_result_t mraa_gpio_isr(mraa_gpio_context dev, mraa_gpio_edge_t edge, void (*fptr)(void*), void* args);
123
124 /**
125  * Stop the current interupt watcher on this Gpio, and set the Gpio edge mode
126  * to MRAA_GPIO_EDGE_NONE
127  *
128  * @param dev The Gpio context
129  * @return Result of operation
130  */
131 mraa_result_t mraa_gpio_isr_exit(mraa_gpio_context dev);
132
133 /**
134  * Set Gpio Output Mode,
135  *
136  * @param dev The Gpio context
137  * @param mode The Gpio Output Mode
138  * @return Result of operation
139  */
140 mraa_result_t mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode);
141
142 /**
143  * Set Gpio direction
144  *
145  * @param dev The Gpio context
146  * @param dir The direction of the Gpio
147  * @return Result of operation
148  */
149 mraa_result_t mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir);
150
151 /**
152  * Close the Gpio context
153  * - Will free the memory for the context and unexport the Gpio
154  *
155  * @param dev The Gpio context
156  * @return Result of operation
157  */
158 mraa_result_t mraa_gpio_close(mraa_gpio_context dev);
159
160 /**
161  * Read the Gpio value. This can be 0 or 1. A resonse of -1 means that there
162  * was a fatal error.
163  *
164  * @param dev The Gpio context
165  * @return Result of operation
166  */
167 int mraa_gpio_read(mraa_gpio_context dev);
168
169 /**
170  * Write to the Gpio Value.
171  *
172  * @param dev The Gpio context
173  * @param value Integer value to write
174  * @return Result of operation
175  */
176 mraa_result_t mraa_gpio_write(mraa_gpio_context dev, int value);
177
178 /**
179  * Change ownership of the context.
180  *
181  * @param dev The Gpio context
182  * @param owner Does this context own the pin
183  * @return Result of operation
184  */
185 mraa_result_t mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t owner);
186
187 /**
188  * Enable using memory mapped io instead of sysfs
189  *
190  * @param dev The Gpio context
191  * @param mmap Use mmap instead of sysfs
192  * @return Result of operation
193  */
194 mraa_result_t mraa_gpio_use_mmaped(mraa_gpio_context dev, mraa_boolean_t mmap);
195
196 /**
197  * Get a pin number of the gpio, invalid will return -1
198  *
199  * @param dev The Gpio context
200  * @return Pin number
201  */
202 int mraa_gpio_get_pin(mraa_gpio_context dev);
203
204 /**
205  * Get a gpio number as used within sysfs, invalid will return -1
206  *
207  * @param dev The Gpio context
208  * @return gpio number
209  */
210 int mraa_gpio_get_pin_raw(mraa_gpio_context dev);
211
212 #ifdef __cplusplus
213 }
214 #endif