VIC/PIC/Functions/Timer.pm

filter: Statementstmt 68.3% 69 / 101 Branchbran 51.7% 30 / 58 Conditioncond 28.5% 10 / 35 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 3 totaltotal 51.5% 118 / 229 SCAR 53.7
CC
45
Cov
56%
_get_timer_prescaler
73.9
timer_disable
56.0
timer_enable
16.6
CRAP 215.3
« VIC/PIC/Functions/GPIO.pm ↑ Summary VIC/PIC/Functions/USART.pm »
1 package VIC::PIC::Functions::Timer;
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 _get_timer_prescaler {
11 2 ▶ my ($self, $freq) = @_;
Subroutine _get_timer_prescaler: called CC=17 SCAR=43.0
12 2 my $f_osc = $self->f_osc;
13 2 my $scale = POSIX::ceil(($f_osc / 4) / $freq); # assume prescaler = 1 here
14 2 ▶ if ($scale <=2) {
Branch: 8 branches
BranchTrueFalseTotal
if ($scale <= 2) { } 0 2 2
elsif ($scale > 2 and $scale <= 4) { } 0 2 2
elsif ($scale > 4 and $scale <= 8) { } 0 2 2
elsif ($scale > 8 and $scale <= 16) { } 0 2 2
elsif ($scale > 16 and $scale <= 32) { } 0 2 2
elsif ($scale > 32 and $scale <= 64) { } 0 2 2
elsif ($scale > 64 and $scale <= 128) { } 0 2 2
elsif ($scale > 128 and $scale <= 256) { } 2 0 2
15 0 $scale = 2;
16 ▶ } elsif ($scale > 2 && $scale <= 4) {
Condition33%1/3
$scale > 2 and $scale <= 4
!ll&&!rl&&r
020
MC/DC0%0/2
$scale > 2 and $scale <= 4
$scale > 2 $scale <= 4
Truth table33%1/3
A and B
A= $scale > 2B= $scale <= 4
ABresult
0X0
100
111
17 0 $scale = 4;
18 ▶ } elsif ($scale > 4 && $scale <= 8) {
Condition33%1/3
$scale > 4 and $scale <= 8
!ll&&!rl&&r
020
MC/DC0%0/2
$scale > 4 and $scale <= 8
$scale > 4 $scale <= 8
Truth table33%1/3
A and B
A= $scale > 4B= $scale <= 8
ABresult
0X0
100
111
19 0 $scale = 8;
20 ▶ } elsif ($scale > 8 && $scale <= 16) {
Condition33%1/3
$scale > 8 and $scale <= 16
!ll&&!rl&&r
020
MC/DC0%0/2
$scale > 8 and $scale <= 16
$scale > 8 $scale <= 16
Truth table33%1/3
A and B
A= $scale > 8B= $scale <= 16
ABresult
0X0
100
111
21 0 $scale = 16;
22 ▶ } elsif ($scale > 16 && $scale <= 32) {
Condition33%1/3
$scale > 16 and $scale <= 32
!ll&&!rl&&r
020
MC/DC0%0/2
$scale > 16 and $scale <= 32
$scale > 16 $scale <= 32
Truth table33%1/3
A and B
A= $scale > 16B= $scale <= 32
ABresult
0X0
100
111
23 0 $scale = 32;
24 ▶ } elsif ($scale > 32 && $scale <= 64) {
Condition33%1/3
$scale > 32 and $scale <= 64
!ll&&!rl&&r
020
MC/DC0%0/2
$scale > 32 and $scale <= 64
$scale > 32 $scale <= 64
Truth table33%1/3
A and B
A= $scale > 32B= $scale <= 64
ABresult
0X0
100
111
25 0 $scale = 64;
26 ▶ } elsif ($scale > 64 && $scale <= 128) {
Condition33%1/3
$scale > 64 and $scale <= 128
!ll&&!rl&&r
020
MC/DC0%0/2
$scale > 64 and $scale <= 128
$scale > 64 $scale <= 128
Truth table33%1/3
A and B
A= $scale > 64B= $scale <= 128
ABresult
0X0
100
111
27 0 $scale = 128;
28 ▶ } elsif ($scale > 128 && $scale <= 256) {
Condition33%1/3
$scale > 128 and $scale <= 256
!ll&&!rl&&r
002
MC/DC0%0/2
$scale > 128 and $scale <= 256
$scale > 128 $scale <= 256
Truth table33%1/3
A and B
A= $scale > 128B= $scale <= 256
ABresult
0X0
100
111
29 2 $scale = 256;
30 } else {
31 0 $scale = 256;
32 }
33 2 ▶ my $psx = $self->timer_prescaler->{$scale} || $self->timer_prescaler->{256};
Condition33%1/3
$self->timer_prescaler->{$scale} || $self->timer_prescaler->{'256'}
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->timer_prescaler->{$scale} || $self->timer_prescaler->{'256'}
$self->timer_prescaler->{$scale} $self->timer_prescaler->{'256'}
Truth table33%1/3
A || B
A= $self->timer_prescaler->{$scale}B= $self->timer_prescaler->{'256'}
ABresult
1X1
011
000
34 2 return $psx;
35 }
36  
37 sub _get_wdt_prescaler {
38 1 ▶ my ($self, $period) = @_;
Subroutine _get_wdt_prescaler: called CC=5 SCAR=17.0
39 1 my $lfintosc = $self->wdt_prescaler->{LFINTOSC};
40 #period is in microseconds. convert to seconds
41 1 $period = ($period * 1.0) / 1.0e6;
42 1 my $scale = POSIX::floor($lfintosc * $period);
43 1 my $wdtps = $self->wdt_prescaler->{WDT};
44 1 my @psv = sort { $a <=> $b } keys %$wdtps;
45 1 my $minscale = $psv[0];
46 1 foreach (@psv) {
47 ## if the scale is 25% above the level, just use the lower level instead
48 #of the higher level
49 5 ▶ if ($scale <= ($_ + $_ / 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $_ + $_ / 4) 1 4 5
50 1 ▶ return wantarray ? ($_, $wdtps->{$_}) : $wdtps->{$_};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
51 }
52 }
53 0 my $maxscale = pop @psv;
54 0 ▶ return wantarray ? ($maxscale, $wdtps->{$maxscale}) : $wdtps->{$maxscale};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
55 }
56  
57 sub timer_enable {
58 3 ▶ my ($self, $tmr, $freq, %isr) = @_;
Subroutine timer_enable: called CC=13 SCAR=28.1
Pod undocumented
59 3 ▶ return unless $self->doesroles(qw(Timer Chip));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesroles("Timer", "Chip") 0 3 3
60 3 my ($code, $funcs, $macros) = ('', {}, {});
61 3 ▶ if ($tmr eq 'WDT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmr eq 'WDT') { } 1 2 3
62 1 ▶ unless (exists $self->registers->{WDTCON}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->registers->{"WDTCON"}) 0 1 1
63 0 carp $self->type, " does not have the register WDTCON";
64 0 return;
65 }
66 1 ▶ if (defined $self->chip_config->{on_off}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->chip_config->{"on_off"}) 1 0 1
67 1 foreach (keys %{$self->chip_config->{on_off}}) {
68 7 ▶ $self->chip_config->{on_off}->{$_} = 1 if $_ =~ /WDT/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /WDT/ 1 6 7
69 }
70 }
71 1 my ($wdtps, $wdtpsbits) = $self->_get_wdt_prescaler($freq);
72 1 $code = << "...";
73 ;;; Period is $freq us so scale is 1:$wdtps
74 \tclrwdt
75 \tclrw
76 \tbanksel WDTCON
77 \tiorlw B'000${wdtpsbits}1'
78 \tmovwf WDTCON
79 ...
80 } else {
81 2 ▶ unless (exists $self->timer_pins->{$tmr}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->timer_pins->{$tmr}) 0 2 2
82 0 carp "$tmr is not a timer.";
83 0 return;
84 }
85 2 ▶ unless (exists $self->registers->{OPTION_REG}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->registers->{"OPTION_REG"}) 0 2 2
86 0 carp $self->type, " does not have the register OPTION_REG";
87 0 return;
88 }
89 2 my $psx = $self->_get_timer_prescaler($freq);
90 2 my $th = $self->timer_pins->{$tmr};
91 2 ▶ unless (ref $th eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $th eq "HASH") 0 2 2
92 0 carp "$tmr does not have a HASH ref as its value";
93 0 return;
94 }
95 2 $code = << "...";
96 ;; timer prescaling
97 \tbanksel OPTION_REG
98 \tclrw
99 \tiorlw B'00000$psx'
100 \tmovwf OPTION_REG
101 ...
102 2 my $end_code = << "...";
103 ;; clear the timer
104 \tbanksel $tmr
105 \tclrf $tmr
106 ...
107 2 ▶ if (%isr) {
Branch: 1 branch
BranchTrueFalseTotal
if (%isr) 1 1 2
108 1 $code .= $self->isr_timer($th);
109 }
110 2 $code .= "\n$end_code\n";
111 2 ▶ if (%isr) {
Branch: 1 branch
BranchTrueFalseTotal
if (%isr) 1 1 2
112 1 $funcs->{isr_timer} = $self->isr_timer($th, %isr);
113 }
114 }
115 3 ▶ return wantarray ? ($code, $funcs, $macros) : $code;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
116 }
117  
118 sub timer_disable {
119 0 ▶ my ($self, $tmr) = @_;
Subroutine timer_disable: not called CC=7 SCAR=40.3
Pod undocumented
120 0 ▶ return unless $self->doesroles(qw(Timer Chip));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesroles("Timer", "Chip") 0 0 0
121 0 ▶ unless (exists $self->timer_pins->{$tmr}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->timer_pins->{$tmr}) 0 0 0
122 0 carp "$tmr is not a timer.";
123 0 return;
124 }
125 0 ▶ unless (exists $self->registers->{OPTION_REG} and
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->registers->{"OPTION_REG"} and exists $self->registers->{"INTCON"}) 0 0 0
Condition0%0/3
exists $self->registers->{"OPTION_REG"} and exists $self->registers->{"INTCON"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->registers->{"OPTION_REG"} and exists $self->registers->{"INTCON"}
exists $self->registers->{"OPTION_REG"} exists $self->registers->{"INTCON"}
Truth table0%0/3
A and B
A= exists $self->registers->{"OPTION_REG"}B= exists $self->registers->{"INTCON"}
ABresult
0X0
100
111
126 exists $self->registers->{INTCON}) {
127 0 carp $self->type, " does not have the register OPTION_REG/INTCON";
128 0 return;
129 }
130 0 my $th = $self->timer_pins->{$tmr};
131 0 ▶ my $tm_en = $th->{enable} || 'T0IE';
Condition0%0/2
$th->{'enable'} || 'T0IE'
l!l
00
MC/DC0%0/1
$th->{'enable'} || 'T0IE'
$th->{'enable'}
Truth table0%0/2
A
A= $th->{'enable'}
Aresult
11
00
132 0 ▶ my $tm_ereg = $th->{ereg} || 'INTCON';
Condition0%0/2
$th->{'ereg'} || 'INTCON'
l!l
00
MC/DC0%0/1
$th->{'ereg'} || 'INTCON'
$th->{'ereg'}
Truth table0%0/2
A
A= $th->{'ereg'}
Aresult
11
00
133 0 return << "...";
134 \tbanksel $tm_ereg
135 \tbcf $tm_ereg, $tm_en ;; disable only the timer bit
136 \tbanksel OPTION_REG
137 \tmovlw B'00001000'
138 \tmovwf OPTION_REG
139 \tbanksel $tmr
140 \tclrf $tmr
141 ...
142  
143 }
144  
145 sub timer {
146 1 ▶ my ($self, $tmr, %action) = @_;
Subroutine timer: called CC=7 SCAR=22.0
Pod undocumented
147 1 ▶ return unless exists $action{ACTION};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $action{"ACTION"} 0 1 1
148 1 ▶ return unless $self->doesroles(qw(Timer Chip));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesroles("Timer", "Chip") 0 1 1
149 1 ▶ return unless exists $action{END};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $action{"END"} 0 1 1
150 1 ▶ unless (exists $self->registers->{INTCON}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->registers->{"INTCON"}) 0 1 1
151 0 carp $self->type, " does not have the register INTCON";
152 0 return;
153 }
154 1 my $th = $self->timer_pins->{$tmr};
155 1 ▶ my $tm_f = $th->{flag} || 'T0IF';
Condition50%1/2
$th->{'flag'} || 'T0IF'
l!l
10
MC/DC0%0/1
$th->{'flag'} || 'T0IF'
$th->{'flag'}
Truth table50%1/2
A
A= $th->{'flag'}
Aresult
11
00
156 1 ▶ my $tm_freg = $th->{freg} || 'INTCON';
Condition50%1/2
$th->{'freg'} || 'INTCON'
l!l
10
MC/DC0%0/1
$th->{'freg'} || 'INTCON'
$th->{'freg'}
Truth table50%1/2
A
A= $th->{'freg'}
Aresult
11
00
157 1 return << "...";
158 \tbtfss $tm_freg, $tm_f
159 \tgoto $action{END}
160 \tbcf $tm_freg, $tm_f
161 \tgoto $action{ACTION}
162 $action{END}:
163 ...
164 }
165  
166 31 1;
167 __END__
« VIC/PIC/Functions/GPIO.pm ↑ Summary VIC/PIC/Functions/USART.pm »