| 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, 2020-2023 -- leonerd@leonerd.org.uk | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 3 | ▶ | use v5.26; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use Object::Pad 0.800; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | package Device::Chip::AD9833 0.05; | ||||||||||||||
| 11 | class Device::Chip::AD9833 | ||||||||||||||
| 12 | 1 | ▶ | :isa(Device::Chip); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 3 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 3 | ▶ | use Data::Bitfield 0.02 qw( bitfield boolfield ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 3 | ▶ | use Future::AsyncAwait 0.38; # aync method | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 3 | ▶ | use constant PROTOCOL => "SPI"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | 3 | ▶ | use constant { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 3 | REG_CONFIG => 0x0000, | |||||||||||||
| 23 | REG_FREQ0 => 0x4000, | ||||||||||||||
| 24 | REG_FREQ1 => 0x8000, | ||||||||||||||
| 25 | REG_PHASE0 => 0xC000, | ||||||||||||||
| 26 | REG_PHASE1 => 0xE000, | ||||||||||||||
| 27 | 3 | }; | |||||||||||||
| 28 | |||||||||||||||
| 29 | =head1 NAME | ||||||||||||||
| 30 | |||||||||||||||
| 31 | C<Device::Chip::AD9833> - chip driver for F<AD9833> | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 SYNOPSIS | ||||||||||||||
| 34 | |||||||||||||||
| 35 | use Device::Chip::AD9833; | ||||||||||||||
| 36 | use Future::AsyncAwait; | ||||||||||||||
| 37 | |||||||||||||||
| 38 | my $chip = Device::Chip::AD9833->new; | ||||||||||||||
| 39 | await $chip->mount( Device::Chip::Adapter::...->new ); | ||||||||||||||
| 40 | |||||||||||||||
| 41 | await $chip->init; | ||||||||||||||
| 42 | |||||||||||||||
| 43 | my $freq = 440; # in Hz | ||||||||||||||
| 44 | await $chip->write_FREQ0( ( $freq << 28 ) / 25E6 ); # presuming 25MHz reference | ||||||||||||||
| 45 | |||||||||||||||
| 46 | =head1 DESCRIPTION | ||||||||||||||
| 47 | |||||||||||||||
| 48 | This L<Device::Chip> subclass provides specific communication to an | ||||||||||||||
| 49 | F<Analog Devices> F<AD9833> attached to a computer via an SPI adapter. | ||||||||||||||
| 50 | |||||||||||||||
| 51 | The reader is presumed to be familiar with the general operation of this chip; | ||||||||||||||
| 52 | the documentation here will not attempt to explain or define chip-specific | ||||||||||||||
| 53 | concepts or features, only the use of this module to access them. | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =cut | ||||||||||||||
| 56 | |||||||||||||||
| 57 | sub SPI_options | ||||||||||||||
| 58 | 2 | ▶ | { | ||||||||||||
|
Subroutine
SPI_options: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 59 | 2 | return ( mode => 2 ); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | field $_config = 0; | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 7 | ▶ | async method _write ( $word ) | ||||||||||||
|
Subroutine
_write: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | 7 | { | |||||||||||||
| 66 | 7 | await $self->protocol->write( pack "S>", $word ) | |||||||||||||
| 67 | 3 | } | |||||||||||||
| 68 | |||||||||||||||
| 69 | =head1 METHODS | ||||||||||||||
| 70 | |||||||||||||||
| 71 | The following methods documented in an C<await> expression return L<Future> | ||||||||||||||
| 72 | instances. | ||||||||||||||
| 73 | |||||||||||||||
| 74 | =cut | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =head2 init | ||||||||||||||
| 77 | |||||||||||||||
| 78 | await $chip->init; | ||||||||||||||
| 79 | |||||||||||||||
| 80 | Resets the chip to a working configuration, including setting the C<B28> bit | ||||||||||||||
| 81 | appropriately for the way this module writes the frequency registers. | ||||||||||||||
| 82 | |||||||||||||||
| 83 | This method must be called before setting the frequency using L</write_FREQ0> | ||||||||||||||
| 84 | or L</write_FREQ1>. | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =cut | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 1 | ▶ | async method init () | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 89 | 1 | { | |||||||||||||
| 90 | 1 | await $self->_write( REG_CONFIG | 0x2100 ); # RESET, B28=1 | |||||||||||||
| 91 | 1 | await $self->_write( REG_CONFIG | 0x2000 ); # unreset, B28=1 | |||||||||||||
| 92 | 3 | } | |||||||||||||
| 93 | |||||||||||||||
| 94 | 3 | bitfield { format => "integer" }, CONFIG => | |||||||||||||
| 95 | B28 => boolfield( 13 ), | ||||||||||||||
| 96 | HLB => boolfield( 12 ), | ||||||||||||||
| 97 | FSELECT => boolfield( 11 ), | ||||||||||||||
| 98 | PSELECT => boolfield( 10 ), | ||||||||||||||
| 99 | SLEEP1 => boolfield( 7 ), | ||||||||||||||
| 100 | SLEEP12 => boolfield( 6 ), | ||||||||||||||
| 101 | OPBITEN => boolfield( 5 ), | ||||||||||||||
| 102 | DIV2 => boolfield( 3 ), | ||||||||||||||
| 103 | MODE => boolfield( 1 ); | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =head2 read_config | ||||||||||||||
| 106 | |||||||||||||||
| 107 | $config = await $chip->read_config; | ||||||||||||||
| 108 | |||||||||||||||
| 109 | Returns a C<HASH> reference containing the current chip configuration. Note | ||||||||||||||
| 110 | that since the chip does not support querying the configuration, this is just | ||||||||||||||
| 111 | an in-memory copy maintained by the object instance, updated by calls to | ||||||||||||||
| 112 | L</change_config>. | ||||||||||||||
| 113 | |||||||||||||||
| 114 | The hash will contain the following named fields, all booleans. | ||||||||||||||
| 115 | |||||||||||||||
| 116 | B28 | ||||||||||||||
| 117 | HLB | ||||||||||||||
| 118 | FSELECT | ||||||||||||||
| 119 | PSELECT | ||||||||||||||
| 120 | SLEEP1 | ||||||||||||||
| 121 | SLEEP12 | ||||||||||||||
| 122 | OPBITEN | ||||||||||||||
| 123 | DIV2 | ||||||||||||||
| 124 | MODE | ||||||||||||||
| 125 | |||||||||||||||
| 126 | In addition, a new value C<wave> will be created combining the current | ||||||||||||||
| 127 | settings of C<MODE>, C<OPBITEN> and C<DIV2> to explain the waveform generated | ||||||||||||||
| 128 | |||||||||||||||
| 129 | wave => "sine" | "triangle" | "square" | "square/2" | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =cut | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 1 | ▶ | async method read_config () | ||||||||||||
|
Subroutine
read_config: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||
| 134 | 1 | { | |||||||||||||
| 135 | 1 | my %config = unpack_CONFIG( $_config ); | |||||||||||||
| 136 | |||||||||||||||
| 137 | 1 | my $wave; | |||||||||||||
| 138 | 1 | ▶ | if( $config{OPBITEN} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 139 | 0 | ▶ | $wave = $config{DIV2} ? "square" : "square/2"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | } | ||||||||||||||
| 141 | elsif( $config{MODE} ) { | ||||||||||||||
| 142 | 0 | $wave = "triangle"; | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | else { | ||||||||||||||
| 145 | 1 | $wave = "sine"; | |||||||||||||
| 146 | } | ||||||||||||||
| 147 | 1 | $config{wave} = $wave; | |||||||||||||
| 148 | |||||||||||||||
| 149 | 1 | return \%config; | |||||||||||||
| 150 | 3 | } | |||||||||||||
| 151 | |||||||||||||||
| 152 | =head2 change_config | ||||||||||||||
| 153 | |||||||||||||||
| 154 | await $chip->change_config( %changes ); | ||||||||||||||
| 155 | |||||||||||||||
| 156 | Writes updates to the chip configuration. Takes named arguments of the same | ||||||||||||||
| 157 | form as returned by L</read_config>, including the synthesized C<wave> | ||||||||||||||
| 158 | setting. | ||||||||||||||
| 159 | |||||||||||||||
| 160 | =cut | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 2 | ▶ | async method change_config ( %changes ) | ||||||||||||
|
Subroutine
change_config: called CC=6 SCAR=22.0
Pod
documented
| |||||||||||||||
| 163 | 2 | { | |||||||||||||
| 164 | 2 | ▶ | if( defined( my $wave = delete $changes{wave} ) ) {{ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | 1 | ▶ | $changes{OPBITEN} = 1, $changes{MODE} = 0, $changes{DIV2} = 1, last if $wave eq "square"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | 0 | ▶ | $changes{OPBITEN} = 1, $changes{MODE} = 0, $changes{DIV2} = 0, last if $wave eq "square/2"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | 0 | ▶ | $changes{OPBITEN} = 0, $changes{MODE} = 1, last if $wave eq "triangle"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | 0 | ▶ | $changes{OPBITEN} = 0, $changes{MODE} = 0, last if $wave eq "sine"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | 0 | croak "Unrecognised value for 'wave' configuration - $wave"; | |||||||||||||
| 170 | }} | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 2 | my %config = ( unpack_CONFIG( $_config ), %changes ); | |||||||||||||
| 173 | |||||||||||||||
| 174 | 2 | $config{B28} = 1; | |||||||||||||
| 175 | |||||||||||||||
| 176 | 2 | await $self->_write( REG_CONFIG | ( $_config = pack_CONFIG( %config ) ) ); | |||||||||||||
| 177 | 3 | } | |||||||||||||
| 178 | |||||||||||||||
| 179 | =head2 write_FREQ0 | ||||||||||||||
| 180 | |||||||||||||||
| 181 | =head2 write_FREQ1 | ||||||||||||||
| 182 | |||||||||||||||
| 183 | await $chip->write_FREQ0( $freq ); | ||||||||||||||
| 184 | await $chip->write_FREQ1( $freq ); | ||||||||||||||
| 185 | |||||||||||||||
| 186 | Writes the C<FREQ0> or C<FREQ1> frequency control register. C<$freq> should | ||||||||||||||
| 187 | be a 28bit integer value. | ||||||||||||||
| 188 | |||||||||||||||
| 189 | =cut | ||||||||||||||
| 190 | |||||||||||||||
| 191 | 1 | ▶ | async method write_FREQ0 ( $freq ) | ||||||||||||
|
Subroutine
write_FREQ0: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 192 | 1 | { | |||||||||||||
| 193 | 1 | await $self->_write( REG_FREQ0 | ( $freq & 0x3FFF ) ); | |||||||||||||
| 194 | 1 | await $self->_write( REG_FREQ0 | ( $freq >> 14 ) ); | |||||||||||||
| 195 | 3 | } | |||||||||||||
| 196 | |||||||||||||||
| 197 | 0 | ▶ | async method write_FREQ1 ( $freq ) | ||||||||||||
|
Subroutine
write_FREQ1: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 198 | 0 | { | |||||||||||||
| 199 | 0 | await $self->_write( REG_FREQ1 | ( $freq & 0x3FFF ) ); | |||||||||||||
| 200 | 0 | await $self->_write( REG_FREQ1 | ( $freq >> 14 ) ); | |||||||||||||
| 201 | 3 | } | |||||||||||||
| 202 | |||||||||||||||
| 203 | =head2 write_PHASE0 | ||||||||||||||
| 204 | |||||||||||||||
| 205 | =head2 write_PHASE1 | ||||||||||||||
| 206 | |||||||||||||||
| 207 | await $chip->write_PHASE0( $phase ); | ||||||||||||||
| 208 | await $chip->write_PHASE1( $phase ); | ||||||||||||||
| 209 | |||||||||||||||
| 210 | Writes the C<PHASE0> or C<PHASE1> phase control register. C<$phase> should | ||||||||||||||
| 211 | be a 12bit integer value. | ||||||||||||||
| 212 | |||||||||||||||
| 213 | =cut | ||||||||||||||
| 214 | |||||||||||||||
| 215 | 1 | ▶ | async method write_PHASE0 ( $phase ) | ||||||||||||
|
Subroutine
write_PHASE0: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 216 | 1 | { | |||||||||||||
| 217 | 1 | await $self->_write( REG_PHASE0 | $phase ); | |||||||||||||
| 218 | 3 | } | |||||||||||||
| 219 | |||||||||||||||
| 220 | 0 | ▶ | async method write_PHASE1 ( $phase ) | ||||||||||||
|
Subroutine
write_PHASE1: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 221 | 0 | { | |||||||||||||
| 222 | 0 | await $self->_write( REG_PHASE1 | $phase ); | |||||||||||||
| 223 | 3 | } | |||||||||||||
| 224 | |||||||||||||||
| 225 | =head1 AUTHOR | ||||||||||||||
| 226 | |||||||||||||||
| 227 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||
| 228 | |||||||||||||||
| 229 | =cut | ||||||||||||||
| 230 | |||||||||||||||
| 231 | 3 | 0x55AA; | |||||||||||||