File Coverage

blib/lib/Device/Chip/ProtocolBase/SPI.pm
Criterion Covered Total %
statement 48 48 100.0
branch n/a
condition n/a
subroutine 14 14 100.0
pod 6 6 100.0
total 68 68 100.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2018-2023 -- leonerd@leonerd.org.uk
5              
6 2     3   2187 use v5.26;
  2         10  
7 2     2   15 use warnings;
  2         5  
  2         152  
8 2     2   44 use Object::Pad 0.800;
  2         18  
  2         108  
9              
10             package Device::Chip::ProtocolBase::SPI 0.26;
11             role Device::Chip::ProtocolBase::SPI :compat(invokable);
12              
13             =head1 NAME
14              
15             C - a role for implementing SPI protocols
16              
17             =head1 DESCRIPTION
18              
19             This role (or abstract base class) provides some convenient wrapper methods
20             for providing higher-level SPI protocol implementations, by using simpler
21             lower-level ones. It can be used by implementation classes to help provide
22             parts of the API.
23              
24             =cut
25              
26             =head1 WRAPPER METHODS
27              
28             =cut
29              
30             =head2 write
31              
32             await $protocol->write( $words );
33              
34             A wrapper for L that ignores the result.
35              
36             =cut
37              
38 1     1 1 95 method write ( $words )
  1         5  
  1         4  
  1         3  
39             {
40 1         4 $self->readwrite( $words )->then_done();
41             }
42              
43             =head2 read
44              
45             $words = await $protocol->read( $len );
46              
47             A wrapper for L that sends unspecified data which the chip will
48             ignore, returning the result.
49              
50             This implementation currently sends all-bits-low.
51              
52             =cut
53              
54 1     1 1 72 method read ( $len )
  1         5  
  1         2  
  1         3  
55             {
56 1         8 $self->readwrite( "\x00" x $len );
57             }
58              
59             =head2 write_no_ss
60              
61             A wrapper for L that ignores the result.
62              
63             =cut
64              
65 1     2 1 3 method write_no_ss ( $words )
  1         5  
  1         3  
  1         2  
66             {
67 1         8 $self->readwrite_no_ss( $words )->then_done();
68             }
69              
70             =head2 read_no_ss
71              
72             A wrapper for L that sends unspecified data which the chip
73             will ignore, returning the result.
74              
75             This implemention currenetly sends all-bits-low.
76              
77             =cut
78              
79 1     2 1 3 method read_no_ss ( $len )
  1         6  
  1         4  
  1         2  
80             {
81 1         9 $self->readwrite_no_ss( "\x00" x $len );
82             }
83              
84             =head2 readwrite
85              
86             $words_in = await $protocol->readwrite( $words_out );
87              
88             A wrapper for performing a complete SPI transfer, using L,
89             L, L.
90              
91             =cut
92              
93 3     4 1 93 method readwrite ( $words_out )
  3         9  
  3         34  
  3         8  
94             {
95             $self->assert_ss
96 3     6   14002 ->then( sub { $self->readwrite_no_ss( $words_out ) } )
97             ->followed_by( sub {
98 3     3   5503 my ( $f ) = @_;
99 3         32 $self->release_ss->then( sub { $f } );
  3         5547  
100 3         24 });
101             }
102              
103             =head2 write_then_read
104              
105             $words_in = await $protocol->write_then_read( $words_out, $len_in )
106              
107             A wrapper for performing a complete SPI transfer in two phases, using
108             L, L, L and L.
109              
110             =cut
111              
112 1     1 1 86 method write_then_read ( $words_out, $len_in )
  1         6  
  1         3  
  1         2  
  1         2  
113             {
114             $self->assert_ss
115 1     2   2044 ->then( sub { $self->write_no_ss( $words_out ) } )
116 1     1   1934 ->then( sub { $self->read_no_ss( $len_in ) } )
117             ->followed_by( sub {
118 1     1   1820 my ( $f ) = @_;
119 1         11 $self->release_ss->then( sub { $f } );
  1         1874  
120 1         7 });
121             }
122              
123             =head1 AUTHOR
124              
125             Paul Evans
126              
127             =cut
128              
129             0x55AA;