VIC/PIC/Functions/ADC.pm

filter: Statementstmt 72.7% 48 / 66 Branchbran 41.6% 15 / 36 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 3 totaltotal 52.4% 76 / 145 SCAR 44.0
CC
26
Cov
57%
adc_disable
30.0
adc_enable
24.0
adc_read
17.3
CRAP 81.4
« VIC/PIC/Functions/USART.pm ↑ Summary VIC/PIC/Functions/ECCP.pm »
1 package VIC::PIC::Functions::ADC;
2 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 our $VERSION = '0.29';
5 31 $VERSION = eval $VERSION;
6 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 31 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub adc_enable {
11 3 ▶ my $self = shift;
Subroutine adc_enable: called CC=14 SCAR=31.8
Pod undocumented
12 3 ▶ return unless $self->doesroles(qw(GPIO ADC Chip CodeGen));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesroles("GPIO", "ADC", "Chip", "CodeGen") 0 3 3
13 3 ▶ unless (defined $self->adc_channels and $self->adc_channels > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->adc_channels and $self->adc_channels > 0) 0 3 3
Condition33%1/3
defined $self->adc_channels and $self->adc_channels > 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $self->adc_channels and $self->adc_channels > 0
defined $self->adc_channels $self->adc_channels > 0
Truth table33%1/3
A and B
A= defined $self->adc_channelsB= $self->adc_channels > 0
ABresult
0X0
100
111
14 0 carp $self->type, " has no ADC";
15 0 return;
16 }
17 3 ▶ unless (exists $self->registers->{ADCON0} and
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADCON1"}) 0 3 3
Condition33%1/3
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADCON1"}
!ll&&!rl&&r
003
MC/DC0%0/2
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADCON1"}
exists $self->registers->{"ADCON0"} exists $self->registers->{"ADCON1"}
Truth table33%1/3
A and B
A= exists $self->registers->{"ADCON0"}B= exists $self->registers->{"ADCON1"}
ABresult
0X0
100
111
18 exists $self->registers->{ADCON1}) {
19 0 carp $self->type, " has no valid ADCON0/ADCON1 registers";
20 0 return;
21 }
22 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
23 3 my ($clock, $channel) = @_;
24 3 my $f_osc = $self->f_osc;
25 3 ▶ my $scale = POSIX::ceil(($f_osc / 4) / $clock) if $clock > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $clock > 0 3 0 3
26 3 ▶ $scale = 2 unless $clock;
Branch: 1 branch
BranchTrueFalseTotal
unless $clock 0 3 3
27 3 ▶ $scale = 2 if $scale < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $scale < 2 0 3 3
28 3 my $adcs = $self->adcs_bits->{$scale};
29 3 ▶ $adcs = $self->adcs_bits->{internal} if $self->code_config->{adc}->{internal};
Branch: 1 branch
BranchTrueFalseTotal
if $self->code_config->{"adc"}{"internal"} 0 3 3
30 3 my $adcon1 = "0$adcs" . '0000';
31 3 my $code = << "...";
32 \tbanksel ADCON1
33 \tmovlw B'$adcon1'
34 \tmovwf ADCON1
35 ...
36 3 ▶ if (defined $channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel) 3 0 3
37 my $adfm = defined $self->code_config->{adc}->{right_justify} ?
38 3 ▶ $self->code_config->{adc}->{right_justify} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->code_config->{'adc'}{'right_justify'} ? : 3 0 3
39 3 ▶ my $vcfg = $self->code_config->{adc}->{vref} || 0;
Condition50%1/2
$self->code_config->{'adc'}{'vref'} || 0
l!l
03
MC/DC0%0/1
$self->code_config->{'adc'}{'vref'} || 0
$self->code_config->{'adc'}{'vref'}
Truth table50%1/2
A
A= $self->code_config->{'adc'}{'vref'}
Aresult
11
00
40 3 my $chs = $self->adc_chs_bits->{$channel};
41 3 my $adcon0 = "$adfm$vcfg$chs" . '01';
42 3 $code .= << "...";
43 \tbanksel ADCON0
44 \tmovlw B'$adcon0'
45 \tmovwf ADCON0
46 ...
47 }
48 3 return $code;
49 }
50 # no arguments have been given
51 0 return << "...";
52 \tbanksel ADCON0
53 \tbsf ADCON0, ADON
54 ...
55 }
56  
57 sub adc_disable {
58 0 ▶ my $self = shift;
Subroutine adc_disable: not called CC=5 SCAR=34.0
Pod undocumented
59 0 ▶ return unless $self->doesroles(qw(ADC));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesroles("ADC") 0 0 0
60 0 ▶ unless (defined $self->adc_channels and $self->adc_channels > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->adc_channels and $self->adc_channels > 0) 0 0 0
Condition0%0/3
defined $self->adc_channels and $self->adc_channels > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->adc_channels and $self->adc_channels > 0
defined $self->adc_channels $self->adc_channels > 0
Truth table0%0/3
A and B
A= defined $self->adc_channelsB= $self->adc_channels > 0
ABresult
0X0
100
111
61 0 carp $self->type, " has no ADC";
62 0 return;
63 }
64 0 ▶ unless (exists $self->registers->{ADCON0}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->registers->{"ADCON0"}) 0 0 0
65 0 carp $self->type, " has no valid ADCON0/ADCON1 registers";
66 0 return;
67 }
68 0 return << "...";
69 \tbanksel ADCON0
70 \tbcf ADCON0, ADON
71 ...
72 }
73  
74 sub adc_read {
75 #TODO: if the variable is 16-bit the varlow should be auto-adjusted
76 3 ▶ my ($self, $varhigh, $varlow) = @_;
Subroutine adc_read: called CC=9 SCAR=28.5
Pod undocumented
77 3 ▶ return unless $self->doesroles(qw(ADC GPIO Chip));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesroles("ADC", "GPIO", "Chip") 0 3 3
78 3 ▶ unless (defined $self->adc_channels and $self->adc_channels > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->adc_channels and $self->adc_channels > 0) 0 3 3
Condition33%1/3
defined $self->adc_channels and $self->adc_channels > 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $self->adc_channels and $self->adc_channels > 0
defined $self->adc_channels $self->adc_channels > 0
Truth table33%1/3
A and B
A= defined $self->adc_channelsB= $self->adc_channels > 0
ABresult
0X0
100
111
79 0 carp $self->type, " has no ADC";
80 0 return;
81 }
82 3 $varhigh = uc $varhigh;
83 3 ▶ $varlow = uc $varlow if defined $varlow;
Branch: 1 branch
BranchTrueFalseTotal
if defined $varlow 0 3 3
84 3 ▶ unless (exists $self->registers->{ADCON0} and
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADRESH"} and exists $self->registers->{"ADRESL"}) 0 3 3
Condition33%2/6
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADRESH"} and exists $self->registers->{"ADRESL"}
!ll&&!rl&&r
003
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADRESH"}
!ll&&!rl&&r
003
MC/DC0%0/3
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADRESH"} and exists $self->registers->{"ADRESL"}
exists $self->registers->{"ADCON0"} exists $self->registers->{"ADRESH"} exists $self->registers->{"ADRESL"}
Truth table25%1/4
A and B and C
A= exists $self->registers->{"ADCON0"}B= exists $self->registers->{"ADRESH"}C= exists $self->registers->{"ADRESL"}
ABCresult
0XX0
10X0
1100
1111
85 exists $self->registers->{ADRESH} and
86 exists $self->registers->{ADRESL}) {
87 0 carp $self->type, " has no valid ADCON0/ADRESH/ADRESL registers";
88 0 return;
89 }
90 3 my $code = << "...";
91 \t;;;delay 5us
92 \tnop
93 \tnop
94 \tnop
95 \tnop
96 \tnop
97 \tbsf ADCON0, GO
98 \tbtfss ADCON0, GO
99 \tgoto \$ - 1
100 \tmovf ADRESH, W
101 \tmovwf $varhigh
102 ...
103 3 ▶ $code .= "\tmovf ADRESL, W\n\tmovwf $varlow\n" if defined $varlow;
Branch: 1 branch
BranchTrueFalseTotal
if defined $varlow 0 3 3
104 3 return $code;
105 }
106  
107 31 1;
108 __END__
« VIC/PIC/Functions/USART.pm ↑ Summary VIC/PIC/Functions/ECCP.pm »