gpio: add ownership gaurd
[contrib/mraa.git] / api / 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 /** @file
28  *
29  * This file defines the gpio interface for libmaa
30  *
31  */
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #ifdef SWIGPYTHON
38 #include <Python.h>
39 #endif
40
41 #include <stdio.h>
42 #include <pthread.h>
43
44 #include "maa.h"
45 /**
46  * A strucutre representing a gpio pin.
47  */
48
49 /**
50  * Opaque pointer definition to the internal struct _gpio
51  */
52 typedef struct _gpio* maa_gpio_context;
53
54 /**
55  * GPIO Output modes
56  */
57 typedef enum {
58     MAA_GPIO_STRONG     = 0, /**< Default. Strong high and low */
59     MAA_GPIO_PULLUP     = 1, /**< Resistive High */
60     MAA_GPIO_PULLDOWN   = 2, /**< Resistive Low */
61     MAA_GPIO_HIZ        = 3  /**< High Z State */
62 } gpio_mode_t;
63
64 /**
65  * GPIO Direction options.
66  */
67 typedef enum {
68     MAA_GPIO_OUT    = 0, /**< Output. A Mode can also be set */
69     MAA_GPIO_IN     = 1  /**< Input. */
70 } gpio_dir_t;
71
72 typedef enum {
73     MAA_GPIO_EDGE_NONE    = 0, /**< No interrupt on GPIO */
74     MAA_GPIO_EDGE_BOTH    = 1, /**< Interupt on rising & falling */
75     MAA_GPIO_EDGE_RISING  = 2, /**< Interupt on rising only */
76     MAA_GPIO_EDGE_FALLING = 3  /**< Interupt on falling only */
77 } gpio_edge_t;
78
79 /** Initialise gpio_context, based on board number
80  *
81  *  @param pin pin number read from the board, i.e IO3 is 3.
82  *
83  *  @returns maa_gpio_context based on the IO pin
84  */
85 maa_gpio_context maa_gpio_init(int pin);
86
87 /** Initialise gpio context without any mapping to a pin.
88  * - For more expert users
89  *
90  * @param gpiopin gpio pin as listed in SYSFS
91  *
92  * @return gpio context
93  */
94 maa_gpio_context maa_gpio_init_raw(int gpiopin);
95
96 /** Set the edge mode on the gpio
97  *
98  * @param dev The GPIO context
99  * @param mode The edge mode to set the gpio into
100  *
101  * @return maa result type.
102  */
103 maa_result_t maa_gpio_edge_mode(maa_gpio_context dev, gpio_edge_t mode);
104
105 /** Set an interupt on pin
106  *
107  * @param dev The GPIO context
108  * @param mode The edge mode to set the gpio into
109  * @param fptr Function pointer to function to be called when interupt is
110  * triggered
111  *
112  * @return maa result type.
113  */
114 maa_result_t
115 maa_gpio_isr(maa_gpio_context dev, gpio_edge_t edge, void (*fptr)(void));
116
117 /** Stop the current interupt watcher on this GPIO, and set the GPIO edge mode
118  * to MAA_GPIO_EDGE_NONE.
119  *
120  * @param dev The GPIO context.
121  *
122  * @return maa result type.
123  */
124 maa_result_t
125 maa_gpio_isr_exit(maa_gpio_context dev);
126
127 /** Set GPIO Output Mode,
128  *
129  * @param dev The GPIO context
130  * @param mode The GPIO Output Mode.
131  *
132  * @return maa result type.
133  */
134 maa_result_t maa_gpio_mode(maa_gpio_context dev, gpio_mode_t mode);
135
136 /** Set GPIO direction
137  *
138  * @param dev The GPIO context.
139  * @param dir The direction of the GPIO.
140  *
141  * @return maa result type.
142  */
143 maa_result_t maa_gpio_dir(maa_gpio_context dev, gpio_dir_t dir);
144
145 /** Close the GPIO context
146  * - Will free the memory for the context and unexport the GPIO
147  *
148  * @param dev the GPIO context
149  *
150  * @return maa result type.
151  */
152 maa_result_t maa_gpio_close(maa_gpio_context dev);
153
154 /** Unexport the GPIO context (maa_gpio_close() will call this function)
155  *
156  * @param dev The GPIO context.
157  *
158  * @return maa result type.
159  */
160 maa_result_t maa_gpio_unexport(maa_gpio_context dev);
161
162 /** Unexport the GPIO context (maa_gpio_close() will call this function)
163  *  Forces regardless to to ownership.
164  *
165  * @param dev The GPIO context.
166  *
167  * @return maa result type.
168  */
169 maa_result_t maa_gpio_unexport_force(maa_gpio_context dev);
170
171 /** Read the GPIO value.
172  *
173  * @param dev The GPIO context.
174  *
175  * @return the integer value of the GPIO
176  */
177 int maa_gpio_read(maa_gpio_context dev);
178
179 /** Write to the GPIO Value.
180  *
181  * @param dev The GPIO context.
182  * @param value Integer value to write.
183  *
184  * @return maa result type
185  */
186 maa_result_t maa_gpio_write(maa_gpio_context dev, int value);
187
188 /** Change ownership of the context.
189  *
190  * @param dev gpio context
191  * @param owner does this context own the pin.
192  */
193 maa_result_t maa_gpio_owner(maa_gpio_context dev, maa_boolean_t owner);
194
195 #ifdef __cplusplus
196 }
197 #endif