| 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.32'; | |||||||||||||||||||||||||||||||||||||
| 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 | 2 | ▶ | sub _get_timer_prescaler { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_timer_prescaler: called CC=17 SCAR=43.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 2 | my ($self, $freq) = @_; | |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 15 | 0 | $scale = 2; | |||||||||||||||||||||||||||||||||||||
| 16 | ▶ | } elsif ($scale > 2 && $scale <= 4) { | |||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$scale > 2 and $scale <= 4
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
| |||||||||||||||||||||||||||||||||||||||
| 17 | 0 | $scale = 4; | |||||||||||||||||||||||||||||||||||||
| 18 | ▶ | } elsif ($scale > 4 && $scale <= 8) { | |||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$scale > 4 and $scale <= 8
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
| |||||||||||||||||||||||||||||||||||||||
| 19 | 0 | $scale = 8; | |||||||||||||||||||||||||||||||||||||
| 20 | ▶ | } elsif ($scale > 8 && $scale <= 16) { | |||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$scale > 8 and $scale <= 16
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
| |||||||||||||||||||||||||||||||||||||||
| 21 | 0 | $scale = 16; | |||||||||||||||||||||||||||||||||||||
| 22 | ▶ | } elsif ($scale > 16 && $scale <= 32) { | |||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$scale > 16 and $scale <= 32
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
| |||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $scale = 32; | |||||||||||||||||||||||||||||||||||||
| 24 | ▶ | } elsif ($scale > 32 && $scale <= 64) { | |||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$scale > 32 and $scale <= 64
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
| |||||||||||||||||||||||||||||||||||||||
| 25 | 0 | $scale = 64; | |||||||||||||||||||||||||||||||||||||
| 26 | ▶ | } elsif ($scale > 64 && $scale <= 128) { | |||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$scale > 64 and $scale <= 128
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
| |||||||||||||||||||||||||||||||||||||||
| 27 | 0 | $scale = 128; | |||||||||||||||||||||||||||||||||||||
| 28 | ▶ | } elsif ($scale > 128 && $scale <= 256) { | |||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$scale > 128 and $scale <= 256
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
| |||||||||||||||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||||||||||||
| 34 | 2 | return $psx; | |||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | sub _get_wdt_prescaler { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_wdt_prescaler: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||||||||||||
| 38 | 1 | my ($self, $period) = @_; | |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | return wantarray ? ($_, $wdtps->{$_}) : $wdtps->{$_}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $maxscale = pop @psv; | |||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return wantarray ? ($maxscale, $wdtps->{$maxscale}) : $wdtps->{$maxscale}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | sub timer_enable { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
timer_enable: called CC=13 SCAR=28.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 58 | 3 | my ($self, $tmr, $freq, %isr) = @_; | |||||||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | return unless $self->doesroles(qw(Timer Chip)); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 60 | 3 | my ($code, $funcs, $macros) = ('', {}, {}); | |||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | if ($tmr eq 'WDT') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | unless (exists $self->registers->{WDTCON}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 67 | 1 | foreach (keys %{$self->chip_config->{on_off}}) { | |||||||||||||||||||||||||||||||||||||
| 68 | 7 | ▶ | $self->chip_config->{on_off}->{$_} = 1 if $_ =~ /WDT/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 82 | 0 | carp "$tmr is not a timer."; | |||||||||||||||||||||||||||||||||||||
| 83 | 0 | return; | |||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | unless (exists $self->registers->{OPTION_REG}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 108 | 1 | $code .= $self->isr_timer($th); | |||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||
| 110 | 2 | $code .= "\n$end_code\n"; | |||||||||||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if (%isr) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 112 | 1 | $funcs->{isr_timer} = $self->isr_timer($th, %isr); | |||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||
| 115 | 3 | ▶ | return wantarray ? ($code, $funcs, $macros) : $code; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub timer_disable { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
timer_disable: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my ($self, $tmr) = @_; | |||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return unless $self->doesroles(qw(Timer Chip)); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | unless (exists $self->timer_pins->{$tmr}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 122 | 0 | carp "$tmr is not a timer."; | |||||||||||||||||||||||||||||||||||||
| 123 | 0 | return; | |||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | unless (exists $self->registers->{OPTION_REG} and | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $self->registers->{"OPTION_REG"} and exists $self->registers->{"INTCON"}
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"}
| |||||||||||||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$th->{'enable'} || 'T0IE'
$th->{'enable'}
Truth table0%0/2
A
A= $th->{'enable'}
| |||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $tm_ereg = $th->{ereg} || 'INTCON'; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$th->{'ereg'} || 'INTCON'
MC/DC0%0/1
$th->{'ereg'} || 'INTCON'
$th->{'ereg'}
Truth table0%0/2
A
A= $th->{'ereg'}
| |||||||||||||||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub timer { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
timer: called CC=7 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 146 | 1 | my ($self, $tmr, %action) = @_; | |||||||||||||||||||||||||||||||||||||
| 147 | 1 | ▶ | return unless exists $action{ACTION}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 148 | 1 | ▶ | return unless $self->doesroles(qw(Timer Chip)); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 149 | 1 | ▶ | return unless exists $action{END}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 150 | 1 | ▶ | unless (exists $self->registers->{INTCON}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$th->{'flag'} || 'T0IF'
$th->{'flag'}
Truth table50%1/2
A
A= $th->{'flag'}
| |||||||||||||||||||||||||||||||||||||||
| 156 | 1 | ▶ | my $tm_freg = $th->{freg} || 'INTCON'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$th->{'freg'} || 'INTCON'
MC/DC0%0/1
$th->{'freg'} || 'INTCON'
$th->{'freg'}
Truth table50%1/2
A
A= $th->{'freg'}
| |||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||