2014-04-10 16:05:11 +01:00
|
|
|
/*
|
|
|
|
|
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
|
2014-05-06 09:32:07 +01:00
|
|
|
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
2014-04-22 15:51:28 +01:00
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
2014-04-10 16:05:11 +01:00
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
2014-05-07 14:48:21 +01:00
|
|
|
#include "gpio.h"
|
2014-04-10 16:05:11 +01:00
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-04-11 11:41:48 +01:00
|
|
|
#include <fcntl.h>
|
2014-05-07 14:48:21 +01:00
|
|
|
#include <string.h>
|
2014-04-11 11:41:48 +01:00
|
|
|
#include <unistd.h>
|
2014-05-07 14:48:21 +01:00
|
|
|
#include <poll.h>
|
2014-04-10 16:05:11 +01:00
|
|
|
|
2014-05-06 09:32:07 +01:00
|
|
|
#define SYSFS_CLASS_GPIO "/sys/class/gpio"
|
|
|
|
|
#define MAX_SIZE 64
|
2014-05-07 14:48:21 +01:00
|
|
|
#define POLL_TIMEOUT
|
2014-05-06 09:32:07 +01:00
|
|
|
|
2014-05-07 14:48:21 +01:00
|
|
|
static maa_result_t
|
2014-04-28 11:31:53 +01:00
|
|
|
maa_gpio_get_valfp(maa_gpio_context *dev)
|
2014-04-23 09:30:27 +01:00
|
|
|
{
|
2014-05-06 09:32:07 +01:00
|
|
|
char bu[MAX_SIZE];
|
|
|
|
|
sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
|
2014-04-23 09:28:04 +01:00
|
|
|
|
2014-04-28 11:31:53 +01:00
|
|
|
if ((dev->value_fp = fopen(bu, "r+b")) == NULL) {
|
2014-05-07 14:48:21 +01:00
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
2014-04-23 09:28:04 +01:00
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
|
2014-05-07 14:48:21 +01:00
|
|
|
return MAA_SUCCESS;
|
2014-04-23 09:28:04 +01:00
|
|
|
}
|
2014-04-11 13:33:46 +01:00
|
|
|
|
2014-04-28 11:31:53 +01:00
|
|
|
maa_gpio_context*
|
|
|
|
|
maa_gpio_init(int pin)
|
2014-04-29 16:46:10 +01:00
|
|
|
{
|
2014-05-01 16:55:23 +01:00
|
|
|
int pinm = maa_check_gpio(pin);
|
|
|
|
|
if (pinm < 0)
|
|
|
|
|
return NULL;
|
2014-05-06 09:32:07 +01:00
|
|
|
|
2014-05-01 16:55:23 +01:00
|
|
|
return maa_gpio_init_raw(pinm);
|
2014-04-29 16:46:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maa_gpio_context*
|
|
|
|
|
maa_gpio_init_raw(int pin)
|
2014-04-23 09:30:27 +01:00
|
|
|
{
|
2014-05-06 09:32:07 +01:00
|
|
|
if (pin < 0)
|
2014-05-01 16:55:23 +01:00
|
|
|
return NULL;
|
2014-05-06 09:32:07 +01:00
|
|
|
|
2014-04-11 11:41:48 +01:00
|
|
|
FILE *export_f;
|
2014-05-06 09:32:07 +01:00
|
|
|
char bu[MAX_SIZE];
|
|
|
|
|
int length;
|
|
|
|
|
|
2014-04-28 11:31:53 +01:00
|
|
|
maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
|
2014-05-07 14:48:21 +01:00
|
|
|
memset(dev, 0, sizeof(maa_gpio_context));
|
2014-05-06 09:32:07 +01:00
|
|
|
dev->pin = pin;
|
2014-04-11 11:41:48 +01:00
|
|
|
|
2014-05-06 09:32:07 +01:00
|
|
|
if ((export_f = fopen(SYSFS_CLASS_GPIO "/export", "w")) == NULL) {
|
2014-04-11 11:41:48 +01:00
|
|
|
fprintf(stderr, "Failed to open export for writing!\n");
|
2014-04-29 16:46:10 +01:00
|
|
|
return NULL;
|
2014-04-11 11:41:48 +01:00
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
length = snprintf(bu, sizeof(bu), "%d", dev->pin);
|
|
|
|
|
fwrite(bu, sizeof(char), length, export_f);
|
|
|
|
|
|
|
|
|
|
fclose(export_f);
|
2014-04-28 11:31:53 +01:00
|
|
|
return dev;
|
2014-04-10 16:05:11 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-07 14:48:21 +01:00
|
|
|
static maa_result_t
|
|
|
|
|
maa_gpio_wait_interrupt(int fd)
|
|
|
|
|
{
|
|
|
|
|
unsigned char c;
|
|
|
|
|
struct pollfd pfd;
|
|
|
|
|
|
|
|
|
|
// setup poll on POLLPRI
|
|
|
|
|
pfd.fd = fd;
|
|
|
|
|
pfd.events = POLLPRI;
|
|
|
|
|
|
|
|
|
|
// do an initial read to clear interupt
|
|
|
|
|
read (fd, &c, 1);
|
|
|
|
|
|
|
|
|
|
if (fd <= 0) {
|
|
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for it forever
|
|
|
|
|
int x = poll (&pfd, 1, -1);
|
|
|
|
|
|
|
|
|
|
// do a final read to clear interupt
|
|
|
|
|
read (fd, &c, 1);
|
|
|
|
|
|
|
|
|
|
return MAA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void*
|
|
|
|
|
maa_gpio_interrupt_handler(void* arg)
|
|
|
|
|
{
|
|
|
|
|
maa_gpio_context* dev = (maa_gpio_context*) arg;
|
|
|
|
|
maa_result_t ret;
|
|
|
|
|
|
|
|
|
|
// open gpio value with open(3)
|
|
|
|
|
char bu[MAX_SIZE];
|
|
|
|
|
sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
|
|
|
|
|
dev->isr_value_fp = open(bu, O_RDONLY);
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
ret = maa_gpio_wait_interrupt(dev->isr_value_fp);
|
|
|
|
|
if (ret == MAA_SUCCESS) {
|
|
|
|
|
dev->isr();
|
|
|
|
|
} else {
|
|
|
|
|
// we must have got an error code so die nicely
|
|
|
|
|
close(dev->isr_value_fp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maa_result_t
|
|
|
|
|
maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode)
|
|
|
|
|
{
|
|
|
|
|
if (dev->value_fp != NULL) {
|
|
|
|
|
dev->value_fp = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char filepath[MAX_SIZE];
|
|
|
|
|
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin);
|
|
|
|
|
|
|
|
|
|
FILE *edge;
|
|
|
|
|
if ((edge= fopen(filepath, "w")) == NULL) {
|
|
|
|
|
fprintf(stderr, "Failed to open edge for writing!\n");
|
|
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char bu[MAX_SIZE];
|
|
|
|
|
int length;
|
|
|
|
|
switch(mode) {
|
|
|
|
|
case MAA_GPIO_EDGE_NONE:
|
|
|
|
|
length = snprintf(bu, sizeof(bu), "none");
|
|
|
|
|
break;
|
|
|
|
|
case MAA_GPIO_EDGE_BOTH:
|
|
|
|
|
length = snprintf(bu, sizeof(bu), "both");
|
|
|
|
|
break;
|
|
|
|
|
case MAA_GPIO_EDGE_RISING:
|
|
|
|
|
length = snprintf(bu, sizeof(bu), "rising");
|
|
|
|
|
break;
|
|
|
|
|
case MAA_GPIO_EDGE_FALLING:
|
|
|
|
|
length = snprintf(bu, sizeof(bu), "falling");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fclose(edge);
|
|
|
|
|
return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
|
|
|
|
}
|
|
|
|
|
fwrite(bu, sizeof(char), length, edge);
|
|
|
|
|
|
|
|
|
|
fclose(edge);
|
|
|
|
|
dev->value_fp = NULL;
|
|
|
|
|
return MAA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maa_result_t
|
|
|
|
|
maa_gpio_isr(maa_gpio_context *dev, gpio_edge_t mode, void (*fptr)(void))
|
|
|
|
|
{
|
|
|
|
|
maa_gpio_edge_mode(dev, mode);
|
|
|
|
|
dev->isr = fptr;
|
|
|
|
|
pthread_create (&dev->thread_id, NULL, maa_gpio_interrupt_handler, (void *) dev);
|
|
|
|
|
|
|
|
|
|
return MAA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maa_result_t
|
|
|
|
|
maa_gpio_isr_exit(maa_gpio_context *dev)
|
|
|
|
|
{
|
|
|
|
|
maa_result_t ret = MAA_SUCCESS;
|
|
|
|
|
maa_gpio_edge_mode(dev, MAA_GPIO_EDGE_NONE);
|
|
|
|
|
|
|
|
|
|
if (pthread_kill(dev->thread_id) != 0) {
|
|
|
|
|
ret = MAA_ERROR_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
if (close(dev->isr_value_fp) != 0) {
|
|
|
|
|
ret = MAA_ERROR_INVALID_PARAMETER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 16:46:10 +01:00
|
|
|
maa_result_t
|
2014-04-28 11:31:53 +01:00
|
|
|
maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
|
2014-04-23 09:30:27 +01:00
|
|
|
{
|
2014-04-29 16:55:54 +01:00
|
|
|
if (dev->value_fp != NULL) {
|
|
|
|
|
dev->value_fp = NULL;
|
|
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
|
|
|
|
|
char filepath[MAX_SIZE];
|
|
|
|
|
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin);
|
2014-04-29 16:55:54 +01:00
|
|
|
|
|
|
|
|
FILE *drive;
|
|
|
|
|
if ((drive = fopen(filepath, "w")) == NULL) {
|
|
|
|
|
fprintf(stderr, "Failed to open drive for writing!\n");
|
|
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
|
|
|
|
|
char bu[MAX_SIZE];
|
|
|
|
|
int length;
|
2014-04-29 16:55:54 +01:00
|
|
|
switch(mode) {
|
|
|
|
|
case MAA_GPIO_STRONG:
|
2014-05-06 09:32:07 +01:00
|
|
|
length = snprintf(bu, sizeof(bu), "strong");
|
2014-04-29 16:55:54 +01:00
|
|
|
break;
|
|
|
|
|
case MAA_GPIO_PULLUP:
|
2014-05-06 09:32:07 +01:00
|
|
|
length = snprintf(bu, sizeof(bu), "pullup");
|
2014-04-29 16:55:54 +01:00
|
|
|
break;
|
|
|
|
|
case MAA_GPIO_PULLDOWN:
|
2014-05-06 09:32:07 +01:00
|
|
|
length = snprintf(bu, sizeof(bu), "pulldown");
|
2014-04-29 16:55:54 +01:00
|
|
|
break;
|
|
|
|
|
case MAA_GPIO_HIZ:
|
2014-05-06 09:32:07 +01:00
|
|
|
length = snprintf(bu, sizeof(bu), "hiz");
|
2014-04-29 16:55:54 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fclose(drive);
|
|
|
|
|
return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
|
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
fwrite(bu, sizeof(char), length, drive);
|
|
|
|
|
|
2014-04-29 16:55:54 +01:00
|
|
|
fclose(drive);
|
|
|
|
|
dev->value_fp = NULL;
|
|
|
|
|
return MAA_SUCCESS;
|
2014-04-10 16:05:11 +01:00
|
|
|
}
|
2014-04-10 18:00:50 +01:00
|
|
|
|
2014-04-29 16:46:10 +01:00
|
|
|
maa_result_t
|
2014-04-28 11:31:53 +01:00
|
|
|
maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
|
2014-04-23 09:30:27 +01:00
|
|
|
{
|
2014-05-01 16:55:23 +01:00
|
|
|
if (dev == NULL)
|
|
|
|
|
return MAA_ERROR_INVALID_HANDLE;
|
2014-04-28 11:31:53 +01:00
|
|
|
if (dev->value_fp != NULL) {
|
|
|
|
|
dev->value_fp = NULL;
|
2014-04-14 17:13:31 +01:00
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
char filepath[MAX_SIZE];
|
|
|
|
|
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
|
2014-04-10 16:05:11 +01:00
|
|
|
|
2014-04-11 17:57:53 +01:00
|
|
|
FILE *direction;
|
2014-04-27 23:11:31 +01:00
|
|
|
if ((direction = fopen(filepath, "w")) == NULL) {
|
2014-04-11 11:41:48 +01:00
|
|
|
fprintf(stderr, "Failed to open direction for writing!\n");
|
2014-04-29 16:46:10 +01:00
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
|
|
|
|
|
char bu[MAX_SIZE];
|
|
|
|
|
int length;
|
2014-04-29 16:46:10 +01:00
|
|
|
switch(dir) {
|
|
|
|
|
case MAA_GPIO_OUT:
|
2014-05-06 09:32:07 +01:00
|
|
|
length = snprintf(bu, sizeof(bu), "out");
|
2014-04-29 16:46:10 +01:00
|
|
|
break;
|
|
|
|
|
case MAA_GPIO_IN:
|
2014-05-06 09:32:07 +01:00
|
|
|
length = snprintf(bu, sizeof(bu), "in");
|
2014-04-29 16:46:10 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fclose(direction);
|
|
|
|
|
return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
2014-04-11 11:41:48 +01:00
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
fwrite(bu, sizeof(char), length, direction);
|
|
|
|
|
|
2014-04-29 16:46:10 +01:00
|
|
|
fclose(direction);
|
|
|
|
|
dev->value_fp = NULL;
|
|
|
|
|
return MAA_SUCCESS;
|
2014-04-10 16:05:11 +01:00
|
|
|
}
|
2014-04-10 18:00:50 +01:00
|
|
|
|
2014-04-10 16:05:11 +01:00
|
|
|
int
|
2014-04-28 11:31:53 +01:00
|
|
|
maa_gpio_read(maa_gpio_context *dev)
|
2014-04-23 09:30:27 +01:00
|
|
|
{
|
2014-04-28 11:31:53 +01:00
|
|
|
if (dev->value_fp == NULL) {
|
|
|
|
|
maa_gpio_get_valfp(dev);
|
2014-04-11 13:33:46 +01:00
|
|
|
}
|
2014-04-28 11:31:53 +01:00
|
|
|
fseek(dev->value_fp, SEEK_SET, 0);
|
2014-05-06 09:32:07 +01:00
|
|
|
char bu[2];
|
|
|
|
|
fread(bu, 2, 1, dev->value_fp);
|
2014-04-28 11:31:53 +01:00
|
|
|
fseek(dev->value_fp, SEEK_SET, 0);
|
2014-05-06 09:32:07 +01:00
|
|
|
int ret = strtol(bu, NULL, 10);
|
2014-05-02 16:32:53 +01:00
|
|
|
|
|
|
|
|
return ret;
|
2014-04-10 16:05:11 +01:00
|
|
|
}
|
2014-04-10 18:00:50 +01:00
|
|
|
|
2014-04-29 16:46:10 +01:00
|
|
|
maa_result_t
|
2014-04-28 11:31:53 +01:00
|
|
|
maa_gpio_write(maa_gpio_context *dev, int value)
|
2014-04-23 09:30:27 +01:00
|
|
|
{
|
2014-04-28 11:31:53 +01:00
|
|
|
if (dev->value_fp == NULL) {
|
|
|
|
|
maa_gpio_get_valfp(dev);
|
2014-04-11 13:33:46 +01:00
|
|
|
}
|
2014-05-02 16:31:16 +01:00
|
|
|
if (fseek(dev->value_fp, SEEK_SET, 0) != 0) {
|
|
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
|
|
|
|
|
char bu[MAX_SIZE];
|
|
|
|
|
int length = snprintf(bu, sizeof(bu), "%d", value);
|
|
|
|
|
fwrite(bu, sizeof(char), length, dev->value_fp);
|
|
|
|
|
|
2014-05-02 16:31:16 +01:00
|
|
|
if (fseek(dev->value_fp, SEEK_SET, 0) != 0) {
|
|
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
2014-04-29 16:46:10 +01:00
|
|
|
if (ferror(dev->value_fp) != 0)
|
|
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
return MAA_SUCCESS;
|
2014-04-11 11:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2014-04-29 16:46:10 +01:00
|
|
|
maa_result_t
|
2014-05-02 16:31:16 +01:00
|
|
|
maa_gpio_unexport(maa_gpio_context *dev)
|
2014-04-23 09:30:27 +01:00
|
|
|
{
|
2014-04-14 17:13:31 +01:00
|
|
|
FILE *unexport_f;
|
2014-04-10 16:05:11 +01:00
|
|
|
|
2014-05-06 09:32:07 +01:00
|
|
|
if ((unexport_f = fopen(SYSFS_CLASS_GPIO "/unexport", "w")) == NULL) {
|
2014-04-14 17:13:31 +01:00
|
|
|
fprintf(stderr, "Failed to open unexport for writing!\n");
|
2014-04-29 16:46:10 +01:00
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
2014-04-14 17:13:31 +01:00
|
|
|
}
|
2014-05-06 09:32:07 +01:00
|
|
|
|
|
|
|
|
char bu[MAX_SIZE];
|
|
|
|
|
int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
|
|
|
|
|
fwrite(bu, sizeof(char), length, unexport_f);
|
2014-04-29 16:46:10 +01:00
|
|
|
fclose(unexport_f);
|
2014-05-06 09:32:07 +01:00
|
|
|
|
2014-05-07 14:48:21 +01:00
|
|
|
maa_gpio_isr_exit(dev);
|
|
|
|
|
|
2014-05-06 09:32:07 +01:00
|
|
|
return MAA_SUCCESS;
|
2014-05-02 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maa_result_t
|
|
|
|
|
maa_gpio_close(maa_gpio_context *dev)
|
|
|
|
|
{
|
2014-05-06 18:12:53 +01:00
|
|
|
if (ferror(dev->value_fp) != 0) {
|
|
|
|
|
return MAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:31:16 +01:00
|
|
|
maa_gpio_unexport(dev);
|
2014-04-29 16:46:10 +01:00
|
|
|
free(dev);
|
|
|
|
|
return MAA_SUCCESS;
|
2014-04-10 16:05:11 +01:00
|
|
|
}
|