blib/lib/RPi/SPI.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package RPi::SPI;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use WiringPi::API qw(:wiringPi);
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 our $VERSION = '3.1802';
10  
11 sub new {
12 my ($class, $channel, $speed) = @_;
13  
14 my $self = bless {}, $class;
15  
16 $self->_speed($speed);
17  
18 # A hashref channel selects full software bit-bang mode; an integer
19 # selects the hardware SPI engine (0/1 = hardware CE0/CE1, above 1 = a
20 # GPIO chip select we drive ourselves while the hardware CE line is
21 # held off with SPI_NO_CS where the platform supports it)
22  
23 if (ref $channel eq 'HASH'){
24 $self->_bitbang($channel);
25 }
26 else {
27 $self->_channel($channel);
28 }
29  
30 if ($self->_bitbang){
31 # Initialize the software SPI lines to their idle state: clock idle
32 # low (mode 0), chip select high (deasserted), MOSI an output and
33 # MISO an input
34  
35 my $bb = $self->_bitbang;
36  
37 pinMode($bb->{clk}, 1);
38 digitalWrite($bb->{clk}, 0);
39 pinMode($bb->{cs}, 1);
40 digitalWrite($bb->{cs}, 1);
41 pinMode($bb->{mosi}, 1);
42 pinMode($bb->{miso}, 0);
43 }
44 else {
45 # A GPIO chip select is an ordinary output we hold high until a
46 # transfer frames it low
47  
48 if ($self->_cs){
49 pinMode($self->_cs, 1);
50 digitalWrite($self->_cs, 1);
51 }
52  
53 if (wiringPiSPISetup($self->_channel, $self->_speed) < 0){
54 die "couldn't establish communication on the SPI bus\n";
55 }
56  
57 # Probe SPI_NO_CS support once. The Pi 5's RP1 SPI controller
58 # rejects this mode bit ("unsupported mode bits 40"), so where it's
59 # unavailable we drop back to plain GPIO framing: the hardware CE
60 # line still strobes, but that's harmless unless a second device
61 # shares CE0/CE1
62  
63 if ($self->_cs){
64 my $ok = eval {
65 spiNoCS($self->_channel, 1);
66 spiNoCS($self->_channel, 0);
67 1;
68 };
69  
70 $self->_spi_no_cs($ok ? 1 : 0);
71  
72 if (! $ok){
73 warn "RPi::SPI: SPI_NO_CS is unsupported on this platform; " .
74 "the GPIO chip select will run without isolating the " .
75 "hardware CE line\n";
76 }
77 }
78 }
79  
80 return $self;
81 }
82 sub rw {
83 my ($self, $buf, $len) = @_;
84  
85 if (! defined $buf || ref $buf ne 'ARRAY') {
86 croak "RPi::SPI rw() requires \$buf as an array reference\n";
87 }
88  
89 if (! defined $len || $len !~ /^\d+$/ || $len == 0) {
90 croak "RPi::SPI rw() requires \$len as a positive integer\n";
91 }
92  
93 if ($self->_bitbang){
94 my $bb = $self->_bitbang;
95  
96 @$buf = spiBitBang(
97 $bb->{clk},
98 $bb->{mosi},
99 $bb->{miso},
100 $bb->{cs},
101 $buf,
102 $len,
103 $bb->{mode},
104 $bb->{delay},
105 );
106 }
107 elsif ($self->_cs){
108 # GPIO chip select: where the platform supports it, hold the hardware
109 # CE line off with SPI_NO_CS so it never strobes a second device.
110 # Either way, frame the transfer with our own GPIO
111  
112 my $iso = $self->_spi_no_cs;
113  
114 spiNoCS($self->_channel, 1) if $iso;
115 digitalWrite($self->_cs, 0);
116 @$buf = spiDataRW($self->_channel, $buf, $len);
117 digitalWrite($self->_cs, 1);
118 spiNoCS($self->_channel, 0) if $iso;
119 }
120 else {
121 @$buf = spiDataRW($self->_channel, $buf, $len);
122 }
123  
124 return @$buf;
125 }
126 sub _bitbang {
127 my ($self, $bb) = @_;
128  
129 if (defined $bb){
130 for my $pin (qw(clk mosi miso cs)){
131 if (! defined $bb->{$pin} || $bb->{$pin} !~ /^-?\d+$/){
132 croak "RPi::SPI bit-bang mode requires an integer '$pin' " .
133 "GPIO pin\n";
134 }
135 }
136  
137 $bb->{mode} = 0 if ! defined $bb->{mode};
138 $bb->{delay} = 0 if ! defined $bb->{delay};
139  
140 $self->{bitbang} = $bb;
141 }
142  
143 return $self->{bitbang};
144 }
145 sub _channel {
146 my ($self, $chan) = @_;
147  
148 if (defined $chan){
149 if ($chan > 1){
150 $self->_cs($chan);
151 $self->{channel} = 0;
152 }
153 else {
154 $self->{channel} = $chan;
155 }
156 }
157  
158 return $self->{channel};
159 }
160 sub _cs {
161 my ($self, $cs) = @_;
162  
163 $self->{cs} = $cs if defined $cs;
164 return $self->{cs};
165 }
166 sub _speed {
167 my ($self, $speed) = @_;
168  
169 # An undefined speed means "use the default"; a defined speed must be a
170 # positive integer. Reject an explicit 0/negative/non-numeric rather than
171 # silently rewriting it to the 1MHz default (which hid caller mistakes)
172  
173 if (defined $speed) {
174 if ($speed !~ /^\d+$/ || $speed == 0) {
175 croak "RPi::SPI speed must be a positive integer (Hz)\n";
176 }
177 $self->{speed} = $speed;
178 }
179  
180 return $self->{speed} || 1000000;
181 }
182 sub _spi_no_cs {
183 my ($self, $state) = @_;
184 $self->{spi_no_cs} = $state if defined $state;
185 return $self->{spi_no_cs};
186 }
187 sub _vim{};
188  
189 1;
190 __END__
↑ Summary