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.
26 #include "mraa_internal.h"
38 #define SYSFS_CLASS_GPIO "/sys/class/gpio"
43 mraa_gpio_get_valfp(mraa_gpio_context dev)
46 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
47 dev->value_fp = open(bu, O_RDWR);
48 if (dev->value_fp == -1) {
49 return MRAA_ERROR_INVALID_RESOURCE;
56 mraa_gpio_init(int pin)
59 syslog(LOG_ERR, "gpio: platform not initialised");
62 if (pin < 0 || pin > plat->phy_pin_count) {
63 syslog(LOG_ERR, "gpio: pin %i beyond platform definition", pin);
66 if (plat->pins[pin].capabilites.gpio != 1) {
67 syslog(LOG_ERR, "gpio: pin %i not capable of gpio", pin);
70 if (plat->pins[pin].gpio.mux_total > 0) {
71 if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS) {
72 syslog(LOG_ERR, "gpio: unable to setup muxes");
77 mraa_gpio_context r = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
79 syslog(LOG_CRIT, "gpio: mraa_gpio_init_raw(%d) returned error", pin);
84 if (advance_func->gpio_init_post != NULL) {
85 mraa_result_t ret = advance_func->gpio_init_post(r);
86 if (ret != MRAA_SUCCESS) {
95 mraa_gpio_init_raw(int pin)
97 if (advance_func->gpio_init_pre != NULL) {
98 if (advance_func->gpio_init_pre(pin) != MRAA_SUCCESS)
108 mraa_gpio_context dev = (mraa_gpio_context) malloc(sizeof(struct _gpio));
110 syslog(LOG_CRIT, "gpio: Failed to allocate memory for context");
114 memset(dev, 0, sizeof(struct _gpio));
116 dev->isr_value_fp = -1;
120 char directory[MAX_SIZE];
121 snprintf(directory, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/", dev->pin);
123 if (stat(directory, &dir) == 0 && S_ISDIR(dir.st_mode)) {
124 dev->owner = 0; // Not Owner
126 int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
128 syslog(LOG_ERR, "gpio: Failed to open export for writing");
132 length = snprintf(bu, sizeof(bu), "%d", dev->pin);
133 if (write(export, bu, length*sizeof(char)) == -1) {
134 syslog(LOG_ERR, "gpio: Failed to write to export");
148 mraa_gpio_wait_interrupt(int fd)
153 // setup poll on POLLPRI
155 pfd.events = POLLPRI;
157 // do an initial read to clear interupt
161 return MRAA_ERROR_INVALID_RESOURCE;
164 // Wait for it forever or until pthread_cancel
165 // poll is a cancelable point like sleep()
166 int x = poll (&pfd, 1, -1);
168 // do a final read to clear interupt
175 mraa_gpio_interrupt_handler(void* arg)
177 mraa_gpio_context dev = (mraa_gpio_context) arg;
180 // open gpio value with open(3)
182 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
183 int fp = open(bu, O_RDONLY);
185 syslog(LOG_ERR, "gpio: failed to open gpio%d/value", dev->pin);
188 dev->isr_value_fp = fp;
191 ret = mraa_gpio_wait_interrupt(dev->isr_value_fp);
192 if (ret == MRAA_SUCCESS) {
193 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
195 // In order to call a python object (all python functions are objects) we
196 // need to aquire the GIL (Global Interpreter Lock). This may not always be
197 // nessecary but especially if doing IO (like print()) python will segfault
198 // if we do not hold a lock on the GIL
199 PyGILState_STATE gilstate = PyGILState_Ensure();
202 arglist = Py_BuildValue("(i)", dev->isr_args);
203 if (arglist == NULL) {
204 syslog(LOG_ERR, "gpio: Py_BuildValue NULL");
206 ret = PyEval_CallObject((PyObject*)dev->isr, arglist);
208 syslog(LOG_ERR, "gpio: PyEval_CallObject failed");
215 PyGILState_Release (gilstate);
217 dev->isr(dev->isr_args);
219 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
221 // we must have got an error code so die nicely
222 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
223 close(dev->isr_value_fp);
224 dev->isr_value_fp = -1;
231 mraa_gpio_edge_mode(mraa_gpio_context dev, gpio_edge_t mode)
233 if (dev->value_fp != -1) {
234 close(dev->value_fp);
238 char filepath[MAX_SIZE];
239 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin);
241 int edge = open(filepath, O_RDWR);
243 syslog(LOG_ERR, "gpio: Failed to open edge for writing");
244 return MRAA_ERROR_INVALID_RESOURCE;
250 case MRAA_GPIO_EDGE_NONE:
251 length = snprintf(bu, sizeof(bu), "none");
253 case MRAA_GPIO_EDGE_BOTH:
254 length = snprintf(bu, sizeof(bu), "both");
256 case MRAA_GPIO_EDGE_RISING:
257 length = snprintf(bu, sizeof(bu), "rising");
259 case MRAA_GPIO_EDGE_FALLING:
260 length = snprintf(bu, sizeof(bu), "falling");
264 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
266 if (write(edge, bu, length*sizeof(char)) == -1) {
267 syslog(LOG_ERR, "gpio: Failed to write to edge");
269 return MRAA_ERROR_INVALID_RESOURCE;
277 mraa_gpio_isr(mraa_gpio_context dev, gpio_edge_t mode, void (*fptr)(void *), void * args)
279 // we only allow one isr per mraa_gpio_context
280 if (dev->thread_id != 0) {
281 return MRAA_ERROR_NO_RESOURCES;
284 if (MRAA_SUCCESS != mraa_gpio_edge_mode(dev, mode)) {
285 return MRAA_ERROR_UNSPECIFIED;
289 dev->isr_args = args;
290 pthread_create (&dev->thread_id, NULL, mraa_gpio_interrupt_handler, (void *) dev);
296 mraa_gpio_isr_exit(mraa_gpio_context dev)
298 mraa_result_t ret = MRAA_SUCCESS;
300 // wasting our time, there is no isr to exit from
301 if (dev->thread_id == 0 && dev->isr_value_fp == -1) {
305 // stop isr being useful
306 ret = mraa_gpio_edge_mode(dev, MRAA_GPIO_EDGE_NONE);
308 if ((dev->thread_id != 0)) {
309 if ((pthread_cancel(dev->thread_id) != 0) || (pthread_join(dev->thread_id, NULL) != 0)) {
310 ret = MRAA_ERROR_INVALID_HANDLE;
314 // close the filehandle in case it's still open
315 if (dev->isr_value_fp != -1) {
316 if (close(dev->isr_value_fp) != 0) {
317 ret = MRAA_ERROR_INVALID_PARAMETER;
322 // Dereference our Python call back function
326 // assume our thread will exit either way we just lost it's handle
328 dev->isr_value_fp = -1;
333 mraa_gpio_mode(mraa_gpio_context dev, gpio_mode_t mode)
335 if (advance_func->gpio_mode_replace != NULL)
336 return advance_func->gpio_mode_replace(dev,mode);
338 if (advance_func->gpio_mode_pre != NULL) {
339 mraa_result_t pre_ret = (advance_func->gpio_mode_pre(dev,mode));
340 if(pre_ret != MRAA_SUCCESS)
344 if (dev->value_fp != -1) {
345 close(dev->value_fp);
349 char filepath[MAX_SIZE];
350 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin);
352 int drive = open(filepath, O_WRONLY);
354 syslog(LOG_ERR, "gpio: Failed to open drive for writing");
355 return MRAA_ERROR_INVALID_RESOURCE;
361 case MRAA_GPIO_STRONG:
362 length = snprintf(bu, sizeof(bu), "strong");
364 case MRAA_GPIO_PULLUP:
365 length = snprintf(bu, sizeof(bu), "pullup");
367 case MRAA_GPIO_PULLDOWN:
368 length = snprintf(bu, sizeof(bu), "pulldown");
371 length = snprintf(bu, sizeof(bu), "hiz");
375 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
377 if (write(drive, bu, length*sizeof(char)) == -1) {
378 syslog(LOG_ERR, "gpio: Failed to write to drive mode");
380 return MRAA_ERROR_INVALID_RESOURCE;
385 if (advance_func->gpio_mode_post != NULL)
386 return advance_func->gpio_mode_post(dev,mode);
391 mraa_gpio_dir(mraa_gpio_context dev, gpio_dir_t dir)
393 if (advance_func->gpio_dir_replace != NULL) {
394 return advance_func->gpio_dir_replace(dev,dir);
396 if (advance_func->gpio_dir_pre != NULL) {
397 mraa_result_t pre_ret = (advance_func->gpio_dir_pre(dev,dir));
398 if (pre_ret != MRAA_SUCCESS) {
404 return MRAA_ERROR_INVALID_HANDLE;
406 if (dev->value_fp != -1) {
407 close(dev->value_fp);
410 char filepath[MAX_SIZE];
411 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
413 int direction = open(filepath, O_RDWR);
415 if (direction == -1) {
416 return MRAA_ERROR_INVALID_RESOURCE;
423 length = snprintf(bu, sizeof(bu), "out");
426 length = snprintf(bu, sizeof(bu), "in");
430 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
433 if (write(direction, bu, length*sizeof(char)) == -1) {
435 return MRAA_ERROR_INVALID_RESOURCE;
439 if (advance_func->gpio_dir_post != NULL)
440 return advance_func->gpio_dir_post(dev,dir);
445 mraa_gpio_read(mraa_gpio_context dev)
450 if (dev->mmap_read != NULL)
451 return dev->mmap_read(dev);
453 if (dev->value_fp == -1) {
454 if (mraa_gpio_get_valfp(dev) != MRAA_SUCCESS) {
455 syslog(LOG_ERR, "gpio: Failed to get value file pointer");
460 // if value_fp is new this is pointless
461 lseek(dev->value_fp, 0, SEEK_SET);
464 if (read(dev->value_fp, bu, 2*sizeof(char)) != 2) {
465 syslog(LOG_ERR, "gpio: Failed to read a sensible value from sysfs");
468 lseek(dev->value_fp, 0, SEEK_SET);
470 return (int) strtol(bu, NULL, 10);
474 mraa_gpio_write(mraa_gpio_context dev, int value)
477 return MRAA_ERROR_INVALID_HANDLE;
479 if (dev->mmap_write != NULL)
480 return dev->mmap_write(dev,value);
482 if (advance_func->gpio_write_pre != NULL) {
483 mraa_result_t pre_ret = (advance_func->gpio_write_pre(dev,value));
484 if(pre_ret != MRAA_SUCCESS)
488 if (dev->value_fp == -1) {
489 if (mraa_gpio_get_valfp(dev) != MRAA_SUCCESS) {
490 return MRAA_ERROR_INVALID_RESOURCE;
494 if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
495 return MRAA_ERROR_INVALID_RESOURCE;
499 int length = snprintf(bu, sizeof(bu), "%d", value);
500 if (write(dev->value_fp, bu, length*sizeof(char)) == -1) {
501 return MRAA_ERROR_INVALID_HANDLE;
504 if (advance_func->gpio_write_post != NULL)
505 return advance_func->gpio_write_post(dev,value);
510 mraa_gpio_unexport_force(mraa_gpio_context dev)
512 int unexport = open(SYSFS_CLASS_GPIO "/unexport", O_WRONLY);
513 if (unexport == -1) {
514 syslog(LOG_ERR, "gpio: Failed to open unexport for writing");
515 return MRAA_ERROR_INVALID_RESOURCE;
519 int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
520 if (write(unexport, bu, length*sizeof(char)) == -1) {
521 syslog(LOG_ERR, "gpio: Failed to write to unexport");
523 return MRAA_ERROR_INVALID_RESOURCE;
527 mraa_gpio_isr_exit(dev);
531 mraa_gpio_unexport(mraa_gpio_context dev)
534 return mraa_gpio_unexport_force(dev);
536 return MRAA_ERROR_INVALID_RESOURCE;
540 mraa_gpio_close(mraa_gpio_context dev)
542 if (dev->value_fp != -1) {
543 close(dev->value_fp);
545 mraa_gpio_unexport(dev);
551 mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t own)
554 return MRAA_ERROR_INVALID_RESOURCE;
560 mraa_gpio_use_mmaped(mraa_gpio_context dev, mraa_boolean_t mmap_en)
562 if (advance_func->gpio_mmap_setup != NULL) {
563 return advance_func->gpio_mmap_setup(dev,mmap_en);
566 syslog(LOG_ERR, "gpio: mmap not implemented on this platform");
567 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;