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)(); /**< the interupt service request */
52 pthread_t thread_id; /**< the isr handler thread id */
53 int isr_value_fp; /**< the isr file pointer on the value */
54 maa_boolean_t owner; /**< If this context originally exported the pin */
58 unsigned int reg_bit_pos;
63 maa_gpio_get_valfp(maa_gpio_context dev)
66 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
67 dev->value_fp = open(bu, O_RDWR);
68 if (dev->value_fp == -1) {
69 return MAA_ERROR_INVALID_RESOURCE;
76 maa_gpio_init(int pin)
78 int pinm = maa_setup_gpio(pin);
82 maa_gpio_context r = maa_gpio_init_raw(pinm);
88 maa_gpio_init_raw(int pin)
96 maa_gpio_context dev = (maa_gpio_context) malloc(sizeof(struct _gpio));
97 memset(dev, 0, sizeof(struct _gpio));
99 dev->isr_value_fp = -1;
103 char directory[MAX_SIZE];
104 snprintf(directory, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/", dev->pin);
106 if (stat(directory, &dir) == 0 && S_ISDIR(dir.st_mode)) {
107 //fprintf(stderr, "GPIO Pin already exporting, continuing.\n");
108 dev->owner = 0; // Not Owner
110 int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
112 fprintf(stderr, "Failed to open export for writing!\n");
115 length = snprintf(bu, sizeof(bu), "%d", dev->pin);
116 if (write(export, bu, length*sizeof(char)) == -1) {
117 fprintf(stderr, "Failed to write to export\n");
128 maa_gpio_write_register(maa_gpio_context dev,int value)
131 *((unsigned *)dev->reg) |= (1<<dev->reg_bit_pos);
134 *((unsigned *)dev->reg) &= ~(1<<dev->reg_bit_pos);
139 maa_gpio_wait_interrupt(int fd)
144 // setup poll on POLLPRI
146 pfd.events = POLLPRI;
148 // do an initial read to clear interupt
152 return MAA_ERROR_INVALID_RESOURCE;
155 // Wait for it forever or until pthread_cancel
156 // poll is a cancelable point like sleep()
157 int x = poll (&pfd, 1, -1);
159 // do a final read to clear interupt
166 maa_gpio_interrupt_handler(void* arg)
168 maa_gpio_context dev = (maa_gpio_context) arg;
171 // open gpio value with open(3)
173 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
174 dev->isr_value_fp = open(bu, O_RDONLY);
177 ret = maa_gpio_wait_interrupt(dev->isr_value_fp);
178 if (ret == MAA_SUCCESS) {
179 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
181 // In order to call a python object (all python functions are objects) we
182 // need to aquire the GIL (Global Interpreter Lock). This may not always be
183 // nessecary but especially if doing IO (like print()) python will segfault
184 // if we do not hold a lock on the GIL
185 PyGILState_STATE gilstate = PyGILState_Ensure();
187 PyEval_CallObject((PyObject*)dev->isr, NULL);
189 PyGILState_Release (gilstate);
193 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
195 // we must have got an error code so die nicely
196 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
197 close(dev->isr_value_fp);
198 dev->isr_value_fp = -1;
205 maa_gpio_edge_mode(maa_gpio_context dev, gpio_edge_t mode)
207 if (dev->value_fp != -1) {
208 close(dev->value_fp);
212 char filepath[MAX_SIZE];
213 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin);
215 int edge = open(filepath, O_RDWR);
217 fprintf(stderr, "Failed to open edge for writing!\n");
218 return MAA_ERROR_INVALID_RESOURCE;
224 case MAA_GPIO_EDGE_NONE:
225 length = snprintf(bu, sizeof(bu), "none");
227 case MAA_GPIO_EDGE_BOTH:
228 length = snprintf(bu, sizeof(bu), "both");
230 case MAA_GPIO_EDGE_RISING:
231 length = snprintf(bu, sizeof(bu), "rising");
233 case MAA_GPIO_EDGE_FALLING:
234 length = snprintf(bu, sizeof(bu), "falling");
238 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
240 if (write(edge, bu, length*sizeof(char)) == -1) {
241 fprintf(stderr, "Failed to write to edge\n");
243 return MAA_ERROR_INVALID_RESOURCE;
251 maa_gpio_isr(maa_gpio_context dev, gpio_edge_t mode, void (*fptr)(void))
253 // we only allow one isr per maa_gpio_context
254 if (dev->thread_id != 0) {
255 return MAA_ERROR_NO_RESOURCES;
258 maa_gpio_edge_mode(dev, mode);
260 pthread_create (&dev->thread_id, NULL, maa_gpio_interrupt_handler, (void *) dev);
266 maa_gpio_isr_exit(maa_gpio_context dev)
268 maa_result_t ret = MAA_SUCCESS;
270 // wasting our time, there is no isr to exit from
271 if (dev->thread_id == 0 && dev->isr_value_fp == -1) {
275 // stop isr being useful
276 ret = maa_gpio_edge_mode(dev, MAA_GPIO_EDGE_NONE);
278 if ((dev->thread_id != 0) &&
279 (pthread_cancel(dev->thread_id) != 0)) {
280 ret = MAA_ERROR_INVALID_HANDLE;
283 // close the filehandle in case it's still open
284 if (dev->isr_value_fp != -1) {
285 if (close(dev->isr_value_fp) != 0) {
286 ret = MAA_ERROR_INVALID_PARAMETER;
291 // Dereference our Python call back function
295 // assume our thread will exit either way we just lost it's handle
297 dev->isr_value_fp = -1;
303 maa_gpio_mode(maa_gpio_context dev, gpio_mode_t mode)
305 if (dev->value_fp != -1) {
306 close(dev->value_fp);
310 char filepath[MAX_SIZE];
311 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin);
313 int drive = open(filepath, O_WRONLY);
315 fprintf(stderr, "Failed to open drive for writing!\n");
316 return MAA_ERROR_INVALID_RESOURCE;
322 case MAA_GPIO_STRONG:
323 length = snprintf(bu, sizeof(bu), "strong");
325 case MAA_GPIO_PULLUP:
326 length = snprintf(bu, sizeof(bu), "pullup");
328 case MAA_GPIO_PULLDOWN:
329 length = snprintf(bu, sizeof(bu), "pulldown");
332 length = snprintf(bu, sizeof(bu), "hiz");
336 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
338 if (write(drive, bu, length*sizeof(char)) == -1) {
339 fprintf(stderr, "Failed to write to drive mode!\n");
341 return MAA_ERROR_INVALID_RESOURCE;
350 maa_gpio_dir(maa_gpio_context dev, gpio_dir_t dir)
353 return MAA_ERROR_INVALID_HANDLE;
355 if (dev->value_fp != -1) {
356 close(dev->value_fp);
359 char filepath[MAX_SIZE];
360 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
362 int direction = open(filepath, O_RDWR);
364 if (direction == -1) {
365 fprintf(stderr, "Failed to open direction for writing!\n");
366 return MAA_ERROR_INVALID_RESOURCE;
374 length = snprintf(bu, sizeof(bu), "out");
378 length = snprintf(bu, sizeof(bu), "in");
382 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
385 if (dev->phy_pin >= 0) {
386 maa_result_t swap_res = maa_swap_complex_gpio(dev->phy_pin, out_switch);
387 if (swap_res != MAA_SUCCESS)
391 if (write(direction, bu, length*sizeof(char)) == -1) {
392 fprintf(stderr, "Failed to write to direction\n");
394 return MAA_ERROR_INVALID_RESOURCE;
402 maa_gpio_read(maa_gpio_context dev)
404 if (dev->value_fp == -1) {
405 if (maa_gpio_get_valfp(dev) != MAA_SUCCESS) {
406 fprintf(stderr, "Failed to get value file pointer\n");
410 // if value_fp is new this is pointless
411 lseek(dev->value_fp, 0, SEEK_SET);
414 if (read(dev->value_fp, bu, 2*sizeof(char)) != 2) {
415 fprintf(stderr, "Failed to read a sensible value from sysfs");
417 lseek(dev->value_fp, 0, SEEK_SET);
418 int ret = strtol(bu, NULL, 10);
424 maa_gpio_write(maa_gpio_context dev, int value)
427 return maa_gpio_write_register(dev,value);
429 if (dev->value_fp == -1) {
430 maa_gpio_get_valfp(dev);
432 if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
433 return MAA_ERROR_INVALID_RESOURCE;
437 int length = snprintf(bu, sizeof(bu), "%d", value);
438 if (write(dev->value_fp, bu, length*sizeof(char)) == -1) {
439 return MAA_ERROR_INVALID_HANDLE;
446 maa_gpio_unexport_force(maa_gpio_context dev)
448 int unexport = open(SYSFS_CLASS_GPIO "/unexport", O_WRONLY);
449 if (unexport == -1) {
450 fprintf(stderr, "Failed to open unexport for writing!\n");
451 return MAA_ERROR_INVALID_RESOURCE;
455 int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
456 if (write(unexport, bu, length*sizeof(char)) == -1) {
457 fprintf(stderr, "Failed to write to unexport\n");
459 return MAA_ERROR_INVALID_RESOURCE;
463 maa_gpio_isr_exit(dev);
467 maa_gpio_unexport(maa_gpio_context dev)
470 return maa_gpio_unexport_force(dev);
472 return MAA_ERROR_INVALID_RESOURCE;
476 maa_gpio_close(maa_gpio_context dev)
478 if (dev->value_fp != -1) {
479 close(dev->value_fp);
481 maa_gpio_unexport(dev);
487 maa_gpio_owner(maa_gpio_context dev, maa_boolean_t own)
490 return MAA_ERROR_INVALID_RESOURCE;
496 maa_gpio_use_mmaped(maa_gpio_context dev, maa_boolean_t mmap_en)
499 return MAA_ERROR_INVALID_RESOURCE;
502 if (maa_pin_mode_test(dev->phy_pin, MAA_PIN_FAST_GPIO) == 0)
503 return MAA_ERROR_NO_RESOURCES;
505 maa_mmap_pin_t *mmp = maa_setup_mmap_gpio(dev->phy_pin);
507 return MAA_ERROR_INVALID_RESOURCE;
510 if (dev->mmap == 0) {
511 close(dev->value_fp);
513 fd = open(mmp->mem_dev, O_RDWR);
515 fprintf(stderr, "Unable to open memory device\n");
516 return MAA_ERROR_INVALID_RESOURCE;
518 dev->reg_sz = mmp->mem_sz;
519 dev->reg = mmap(NULL, dev->reg_sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
520 dev->reg_bit_pos = mmp->bit_pos;
524 return MAA_ERROR_INVALID_PARAMETER;
528 if (dev ->mmap == 1) {
529 munmap(dev->reg, dev->reg_sz);
532 return MAA_ERROR_INVALID_PARAMETER;