HiPi
Perl Modules for Raspberry Pi
Version 0.92 - released 28 March 2024

HiPi::GPIO

Provides access to the GPIO header pin functions as mapped to the device /dev/gpiomem. Only functions available to none root users are mapped so use of the module does not require root permissions. The effective user must be a member of the gpio group.

For Raspberry Pi versions before Raspberry Pi 5, the Perl XS code owes much to Joan's pigpio libraries but it is not a direct wrapper for those libraries.

The XS code for Raspberry Pi 5 is based on the reference implementation noted in the RP1 Peripherals document.

The RPI_PIN constants allow you to refer to GPIO pins by their position on the GPIO header, rather than their BCM GPIO numbers. See the Raspberry Pi Pinout

The HiPi::GPIO::Pin module provides a wrapper around the HiPi::GPIO methods

The HiPi::Device::GPIO module provides access to the GPIO pins using the '/sys/class/gpio' file system interface. Use that if you wish to experiment capturing interrupts. Using this module you should poll the pin value if you wish to capture changes in state.

Example

Methods

Creates a new instance of the class

use HiPi::GPIO;
my $gpio = HiPi::GPIO->new;

Returns an object of the class HiPi::Device::GPIO::Pin. This provides and alternative interface for the pin functions.

use HiPi qw( :rpi );
my $pin = $gpio->get_pin( RPI_PIN_36 );

Set the pin level high or low. The method 'pin_write' is an alias for this method. Returns the value set.

use HiPi qw( :rpi );
...
$gpio->set_pin_level( RPI_PIN_36, RPI_HIGH );
$gpio->set_pin_level( RPI_PIN_36, 1 );
$gpio->set_pin_level( RPI_PIN_36, RPI_LOW );
$gpio->set_pin_level( RPI_PIN_36, 0 );
$gpio->pin_write( RPI_PIN_36, RPI_HIGH )
$gpio->pin_write( RPI_PIN_36, 1 )

Get the level ( RPI_HIGH / RPI_LOW, 1 / 0 ) of a gpio pin. The method 'pin_read' is an alias for this method.

use HiPi qw( :rpi );
...
my $value = $gpio->get_pin_level( RPI_PIN_36 );
$value = $gpio->get_pin_level( 16 );
$value = $gpio->pin_read( RPI_PIN_36 );

Set the pint mode. Normally, RPI_MODE_INPUT or RPI_MODE_OUTPUT, but will accept any of the following constants:

RPI_MODE_INPUT
RPI_MODE_OUTPUT
RPI_MODE_ALT0
RPI_MODE_ALT1
RPI_MODE_ALT2
RPI_MODE_ALT3
RPI_MODE_ALT4
RPI_MODE_ALT5
( below for Raspberry Pi 5 only )
RPI_MODE_ALT6
RPI_MODE_ALT7
RPI_MODE_ALT8
RPI_MODE_NONE

Returns the mode set.

use HiPi qw( :rpi );
...
$gpio->set_pin_mode( RPI_PIN_36, RPI_MODE_OUTPUT );

Get the current pin mode. Returns one of the constants:

RPI_MODE_INPUT
RPI_MODE_OUTPUT
RPI_MODE_ALT0
RPI_MODE_ALT1
RPI_MODE_ALT2
RPI_MODE_ALT3
RPI_MODE_ALT4
RPI_MODE_ALT5
( below for Raspberry Pi 5 only )
RPI_MODE_ALT6
RPI_MODE_ALT7
RPI_MODE_ALT8
RPI_MODE_NONE
use HiPi qw( :rpi );
...
my $mode = $gpio->get_pin_mode( RPI_PIN_36 );

Set or remove the internal pull up or pull down resistor on a gpio pin. Accepts constants:

RPI_PUD_OFF
RPI_PUD_DOWN
RPI_PUD_UP
use HiPi qw( :rpi );
...
$gpio->set_pin_pud( RPI_PIN_36, RPI_PUD_UP );

