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.
33 #define SYSFS_CLASS_GPIO "/sys/class/gpio"
38 maa_gpio_get_valfp(maa_gpio_context *dev)
41 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
43 dev->value_fp = open(bu, O_RDWR);
44 if (dev->value_fp == -1) {
45 return MAA_ERROR_INVALID_RESOURCE;
52 maa_gpio_init(int pin)
54 int pinm = maa_check_gpio(pin);
58 return maa_gpio_init_raw(pinm);
62 maa_gpio_init_raw(int pin)
71 maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
72 memset(dev, 0, sizeof(maa_gpio_context));
76 if ((export_f = fopen(SYSFS_CLASS_GPIO "/export", "w")) == NULL) {
77 fprintf(stderr, "Failed to open export for writing!\n");
80 length = snprintf(bu, sizeof(bu), "%d", dev->pin);
81 fwrite(bu, sizeof(char), length, export_f);
88 maa_gpio_wait_interrupt(int fd)
93 // setup poll on POLLPRI
97 // do an initial read to clear interupt
101 return MAA_ERROR_INVALID_RESOURCE;
104 // Wait for it forever
105 int x = poll (&pfd, 1, -1);
107 // do a final read to clear interupt
114 maa_gpio_interrupt_handler(void* arg)
116 maa_gpio_context* dev = (maa_gpio_context*) arg;
119 // open gpio value with open(3)
121 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
122 dev->isr_value_fp = open(bu, O_RDONLY);
125 ret = maa_gpio_wait_interrupt(dev->isr_value_fp);
126 if (ret == MAA_SUCCESS) {
128 // In order to call a python object (all python functions are objects) we
129 // need to aquire the GIL (Global Interpreter Lock). This may not always be
130 // nessecary but especially if doing IO (like print()) python will segfault
131 // if we do not hold a lock on the GIL
132 PyGILState_STATE gilstate = PyGILState_Ensure();
134 PyEval_CallObject(dev->isr, NULL);
136 PyGILState_Release (gilstate);
141 // we must have got an error code so die nicely
142 close(dev->isr_value_fp);
149 maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode)
151 if (dev->value_fp != -1) {
152 close(dev->value_fp);
156 char filepath[MAX_SIZE];
157 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin);
160 if ((edge= fopen(filepath, "w")) == NULL) {
161 fprintf(stderr, "Failed to open edge for writing!\n");
162 return MAA_ERROR_INVALID_RESOURCE;
168 case MAA_GPIO_EDGE_NONE:
169 length = snprintf(bu, sizeof(bu), "none");
171 case MAA_GPIO_EDGE_BOTH:
172 length = snprintf(bu, sizeof(bu), "both");
174 case MAA_GPIO_EDGE_RISING:
175 length = snprintf(bu, sizeof(bu), "rising");
177 case MAA_GPIO_EDGE_FALLING:
178 length = snprintf(bu, sizeof(bu), "falling");
182 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
184 fwrite(bu, sizeof(char), length, edge);
191 maa_gpio_isr(maa_gpio_context *dev, gpio_edge_t mode, void (*fptr)(void))
193 maa_gpio_edge_mode(dev, mode);
195 pthread_create (&dev->thread_id, NULL, maa_gpio_interrupt_handler, (void *) dev);
201 maa_gpio_isr_exit(maa_gpio_context *dev)
203 maa_result_t ret = MAA_SUCCESS;
206 // Dereference our Python call back function
210 if (dev->thread_id == 0) {
214 if (pthread_kill(dev->thread_id) != 0) {
215 ret = MAA_ERROR_INVALID_HANDLE;
217 if (close(dev->isr_value_fp) != 0) {
218 ret = MAA_ERROR_INVALID_PARAMETER;
221 // this is only required if we had an isr setup
222 if (ret == MAA_SUCCESS) {
223 ret = maa_gpio_edge_mode(dev, MAA_GPIO_EDGE_NONE);
230 maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
232 if (dev->value_fp != -1) {
233 close(dev->value_fp);
237 char filepath[MAX_SIZE];
238 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin);
241 if ((drive = fopen(filepath, "w")) == NULL) {
242 fprintf(stderr, "Failed to open drive for writing!\n");
243 return MAA_ERROR_INVALID_RESOURCE;
249 case MAA_GPIO_STRONG:
250 length = snprintf(bu, sizeof(bu), "strong");
252 case MAA_GPIO_PULLUP:
253 length = snprintf(bu, sizeof(bu), "pullup");
255 case MAA_GPIO_PULLDOWN:
256 length = snprintf(bu, sizeof(bu), "pulldown");
259 length = snprintf(bu, sizeof(bu), "hiz");
263 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
265 fwrite(bu, sizeof(char), length, drive);
272 maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
275 return MAA_ERROR_INVALID_HANDLE;
277 if (dev->value_fp != -1) {
278 close(dev->value_fp);
281 char filepath[MAX_SIZE];
282 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
285 if ((direction = fopen(filepath, "w")) == NULL) {
286 fprintf(stderr, "Failed to open direction for writing!\n");
287 return MAA_ERROR_INVALID_RESOURCE;
294 length = snprintf(bu, sizeof(bu), "out");
297 length = snprintf(bu, sizeof(bu), "in");
301 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
303 fwrite(bu, sizeof(char), length, direction);
310 maa_gpio_read(maa_gpio_context *dev)
312 if (dev->value_fp == -1) {
313 if (maa_gpio_get_valfp(dev) != MAA_SUCCESS) {
314 fprintf(stderr, "Failed to get value file pointer\n");
318 // if value_fp is new this is pointless
319 lseek(dev->value_fp, 0, SEEK_SET);
322 if (read(dev->value_fp, bu, 2*sizeof(char)) != 2) {
323 fprintf(stderr, "Failed to read a sensible value from sysfs");
325 lseek(dev->value_fp, 0, SEEK_SET);
326 int ret = strtol(bu, NULL, 10);
332 maa_gpio_write(maa_gpio_context *dev, int value)
334 if (dev->value_fp == -1) {
335 maa_gpio_get_valfp(dev);
337 if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
338 return MAA_ERROR_INVALID_RESOURCE;
342 int length = snprintf(bu, sizeof(bu), "%d", value);
343 if (write(dev->value_fp, bu, length*sizeof(char)) == -1) {
344 return MAA_ERROR_INVALID_HANDLE;
351 maa_gpio_unexport(maa_gpio_context *dev)
355 if ((unexport_f = fopen(SYSFS_CLASS_GPIO "/unexport", "w")) == NULL) {
356 fprintf(stderr, "Failed to open unexport for writing!\n");
357 return MAA_ERROR_INVALID_RESOURCE;
361 int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
362 fwrite(bu, sizeof(char), length, unexport_f);
365 maa_gpio_isr_exit(dev);
371 maa_gpio_close(maa_gpio_context *dev)
373 if (dev->value_fp != -1) {
374 close(dev->value_fp);
376 maa_gpio_unexport(dev);