/********************************************************************************* * Copyright: (C) 2019 LingYun IoT System Studio * All rights reserved. * * Filename: gpio.c * Description: This file is GPIO input/output functions * * Version: 1.0.0(2019年06月24日) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "2019年06月24日 23时46分47秒" * ********************************************************************************/ #include "lylib/logger.h" #include "gpio.h" #define RPI_GPIONAME "gpiochip0" static struct gpiod_chip *s_chip; static gpio_t *s_gpio = NULL; int gpio_init(gpio_t *gpio) { int i; int rv; gpio_info_t *gpioinfo; s_gpio = gpio; if( !gpio ) { log_err("Invalid input arguments $gpio\n"); return -1; } if( !gpio->incnt && !gpio->outcnt ) { log_warn("WARNNING: No GPIO pins configured\n"); return 0; } /* gpiod open chip */ s_chip = gpiod_chip_open_by_name(RPI_GPIONAME); if( !s_chip ) { log_err("gpiod open chip failure, maybe you need running as root\n"); return -2; } log_nrml("gpiod initialise open chip ok\n"); /* gpiod request all output pins */ for(i=0; ioutcnt; i++) { gpio->output[i].line = gpiod_chip_get_line(s_chip, gpio->output[i].pin); if( !gpio->output[i].line ) { log_err("gpiod get line for '%s' pin[#%d] failure\n", gpio->output[i].name, gpio->output[i].pin ); return -2; } if( gpiod_line_request_output(gpio->output[i].line, gpio->output[i].name, !gpio->output[i].active_level) < 0 ) { log_err("gpiod request '%s' pin[#%d] output failure: %s\n", gpio->output[i].name, gpio->output[i].pin, strerror(errno)); } else { log_nrml("gpiod request '%s' pin[#%d] output ok\n", gpio->output[i].name, gpio->output[i].pin); } } /* gpiod request all input pins */ for(i=0; iincnt; i++) { gpio->input[i].line = gpiod_chip_get_line(s_chip, gpio->input[i].pin); if( !gpio->input[i].line ) { log_err("gpiod get line for '%s' pin[#%d] failure\n", gpio->input[i].name, gpio->input[i].pin ); return -2; } if( gpio->output[i].active_level ) rv = gpiod_line_request_rising_edge_events(gpio->input[i].line, gpio->input[i].name) ; else rv = gpiod_line_request_falling_edge_events(gpio->input[i].line, gpio->input[i].name) ; if( rv < 0 ) { log_err("gpiod request '%s' pin[#%d] event edge [%s] failure: %s\n", gpio->input[i].name, gpio->input[i].pin, gpio->output[i].active_level?"rising":"falling", strerror(errno)); } else { log_nrml("gpiod request '%s' pin[#%d] event edge [%s] ok\n", gpio->input[i].name, gpio->input[i].pin, gpio->output[i].active_level?"rising":"falling"); } } } void gpio_term(void) { int i; log_nrml("start teriminated GPIO\n"); if( !s_gpio->incnt && !s_gpio->outcnt ) { return ; } for(i=0; ioutcnt; i++) { gpiod_line_release(s_gpio->output[i].line); } for(i=0; iincnt; i++) { gpiod_line_release(s_gpio->input[i].line); } gpiod_chip_close(s_chip); } void gpio_out(char *name, char *cmd) { int i; int found = 0; for( i=0; ioutcnt; i++ ) { if( !strncasecmp(s_gpio->output[i].name, name, strlen(name))) { found = 1; break; } } if( !found ) { log_err("GPIO output for '%s' pin not found\n", name); return ; } if( strstr(cmd, "on") ) { gpiod_line_set_value(s_gpio->output[i].line, s_gpio->output[i].active_level); } else if( strstr(cmd, "off") ) { gpiod_line_set_value(s_gpio->output[i].line, !s_gpio->output[i].active_level); } return ; } #define MS_NANOSEC (1*1000*1000) /* Return value: 0(LOW): Nobody detected, !0: indoor or hallway detected */ int infrared_detect(int interval) { int i; int rv = 0; int res = 0; struct gpiod_line_event event; int event_type; struct timespec ts = { 0, interval*MS_NANOSEC }; /* second and nanoseconds, */ for(i=0; iincnt; i++) { if( strstr(s_gpio->input[i].name, "infrared")) { /* This function will block, must call it to setup */ rv = gpiod_line_event_wait(s_gpio->input[i].line, &ts); if( rv < 0 ) { //log_err("infrared gpiod line wait[%s] event failure!\n", s_gpio->input[i].name); return 0; } else if( rv == 0 ) { //log_dbg("infrared gpiod line[%s] wait event timeout\n", s_gpio->input[i].name); continue; } /* Must read to clear the event */ rv = gpiod_line_event_read(s_gpio->input[i].line, &event); event_type = s_gpio->input[i].active_level? GPIOD_LINE_EVENT_RISING_EDGE : GPIOD_LINE_EVENT_FALLING_EDGE; if ( event.event_type == event_type ) { if( strstr(s_gpio->input[i].name, "indoor") ) { res |= FLAG_INFRARED_INDOOR; } else if( strstr(s_gpio->input[i].name, "hallway") ) { res |= FLAG_INFRARED_HALLWAY; } } } } return res; }