On the Raspberry Pi 4 and Raspberry Pi 5 you can retrieve the current pull up / down setting for a gpio pin. Returns one of the constants:

RPI_PUD_OFF
RPI_PUD_DOWN
RPI_PUD_UP
RPI_PUD_UNSET

For BCM2835/6/7 based Raspberry Pi's this method will always return RPI_PUD_UNSET

use HiPi qw( :rpi );
...
my $setting = $gpio->get_pin_pud( RPI_PIN_36 );

Returns descriptive text for the current pin function.

use HiPi qw( :rpi );
my $description = $gpio->get_pin_function( RPI_PIN_36 );

RP1 based Raspberry Pi only ( Raspberry Pi 5 ).

You may check HiPi::RaspberryPi::has_rp1() before calling.

Set the pin schmitt trigger on or off. Accepts constants:

RPI_SCHMITT_OFF
RPI_SCHMITT_ON
use HiPi qw( :rpi );
...
$gpio->set_pin_schmitt( RPI_PIN_36, RPI_SCHMITT_OFF );
$gpio->set_pin_schmitt( RPI_PIN_36, 0 );
$gpio->set_pin_schmitt( RPI_PIN_36, RPI_SCHMITT_ON );
$gpio->set_pin_schmitt( RPI_PIN_36, 1 );
        

RP1 based Raspberry Pi only ( Raspberry Pi 5 ).

You may check HiPi::RaspberryPi::has_rp1() before calling.

Get the current setting of the pin schmitt trigger. Returns one of the constants:

RPI_SCHMITT_OFF
RPI_SCHMITT_ON
use HiPi qw( :rpi );
...
my $setting = $gpio->get_pin_schmitt( RPI_PIN_36 );

RP1 based Raspberry Pi only ( Raspberry Pi 5 ).

You may check HiPi::RaspberryPi::has_rp1() before calling.

Set the pin slew slow or fast. Accepts constants:

RPI_SLEW_SLOW
RPI_SLEW_FAST
use HiPi qw( :rpi );
...
$gpio->set_pin_slew( RPI_PIN_36, RPI_SLEW_SLOW );
$gpio->set_pin_slew( RPI_PIN_36, 0 );
$gpio->set_pin_slew( RPI_PIN_36, RPI_SLEW_FAST );
$gpio->set_pin_slew( RPI_PIN_36, 1 );
        

RP1 based Raspberry Pi only ( Raspberry Pi 5 ).

You may check HiPi::RaspberryPi::has_rp1() before calling.

Get the current pin slew. Returns one of the constants:

RPI_SLEW_SLOW
RPI_SLEW_FAST
use HiPi qw( :rpi );
...
my $slew = $gpio->get_pin_slew( RPI_PIN_36 );

Module Use Examples

use HiPi qw( :rpi );
use HiPi::GPIO;
my $header = HiPi::GPIO->new;

# set GPIO_17 as an output pin
$header->set_pin_mode( RPI_PIN_11, RPI_MODE_OUPUT );
# or without pin constant
$header->set_pin_mode( 17, RPI_MODE_OUPUT );

# set GPIO_17 high
$header->set_pin_level( RPI_PIN_11, RPI_HIGH );
# or without constants
$header->set_pin_level( 17, 1 );

# set GPIO_17 low
$header->set_pin_level( RPI_PIN_11, RPI_LOW );
# or without constants
$header->set_pin_level( 17, 0 );

# set GPIO_17 as an input pin
$header->set_pin_mode( RPI_PIN_11, RPI_MODE_INPUT );

# activate pull up resistor on GPIO_17
$header->set_pin_pud( RPI_PIN_11, RPI_PUD_UP );

# read value of GPIO_17
my $val = $header->get_pin_level( RPI_PIN_11 );

# activate pull down resistor on GPIO_17
$header->set_pin_pud( RPI_PIN_11, RPI_PUD_DOWN );

# remove pull up / down resistor from GPIO_17
$header->set_pin_pud( RPI_PIN_11, RPI_PUD_OFF );