| 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 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Object::Pad 0.800; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | package Device::Chip::ProtocolBase::SPI 0.27; | ||||||||||
| 11 | role Device::Chip::ProtocolBase::SPI :compat(invokable); | ||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use Future::AsyncAwait; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | C<Device::Chip::ProtocolBase::SPI> - a role for implementing SPI protocols | ||||||||||
| 18 | |||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||
| 20 | |||||||||||
| 21 | =for highlighter language=perl | ||||||||||
| 22 | |||||||||||
| 23 | This role (or abstract base class) provides some convenient wrapper methods | ||||||||||
| 24 | for providing higher-level SPI protocol implementations, by using simpler | ||||||||||
| 25 | lower-level ones. It can be used by implementation classes to help provide | ||||||||||
| 26 | parts of the API. | ||||||||||
| 27 | |||||||||||
| 28 | =cut | ||||||||||
| 29 | |||||||||||
| 30 | =head1 WRAPPER METHODS | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | =head2 write | ||||||||||
| 35 | |||||||||||
| 36 | await $protocol->write( $words ); | ||||||||||
| 37 | |||||||||||
| 38 | A wrapper for L</readwrite> that ignores the result. | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | async method write ( $words ) | ||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 1 | { | |||||||||
| 44 | 1 | await $self->readwrite( $words ); | |||||||||
| 45 | 1 | return; | |||||||||
| 46 | 2 | } | |||||||||
| 47 | |||||||||||
| 48 | =head2 read | ||||||||||
| 49 | |||||||||||
| 50 | $words = await $protocol->read( $len ); | ||||||||||
| 51 | |||||||||||
| 52 | A wrapper for L</readwrite> that sends unspecified data which the chip will | ||||||||||
| 53 | ignore, returning the result. | ||||||||||
| 54 | |||||||||||
| 55 | This implementation currently sends all-bits-low. | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | 1 | ▶ | async method read ( $len ) | ||||||||
|
Subroutine
read: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 60 | 1 | { | |||||||||
| 61 | 1 | return await $self->readwrite( "\x00" x $len ); | |||||||||
| 62 | 2 | } | |||||||||
| 63 | |||||||||||
| 64 | =head2 write_no_ss | ||||||||||
| 65 | |||||||||||
| 66 | await $protocol->write_no_ss( $words ); | ||||||||||
| 67 | |||||||||||
| 68 | A wrapper for L</readwrite_no_ss> that ignores the result. | ||||||||||
| 69 | |||||||||||
| 70 | =cut | ||||||||||
| 71 | |||||||||||
| 72 | 2 | ▶ | async method write_no_ss ( $words ) | ||||||||
|
Subroutine
write_no_ss: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 2 | { | |||||||||
| 74 | 2 | await $self->readwrite_no_ss( $words ); | |||||||||
| 75 | return | ||||||||||
| 76 | 2 | } | |||||||||
| 77 | |||||||||||
| 78 | =head2 read_no_ss | ||||||||||
| 79 | |||||||||||
| 80 | $words = await $protocol->read_no_ss( $len ); | ||||||||||
| 81 | |||||||||||
| 82 | A wrapper for L</readwrite_no_ss> that sends unspecified data which the chip | ||||||||||
| 83 | will ignore, returning the result. | ||||||||||
| 84 | |||||||||||
| 85 | This implemention currenetly sends all-bits-low. | ||||||||||
| 86 | |||||||||||
| 87 | =cut | ||||||||||
| 88 | |||||||||||
| 89 | 2 | ▶ | async method read_no_ss ( $len ) | ||||||||
|
Subroutine
read_no_ss: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 90 | 2 | { | |||||||||
| 91 | 2 | return await $self->readwrite_no_ss( "\x00" x $len ); | |||||||||
| 92 | 2 | } | |||||||||
| 93 | |||||||||||
| 94 | =head2 readwrite | ||||||||||
| 95 | |||||||||||
| 96 | $words_in = await $protocol->readwrite( $words_out ); | ||||||||||
| 97 | |||||||||||
| 98 | A wrapper for performing a complete SPI transfer, using L</assert_ss>, | ||||||||||
| 99 | L</readwrite_no_ss>, L</release_ss>. | ||||||||||
| 100 | |||||||||||
| 101 | =cut | ||||||||||
| 102 | |||||||||||
| 103 | # We can't use try/finally because you can't await inside finally blocks | ||||||||||
| 104 | # We can't defer {} for the same reason | ||||||||||
| 105 | # We'll use some eval {} hackery here | ||||||||||
| 106 | |||||||||||
| 107 | 4 | ▶ | async method readwrite ( $words_out ) | ||||||||
|
Subroutine
readwrite: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 108 | 4 | { | |||||||||
| 109 | 4 | my $words_in; | |||||||||
| 110 | 4 | ▶ | my ( $ok, $e ) = eval { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 4 | await $self->assert_ss; | |||||||||
| 112 | 4 | $words_in = await $self->readwrite_no_ss( $words_out ); | |||||||||
| 113 | 3 | 1; | |||||||||
| 114 | } ? ( 1 ) : ( 0, $@ ); | ||||||||||
| 115 | 4 | await $self->release_ss; | |||||||||
| 116 | 4 | ▶ | $ok ? return $words_in : die $e; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 2 | } | |||||||||
| 118 | |||||||||||
| 119 | =head2 write_then_read | ||||||||||
| 120 | |||||||||||
| 121 | $words_in = await $protocol->write_then_read( $words_out, $len_in ); | ||||||||||
| 122 | |||||||||||
| 123 | A wrapper for performing a complete SPI transfer in two phases, using | ||||||||||
| 124 | L</assert_ss>, L</write_no_ss>, L</read_no_ss> and L</release_ss>. | ||||||||||
| 125 | |||||||||||
| 126 | =cut | ||||||||||
| 127 | |||||||||||
| 128 | 2 | ▶ | async method write_then_read ( $words_out, $len_in ) | ||||||||
|
Subroutine
write_then_read: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 129 | 2 | { | |||||||||
| 130 | 2 | my $words_in; | |||||||||
| 131 | 2 | ▶ | my ( $ok, $e ) = eval { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 2 | await $self->assert_ss; | |||||||||
| 133 | 2 | await $self->write_no_ss( $words_out ); | |||||||||
| 134 | 2 | $words_in = await $self->read_no_ss( $len_in ); | |||||||||
| 135 | 1 | 1; | |||||||||
| 136 | } ? ( 1 ) : ( 0, $@ ); | ||||||||||
| 137 | 2 | await $self->release_ss; | |||||||||
| 138 | 2 | ▶ | $ok ? return $words_in : die $e; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 2 | } | |||||||||
| 140 | |||||||||||
| 141 | =head1 AUTHOR | ||||||||||
| 142 | |||||||||||
| 143 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 144 | |||||||||||
| 145 | =cut | ||||||||||
| 146 | |||||||||||
| 147 | 2 | 0x55AA; | |||||||||