NAME
HiPi::GPIO
VERSION
Version 0.01
SYNOPSYS
use HiPi::Constant qw( :pinmode );
use HiPi::GPIO::PAD1;
use HiPi::GPIO::PAD5;
my $pad1 = HiPi::GPIO::PAD1->new;
# Set RPi header pins 8 & 10 to output and value high
for ( 8, 10 ) {
$pad1->set_pin_mode($_, PIN_MODE_OUTPUT);
$pad1->set_pin($_);
}
...
...
# we used the standard UART pins so we ought to set
# them back to defaults
$pad1->prepare_UART0();
DESCRIPTION
This module provides a base high level wrapper for Rasperry Pi GPIO access using the lower level HiPi::BCM2835 as a backend through classes HiPi::GPIO::PAD1 and HiPi::GPIO::PAD5
The Raspberry Pi provides pin out headers P1 and P5 ( although only the 26 pin P1 has a surface mounted connector.)
These have pins numbered as follows: ( numbering follows physical location on the header. )
PAD 1 ( P1 26 pins) PAD 5 ( P5 8 pins )
----------- -----------
| 1 2 | | 1 2 |
| 3 4 | | 3 4 |
| 5 6 | | 5 6 |
| 7 8 | | 7 8 |
| 9 10 | -----------
| 11 12 |
| 13 14 |
| 15 16 |
| 17 18 |
| 19 20 |
| 21 22 |
| 23 24 |
| 25 26 |
-----------
The methods of HiPi::GPIO::PADx expect the physical pin header number as a pin identifier, so to set physical RPi pin 8 as an output:
$pad1->set_pin_mode(8, PIN_MODE_OUTPUT);
In reference manuals and other project code you may see reference to the BCM GPIO pin numbers. These do not correspond to the Raspberry Pi pin number physical layout above. The WiringPi C library has its own pin numbering system, which is again different. Some pins can also be referred to by name. The following tables provide a mapping for the RPi numbers above to their names at their default settings, their BCM GPIO numbers, and their WiringPi numbers. Please note that the following layouts are provided aaccording to my understanding. If you use them you accept that if you fry your Raspberry Pi as a consequence it is entirely your own fault. If you want to be certain of correct RPi pint out and mapping, research elsewhere.
PAD 1 (P1 Header)
WIRING GPIO NAME RPI NAME GPIO WIRING
--------------------------------------------------------------
- - 3.3v | 1 2 | 5.0V - -
8 2 I2C0_SDA | 3 4 | 5.0V - -
9 3 I2C0_SCL | 5 6 | GND - -
7 4 GPIO_04 | 7 8 | UART0_TXD 14 15
- - GND | 9 10 | UART0_RXD 15 16
0 17 GPIO_17 | 11 12 | GPIO_18 18 1
2 27 GPIO_27 | 13 14 | GND - -
3 22 GPIO_22 | 15 16 | GPIO_23 23 4
- - 3.3v | 17 18 | GPIO_24 24 5
12 10 SPI0_MOSI | 19 20 | GND - -
13 9 SPI0_MISO | 21 22 | GPIO_25 25 6
14 11 SPI0_SCLK | 23 24 | SPI0_CE0_N 8 10
- - GND | 25 26 | SPI0_CE1_N 7 11
-------------------------------------------------------------
PAD 5 (P5 Header)
WIRING GPIO NAME RPI NAME GPIO WIRING
--------------------------------------------------------------
- - 5.0V | 1 2 | 3.3v - -
17 28 GPIO_28 | 3 4 | GPIO_29 29 18
19 30 GPIO_30 | 5 6 | GPIO_31 31 20
- - GND | 7 8 | GND - -
--------------------------------------------------------------
The HiPi::GPIO::PADx modules expect the physical Raspberry Pi header pin number. So to set physical pin 8 from header P1 as an output and then to set the pin high:
use HiPi::GPIO::PAD1;
use HiPi::Constant qw( :pinmode );
my $pad1 = HiPi::GPIO::PAD1->new;
$pad1->set_pin_mode(8, PIN_MODE_OUTPUT);
$pad1->set_pin(8);
If you are translating code that uses the BCM GPIO pin id's or perhaps the WiringPi pin id's you can simplify your task by exporting and using the BCM pin constants or the WiringPi constants as required and using these instead of the RPi pin literals.
For example, bcm2835 based code would identify the RPi header pi 8 as GPIO number 14. The WiringPi library would identify it as pin 15.
You don't have to translate manually using the above table:
use HiPi::GPIO::PAD1;
use HiPi::Constant qw( :pinmode :bcm2835);
my $pad1 = HiPi::GPIO::PAD1->new;
$pad1->set_pin_mode(BCM_14, PIN_MODE_OUTPUT);
$pad1->set_pin(BCM_14);
or
use HiPi::GPIO::PAD1;
use HiPi::Constant qw( :pinmode :wiring);
my $pad1 = HiPi::GPIO::PAD1->new;
$pad1->set_pin_mode(WPI_PIN_15, PIN_MODE_OUTPUT);
$pad1->set_pin(WPI_PIN_15);
METHODS
Where methods are wrappers for a corresponding bcm2835 library function ful documentation can be found at http://www.open.com.au/mikem/bcm2835/modules.html
new()
my $pad1 = HiPi::GPIO::PAD1->new;
my $pad5 = HiPi::GPIO::PAD5->new;
Returns a new HiPi::GPIO::PADx object
delay
$obj->delay( $milliseconds )
Wrapper for the bcm2835_delay function.
delay_microseconds
$obj->delay_microseconds( $microseconds )
Wrapper for the bcm2835_delayMicroseconds function.
set_pin_mode
$obj->set_pin_mode( $pin, $iomode )
Wrapper for the bcm2835_gpio_fsel function.
write_pin
$obj->set_pin_mode( $pin, $value )
Wrapper for the bcm2835_gpio_write function.
set_pin
$obj->set_pin( $pin )
Wrapper for the bcm2835_gpio_set function.
clr_pin
$obj->clr_pin( $pin )
Wrapper for the bcm2835_gpio_clr function.
read_pin
my $val = $obj->read_pin( $pin )
Wrapper for the bcm2835_gpio_lev function.
set_pin_pud
$obj->set_pin_pud( $pin, $value )
Wrapper for the bcm2835_gpio_set_pud function.
set_pin_afen
$obj->set_pin_afen( $pin, $value )
Wrapper for the bcm2835_gpio_afen and bcm2835_gpio_clr_afen functions.
bcm2835_gpio_clr_afen is called if $value evaluates to false
set_pin_aren
$obj->set_pin_aren( $pin, $value )
Wrapper for the bcm2835_gpio_aren and bcm2835_gpio_clr_aren functions.
bcm2835_gpio_clr_aren is called if $value evaluates to false
set_pin_fen
$obj->set_pin_fen( $pin, $value )
Wrapper for the bcm2835_gpio_fen and bcm2835_gpio_clr_fen functions.
bcm2835_gpio_clr_fen is called if $value evaluates to false
set_pin_ren
$obj->set_pin_ren( $pin, $value )
Wrapper for the bcm2835_gpio_ren and bcm2835_gpio_clr_ren functions.
bcm2835_gpio_clr_ren is called if $value evaluates to false
set_pin_hen
$obj->set_pin_hen( $pin, $value )
Wrapper for the bcm2835_gpio_hen and bcm2835_gpio_clr_hen functions.
bcm2835_gpio_clr_hen is called if $value evaluates to false
set_pin_len
$obj->set_pin_len( $pin, $value )
Wrapper for the bcm2835_gpio_len and bcm2835_gpio_clr_len functions.
bcm2835_gpio_clr_len is called if $value evaluates to false
get_pin_eds
my $var = $obj->get_pin_eds( $pin )
Wrapper for the bcm2835_gpio_eds function.
clear_pin_eds
$obj->clear_pin_eds( $pin )
Wrapper for the bcm2835_gpio_set_eds function.
spi_begin
$obj->spi_begin()
Wrapper for the bcm2835_spi_begin function.
spi_end
$obj->spi_end()
Wrapper for the bcm2835_spi_end function.
spi_set_bit_order
$obj->spi_set_bit_order($order)
Wrapper for the bcm2835_spi_setBitOrder function.
spi_set_clock_divider
$obj->spi_set_clock_divider($divider)
Wrapper for the bcm2835_spi_setClockDivider function.
spi_set_data_mode
$obj->spi_set_data_mode($mode)
Wrapper for the bcm2835_spi_setDataMode function.
spi_chip_select
$obj->spi_chip_select($chipselect)
Wrapper for the bcm2835_spi_chipSelect function.
spi_set_chip_select_polarity
$obj->spi_set_chip_select_polarity($chipselect, $active)
Wrapper for the bcm2835_spi_setChipSelectPolarity function.
spi_transfer_byte
$obj->spi_transfer_byte($value)
Wrapper for the bcm2835_spi_transfer function.
spi_transfer_buffer
my $outbuffer = $obj->spi_transfer_buffer($inbuffer)
Wrapper for the bcm2835_spi_transfernb function.
get_pad_control
my $ct = $obj->get_pad_control($padgroup)
Wrapper for the bcm2835_gpio_pad function.
set_pad_control
$obj->set_pad_control($padgroup, $control)
Wrapper for the bcm2835_gpio_set_pad function.
prepare_UART0
$obj->prepare_UART0()
Set up standard pins used for UART0
prepare_UART0_RTS
$obj->prepare_UART0_RTS()
Set up standard pins used for UART0 with RTS
prepare_UART1
$obj->prepare_UART1()
Set up standard pins used for UART1
prepare_UART1_RTS
$obj->prepare_UART1_RTS()
Set up standard pins used for UART1 with RTS
prepare_I2C0
$obj->prepare_I2C0()
Set up standard pins used for I2C0
LICENSE
This work is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version.
License Note
I would normally release any Perl code under the Perl Artistic License but this module wraps several GPL / LGPL C libraries and I feel that the licensing of the entire distribution is simpler if the Perl code is under GPL too.
AUTHOR
Mark Dootson, <mdootson at cpan.org>
COPYRIGHT
Copyright (C) 2012-2013 Mark Dootson, all rights reserved.