2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
4 * Copyright (c) 2014 Intel Corporation.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #define SYSFS_CLASS_GPIO "/sys/class/gpio"
40 maa_gpio_get_valfp(maa_gpio_context *dev)
43 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
45 dev->value_fp = open(bu, O_RDWR);
46 if (dev->value_fp == -1) {
47 return MAA_ERROR_INVALID_RESOURCE;
54 maa_gpio_init(int pin)
56 int pinm = maa_check_gpio(pin);
60 return maa_gpio_init_raw(pinm);
64 maa_gpio_init_raw(int pin)
73 maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
74 memset(dev, 0, sizeof(maa_gpio_context));
76 dev->isr_value_fp = -1;
79 if ((export_f = fopen(SYSFS_CLASS_GPIO "/export", "w")) == NULL) {
80 fprintf(stderr, "Failed to open export for writing!\n");
83 length = snprintf(bu, sizeof(bu), "%d", dev->pin);
84 fwrite(bu, sizeof(char), length, export_f);
91 maa_gpio_wait_interrupt(int fd)
96 // setup poll on POLLPRI
100 // do an initial read to clear interupt
104 return MAA_ERROR_INVALID_RESOURCE;
107 // Wait for it forever or until pthread_cancel
108 // poll is a cancelable point like sleep()
109 int x = poll (&pfd, 1, -1);
111 // do a final read to clear interupt
118 maa_gpio_interrupt_handler(void* arg)
120 maa_gpio_context* dev = (maa_gpio_context*) arg;
123 // open gpio value with open(3)
125 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
126 dev->isr_value_fp = open(bu, O_RDONLY);
129 ret = maa_gpio_wait_interrupt(dev->isr_value_fp);
130 if (ret == MAA_SUCCESS) {
131 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
133 // In order to call a python object (all python functions are objects) we
134 // need to aquire the GIL (Global Interpreter Lock). This may not always be
135 // nessecary but especially if doing IO (like print()) python will segfault
136 // if we do not hold a lock on the GIL
137 PyGILState_STATE gilstate = PyGILState_Ensure();
139 PyEval_CallObject(dev->isr, NULL);
141 PyGILState_Release (gilstate);
145 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
147 // we must have got an error code so die nicely
148 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
149 close(dev->isr_value_fp);
150 dev->isr_value_fp = -1;
157 maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode)
159 if (dev->value_fp != -1) {
160 close(dev->value_fp);
164 char filepath[MAX_SIZE];
165 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin);
168 if ((edge= fopen(filepath, "w")) == NULL) {
169 fprintf(stderr, "Failed to open edge for writing!\n");
170 return MAA_ERROR_INVALID_RESOURCE;
176 case MAA_GPIO_EDGE_NONE:
177 length = snprintf(bu, sizeof(bu), "none");
179 case MAA_GPIO_EDGE_BOTH:
180 length = snprintf(bu, sizeof(bu), "both");
182 case MAA_GPIO_EDGE_RISING:
183 length = snprintf(bu, sizeof(bu), "rising");
185 case MAA_GPIO_EDGE_FALLING:
186 length = snprintf(bu, sizeof(bu), "falling");
190 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
192 fwrite(bu, sizeof(char), length, edge);
199 maa_gpio_isr(maa_gpio_context *dev, gpio_edge_t mode, void (*fptr)(void))
201 // we only allow one isr per maa_gpio_context
202 if (dev->thread_id != 0) {
203 return MAA_ERROR_NO_RESOURCES;
206 maa_gpio_edge_mode(dev, mode);
208 pthread_create (&dev->thread_id, NULL, maa_gpio_interrupt_handler, (void *) dev);
214 maa_gpio_isr_exit(maa_gpio_context *dev)
216 maa_result_t ret = MAA_SUCCESS;
218 // wasting our time, there is no isr to exit from
219 if (dev->thread_id == 0 && dev->isr_value_fp == -1) {
223 // stop isr being useful
224 ret = maa_gpio_edge_mode(dev, MAA_GPIO_EDGE_NONE);
226 if ((dev->thread_id != 0) &&
227 (pthread_cancel(dev->thread_id) != 0)) {
228 ret = MAA_ERROR_INVALID_HANDLE;
231 // close the filehandle in case it's still open
232 if (dev->isr_value_fp != -1) {
233 if (close(dev->isr_value_fp) != 0) {
234 ret = MAA_ERROR_INVALID_PARAMETER;
239 // Dereference our Python call back function
243 // assume our thread will exit either way we just lost it's handle
245 dev->isr_value_fp = -1;
251 maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
253 if (dev->value_fp != -1) {
254 close(dev->value_fp);
258 char filepath[MAX_SIZE];
259 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin);
262 if ((drive = fopen(filepath, "w")) == NULL) {
263 fprintf(stderr, "Failed to open drive for writing!\n");
264 return MAA_ERROR_INVALID_RESOURCE;
270 case MAA_GPIO_STRONG:
271 length = snprintf(bu, sizeof(bu), "strong");
273 case MAA_GPIO_PULLUP:
274 length = snprintf(bu, sizeof(bu), "pullup");
276 case MAA_GPIO_PULLDOWN:
277 length = snprintf(bu, sizeof(bu), "pulldown");
280 length = snprintf(bu, sizeof(bu), "hiz");
284 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
286 fwrite(bu, sizeof(char), length, drive);
293 maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
296 return MAA_ERROR_INVALID_HANDLE;
298 if (dev->value_fp != -1) {
299 close(dev->value_fp);
302 char filepath[MAX_SIZE];
303 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
306 if ((direction = fopen(filepath, "w")) == NULL) {
307 fprintf(stderr, "Failed to open direction for writing!\n");
308 return MAA_ERROR_INVALID_RESOURCE;
315 length = snprintf(bu, sizeof(bu), "out");
318 length = snprintf(bu, sizeof(bu), "in");
322 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
324 fwrite(bu, sizeof(char), length, direction);
331 maa_gpio_read(maa_gpio_context *dev)
333 if (dev->value_fp == -1) {
334 if (maa_gpio_get_valfp(dev) != MAA_SUCCESS) {
335 fprintf(stderr, "Failed to get value file pointer\n");
339 // if value_fp is new this is pointless
340 lseek(dev->value_fp, 0, SEEK_SET);
343 if (read(dev->value_fp, bu, 2*sizeof(char)) != 2) {
344 fprintf(stderr, "Failed to read a sensible value from sysfs");
346 lseek(dev->value_fp, 0, SEEK_SET);
347 int ret = strtol(bu, NULL, 10);
353 maa_gpio_write(maa_gpio_context *dev, int value)
355 if (dev->value_fp == -1) {
356 maa_gpio_get_valfp(dev);
358 if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
359 return MAA_ERROR_INVALID_RESOURCE;
363 int length = snprintf(bu, sizeof(bu), "%d", value);
364 if (write(dev->value_fp, bu, length*sizeof(char)) == -1) {
365 return MAA_ERROR_INVALID_HANDLE;
372 maa_gpio_unexport(maa_gpio_context *dev)
376 if ((unexport_f = fopen(SYSFS_CLASS_GPIO "/unexport", "w")) == NULL) {
377 fprintf(stderr, "Failed to open unexport for writing!\n");
378 return MAA_ERROR_INVALID_RESOURCE;
382 int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
383 fwrite(bu, sizeof(char), length, unexport_f);
386 maa_gpio_isr_exit(dev);
392 maa_gpio_close(maa_gpio_context *dev)
394 if (dev->value_fp != -1) {
395 close(dev->value_fp);
397 maa_gpio_unexport(dev);