int libsoc_gpio_free (gpio * gpio) { char tmp_str[STR_BUF]; int fd; if (gpio == NULL) { libsoc_gpio_debug (__func__, -1, "invalid gpio pointer"); return EXIT_FAILURE; } libsoc_gpio_debug (__func__, gpio->gpio, "freeing gpio"); if (gpio->callback != NULL) { printf ("Freeing callback!\n"); // Turn off the callback if there is one enabled libsoc_gpio_callback_interrupt_cancel (gpio); } if (file_close (gpio->value_fd) < 0) return EXIT_FAILURE; if (gpio->shared == 1) { free (gpio); return EXIT_SUCCESS; } fd = file_open ("/sys/class/gpio/unexport", O_SYNC | O_WRONLY); if (fd < 0) return EXIT_FAILURE; sprintf (tmp_str, "%d", gpio->gpio); if (file_write (fd, tmp_str, STR_BUF) < 0) return EXIT_FAILURE; if (file_close (fd) < 0) return EXIT_FAILURE; sprintf (tmp_str, "/sys/class/gpio/gpio%d", gpio->gpio); if (file_valid (tmp_str)) { libsoc_gpio_debug (__func__, gpio->gpio, "freeing failed"); return EXIT_FAILURE; } free (gpio); return EXIT_SUCCESS; }
int main(void) { // Enable debug output libsoc_set_debug(1); // Request gpios gpio_output = libsoc_gpio_request(GPIO_OUTPUT, LS_SHARED); gpio_input = libsoc_gpio_request(GPIO_INPUT, LS_SHARED); // Set direction to OUTPUT libsoc_gpio_set_direction(gpio_output, OUTPUT); // Set direction to INPUT libsoc_gpio_set_direction(gpio_input, INPUT); // Set edge to BOTH libsoc_gpio_set_edge(gpio_input, BOTH); // Setup callback libsoc_gpio_callback_interrupt(gpio_input, &callback_test, (void*) &interrupt_count); printf("Push the button...\n"); // Disaple debug output so the code will respond faster libsoc_set_debug(0); sleep(10); libsoc_set_debug(1); // Cancel the callback on interrupt libsoc_gpio_callback_interrupt_cancel(gpio_input); //If gpio_request was successful if (gpio_input) { // Free gpio request memory libsoc_gpio_free(gpio_input); } if (gpio_output) { // Free gpio request memory libsoc_gpio_free(gpio_output); } return EXIT_SUCCESS; }