line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
14330
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
2
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
100
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Device::I2C; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# PODNAME: Device::I2C |
7
|
|
|
|
|
|
|
# ABSTRACT: Control and read hardware devices with i2c(SMBus) |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# This file is part of Device-I2C |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# This software is copyright (c) 2016 by Slava Volkov. |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
14
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
our $VERSION = '0.06'; # VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our @ISA = qw(IO::Handle); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Dependencies |
21
|
1
|
|
|
1
|
|
12
|
use v5.10.1; |
|
1
|
|
|
|
|
3
|
|
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
3
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
68
|
|
24
|
1
|
|
|
1
|
|
418
|
use IO::File; |
|
1
|
|
|
|
|
6502
|
|
|
1
|
|
|
|
|
88
|
|
25
|
1
|
|
|
1
|
|
8
|
use Fcntl; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
344
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
require XSLoader; |
28
|
|
|
|
|
|
|
XSLoader::load( 'Device::I2C', $VERSION ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
1
|
|
5
|
use constant I2C_SLAVE => 0x0703; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
73
|
|
32
|
1
|
|
|
1
|
|
3
|
use constant I2C_SLAVE_FORCE => 0x0706; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
630
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub new { |
35
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
36
|
0
|
0
|
0
|
|
|
|
@_ >= 0 && @_ <= 2 |
37
|
|
|
|
|
|
|
or croak "usage: $class->new(DEVICENAME [,MODE])"; |
38
|
0
|
|
|
|
|
|
my $fh = IO::File->new(@_); |
39
|
0
|
0
|
|
|
|
|
if ( !$fh ) { |
40
|
0
|
|
|
|
|
|
croak "Unable to open I2C Device File at $_[0]"; |
41
|
0
|
|
|
|
|
|
return undef; |
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
|
|
|
bless( $fh, $class ); |
44
|
0
|
|
|
|
|
|
$fh; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub fileError { |
49
|
0
|
|
|
0
|
1
|
|
my ($fh) = @_; |
50
|
0
|
|
|
|
|
|
return $fh->error(); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub checkDevice { |
55
|
0
|
|
|
0
|
1
|
|
my ( $fh, $addr ) = @_; |
56
|
0
|
|
|
|
|
|
my $retval = Device::I2C::_checkDevice( $fh->fileno(), $addr ); |
57
|
0
|
|
|
|
|
|
return $retval; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub selectDevice { |
62
|
0
|
|
|
0
|
1
|
|
my ( $fh, $addr ) = @_; |
63
|
0
|
0
|
|
|
|
|
if ( $fh->ioctl( I2C_SLAVE_FORCE, $addr ) < 0 ) { |
64
|
0
|
|
|
|
|
|
printf( "Device 0x%x not found\n", $addr ); |
65
|
0
|
|
|
|
|
|
exit(1); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub writeQuick { |
71
|
0
|
|
|
0
|
1
|
|
my ( $fh, $value ) = @_; |
72
|
0
|
|
|
|
|
|
my $retval = Device::I2C::_writeQuick( $fh->fileno(), $value ); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub readByte { |
77
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
78
|
0
|
|
|
|
|
|
my $retval = Device::I2C::_readByte( $self->fileno() ); |
79
|
0
|
|
|
|
|
|
return $retval; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub writeByte { |
84
|
0
|
|
|
0
|
1
|
|
my ( $self, $value ) = @_; |
85
|
0
|
|
|
|
|
|
my $retval = Device::I2C::_writeByte( $self->fileno(), $value ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub readByteData { |
90
|
0
|
|
|
0
|
1
|
|
my ( $self, $register_address ) = @_; |
91
|
0
|
|
|
|
|
|
my $retval = |
92
|
|
|
|
|
|
|
Device::I2C::_readByteData( $self->fileno(), $register_address ); |
93
|
0
|
|
|
|
|
|
return $retval; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub writeByteData { |
98
|
0
|
|
|
0
|
1
|
|
my ( $self, $register_address, $value ) = @_; |
99
|
0
|
|
|
|
|
|
my $retval = |
100
|
|
|
|
|
|
|
Device::I2C::_writeByteData( $self->fileno(), $register_address, $value ); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub readNBytes { |
105
|
0
|
|
|
0
|
1
|
|
my ( $self, $reg, $numBytes ) = @_; |
106
|
0
|
|
|
|
|
|
my $retval = 0; |
107
|
|
|
|
|
|
|
$retval = ( $retval << 8 ) | $self->readByteData( $reg + $numBytes - $_ ) |
108
|
0
|
|
|
|
|
|
for ( 1 .. $numBytes ); |
109
|
0
|
|
|
|
|
|
return $retval; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub readWordData { |
114
|
0
|
|
|
0
|
1
|
|
my ( $self, $register_address ) = @_; |
115
|
0
|
|
|
|
|
|
my $retval = |
116
|
|
|
|
|
|
|
Device::I2C::_readWordData( $self->fileno(), $register_address ); |
117
|
0
|
|
|
|
|
|
return $retval; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub writeWordData { |
122
|
0
|
|
|
0
|
1
|
|
my ( $self, $register_address, $value ) = @_; |
123
|
0
|
|
|
|
|
|
my $retval = |
124
|
|
|
|
|
|
|
Device::I2C::_writeWordData( $self->fileno(), $register_address, $value ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub processCall { |
129
|
0
|
|
|
0
|
1
|
|
my ( $self, $register_address, $value ) = @_; |
130
|
0
|
|
|
|
|
|
my $retval = |
131
|
|
|
|
|
|
|
Device::I2C::_processCall( $self->fileno(), $register_address, $value ); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub writeBlockData { |
136
|
0
|
|
|
0
|
1
|
|
my ( $self, $register_address, $values ) = @_; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
my $value = pack "C*", @{$values}; |
|
0
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
my $retval = |
141
|
|
|
|
|
|
|
Device::I2C::_writeI2CBlockData( $self->fileno(), $register_address, |
142
|
|
|
|
|
|
|
$value ); |
143
|
0
|
|
|
|
|
|
return $retval; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub readBlockData { |
148
|
0
|
|
|
0
|
1
|
|
my ( $self, $register_address, $numBytes ) = @_; |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
my $read_val = '0' x ($numBytes); |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
my $retval = Device::I2C::_readI2CBlockData( $self->fileno(), |
153
|
|
|
|
|
|
|
$register_address, $read_val ); |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
my @result = unpack( "C*", $read_val ); |
156
|
0
|
|
|
|
|
|
return @result; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# Preloaded methods go here. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub DEMOLISH { |
163
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
164
|
0
|
0
|
|
|
|
|
$self->close() if defined( $self->fileno() ); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
1; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
__END__ |