pynq.gpio Module

The pynq.gpio module is a driver for reading and writing PS GPIO pins on a board. PS GPIO pins are not connected to the PL.

class pynq.gpio.GPIO(gpio_index, direction)[source]

Bases: object

Class to wrap Linux’s GPIO Sysfs API.

This GPIO class does not handle PL I/O without the use of device tree overlays.

index

The index of the GPIO, starting from the GPIO base.

Type:int
direction

Input/output direction of the GPIO.

Type:str
path

The path of the GPIO device in the linux system.

Type:str
direction
static get_gpio_base(target_label=None)[source]

This method returns the GPIO base using Linux’s GPIO Sysfs API.

This is a static method. To use:

>>> from pynq import GPIO
>>> gpio = GPIO.get_gpio_base()

Note

For path ‘/sys/class/gpio/gpiochip138/’, this method returns 138.

Parameters:target_label (str) – The label of the GPIO driver to look for, as defined in a device tree entry.
Returns:The GPIO index of the base.
Return type:int
static get_gpio_base_path(target_label=None)[source]

This method returns the path to the GPIO base using Linux’s GPIO Sysfs API.

This is a static method. To use:

>>> from pynq import GPIO
>>> gpio = GPIO.get_gpio_base_path()
Parameters:target_label (str) – The label of the GPIO driver to look for, as defined in a device tree entry.
Returns:The path to the GPIO base.
Return type:str
static get_gpio_npins(target_label=None)[source]

This method returns the number of GPIO pins for the GPIO base using Linux’s GPIO Sysfs API.

This is a static method. To use:

>>> from pynq import GPIO
>>> gpio = GPIO.get_gpio_npins()
Parameters:target_label (str) – The label of the GPIO driver to look for, as defined in a device tree entry.
Returns:The number of GPIO pins for the GPIO base.
Return type:int
static get_gpio_pin(gpio_user_index, target_label=None)[source]

This method returns a GPIO instance for PS GPIO pins.

Users only need to specify an index starting from 0; this static method will map this index to the correct Linux GPIO pin number.

Note

The GPIO pin number can be calculated using: GPIO pin number = GPIO base + GPIO offset + user index e.g. The GPIO base is 138, and pin 54 is the base GPIO offset. Then the Linux GPIO pin would be (138 + 54 + 0) = 192.

Parameters:
  • gpio_user_index (int) – The index specified by users, starting from 0.
  • target_label (str) – The label of the GPIO driver to look for, as defined in a device tree entry.
Returns:

The Linux Sysfs GPIO pin number.

Return type:

int

index
path
read()[source]

The method to read a value from the GPIO.

Returns:An integer read from the GPIO
Return type:int
release()[source]

The method to release the GPIO.

Returns:
Return type:None
write(value)[source]

The method to write a value into the GPIO.

Parameters:value (int) – An integer value, either 0 or 1
Returns:
Return type:None