| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Device::I2C; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package PINE64::MCP23008; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.901'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | #global vars | ||||||||||||||||||||||||||||
| 12 | 1 | my ($i2cbus, $addr, $gpext); | |||||||||||||||||||||||||||
| 13 | 1 | my $gpregval = 0; #init gpio register value to 0 | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | my @pin_nums = (1,2,4,8,16,32,64,128); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub new{ | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 19 | 0 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #first arg is device address | ||||||||||||||||||||||||||||
| 22 | 0 | $addr = $_[0]; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | #second arg i2c bus; optional | ||||||||||||||||||||||||||||
| 25 | 0 | $i2cbus = $_[1]; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if($i2cbus eq ''){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | $i2cbus = '/dev/i2c-0'; | |||||||||||||||||||||||||||
| 29 | }#end if | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | $gpext = Device::I2C->new($i2cbus, "r+"); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | #init i2c device | ||||||||||||||||||||||||||||
| 34 | 0 | $gpext->checkDevice($addr); | |||||||||||||||||||||||||||
| 35 | 0 | $gpext->selectDevice($addr); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | #init gp register val to all off | ||||||||||||||||||||||||||||
| 38 | 0 | $gpregval = 0; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | return $self; | |||||||||||||||||||||||||||
| 41 | }#end new | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub set_direction{ | ||||||||||||||||||||||||||
|
Subroutine
set_direction: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | #sets value of the IO direction register | ||||||||||||||||||||||||||||
| 45 | #ie 255 makes all input; 0 makes all output | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | my $direction = $_[1]; | |||||||||||||||||||||||||||
| 48 | 0 | $gpext->writeByteData(0x00, $direction); | |||||||||||||||||||||||||||
| 49 | }#end set_direction | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub enable_pullup{ | ||||||||||||||||||||||||||
|
Subroutine
enable_pullup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | #when a pin is configured as an input | ||||||||||||||||||||||||||||
| 53 | #you can enable internal 100K pull-up | ||||||||||||||||||||||||||||
| 54 | #resistors by writing to the GPPU register | ||||||||||||||||||||||||||||
| 55 | #0-255 are valid values: 0 - all disabled | ||||||||||||||||||||||||||||
| 56 | #255 - all enabled | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my $en_gppu = $_[1]; | |||||||||||||||||||||||||||
| 59 | 0 | $gpext->writeByteData(0x06, $en_gppu); | |||||||||||||||||||||||||||
| 60 | }#end enable_pullup | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub set_polarity{ | ||||||||||||||||||||||||||
|
Subroutine
set_polarity: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | #sets the polarity of the gpio pins; | ||||||||||||||||||||||||||||
| 64 | #0 is normal polarity | ||||||||||||||||||||||||||||
| 65 | #255 is all pins reversed | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $io_pol = $_[1]; | |||||||||||||||||||||||||||
| 68 | 0 | $gpext->writeByteData(0x01, $io_pol); | |||||||||||||||||||||||||||
| 69 | }#end set_polarity | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub write_pin{ | ||||||||||||||||||||||||||
|
Subroutine
write_pin: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 0 | my $ind = $_[1]; | |||||||||||||||||||||||||||
| 73 | 0 | my $iox = $pin_nums[$ind]; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | #1 or 0 | ||||||||||||||||||||||||||||
| 76 | 0 | my $val = $_[2]; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | #check 0x09 register to see if | ||||||||||||||||||||||||||||
| 79 | #pin already high; if so, do nothing | ||||||||||||||||||||||||||||
| 80 | 0 | my $regval = $gpext->readByteData(0x09); | |||||||||||||||||||||||||||
| 81 | 0 | my $binout = sprintf("%08b", $regval); | |||||||||||||||||||||||||||
| 82 | 0 | my @pinvals = split(//, $binout); | |||||||||||||||||||||||||||
| 83 | 0 | @pinvals = reverse(@pinvals); | |||||||||||||||||||||||||||
| 84 | 0 | my $already_high = $pinvals[$ind]; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if($val == 1 && $already_high == 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$val == 1 and $already_high == 0
MC/DC0%0/2
$val == 1 and $already_high == 0
$val == 1
$already_high == 0
Truth table0%0/3
A and B
A= $val == 1B= $already_high == 0
| |||||||||||||||||||||||||||||
| 87 | 0 | $gpregval+=$iox; | |||||||||||||||||||||||||||
| 88 | }#end if | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if($val == 0 && $already_high == 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$val == 0 and $already_high == 1
MC/DC0%0/2
$val == 0 and $already_high == 1
$val == 0
$already_high == 1
Truth table0%0/3
A and B
A= $val == 0B= $already_high == 1
| |||||||||||||||||||||||||||||
| 90 | 0 | $gpregval-=$iox; | |||||||||||||||||||||||||||
| 91 | }#end if | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | $gpext->writeByteData(0x09, $gpregval); | |||||||||||||||||||||||||||
| 94 | }#end write_pin | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub read_pin{ | ||||||||||||||||||||||||||
|
Subroutine
read_pin: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 0 | my $ind = $_[1]; | |||||||||||||||||||||||||||
| 98 | 0 | my $iox = $pin_nums[$ind]; | |||||||||||||||||||||||||||
| 99 | 0 | my $pinval = 0; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | #read GPIO register | ||||||||||||||||||||||||||||
| 102 | 0 | my $regval = $gpext->readByteData(0x09); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #ensure 8 binary places are displayed | ||||||||||||||||||||||||||||
| 105 | 0 | my $binout = sprintf("%08b", $regval); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | #parse eight binary digits into an array | ||||||||||||||||||||||||||||
| 108 | 0 | my @pinvals = split(//, $binout); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | #reverse array to match pin #'s | ||||||||||||||||||||||||||||
| 111 | 0 | @pinvals = reverse(@pinvals); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | #value of pin is index of $pinvals | ||||||||||||||||||||||||||||
| 114 | 0 | $pinval = $pinvals[$ind]; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | return $pinval; | |||||||||||||||||||||||||||
| 117 | }#end read_pin | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub read_gpio_register{ | ||||||||||||||||||||||||||
|
Subroutine
read_gpio_register: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | #read GPIO register | ||||||||||||||||||||||||||||
| 121 | 0 | my $regval = $gpext->readByteData(0x09); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | return $regval; | |||||||||||||||||||||||||||
| 124 | }#end read_gpio_register | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | 1; | |||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||