1 /* 2 * Author: Thomas Ingleby <thomas.c.ingleby (at) 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 #if defined(SWIGJAVA) || defined(JAVACALLBACK) 48 #include <jni.h> 49 extern JavaVM *globVM; 50 extern void mraa_java_isr_callback(void *); 51 #endif 52 53 #include <stdio.h> 54 #include <pthread.h> 55 56 #include "common.h" 57 58 /** 59 * Opaque pointer definition to the internal struct _gpio 60 */ 61 typedef struct _gpio* mraa_gpio_context; 62 63 /** 64 * Gpio Output modes 65 */ 66 typedef enum { 67 MRAA_GPIO_STRONG = 0, /**< Default. Strong high and low */ 68 MRAA_GPIO_PULLUP = 1, /**< Resistive High */ 69 MRAA_GPIO_PULLDOWN = 2, /**< Resistive Low */ 70 MRAA_GPIO_HIZ = 3 /**< High Z State */ 71 } mraa_gpio_mode_t; 72 73 /** 74 * Gpio Direction options 75 */ 76 typedef enum { 77 MRAA_GPIO_OUT = 0, /**< Output. A Mode can also be set */ 78 MRAA_GPIO_IN = 1, /**< Input */ 79 MRAA_GPIO_OUT_HIGH = 2, /**< Output. Init High */ 80 MRAA_GPIO_OUT_LOW = 3 /**< Output. Init Low */ 81 } mraa_gpio_dir_t; 82 83 /** 84 * Gpio Edge types for interupts 85 */ 86 typedef enum { 87 MRAA_GPIO_EDGE_NONE = 0, /**< No interrupt on Gpio */ 88 MRAA_GPIO_EDGE_BOTH = 1, /**< Interupt on rising & falling */ 89 MRAA_GPIO_EDGE_RISING = 2, /**< Interupt on rising only */ 90 MRAA_GPIO_EDGE_FALLING = 3 /**< Interupt on falling only */ 91 } mraa_gpio_edge_t; 92 93 /** 94 * Initialise gpio_context, based on board number 95 * 96 * @param pin Pin number read from the board, i.e IO3 is 3 97 * @returns gpio context or NULL 98 */ 99 mraa_gpio_context mraa_gpio_init(int pin); 100 101 /** 102 * Initialise gpio context without any mapping to a pin 103 * 104 * @param gpiopin gpio pin as listed in SYSFS 105 * @return gpio context or NULL 106 */ 107 mraa_gpio_context mraa_gpio_init_raw(int gpiopin); 108 109 /** 110 * Set the edge mode on the gpio 111 * 112 * @param dev The Gpio context 113 * @param mode The edge mode to set the gpio into 114 * @return Result of operation 115 */ 116 mraa_result_t mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode); 117 118 /** 119 * Set an interupt on pin 120 * 121 * @param dev The Gpio context 122 * @param edge The edge mode to set the gpio into 123 * @param fptr Function pointer to function to be called when interupt is 124 * triggered 125 * @param args Arguments passed to the interrupt handler (fptr) 126 * @return Result of operation 127 */ 128 mraa_result_t mraa_gpio_isr(mraa_gpio_context dev, mraa_gpio_edge_t edge, void (*fptr)(void*), void* args); 129 130 /** 131 * Stop the current interupt watcher on this Gpio, and set the Gpio edge mode 132 * to MRAA_GPIO_EDGE_NONE 133 * 134 * @param dev The Gpio context 135 * @return Result of operation 136 */ 137 mraa_result_t mraa_gpio_isr_exit(mraa_gpio_context dev); 138 139 /** 140 * Set Gpio Output Mode, 141 * 142 * @param dev The Gpio context 143 * @param mode The Gpio Output Mode 144 * @return Result of operation 145 */ 146 mraa_result_t mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode); 147 148 /** 149 * Set Gpio direction 150 * 151 * @param dev The Gpio context 152 * @param dir The direction of the Gpio 153 * @return Result of operation 154 */ 155 mraa_result_t mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir); 156 157 /** 158 * Close the Gpio context 159 * - Will free the memory for the context and unexport the Gpio 160 * 161 * @param dev The Gpio context 162 * @return Result of operation 163 */ 164 mraa_result_t mraa_gpio_close(mraa_gpio_context dev); 165 166 /** 167 * Read the Gpio value. This can be 0 or 1. A resonse of -1 means that there 168 * was a fatal error. 169 * 170 * @param dev The Gpio context 171 * @return Result of operation 172 */ 173 int mraa_gpio_read(mraa_gpio_context dev); 174 175 /** 176 * Write to the Gpio Value. 177 * 178 * @param dev The Gpio context 179 * @param value Integer value to write 180 * @return Result of operation 181 */ 182 mraa_result_t mraa_gpio_write(mraa_gpio_context dev, int value); 183 184 /** 185 * Change ownership of the context. 186 * 187 * @param dev The Gpio context 188 * @param owner Does this context own the pin 189 * @return Result of operation 190 */ 191 mraa_result_t mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t owner); 192 193 /** 194 * Enable using memory mapped io instead of sysfs 195 * 196 * @param dev The Gpio context 197 * @param mmap Use mmap instead of sysfs 198 * @return Result of operation 199 */ 200 mraa_result_t mraa_gpio_use_mmaped(mraa_gpio_context dev, mraa_boolean_t mmap); 201 202 /** 203 * Get a pin number of the gpio, invalid will return -1 204 * 205 * @param dev The Gpio context 206 * @return Pin number 207 */ 208 int mraa_gpio_get_pin(mraa_gpio_context dev); 209 210 /** 211 * Get a gpio number as used within sysfs, invalid will return -1 212 * 213 * @param dev The Gpio context 214 * @return gpio number 215 */ 216 int mraa_gpio_get_pin_raw(mraa_gpio_context dev); 217 218 #ifdef __cplusplus 219 } 220 #endif 221