MAX11200.pm

filter: Statementstmt 86.0% 216 / 251 Branchbran 55.0% 11 / 20 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 86.0% 37 / 43 Podpod 66.6% 14 / 21 totaltotal 80.5% 281 / 349 CC 20 SCAR 9.2
CC
20
Cov
82%
tris_gpios
7.5
read_adc_ratio
5.8
trigger
3.0
CRAP 2.5
« MCP4725.pm ↑ Summary DAC75xx.pm »
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::MAX11200 0.16;
11 class Device::Chip::MAX11200
12 :isa(Device::Chip);
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 2 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Data::Bitfield 0.02 qw( bitfield boolfield enumfield );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use constant PROTOCOL => "SPI";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 C<Device::Chip::MAX11200> - chip driver for F<MAX11200>
25  
26 =head1 SYNOPSIS
27  
28 use Device::Chip::MAX11200;
29 use Future::AsyncAwait;
30  
31 my $chip = Device::Chip::MAX11200->new;
32 await $chip->mount( Device::Chip::Adapter::...->new );
33  
34 await $chip->trigger;
35  
36 printf "The reading is %d\n", await $chip->read_adc;
37  
38 =head1 DESCRIPTION
39  
40 This L<Device::Chip> subclass provides specific communications to a F<Maxim>
41 F<MAX11200> or F<MAX11210> chip.
42  
43 The reader is presumed to be familiar with the general operation of this chip;
44 the documentation here will not attempt to explain or define chip-specific
45 concepts or features, only the use of this module to access them.
46  
47 =cut
48  
49 sub SPI_options
50 1 ▶{
Subroutine SPI_options: called CC=1 SCAR=0.0
Pod undocumented
51 return (
52 1 mode => 0,
53 max_bitrate => 2E6,
54 );
55 }
56  
57 =head1 METHODS
58  
59 The following methods documented in an C<await> expression return L<Future>
60 instances.
61  
62 =cut
63  
64 =head2 init
65  
66 await $chip->init;
67  
68 Performs startup self-calibration by setting C<NOSCG> and C<NOSCO> to zero
69 then requesting a calibration cycle.
70  
71 =cut
72  
73 0 ▶async method init ()
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
74 0 {
75 0 await $self->change_config(
76 NOSCG => 0,
77 NOSCO => 0,
78 );
79  
80 0 await $self->selfcal;
81  
82 0 await Future::IO->sleep( 0.25 ); # selfcal takes 200msec (longer at LINEF=50Hz)
83 2 }
84  
85 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 REG_STAT => 0,
87 REG_CTRL1 => 1,
88 REG_CTRL2 => 2,
89 REG_CTRL3 => 3,
90 REG_DATA => 4,
91 REG_SOC => 5,
92 REG_SGC => 6,
93 REG_SCOC => 7,
94 REG_SCGC => 8,
95 2 };
96  
97 11 ▶async method read_register ( $reg, $len = 1 )
Subroutine read_register: called CC=2 SCAR=6.9
Pod undocumented
98 11 {
99 11 my $bytes = await $self->protocol->readwrite(
100 pack "C a*", 0xC1 | ( $reg << 1 ), "\0" x $len
101 );
102  
103 11 return substr $bytes, 1;
104 2 }
105  
106 6 ▶async method write_register ( $reg, $val )
Subroutine write_register: called CC=1 SCAR=0.0
Pod undocumented
107 6 {
108 6 await $self->protocol->write(
109 pack "C a*", 0xC0 | ( $reg << 1 ), $val
110 );
111 2 }
112  
113 2 my @RATES = qw( 1 2.5 5 10 15 30 60 120 );
114  
115 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
116 2 CMD_SELFCAL => 0x10,
117 CMD_SYSOCAL => 0x20,
118 CMD_SYSGCAL => 0x30,
119 CMD_POWERDOWN => 0x08,
120 CMD_CONV => 0,
121 2 };
122  
123 3 ▶async method command ( $cmd )
Subroutine command: called CC=1 SCAR=0.0
Pod undocumented
124 3 {
125 3 await $self->protocol->write( pack "C", 0x80 | $cmd );
126 2 }
127  
128 =head2 read_status
129  
130 $status = await $chip->read_status;
131  
132 Returns a C<HASH> reference containing the chip's current status.
133  
134 RDY => 0 | 1
135 MSTAT => 0 | 1
136 UR => 0 | 1
137 OR => 0 | 1
138 RATE => 1 | 2.5 | 5 | 10 | 15 | 30 | 60 | 120
139 SYSOR => 0 | 1
140  
141 =cut
142  
143 2 bitfield { format => "bytes-LE" }, STAT =>
144 RDY => boolfield(0),
145 MSTAT => boolfield(1),
146 UR => boolfield(2),
147 OR => boolfield(3),
148 RATE => enumfield(4, @RATES),
149 SYSOR => boolfield(7);
150  
151 1 ▶async method read_status ()
Subroutine read_status: called CC=1 SCAR=0.0
Pod documented
152 1 {
153 1 my $bytes = await $self->read_register( REG_STAT );
154  
155 1 return unpack_STAT( $bytes );
156 2 }
157  
158 =head2 read_config
159  
160 $config = await $chip->read_config;
161  
162 Returns a C<HASH> reference containing the chip's current configuration.
163  
164 SCYCLE => 0 | 1
165 FORMAT => "TWOS_COMP" | "OFFSET"
166 SIGBUF => 0 | 1
167 REFBUF => 0 | 1
168 EXTCLK => 0 | 1
169 UB => "UNIPOLAR" | "BIPOLAR"
170 LINEF => "60Hz" | "50Hz"
171 NOSCO => 0 | 1
172 NOSCG => 0 | 1
173 NOSYSO => 0 | 1
174 NOSYSG => 0 | 1
175 DGAIN => 1 2 4 8 16 # only valid for the MAX11210
176  
177 =cut
178  
179 2 bitfield { format => "bytes-LE" }, CONFIG =>
180 # CTRL1
181 SCYCLE => boolfield(1),
182 FORMAT => enumfield(2, qw( TWOS_COMP OFFSET )),
183 SIGBUF => boolfield(3),
184 REFBUF => boolfield(4),
185 EXTCLK => boolfield(5),
186 UB => enumfield(6, qw( BIPOLAR UNIPOLAR )),
187 LINEF => enumfield(7, qw( 60Hz 50Hz )),
188 # CTRL2 is all GPIO control; we'll do that elsewhere
189 # CTRL3
190 NOSCO => boolfield(8+1),
191 NOSCG => boolfield(8+2),
192 NOSYSO => boolfield(8+3),
193 NOSYSG => boolfield(8+4),
194 DGAIN => enumfield(8+5, qw( 1 2 4 8 16 ));
195  
196 1 ▶async method read_config ()
Subroutine read_config: called CC=1 SCAR=0.0
Pod documented
197 1 {
198 1 my ( $ctrl1, $ctrl3 ) = await Future->needs_all(
199 $self->read_register( REG_CTRL1 ), $self->read_register( REG_CTRL3 )
200 );
201  
202 1 return $self->{config} = { unpack_CONFIG( $ctrl1 . $ctrl3 ) };
203 2 }
204  
205 =head2 change_config
206  
207 await $chip->change_config( %changes );
208  
209 Changes the configuration. Any field names not mentioned will be preserved at
210 their existing values.
211  
212 =cut
213  
214 1 ▶async method change_config ( %changes )
Subroutine change_config: called CC=2 SCAR=7.0
Pod documented
215 1 {
216 1 ▶ my $config = $self->{config} // await $self->read_config;
Condition33%1/3
$self->{"config"} // <custom op await>
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"config"} // <custom op await>
$self->{"config"} <custom op await>
Truth table33%1/3
A // B
A= $self->{"config"}B= <custom op await>
ABresult
1X1
011
000
217  
218 1 $self->{config} = { %$config, %changes };
219 1 my $ctrlb = pack_CONFIG( %{ $self->{config} } );
220  
221 1 await Future->needs_all(
222 $self->write_register( REG_CTRL1, substr $ctrlb, 0, 1 ),
223 $self->write_register( REG_CTRL3, substr $ctrlb, 1, 1 ),
224 );
225 2 }
226  
227 =head2 selfcal
228  
229 await $chip->selfcal;
230  
231 Requests the chip perform a self-calibration.
232  
233 =cut
234  
235 1 ▶async method selfcal ()
Subroutine selfcal: called CC=1 SCAR=0.0
Pod documented
236 1 {
237 1 await $self->command( CMD_SELFCAL );
238 2 }
239  
240 =head2 syscal_offset
241  
242 await $chip->syscal_offset;
243  
244 Requests the chip perform the offset part of system calibration.
245  
246 =cut
247  
248 0 ▶async method syscal_offset ()
Subroutine syscal_offset: not called CC=1 SCAR=6.9
Pod documented
249 0 {
250 0 await $self->command( CMD_SYSOCAL );
251 2 }
252  
253 =head2 syscal_gain
254  
255 await $chip->syscal_gain;
256  
257 Requests the chip perform the gain part of system calibration.
258  
259 =cut
260  
261 0 ▶async method syscal_gain ()
Subroutine syscal_gain: not called CC=1 SCAR=6.9
Pod documented
262 0 {
263 0 await $self->command( CMD_SYSGCAL );
264 2 }
265  
266 =head2 trigger
267  
268 await $chip->trigger( $rate );
269  
270 Requests the chip perform a conversion of the input level, at the given
271 rate (which must be one of the values specified for the C<RATE> configuration
272 option); defaulting to the value of L</default_trigger_rate> if not defined.
273 Once the conversion is complete it can be read using the C<read_adc> method.
274  
275 =head2 default_trigger_rate
276  
277 $rate = $chip->default_trigger_rate
278 $chip->default_trigger_rate = $new_rate
279  
280 Lvalue accessor for the default trigger rate if L</trigger> is invoked without
281 one. Initialised to 120.
282  
283 =cut
284  
285 field $_default_trigger_rate = 120;
286 1 ▶method default_trigger_rate :lvalue { $_default_trigger_rate }
Subroutine default_trigger_rate: called CC=1 SCAR=0.0
Pod documented
287  
288 2 ▶async method trigger ( $rate = $_default_trigger_rate )
Subroutine trigger: called CC=3 SCAR=11.0
Pod documented
289 2 {
290 2 ▶ defined( my $rateidx = first { $RATES[$_] == $rate } 0 .. $#RATES )
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
defined(my $rateidx = &first(sub { $RATES[$_] == $rate; } , 0 .. $#RATES)) or croak("Unrecognised conversion rate $rate") 2 0 2
291 or croak "Unrecognised conversion rate $rate";
292  
293 2 await $self->command( CMD_CONV | $rateidx );
294 2 }
295  
296 =head2 read_adc
297  
298 $value = await $chip->read_adc;
299  
300 Reads the most recent reading from the result register on the tip. This method
301 should be called after a suitable delay after the L</trigger> method when in
302 single cycle mode, or at any time when in continuous mode.
303  
304 The reading is returned directly from the chip as a plain 24-bit integer,
305 either signed or unsigned as per the C<FORMAT> configuration.
306  
307 =cut
308  
309 4 ▶async method read_adc ()
Subroutine read_adc: called CC=1 SCAR=0.0
Pod documented
310 4 {
311 4 my $bytes = await $self->read_register( REG_DATA, 3 );
312  
313 4 return unpack "L>", "\0$bytes";
314 2 }
315  
316 =head2 read_adc_ratio
317  
318 $ratio = await $chip->read_adc_ratio;
319  
320 Converts a reading obtained by L</read_adc> into a ratio between -1 and 1,
321 taking into account the current mode setting of the chip.
322  
323 =cut
324  
325 1 ▶async method read_adc_ratio ()
Subroutine read_adc_ratio: called CC=5 SCAR=17.6
Pod documented
326 1 {
327 my ( $value, $config ) = await Future->needs_all(
328 $self->read_adc,
329 1 ▶ ( $self->{config} ? Future->done( $self->{config} ) : $self->read_config )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'config'} ? : 1 0 1
330 );
331  
332 1 ▶ if( $config->{UB} eq "UNIPOLAR" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'UB'} eq 'UNIPOLAR') { } 0 1 1
333 # Raw 24bit integer
334 0 return $value / 2**24;
335 }
336 else {
337 1 ▶ if( $config->{FORMAT} eq "TWOS_COMP" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'FORMAT'} eq 'TWOS_COMP') { } 1 0 1
338 # Signed integer in twos-complement form
339 1 ▶ $value -= 2**24 if $value >= 2**23;
Branch: 1 branch
BranchTrueFalseTotal
if $value >= 8388608 0 1 1
340 }
341 else {
342 # Signed-integer in offset form
343 0 $value -= 2**23;
344 }
345 1 return $value / 2**23;
346 }
347 2 }
348  
349 =head2 write_gpios
350  
351 await $chip->write_gpios( $values, $direction );
352  
353 =head2 read_gpios
354  
355 $values = await $chip->read_gpios;
356  
357 Sets or reads the values of the GPIO pins as a 4-bit integer. Bits in the
358 C<$direction> should be high to put the corresponding pin into output mode, or
359 low to put it into input mode.
360  
361 As an alternative to these methods, see instead L</gpio_adapter>.
362  
363 =cut
364  
365 3 ▶async method write_gpios ( $values, $dir )
Subroutine write_gpios: called CC=1 SCAR=0.0
Pod documented
366 3 {
367 3 await $self->write_register( REG_CTRL2, pack "C", ( $dir << 4 ) | $values );
368 2 }
369  
370 3 ▶async method read_gpios ()
Subroutine read_gpios: called CC=1 SCAR=0.0
Pod documented
371 3 {
372 3 my $bytes = await $self->read_register( REG_CTRL2 );
373  
374 3 return 0x0F & unpack "C", $bytes;
375 2 }
376  
377 =head2 Calibration Registers
378  
379 $value = await $chip->read_selfcal_offset;
380 $value = await $chip->read_selfcal_gain;
381 $value = await $chip->read_syscal_offset;
382 $value = await $chip->read_syscal_gain;
383  
384 await $chip->write_selfcal_offset( $value );
385 await $chip->write_selfcal_gain( $value );
386 await $chip->write_syscal_offset( $value );
387 await $chip->write_syscal_gain( $value );
388  
389 Reads or writes the values of the calibration registers, as plain 24-bit
390 integers.
391  
392 =cut
393  
394 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.9
395 2 ▶ use Object::Pad 0.800 ':experimental(mop)';
Subroutine BEGIN: called CC=1 SCAR=0.0
396 2 my $mop = Object::Pad::MOP::Class->for_caller;
397  
398 2 foreach (
399 [ "selfcal_offset", REG_SCOC ],
400 [ "selfcal_gain", REG_SCGC ],
401 [ "syscal_offset", REG_SOC ],
402 [ "syscal_gain", REG_SGC ],
403 ) {
404 8 my ( $name, $reg ) = @$_;
405  
406 0 ▶ $mop->add_method( "read_$name" => async method () {
Subroutine read_syscal_offset: not called CC=1 SCAR=1.2
Subroutine read_syscal_gain: called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
407 0 my $bytes = await $self->read_register( $reg, 3 );
408 0 return unpack "I>", "\0" . $bytes;
409 8 } );
410  
411 0 ▶ $mop->add_method( "write_$name" => async method ( $value ) {
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine write_selfcal_offset: called CC=1 SCAR=1.2
Pod undocumented
412 0 await $self->write_register( $reg,
413 substr( pack( "I>", $value ), 1 )
414 );
415 8 } );
416 }
417 }
418  
419 =head2 as_gpio_adapter
420  
421 $adapter = $chip->as_gpio_adapter
422  
423 Returns an instance implementing the L<Device::Chip::Adapter> interface,
424 allowing access to the four GPIO pins via the standard adapter API.
425  
426 =cut
427  
428 method as_gpio_adapter
429 1 ▶{
Subroutine as_gpio_adapter: called CC=1 SCAR=0.0
Pod documented
430 1 return Device::Chip::MAX11200::_GPIOAdapter->new( chip => $self );
431 2 }
432  
433 class Device::Chip::MAX11200::_GPIOAdapter {
434 2 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
435  
436 2 field $_chip :param;
437  
438 1 ▶ async method make_protocol ( $pname )
Subroutine make_protocol: called CC=2 SCAR=7.0
439 1 {
440 1 ▶ $pname eq "GPIO" or
Branch: 1 branch
BranchTrueFalseTotal
$pname eq "GPIO" or croak("Unrecognised protocol name $pname") 1 0 1
441 croak "Unrecognised protocol name $pname";
442  
443 1 return $self;
444 2 }
445  
446 1 ▶ method list_gpios { qw( GPIO1 GPIO2 GPIO3 GPIO4 ) }
Subroutine list_gpios: called CC=1 SCAR=0.0
447  
448 method meta_gpios
449 0 ▶ {
Subroutine meta_gpios: not called CC=1 SCAR=6.9
450 return map {
451 0 Device::Chip::Adapter::GPIODefinition( $_, "rw", 0 )
452 } $self->list_gpios;
453 2 }
454  
455 field $_dir = "";
456 2 field $_val = "";
457  
458 2 ▶ async method write_gpios ( $values )
Subroutine write_gpios: called CC=3 SCAR=11.0
459 2 {
460 2 foreach my $n ( 1 .. 4 ) {
461 8 ▶ defined( my $v = $values->{"GPIO$n"} ) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $v = $$values{"GPIO$n"}) or next 2 6 8
462  
463 2 vec( $_dir, $n-1, 1 ) = 1;
464 2 vec( $_val, $n-1, 1 ) = $v;
465 }
466  
467 2 await $_chip->write_gpios( ord $_val, ord $_dir );
468 2 }
469  
470 1 ▶ async method tris_gpios ( $pins )
Subroutine tris_gpios: called CC=6 SCAR=20.2
471 1 {
472 1 my $newdir = $_dir;
473 1 foreach my $pin ( @$pins ) {
474 1 ▶ $pin =~ m/^GPIO(\d)/ and $1 >= 1 and $1 <= 4 or
Branch: 1 branch
BranchTrueFalseTotal
$pin =~ /^GPIO(\d)/u and $1 >= 1 and $1 <= 4 or croak("Unrecognised GPIO pin name $pin") 1 0 1
Condition33%2/6
$pin =~ /^GPIO(\d)/u and $1 >= 1 and $1 <= 4
!ll&&!rl&&r
001
$pin =~ /^GPIO(\d)/u and $1 >= 1
!ll&&!rl&&r
001
MC/DC0%0/3
$pin =~ /^GPIO(\d)/u and $1 >= 1 and $1 <= 4
$pin =~ /^GPIO(\d)/u $1 >= 1 $1 <= 4
Truth table25%1/4
A and B and C
A= $pin =~ /^GPIO(\d)/uB= $1 >= 1C= $1 <= 4
ABCresult
0XX0
10X0
1100
1111
475 croak "Unrecognised GPIO pin name $pin";
476 1 my $n = $1;
477  
478 1 vec( $newdir, $n-1, 1 ) = 0;
479 }
480  
481 1 ▶ if( $newdir ne $_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newdir ne $_dir) 0 1 1
482 0 $_dir = $newdir;
483 0 await $_chip->write_gpios( ord $_val, ord $_dir );
484 }
485 2 }
486  
487 1 ▶ async method read_gpios ( $pins )
Subroutine read_gpios: called CC=3 SCAR=11.0
488 1 {
489 1 await $self->tris_gpios( $pins );
490  
491 1 my $read = chr await $_chip->read_gpios;
492  
493 1 my %ret;
494 1 foreach my $pin ( @$pins ) {
495 1 ▶ $pin =~ m/^GPIO(\d)/ and my $n = $1;
Branch: 1 branch
BranchTrueFalseTotal
$pin =~ /^GPIO(\d)/u and my $n = $1 1 0 1
496 1 $ret{$pin} = vec( $read, $n-1, 1 );
497 }
498  
499 1 return \%ret;
500 2 }
501 }
502  
503 2 =head1 AUTHOR
504  
505 Paul Evans <leonerd@leonerd.org.uk>
506  
507 =cut
508  
509 2 0x55AA;
« MCP4725.pm ↑ Summary DAC75xx.pm »