Skip to content

Commit

Permalink
Perl modules for working with GPIO
Browse files Browse the repository at this point in the history
  • Loading branch information
TJC committed May 17, 2012
1 parent e6aa9ea commit c8cf498
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
78 changes: 78 additions & 0 deletions perl/BeagleBone/Pin.pm
@@ -0,0 +1,78 @@
package BeagleBone::Pin;
use 5.14.1;
use warnings;
use Class::Accessor qw(antlers);
use Fcntl qw(:DEFAULT);
use autodie;

has 'desc' => (
is => 'ro',
isa => 'Str',
);

has 'mux' => (
is => 'ro',
isa => 'Str',
);

has 'gpio' => (
is => 'ro',
isa => 'Int',
);

has 'gpio_fh' => (
is => 'rw',
);

sub set_mode {
my ($self, $mode) = @_;
sysopen(my $fh, "/sys/kernel/debug/omap_mux/" . $self->mux, O_WRONLY);
syswrite($fh, "$mode", 1);
close($fh);
}

sub gpio_open {
my ($self, $direction) = @_;

if (not -d "/sys/class/gpio/gpio" . $self->gpio) {
sysopen(my $tmp, "/sys/class/gpio/export", O_WRONLY);
my $str = sprintf('%d', $self->gpio);
syswrite($tmp, $str, length($str));
close($tmp);
}
if (not -d "/sys/class/gpio/gpio" . $self->gpio) {
die "Could not export this pin (" . $self->name . ")\n";
}
sysopen(my $dir, "/sys/class/gpio/gpio" . $self->gpio . "/direction", O_WRONLY);
syswrite($dir, $direction, length($direction));
close($dir);

sysopen(my $fh, "/sys/class/gpio/gpio" . $self->gpio . "/value",
$direction eq 'out' ? O_WRONLY : O_RDONLY
);
$self->gpio_fh($fh);
return $fh;
}

sub digitalWrite {
my ($self, $value) = @_;
$self->gpio_open('out') unless (defined $self->gpio_fh);
syswrite($self->gpio_fh, "$value", 1);
}

sub digitalRead {
my ($self) = @_;
$self->gpio_open('in') unless (defined $self->gpio_fh);
my $buf;
sysread($self->gpio_fh, $buf, 1);
}

sub DESTROY {
my $self = shift;
if (defined $self->gpio_fh) {
close($self->gpio_fh);
$self->gpio_fh(undef);
}
}

1;
83 changes: 83 additions & 0 deletions perl/BeagleBone/Pins.pm
@@ -0,0 +1,83 @@
package BeagleBone::Pins;
use 5.14.1;
use warnings;
use YAML::Tiny;
use BeagleBone::Pin;

our $data;

sub import {
# $data = YAML::XS::Load(join('',<DATA>));
my $tmp = YAML::Tiny->read_string(join('',<DATA>));
$data = $tmp->[0];
}

sub new {
my ($class, $id) = @_;
die "Pin $id not yet defined\n" unless exists $data->{$id};
return BeagleBone::Pin->new(
$data->{$id}
);
}


1;
__DATA__
---
P9_1:
desc: GND
P9_2:
desc: GND
P9_3:
desc: DC_3V3
P9_4:
desc: DC_3V3
P9_5:
desc: VDD_5V
P9_6:
desc: VDD_5V
P9_7:
desc: SYS_5V
P9_8:
desc: SYS_5V
P9_9:
desc: PWR_BUT
P9_10:
desc: RESET_OUT
P9_11:
mux: gpmc_wait0
gpio: 30
P9_12:
mux: gpmc_ben1
gpio: 28
P9_13:
mux: gpmc_wpn
gpio: 31
P9_14:
mux: gpmc_a2
gpio: 18
P9_15:
mux: gpmc_a0
gpio: 16
P9_16:
mux: gpmc_a3
gpio: 19
P9_17:
mux: spi0_cs0
gpio: 5
P9_18:
mux: spi0_d1
gpio: 4
P9_19:
mux: uart1_rtsn
gpio: 13
P9_20:
mux: spi0_d0
gpio: 12
P9_21:
mux: spi0_d0
gpio: 3
P9_22:
mux: spi0_sclk
gpio: 2

0 comments on commit c8cf498

Please sign in to comment.