2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 maa_gpio_get_valfp(maa_gpio_context *dev)
36 sprintf(bu, "/sys/class/gpio/gpio%d/value", dev->pin);
38 if ((dev->value_fp = fopen(bu, "r+b")) == NULL) {
45 maa_gpio_init(int pin)
47 int pinm = maa_check_gpio(pin);
50 return maa_gpio_init_raw(pinm);
54 maa_gpio_init_raw(int pin)
59 maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
61 if ((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) {
62 fprintf(stderr, "Failed to open export for writing!\n");
65 fprintf(export_f, "%d", pin);
73 maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
75 if (dev->value_fp != NULL) {
79 snprintf(filepath, 64, "/sys/class/gpio/gpio%d/drive", dev->pin);
82 if ((drive = fopen(filepath, "w")) == NULL) {
83 fprintf(stderr, "Failed to open drive for writing!\n");
84 return MAA_ERROR_INVALID_RESOURCE;
88 fprintf(drive, "strong");
91 fprintf(drive, "pullup");
93 case MAA_GPIO_PULLDOWN:
94 fprintf(drive, "pulldown");
97 fprintf(drive, "hiz");
101 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
105 dev->value_fp = NULL;
110 maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
113 return MAA_ERROR_INVALID_HANDLE;
114 if (dev->value_fp != NULL) {
115 dev->value_fp = NULL;
118 snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", dev->pin);
121 if ((direction = fopen(filepath, "w")) == NULL) {
122 fprintf(stderr, "Failed to open direction for writing!\n");
123 return MAA_ERROR_INVALID_RESOURCE;
127 fprintf(direction, "out");
130 fprintf(direction, "in");
134 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
138 dev->value_fp = NULL;
143 maa_gpio_read(maa_gpio_context *dev)
145 if (dev->value_fp == NULL) {
146 maa_gpio_get_valfp(dev);
148 fseek(dev->value_fp, SEEK_SET, 0);
150 fread(buffer, 2, 1, dev->value_fp);
151 fseek(dev->value_fp, SEEK_SET, 0);
156 maa_gpio_write(maa_gpio_context *dev, int value)
158 if (dev->value_fp == NULL) {
159 maa_gpio_get_valfp(dev);
161 if (fseek(dev->value_fp, SEEK_SET, 0) != 0) {
162 return MAA_ERROR_INVALID_RESOURCE;
164 fprintf(dev->value_fp, "%d", value);
165 if (fseek(dev->value_fp, SEEK_SET, 0) != 0) {
166 return MAA_ERROR_INVALID_RESOURCE;
168 if (ferror(dev->value_fp) != 0)
169 return MAA_ERROR_INVALID_RESOURCE;
174 maa_gpio_unexport(maa_gpio_context *dev)
178 if ((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) {
179 fprintf(stderr, "Failed to open unexport for writing!\n");
180 return MAA_ERROR_INVALID_RESOURCE;
182 fprintf(unexport_f, "%d", dev->pin);
184 if (ferror(dev->value_fp) != 0)
185 return MAA_ERROR_INVALID_RESOURCE;
189 maa_gpio_close(maa_gpio_context *dev)
191 maa_gpio_unexport(dev);