GPIO


Data Types


gpio_mode

Determines the way in which libsoc handles exporting and unexporting GPIOs in the Linux subsystem.


gpio_direction

Used for setting and reading the direction of the GPIO pin.


gpio_level

Used for setting and reading the level of the GPIO pin.


gpio_edge

Used for setting and reading the edge of the GPIO pin.


gpio_int_ret

Return type for blocked GPIO interrupts.

Functions


libsoc_gpio_request

gpio * libsoc_gpio_request(unsigned int gpio_id, gpio_mode mode)

Request a GPIO by it's Linux ID number and set the gpio_mode under which libsoc will hold the file descriptor. Returns a malloced gpio struct which will need to be freed by libsoc_gpio_free when no longer needed.

Returns NULL on failure.


libsoc_gpio_free

int libsoc_gpio_free(gpio * gpio)

Free the memory associated with a previously requested GPIO and close the file descriptors.

Returns EXIT_SUCCESS/EXIT_FAILURE


libsoc_gpio_set_direction

int libsoc_gpio_set_direction(gpio * current_gpio, gpio_direction direction)

Set the direction of a GPIO, INPUT for reading a GPIO, OUTPUT for setting a GPIO.

Returns EXIT_SUCCESS/EXIT_FAILURE


libsoc_gpio_get_direction

gpio_direction libsoc_gpio_get_direction(gpio * current_gpio)

Get the current gpio_direction of a requested GPIO.

Returns gpio_direction, DIRECTION_ERROR on failure, INPUT/OUTPUT on success.


libsoc_gpio_set_level

int libsoc_gpio_set_level(gpio * current_gpio, gpio_level level)

Set the output level of a requested GPIO to HIGH or LOW.

Returns EXIT_SUCCESS/EXIT_FAILURE


libsoc_gpio_get_level

gpio_level libsoc_gpio_get_level(gpio * current_gpio)

Get the current gpio_level of a requested GPIO.

Returns gpio_level, LEVEL_ERROR on failure, LOW/HIGH on success.


libsoc_gpio_set_edge

int libsoc_gpio_set_edge(gpio * current_gpio, gpio_edge edge)

Set the edge type of a requested GPIO. The edge is the type of signal change that will trigger interrupt detection. See gpio_edge for explanations of the different types of edges and how they work.

Returns EXIT_SUCCESS/EXIT_FAILURE


libsoc_gpio_get_edge

gpio_edge libsoc_gpio_get_edge(gpio * current_gpio)

Get the current gpio_edge of a requested GPIO.

Returns gpio_edge, EDGE_ERROR on failure, RISING/FALLING/BOTH/NONE on success.


libsoc_gpio_wait_interrupt

int libsoc_gpio_wait_interrupt(gpio * gpio, int timeout)

Block for a set amount of seconds (or indefinitly) waiting for an interrupt on a GPIO to occur. If you wish to use a non-blocking interrupt mechanism see libsoc_gpio_callback_interrupt.

Returns LS_INT_ERROR on failure, LS_INT_TRIGGERED on captured interrupt, LS_INT_TIMEOUT if no interrupt was captured in the specified time.


libsoc_gpio_callback_interrupt

int libsoc_gpio_callback_interrupt(gpio * gpio, int (*callback_fn) (void *), void *arg)

Setup an interrupt handler on a GPIO. This will start a new pthread with an infinite poll on the GPIO waiting for the interrupt. When an interrupt is detected the supplied function will run in the thread, and then return to waiting for an interrupt.

Only one interrupt will be queued if it arrives while your supplied function is being run, so it is possible to miss interrupts if they happen too fast.

Returns EXIT_SUCCESS/EXIT_FAILURE


libsoc_gpio_callback_interrupt_cancel

int libsoc_gpio_callback_interrupt_cancel(gpio * gpio)

Cancel a previously set interrupt handler on a GPIO. This uses the pthread_cancel function, so it may cancel mid way through your interrupt handler function.

Returns EXIT_SUCCESS/EXIT_FAILURE