line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
######################################################################################### |
2
|
|
|
|
|
|
|
# Package HiPi::Interface::MCP23017 |
3
|
|
|
|
|
|
|
# Description: Control MCP23017 Port Extender via I2C |
4
|
|
|
|
|
|
|
# Copyright : Copyright (c) 2013-2017 Mark Dootson |
5
|
|
|
|
|
|
|
# License : This is free software; you can redistribute it and/or modify it under |
6
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
7
|
|
|
|
|
|
|
######################################################################################### |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package HiPi::Interface::MCP23017; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
######################################################################################### |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
1172
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
14
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
15
|
1
|
|
|
1
|
|
6
|
use parent qw( HiPi::Interface::Common::MCP23X17 ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
16
|
1
|
|
|
1
|
|
57
|
use HiPi qw( :rpi :mcp23x17 ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
494
|
|
17
|
1
|
|
|
1
|
|
8
|
use HiPi::RaspberryPi; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
18
|
1
|
|
|
1
|
|
44
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
544
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION ='0.81'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# compatibility |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT = (); |
25
|
|
|
|
|
|
|
our @EXPORT_OK = (); |
26
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => \@EXPORT_OK ); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# legacy compat exports |
29
|
|
|
|
|
|
|
{ |
30
|
|
|
|
|
|
|
my @const = qw( |
31
|
|
|
|
|
|
|
MCP23S17_A0 MCP23S17_A1 MCP23S17_A2 MCP23S17_A3 MCP23S17_A4 MCP23S17_A5 MCP23S17_A6 MCP23S17_A7 |
32
|
|
|
|
|
|
|
MCP23S17_B0 MCP23S17_B1 MCP23S17_B2 MCP23S17_B3 MCP23S17_B4 MCP23S17_B5 MCP23S17_B6 MCP23S17_B7 |
33
|
|
|
|
|
|
|
MCP23S17_BANK MCP23S17_MIRROR MCP23S17_SEQOP MCP23S17_DISSLW MCP23S17_HAEN MCP23S17_ODR MCP23S17_INTPOL |
34
|
|
|
|
|
|
|
MCP23S17_INPUT MCP23S17_OUTPUT MCP23S17_HIGH MCP23S17_LOW |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
MCP23017_A0 MCP23017_A1 MCP23017_A2 MCP23017_A3 MCP23017_A4 MCP23017_A5 MCP23017_A6 MCP23017_A7 |
37
|
|
|
|
|
|
|
MCP23017_B0 MCP23017_B1 MCP23017_B2 MCP23017_B3 MCP23017_B4 MCP23017_B5 MCP23017_B6 MCP23017_B7 |
38
|
|
|
|
|
|
|
MCP23017_BANK MCP23017_MIRROR MCP23017_SEQOP MCP23017_DISSLW MCP23017_HAEN MCP23017_ODR MCP23017_INTPOL |
39
|
|
|
|
|
|
|
MCP23017_INPUT MCP23017_OUTPUT MCP23017_HIGH MCP23017_LOW |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my @constpins = qw( |
43
|
|
|
|
|
|
|
MCP_PIN_A0 MCP_PIN_A1 MCP_PIN_A2 MCP_PIN_A3 MCP_PIN_A4 MCP_PIN_A5 MCP_PIN_A6 MCP_PIN_A7 |
44
|
|
|
|
|
|
|
MCP_PIN_B0 MCP_PIN_B1 MCP_PIN_B2 MCP_PIN_B3 MCP_PIN_B4 MCP_PIN_B5 MCP_PIN_B6 MCP_PIN_B7 |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
push( @EXPORT_OK, @const, @constpins ); |
48
|
|
|
|
|
|
|
$EXPORT_TAGS{mcp23017} = \@const; |
49
|
|
|
|
|
|
|
$EXPORT_TAGS{mcp23S17} = \@const; |
50
|
|
|
|
|
|
|
$EXPORT_TAGS{mcppin} = \@constpins; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub new { |
54
|
0
|
|
|
0
|
0
|
|
my ($class, %userparams) = @_; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $pi = HiPi::RaspberryPi->new(); |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
my %params = ( |
59
|
|
|
|
|
|
|
devicename => ( $pi->board_type == RPI_BOARD_TYPE_1 ) ? '/dev/i2c-0' : '/dev/i2c-1', |
60
|
|
|
|
|
|
|
address => 0x20, |
61
|
|
|
|
|
|
|
device => undef, |
62
|
|
|
|
|
|
|
backend => 'smbus', |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# get user params |
66
|
0
|
|
|
|
|
|
foreach my $key( keys (%userparams) ) { |
67
|
0
|
|
|
|
|
|
$params{$key} = $userparams{$key}; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
unless( defined($params{device}) ) { |
71
|
0
|
0
|
|
|
|
|
if ( $params{backend} eq 'bcm2835' ) { |
72
|
0
|
|
|
|
|
|
require HiPi::BCM2835::I2C; |
73
|
|
|
|
|
|
|
$params{device} = HiPi::BCM2835::I2C->new( |
74
|
|
|
|
|
|
|
address => $params{address}, |
75
|
0
|
0
|
|
|
|
|
peripheral => ( $params{devicename} eq '/dev/i2c-0' ) ? HiPi::BCM2835::I2C::BB_I2C_PERI_0() : HiPi::BCM2835::I2C::BB_I2C_PERI_1(), |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
} else { |
78
|
0
|
|
|
|
|
|
require HiPi::Device::I2C; |
79
|
|
|
|
|
|
|
$params{device} = HiPi::Device::I2C->new( |
80
|
|
|
|
|
|
|
devicename => $params{devicename}, |
81
|
|
|
|
|
|
|
address => $params{address}, |
82
|
|
|
|
|
|
|
busmode => $params{backend}, |
83
|
0
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(%params); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# get current register address config so correct settings are loaded |
90
|
0
|
|
|
|
|
|
$self->read_register_bytes('IOCON'); |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return $self; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub do_write_register_bytes { |
96
|
0
|
|
|
0
|
0
|
|
my($self, $regaddress, @bytes) = @_; |
97
|
0
|
|
|
|
|
|
my $rval = $self->device->bus_write($regaddress, @bytes); |
98
|
0
|
|
|
|
|
|
return $rval; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub do_read_register_bytes { |
102
|
0
|
|
|
0
|
0
|
|
my($self, $regaddress, $numbytes) = @_; |
103
|
0
|
|
|
|
|
|
my @vals = $self->device->bus_read($regaddress, $numbytes); |
104
|
0
|
|
|
|
|
|
return @vals; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |