Lines Matching full:gpio
14 #include "gpio.h"
18 #include <linux/gpio.h>
21 #define GPIO_BASE_PATH "/sys/class/gpio"
26 int gpio_write(GPIO* gpio, uint8_t value) in gpio_write() argument
28 g_assert (gpio != NULL); in gpio_write()
33 if (gpio->fd <= 0) in gpio_write()
38 if (ioctl(gpio->fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data) < 0) in gpio_write()
46 int gpio_read(GPIO* gpio, uint8_t *value) in gpio_read() argument
48 g_assert (gpio != NULL); in gpio_read()
52 if (gpio->fd <= 0) in gpio_read()
57 if (ioctl(gpio->fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data) < 0) in gpio_read()
68 * Determine the GPIO base number for the system. It is found in
69 * the 'base' file in the /sys/class/gpio/gpiochipX/ directory where the
70 * /sys/class/gpio/gpiochipX/label file has a '1e780000.gpio' in it.
75 * @return int - the GPIO base number, or < 0 if not found
94 /* that contains '1e780000.gpio', then in that directory read */ in get_gpio_base()
95 /* the GPIO base out of the 'base' file. */ in get_gpio_base()
123 if (strcmp(label, "1e780000.gpio") == 0) in get_gpio_base()
165 fprintf(stderr, "Could not find GPIO base\n"); in get_gpio_base()
172 * Converts the GPIO port/offset nomenclature value
180 * @param[in] gpio - the GPIO name
182 * @return int - the GPIO number or < 0 if not found
184 int convert_gpio_to_num(const char* gpio) in convert_gpio_to_num() argument
186 size_t len = strlen(gpio); in convert_gpio_to_num()
189 fprintf(stderr, "Invalid GPIO name %s\n", gpio); in convert_gpio_to_num()
194 if (!isdigit(gpio[len-1])) in convert_gpio_to_num()
196 fprintf(stderr, "Invalid GPIO offset in GPIO %s\n", gpio); in convert_gpio_to_num()
200 int offset = gpio[len-1] - '0'; in convert_gpio_to_num()
203 if (!isalpha(gpio[len-2])) in convert_gpio_to_num()
205 fprintf(stderr, "Invalid GPIO port in GPIO %s\n", gpio); in convert_gpio_to_num()
208 int port = toupper(gpio[len-2]) - 'A'; in convert_gpio_to_num()
211 if ((len == 3) && isalpha(gpio[len-3])) in convert_gpio_to_num()
213 port += 26 * (toupper(gpio[len-3]) - 'A' + 1); in convert_gpio_to_num()
220 * Returns the cJSON pointer to the GPIO definition
221 * for the GPIO passed in.
223 * @param[in] gpio_name - the GPIO name, like BMC_POWER_UP
273 * the GPIO structure.
275 * @param gpio - the GPIO structure to fill in
279 int gpio_get_params(GPIO* gpio) in gpio_get_params() argument
281 gpio->dev = g_strdup(GPIO_BASE_PATH); in gpio_get_params()
283 const cJSON* def = get_gpio_def(gpio->name); in gpio_get_params()
286 fprintf(stderr, "Unable to find GPIO %s in the JSON\n", in gpio_get_params()
287 gpio->name); in gpio_get_params()
293 gpio->direction = g_strdup(dir->valuestring); in gpio_get_params()
314 // Query GPIO chip info in gpio_get_params()
327 gpio->num = (num->valueint) % info.lines; in gpio_get_params()
328 gpio->chip_id = (num->valueint) / info.lines; in gpio_get_params()
340 gpio->num = (size_t) pin_id; in gpio_get_params()
346 gpio->chip_id = 0; in gpio_get_params()
350 int gpio_open(GPIO* gpio, uint8_t default_value) in gpio_open() argument
352 g_assert (gpio != NULL); in gpio_open()
355 sprintf(buf, "/dev/gpiochip%d", gpio->chip_id); in gpio_open()
356 gpio->fd = open(buf, 0); in gpio_open()
357 if (gpio->fd == -1) in gpio_open()
364 strncpy(req.consumer_label, "skeleton-gpio", sizeof(req.consumer_label)); in gpio_open()
366 // open gpio for writing or reading in gpio_open()
367 if (gpio->direction == NULL) in gpio_open()
369 gpio_close(gpio); in gpio_open()
372 req.flags = (strcmp(gpio->direction,"in") == 0) ? GPIOHANDLE_REQUEST_INPUT in gpio_open()
375 req.lineoffsets[0] = gpio->num; in gpio_open()
378 if (strcmp(gpio->direction,"out") == 0) in gpio_open()
383 int rc = ioctl(gpio->fd, GPIO_GET_LINEHANDLE_IOCTL, &req); in gpio_open()
386 gpio_close(gpio); in gpio_open()
389 gpio_close(gpio); in gpio_open()
390 gpio->fd = req.fd; in gpio_open()
395 void gpio_close(GPIO* gpio) in gpio_close() argument
397 if(gpio->fd < 0) in gpio_close()
400 close(gpio->fd); in gpio_close()
401 gpio->fd = -1; in gpio_close()