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 "maa_internal.h"
38 #define SYSFS_CLASS_GPIO "/sys/class/gpio"
43 * A structure representing a gpio pin.
48 int pin; /**< the pin number, as known to the os. */
49 int phy_pin; /**< pin passed to clean init. -1 none and raw*/
50 int value_fp; /**< the file pointer to the value of the gpio */
51 void (* isr)(void *); /**< the interupt service request */
52 void *isr_args; /**< args return when interupt service request triggered */
53 pthread_t thread_id; /**< the isr handler thread id */
54 int isr_value_fp; /**< the isr file pointer on the value */
55 maa_boolean_t owner; /**< If this context originally exported the pin */
59 unsigned int reg_bit_pos;
64 maa_gpio_get_valfp(maa_gpio_context dev)
67 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
68 dev->value_fp = open(bu, O_RDWR);
69 if (dev->value_fp == -1) {
70 return MAA_ERROR_INVALID_RESOURCE;
77 maa_gpio_init(int pin)
79 int pinm = maa_setup_gpio(pin);
83 maa_gpio_context r = maa_gpio_init_raw(pinm);
89 maa_gpio_init_raw(int pin)
97 maa_gpio_context dev = (maa_gpio_context) malloc(sizeof(struct _gpio));
98 memset(dev, 0, sizeof(struct _gpio));
100 dev->isr_value_fp = -1;
104 char directory[MAX_SIZE];
105 snprintf(directory, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/", dev->pin);
107 if (stat(directory, &dir) == 0 && S_ISDIR(dir.st_mode)) {
108 //fprintf(stderr, "GPIO Pin already exporting, continuing.\n");
109 dev->owner = 0; // Not Owner
111 int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
113 fprintf(stderr, "Failed to open export for writing!\n");
116 length = snprintf(bu, sizeof(bu), "%d", dev->pin);
117 if (write(export, bu, length*sizeof(char)) == -1) {
118 fprintf(stderr, "Failed to write to export\n");
129 maa_gpio_write_register(maa_gpio_context dev,int value)
132 *((unsigned *)dev->reg) |= (1<<dev->reg_bit_pos);
135 *((unsigned *)dev->reg) &= ~(1<<dev->reg_bit_pos);
140 maa_gpio_wait_interrupt(int fd)
145 // setup poll on POLLPRI
147 pfd.events = POLLPRI;
149 // do an initial read to clear interupt
153 return MAA_ERROR_INVALID_RESOURCE;
156 // Wait for it forever or until pthread_cancel
157 // poll is a cancelable point like sleep()
158 int x = poll (&pfd, 1, -1);
160 // do a final read to clear interupt
167 maa_gpio_interrupt_handler(void* arg)
169 maa_gpio_context dev = (maa_gpio_context) arg;
172 // open gpio value with open(3)
174 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
175 dev->isr_value_fp = open(bu, O_RDONLY);
178 ret = maa_gpio_wait_interrupt(dev->isr_value_fp);
179 if (ret == MAA_SUCCESS) {
180 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
182 // In order to call a python object (all python functions are objects) we
183 // need to aquire the GIL (Global Interpreter Lock). This may not always be
184 // nessecary but especially if doing IO (like print()) python will segfault
185 // if we do not hold a lock on the GIL
186 PyGILState_STATE gilstate = PyGILState_Ensure();
189 arglist = Py_BuildValue("(i)", dev->isr_args);
190 if (arglist == NULL) {
191 fprintf(stdout, "Py_BuildValue NULL\n");
193 ret = PyEval_CallObject((PyObject*)dev->isr, arglist);
195 fprintf(stdout, "PyEval_CallObject failed\n");
202 PyGILState_Release (gilstate);
204 dev->isr(dev->isr_args);
206 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
208 // we must have got an error code so die nicely
209 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
210 close(dev->isr_value_fp);
211 dev->isr_value_fp = -1;
218 maa_gpio_edge_mode(maa_gpio_context dev, gpio_edge_t mode)
220 if (dev->value_fp != -1) {
221 close(dev->value_fp);
225 char filepath[MAX_SIZE];
226 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin);
228 int edge = open(filepath, O_RDWR);
230 fprintf(stderr, "Failed to open edge for writing!\n");
231 return MAA_ERROR_INVALID_RESOURCE;
237 case MAA_GPIO_EDGE_NONE:
238 length = snprintf(bu, sizeof(bu), "none");
240 case MAA_GPIO_EDGE_BOTH:
241 length = snprintf(bu, sizeof(bu), "both");
243 case MAA_GPIO_EDGE_RISING:
244 length = snprintf(bu, sizeof(bu), "rising");
246 case MAA_GPIO_EDGE_FALLING:
247 length = snprintf(bu, sizeof(bu), "falling");
251 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
253 if (write(edge, bu, length*sizeof(char)) == -1) {
254 fprintf(stderr, "Failed to write to edge\n");
256 return MAA_ERROR_INVALID_RESOURCE;
264 maa_gpio_isr(maa_gpio_context dev, gpio_edge_t mode, void (*fptr)(void *), void * args)
266 // we only allow one isr per maa_gpio_context
267 if (dev->thread_id != 0) {
268 return MAA_ERROR_NO_RESOURCES;
271 if (MAA_SUCCESS != maa_gpio_edge_mode(dev, mode)) {
272 return MAA_ERROR_UNSPECIFIED;
276 dev->isr_args = args;
277 pthread_create (&dev->thread_id, NULL, maa_gpio_interrupt_handler, (void *) dev);
283 maa_gpio_isr_exit(maa_gpio_context dev)
285 maa_result_t ret = MAA_SUCCESS;
287 // wasting our time, there is no isr to exit from
288 if (dev->thread_id == 0 && dev->isr_value_fp == -1) {
292 // stop isr being useful
293 ret = maa_gpio_edge_mode(dev, MAA_GPIO_EDGE_NONE);
295 if ((dev->thread_id != 0) &&
296 (pthread_cancel(dev->thread_id) != 0)) {
297 ret = MAA_ERROR_INVALID_HANDLE;
300 // close the filehandle in case it's still open
301 if (dev->isr_value_fp != -1) {
302 if (close(dev->isr_value_fp) != 0) {
303 ret = MAA_ERROR_INVALID_PARAMETER;
308 // Dereference our Python call back function
312 // assume our thread will exit either way we just lost it's handle
314 dev->isr_value_fp = -1;
320 maa_gpio_mode(maa_gpio_context dev, gpio_mode_t mode)
322 if (dev->value_fp != -1) {
323 close(dev->value_fp);
327 char filepath[MAX_SIZE];
328 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin);
330 int drive = open(filepath, O_WRONLY);
332 fprintf(stderr, "Failed to open drive for writing!\n");
333 return MAA_ERROR_INVALID_RESOURCE;
339 case MAA_GPIO_STRONG:
340 length = snprintf(bu, sizeof(bu), "strong");
342 case MAA_GPIO_PULLUP:
343 length = snprintf(bu, sizeof(bu), "pullup");
345 case MAA_GPIO_PULLDOWN:
346 length = snprintf(bu, sizeof(bu), "pulldown");
349 length = snprintf(bu, sizeof(bu), "hiz");
353 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
355 if (write(drive, bu, length*sizeof(char)) == -1) {
356 fprintf(stderr, "Failed to write to drive mode!\n");
358 return MAA_ERROR_INVALID_RESOURCE;
367 maa_gpio_dir(maa_gpio_context dev, gpio_dir_t dir)
370 return MAA_ERROR_INVALID_HANDLE;
372 if (dev->value_fp != -1) {
373 close(dev->value_fp);
376 char filepath[MAX_SIZE];
377 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
379 int direction = open(filepath, O_RDWR);
381 if (direction == -1) {
382 fprintf(stderr, "Failed to open direction for writing!\n");
383 return MAA_ERROR_INVALID_RESOURCE;
391 length = snprintf(bu, sizeof(bu), "out");
395 length = snprintf(bu, sizeof(bu), "in");
399 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
402 if (dev->phy_pin >= 0) {
403 maa_result_t swap_res = maa_swap_complex_gpio(dev->phy_pin, out_switch);
404 if (swap_res != MAA_SUCCESS)
408 if (write(direction, bu, length*sizeof(char)) == -1) {
410 return MAA_ERROR_INVALID_RESOURCE;
418 maa_gpio_read(maa_gpio_context dev)
420 if (dev->value_fp == -1) {
421 if (maa_gpio_get_valfp(dev) != MAA_SUCCESS) {
422 fprintf(stderr, "Failed to get value file pointer\n");
426 // if value_fp is new this is pointless
427 lseek(dev->value_fp, 0, SEEK_SET);
430 if (read(dev->value_fp, bu, 2*sizeof(char)) != 2) {
431 fprintf(stderr, "Failed to read a sensible value from sysfs");
433 lseek(dev->value_fp, 0, SEEK_SET);
434 int ret = strtol(bu, NULL, 10);
440 maa_gpio_write(maa_gpio_context dev, int value)
443 return maa_gpio_write_register(dev,value);
445 if (dev->value_fp == -1) {
446 maa_gpio_get_valfp(dev);
448 if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
449 return MAA_ERROR_INVALID_RESOURCE;
453 int length = snprintf(bu, sizeof(bu), "%d", value);
454 if (write(dev->value_fp, bu, length*sizeof(char)) == -1) {
455 return MAA_ERROR_INVALID_HANDLE;
462 maa_gpio_unexport_force(maa_gpio_context dev)
464 int unexport = open(SYSFS_CLASS_GPIO "/unexport", O_WRONLY);
465 if (unexport == -1) {
466 fprintf(stderr, "Failed to open unexport for writing!\n");
467 return MAA_ERROR_INVALID_RESOURCE;
471 int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
472 if (write(unexport, bu, length*sizeof(char)) == -1) {
473 fprintf(stderr, "Failed to write to unexport\n");
475 return MAA_ERROR_INVALID_RESOURCE;
479 maa_gpio_isr_exit(dev);
483 maa_gpio_unexport(maa_gpio_context dev)
486 return maa_gpio_unexport_force(dev);
488 return MAA_ERROR_INVALID_RESOURCE;
492 maa_gpio_close(maa_gpio_context dev)
494 if (dev->value_fp != -1) {
495 close(dev->value_fp);
497 maa_gpio_unexport(dev);
503 maa_gpio_owner(maa_gpio_context dev, maa_boolean_t own)
506 return MAA_ERROR_INVALID_RESOURCE;
512 maa_gpio_use_mmaped(maa_gpio_context dev, maa_boolean_t mmap_en)
515 return MAA_ERROR_INVALID_RESOURCE;
518 if (maa_pin_mode_test(dev->phy_pin, MAA_PIN_FAST_GPIO) == 0)
519 return MAA_ERROR_NO_RESOURCES;
521 maa_mmap_pin_t *mmp = maa_setup_mmap_gpio(dev->phy_pin);
523 return MAA_ERROR_INVALID_RESOURCE;
526 if (dev->mmap == 0) {
527 close(dev->value_fp);
529 fd = open(mmp->mem_dev, O_RDWR);
531 fprintf(stderr, "Unable to open memory device\n");
532 return MAA_ERROR_INVALID_RESOURCE;
534 dev->reg_sz = mmp->mem_sz;
535 dev->reg = mmap(NULL, dev->reg_sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
536 dev->reg_bit_pos = mmp->bit_pos;
540 return MAA_ERROR_INVALID_PARAMETER;
544 if (dev ->mmap == 1) {
545 munmap(dev->reg, dev->reg_sz);
548 return MAA_ERROR_INVALID_PARAMETER;