| 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, 2017-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::AD5691R 0.16; | ||||||||||||
| 11 | class Device::Chip::AD5691R | ||||||||||||
| 12 | 1 | ▶ | :isa(Device::Chip); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 2 | ▶ | use Data::Bitfield qw( bitfield boolfield enumfield ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 2 | ▶ | use constant PROTOCOL => "I2C"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | =encoding UTF-8 | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 NAME | ||||||||||||
| 24 | |||||||||||||
| 25 | C<Device::Chip::AD5691R> - chip driver for F<AD5691R> | ||||||||||||
| 26 | |||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||
| 28 | |||||||||||||
| 29 | use Device::Chip::AD5691R; | ||||||||||||
| 30 | use Future::AsyncAwait; | ||||||||||||
| 31 | |||||||||||||
| 32 | my $chip = Device::Chip::AD5691R->new; | ||||||||||||
| 33 | await $chip->mount( Device::Chip::Adapter::...->new ); | ||||||||||||
| 34 | |||||||||||||
| 35 | my $voltage = 1.23; | ||||||||||||
| 36 | await $chip->write_dac( 4096 * $voltage / 2.5 ); | ||||||||||||
| 37 | print "Output is now set to 1.23V\n"; | ||||||||||||
| 38 | |||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||
| 40 | |||||||||||||
| 41 | This L<Device::Chip> subclass provides specific communications to an | ||||||||||||
| 42 | F<Analog Devices> F<AD5691R> attached to the computer via an I²C adapter. | ||||||||||||
| 43 | |||||||||||||
| 44 | The reader is presumed to be familiar with the general operation of this chip; | ||||||||||||
| 45 | the documentation here will not attempt to explain or define chip-specific | ||||||||||||
| 46 | concepts or features, only the use of this module to access them. | ||||||||||||
| 47 | |||||||||||||
| 48 | =cut | ||||||||||||
| 49 | |||||||||||||
| 50 | 2 | ▶ | use constant { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 51 | 2 | CMD_WRITE_INPUT => 0x10, | |||||||||||
| 52 | CMD_UPDATE_DAC => 0x20, | ||||||||||||
| 53 | CMD_WRITE_AND_UPDATE => 0x30, | ||||||||||||
| 54 | CMD_WRITE_CTRL => 0x40, | ||||||||||||
| 55 | 2 | }; | |||||||||||
| 56 | |||||||||||||
| 57 | =head1 MOUNT PARAMETERS | ||||||||||||
| 58 | |||||||||||||
| 59 | =head2 addr | ||||||||||||
| 60 | |||||||||||||
| 61 | The I²C address of the device. Can be specified in decimal, octal or hex with | ||||||||||||
| 62 | leading C<0> or C<0x> prefixes. | ||||||||||||
| 63 | |||||||||||||
| 64 | =cut | ||||||||||||
| 65 | |||||||||||||
| 66 | 1 | ▶ | sub I2C_options ( $, %params ) | ||||||||||
|
Subroutine
I2C_options: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||
| 67 | { | ||||||||||||
| 68 | 1 | ▶ | my $addr = delete $params{addr} // 0x4C; | ||||||||||
|
Condition50%1/2
delete $params{"addr"} // 76
MC/DC0%0/1
delete $params{"addr"} // 76
delete $params{"addr"}
Truth table50%1/2
A
A= delete $params{"addr"}
| |||||||||||||
| 69 | 1 | ▶ | $addr = oct $addr if $addr =~ m/^0/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | |||||||||||||
| 71 | return ( | ||||||||||||
| 72 | 1 | addr => $addr, | |||||||||||
| 73 | max_bitrate => 400E3, | ||||||||||||
| 74 | ); | ||||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | =head1 ACCESSORS | ||||||||||||
| 78 | |||||||||||||
| 79 | The following methods documented in an C<await> expression return L<Future> | ||||||||||||
| 80 | instances. | ||||||||||||
| 81 | |||||||||||||
| 82 | =cut | ||||||||||||
| 83 | |||||||||||||
| 84 | 2 | bitfield CONFIG => | |||||||||||
| 85 | GAIN => enumfield( 0, qw( 1 2 )), | ||||||||||||
| 86 | REF => enumfield( 1, qw( 1 0 )), | ||||||||||||
| 87 | PD => enumfield( 2, qw( normal 1k 100k hiZ )); | ||||||||||||
| 88 | |||||||||||||
| 89 | =head2 read_config | ||||||||||||
| 90 | |||||||||||||
| 91 | $config = await $chip->read_config; | ||||||||||||
| 92 | |||||||||||||
| 93 | Returns a C<HASH> reference containing the chip's current configuration | ||||||||||||
| 94 | |||||||||||||
| 95 | GAIN => 1 | 2 | ||||||||||||
| 96 | REF => 0 | 1 | ||||||||||||
| 97 | PD => "normal" | "1k" | "100k" | "hiZ" | ||||||||||||
| 98 | |||||||||||||
| 99 | Note that since the chip does not itself allow reading of its configuration, | ||||||||||||
| 100 | this method simply returns the internally-cached values. This cache is | ||||||||||||
| 101 | initialised to power-on defaults, and tracked by the C<change_config> method. | ||||||||||||
| 102 | |||||||||||||
| 103 | =cut | ||||||||||||
| 104 | |||||||||||||
| 105 | 2 | field $_config; | |||||||||||
| 106 | |||||||||||||
| 107 | 3 | ▶ | async method read_config () | ||||||||||
|
Subroutine
read_config: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 108 | 3 | { | |||||||||||
| 109 | # The chip doesn't allow us to read its config. These are the power-on | ||||||||||||
| 110 | # defaults. We'll track updates. | ||||||||||||
| 111 | 3 | ▶ | $_config //= 0; | ||||||||||
|
Condition100%2/2
$_config //= 0
MC/DC100%1/1
$_config //= 0
$_config
Truth table100%2/2
A
A= $_config
| |||||||||||||
| 112 | |||||||||||||
| 113 | 3 | return { unpack_CONFIG( $_config ) }; | |||||||||||
| 114 | 2 | } | |||||||||||
| 115 | |||||||||||||
| 116 | =head2 change_config | ||||||||||||
| 117 | |||||||||||||
| 118 | await $chip->change_config( %changes ); | ||||||||||||
| 119 | |||||||||||||
| 120 | Changes the configuration. Any field names not mentioned will be preserved at | ||||||||||||
| 121 | their existing values. | ||||||||||||
| 122 | |||||||||||||
| 123 | =cut | ||||||||||||
| 124 | |||||||||||||
| 125 | 1 | ▶ | async method change_config ( %changes ) | ||||||||||
|
Subroutine
change_config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 126 | 1 | { | |||||||||||
| 127 | 1 | my $config = await $self->read_config; | |||||||||||
| 128 | |||||||||||||
| 129 | 1 | $_config = pack_CONFIG( %$config, %changes ); | |||||||||||
| 130 | |||||||||||||
| 131 | 1 | await $self->protocol->write( pack "C S>", | |||||||||||
| 132 | CMD_WRITE_CTRL, $_config << 11 ); | ||||||||||||
| 133 | 2 | } | |||||||||||
| 134 | |||||||||||||
| 135 | =head1 METHODS | ||||||||||||
| 136 | |||||||||||||
| 137 | =cut | ||||||||||||
| 138 | |||||||||||||
| 139 | =head2 write_dac | ||||||||||||
| 140 | |||||||||||||
| 141 | await $chip->write_dac( $dac, $update ); | ||||||||||||
| 142 | |||||||||||||
| 143 | Writes a new value for the DAC output. | ||||||||||||
| 144 | |||||||||||||
| 145 | If C<$update> is true this will use the "update" form of write command, which | ||||||||||||
| 146 | writes both the DAC register and the input register. If false it only writes | ||||||||||||
| 147 | the input register, requiring a pulse on the F<LDAC> pin to actually change | ||||||||||||
| 148 | the output voltage. | ||||||||||||
| 149 | |||||||||||||
| 150 | =cut | ||||||||||||
| 151 | |||||||||||||
| 152 | 2 | ▶ | async method write_dac ( $value, $update = 0 ) | ||||||||||
|
Subroutine
write_dac: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 153 | 2 | { | |||||||||||
| 154 | 2 | ▶ | await $self->protocol->write( pack "C S>", | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | ( $update ? CMD_WRITE_AND_UPDATE : CMD_WRITE_INPUT ), $value << 4 ); | ||||||||||||
| 156 | 2 | } | |||||||||||
| 157 | |||||||||||||
| 158 | =head2 write_dac_voltage | ||||||||||||
| 159 | |||||||||||||
| 160 | await $chip->write_dac_voltage( $voltage ); | ||||||||||||
| 161 | |||||||||||||
| 162 | Writes a new value for the DAC output immediately, converting the given | ||||||||||||
| 163 | voltage to the required raw value, taking into account the setting of the | ||||||||||||
| 164 | C<GAIN> config bit. | ||||||||||||
| 165 | |||||||||||||
| 166 | =cut | ||||||||||||
| 167 | |||||||||||||
| 168 | 1 | ▶ | async method write_dac_voltage ( $voltage ) | ||||||||||
|
Subroutine
write_dac_voltage: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 169 | 1 | { | |||||||||||
| 170 | 1 | my $config = await $self->read_config; | |||||||||||
| 171 | |||||||||||||
| 172 | 1 | my $value = $voltage * ( 1 << 12 ) / 2.5; | |||||||||||
| 173 | 1 | $value /= $config->{GAIN}; | |||||||||||
| 174 | |||||||||||||
| 175 | 1 | ▶ | croak "Cannot set DAC voltage to $voltage - too high" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 176 | if $value >= ( 1 << 12 ); | ||||||||||||
| 177 | 1 | ▶ | croak "Cannot set DAC voltage to $voltage - too low" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 178 | if $value < 0; | ||||||||||||
| 179 | |||||||||||||
| 180 | 1 | await $self->write_dac( $value, 1 ); | |||||||||||
| 181 | 2 | } | |||||||||||
| 182 | |||||||||||||
| 183 | =head1 AUTHOR | ||||||||||||
| 184 | |||||||||||||
| 185 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||
| 186 | |||||||||||||
| 187 | =cut | ||||||||||||
| 188 | |||||||||||||
| 189 | 2 | 0x55AA; | |||||||||||