VIC/PIC/Functions/ECCP.pm

filter: Statementstmt 90.5% 125 / 138 Branchbran 61.6% 37 / 60 Conditioncond 36.6% 11 / 30 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 4 totaltotal 71.6% 187 / 261 SCAR 42.7
CC
44
Cov
76%
_pwm_details
22.6
pwm_fullbridge
14.4
pwm_update
11.1
CRAP 71.2
« VIC/PIC/Functions/ADC.pm ↑ Summary VIC/PIC/Base.pm »
1 package VIC::PIC::Functions::ECCP;
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 ▶use bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 our $VERSION = '0.29';
6 31 $VERSION = eval $VERSION;
7 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #FIXME: C2OUT and P1B may be conflicting. check datasheet
12 sub _pwm_details {
13 6 ▶ my ($self, $pwm_frequency, $duty, $type, @pins) = @_;
Subroutine _pwm_details: called CC=21 SCAR=31.2
14 6 ▶ return unless $self->doesrole('Chip');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesrole("Chip") 0 6 6
15 6 ▶ unless (exists $self->registers->{CCP1CON}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->registers->{"CCP1CON"}) 0 6 6
16 0 carp $self->type, " does not have CCP1CON for ECCP features";
17 0 return;
18 }
19 31 ▶ no bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 #pulse_width = $duty / $pwm_frequency;
21 # timer2 prescaler
22 6 my $prescaler = 1; # can be 1, 4 or 16
23 # Tosc = 1 / Fosc
24 6 my $f_osc = $self->f_osc;
25 6 my $pr2 = POSIX::ceil(($f_osc / 4) / $pwm_frequency); # assume prescaler = 1 here
26 6 ▶ if (($pr2 - 1) <= 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pr2 - 1 <= 255) { } 0 6 6
27 0 $prescaler = 1; # prescaler stays 1
28 } else {
29 6 $pr2 = POSIX::ceil($pr2 / 4); # prescaler is 4 or 16
30 6 ▶ $prescaler = (($pr2 - 1) <= 0xFF) ? 4 : 16;
Branch: 1 branch
BranchTrueFalseTotal
$pr2 - 1 <= 255 ? : 6 0 6
31 }
32 6 my $t2con = q{b'00000100'}; # prescaler is 1 or anything else
33 6 ▶ $t2con = q{b'00000101'} if $prescaler == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $prescaler == 4 6 0 6
34 6 ▶ $t2con = q{b'00000111'} if $prescaler == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $prescaler == 16 0 6 6
35 # readjusting PR2 as per supported pre-scalers
36 6 $pr2 = POSIX::ceil((($f_osc / 4) / $pwm_frequency) / $prescaler);
37 6 $pr2--;
38 6 $pr2 &= 0xFF;
39 6 my $ccpr1l_ccp1con54 = POSIX::ceil(($duty * 4 * ($pr2)) / 100.0);
40 6 my $ccp1con5 = ($ccpr1l_ccp1con54 & 0x02); #bit 5
41 6 my $ccp1con4 = ($ccpr1l_ccp1con54 & 0x01); #bit 4
42 6 my $ccpr1l = ($ccpr1l_ccp1con54 >> 2) & 0xFF;
43 6 my $ccpr1l_x = sprintf "0x%02X", $ccpr1l;
44 6 my $pr2_x = sprintf "0x%02X", ($pr2 - 1); ##HACK
45 6 ▶ my $p1m = '00' if $type eq 'single';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "single" 3 3 6
46 6 ▶ $p1m = '01' if $type eq 'full_forward';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "full_forward" 1 5 6
47 6 ▶ $p1m = '10' if $type eq 'half';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "half" 1 5 6
48 6 ▶ $p1m = '11' if $type eq 'full_reverse';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "full_reverse" 1 5 6
49 6 ▶ $p1m = '00' unless defined $p1m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p1m 0 6 6
50 6 my $ccp1con = sprintf "b'%s%d%d1100'", $p1m, $ccp1con5, $ccp1con4;
51 6 my %str = (CCP1 => 0, P1D => 0, P1C => 0, P1B => 0, P1A => 0); # default all are port pins
52 6 my %trisc = ();
53 6 foreach my $pin (@pins) {
54 16 ▶ unless (exists $self->pins->{$pin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->pins->{$pin}) 0 16 16
55 0 carp "$pin is not a valid pin on the microcontroller. Ignoring\n";
56 0 next;
57 }
58 16 my $pinno = $self->pins->{$pin};
59 16 my $allpins = $self->pins->{$pinno};
60 16 my $pwm_pin;
61 16 foreach (@$allpins) {
62 48 ▶ next unless exists $self->eccp_pins->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->eccp_pins->{$_} 32 16 48
63 16 $pwm_pin = $_;
64 16 last;
65 }
66 16 ▶ next unless defined $pwm_pin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pwm_pin 0 16 16
67 # the user may use say RC5 instead of CCP1 and we still want the
68 # CCP1 name which should really be returned as P1A here
69 # pulse steering only needed in Single mode
70 16 my ($p0, $trisp, $portpin) = @{$self->eccp_pins->{$pwm_pin}};
71 16 ▶ $str{$pwm_pin} = 1 if $type eq 'single';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "single" 6 10 16
72 16 $trisc{$portpin} = $trisp;
73 }
74 6 ▶ my $p1a = $str{P1A} || $str{CCP1};
Condition66%2/3
$str{'P1A'} || $str{'CCP1'}
l!l&&r!l&&!r
033
MC/DC50%1/2
$str{'P1A'} || $str{'CCP1'}
$str{'P1A'} $str{'CCP1'}
Truth table66%2/3
A || B
A= $str{'P1A'}B= $str{'CCP1'}
ABresult
1X1
011
000
75 6 my $pstrcon = sprintf "b'0001%d%d%d%d'", $str{P1D}, $str{P1C}, $str{P1B}, $p1a;
76 6 my $trisc_bsf = '';
77 6 my $trisc_bcf = '';
78 6 foreach (sort (keys %trisc)) {
79 16 my $trisp = $trisc{$_};
80 16 $trisc_bsf .= "\tbsf $trisp, $trisp$_\n";
81 16 $trisc_bcf .= "\tbcf $trisp, $trisp$_\n";
82 }
83 6 my $pstrcon_code = '';
84 6 ▶ if ($type eq 'single') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "single") 3 3 6
85 3 $pstrcon_code = << "...";
86 \tbanksel PSTRCON
87 \tmovlw $pstrcon
88 \tmovwf PSTRCON
89 ...
90 }
91 return (
92 # actual register values
93 6 CCP1CON => $ccp1con,
94 PR2 => $pr2_x,
95 T2CON => $t2con,
96 CCPR1L => $ccpr1l_x,
97 PSTRCON => $pstrcon,
98 PSTRCON_CODE => $pstrcon_code,
99 # no ECCPAS
100 PWM1CON => '0x80', # default
101 # code to be added
102 TRISC_BSF => $trisc_bsf,
103 TRISC_BCF => $trisc_bcf,
104 # general comments
105 CCPR1L_CCP1CON54 => $ccpr1l_ccp1con54,
106 FOSC => $f_osc,
107 PRESCALER => $prescaler,
108 PWM_FREQUENCY => $pwm_frequency,
109 DUTYCYCLE => $duty,
110 PINS => \@pins,
111 TYPE => $type,
112 );
113 }
114  
115 sub _pwm_code {
116 5 ▶ my $self = shift;
Subroutine _pwm_code: called CC=1 SCAR=0.0
117 5 my %details = @_;
118 5 my @pins = @{$details{PINS}};
119 5 return << "...";
120 ;;; PWM Type: $details{TYPE}
121 ;;; PWM Frequency = $details{PWM_FREQUENCY} Hz
122 ;;; Duty Cycle = $details{DUTYCYCLE} / 100
123 ;;; CCPR1L:CCP1CON<5:4> = $details{CCPR1L_CCP1CON54}
124 ;;; CCPR1L = $details{CCPR1L}
125 ;;; CCP1CON = $details{CCP1CON}
126 ;;; T2CON = $details{T2CON}
127 ;;; PR2 = $details{PR2}
128 ;;; PSTRCON = $details{PSTRCON}
129 ;;; PWM1CON = $details{PWM1CON}
130 ;;; Prescaler = $details{PRESCALER}
131 ;;; Fosc = $details{FOSC}
132 ;;; disable the PWM output driver for @pins by setting the associated TRIS bit
133 \tbanksel TRISC
134 $details{TRISC_BSF}
135 ;;; set PWM period by loading PR2
136 \tbanksel PR2
137 \tmovlw $details{PR2}
138 \tmovwf PR2
139 ;;; configure the CCP module for the PWM mode by setting CCP1CON
140 \tbanksel CCP1CON
141 \tmovlw $details{CCP1CON}
142 \tmovwf CCP1CON
143 ;;; set PWM duty cycle
144 \tmovlw $details{CCPR1L}
145 \tmovwf CCPR1L
146 ;;; configure and start TMR2
147 ;;; - clear TMR2IF flag of PIR1 register
148 \tbanksel PIR1
149 \tbcf PIR1, TMR2IF
150 \tmovlw $details{T2CON}
151 \tmovwf T2CON
152 ;;; enable PWM output after a new cycle has started
153 \tbtfss PIR1, TMR2IF
154 \tgoto \$ - 1
155 \tbcf PIR1, TMR2IF
156 ;;; enable @pins pin output driver by clearing the associated TRIS bit
157 $details{PSTRCON_CODE}
158 ;;; disable auto-shutdown mode
159 \tbanksel ECCPAS
160 \tclrf ECCPAS
161 ;;; set PWM1CON if half bridge mode
162 \tbanksel PWM1CON
163 \tmovlw $details{PWM1CON}
164 \tmovwf PWM1CON
165 \tbanksel TRISC
166 $details{TRISC_BCF}
167 ...
168 }
169  
170 sub pwm_single {
171 2 ▶ my ($self, $pwm_frequency, $duty, @pins) = @_;
Subroutine pwm_single: called CC=4 SCAR=17.2
Pod undocumented
172 2 ▶ return unless $self->doesrole('ECCP');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesrole("ECCP") 0 2 2
173 2 ▶ unless (exists $self->eccp_pins->{P1A}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->eccp_pins->{"P1A"}) 0 2 2
174 0 ▶ if (exists $self->eccp_pins->{CCP1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->eccp_pins->{"CCP1"}) 0 0 0
175 # override the pins to CCP1
176 0 @pins = qw(CCP1);
177 }
178 }
179 2 my %details = $self->_pwm_details($pwm_frequency, $duty, 'single', @pins);
180 # pulse steering automatically taken care of
181 2 return $self->_pwm_code(%details);
182 }
183  
184 sub pwm_halfbridge {
185 1 ▶ my ($self, $pwm_frequency, $duty, $deadband, @pins) = @_;
Subroutine pwm_halfbridge: called CC=6 SCAR=20.1
Pod undocumented
186 1 ▶ return unless $self->doesrole('ECCP');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesrole("ECCP") 0 1 1
187 1 ▶ if (exists $self->eccp_pins->{P1A} and exists $self->eccp_pins->{P1B}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->eccp_pins->{'P1A'} and exists $self->eccp_pins->{'P1B'}) { } 1 0 1
Condition33%1/3
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"}
exists $self->eccp_pins->{"P1A"} exists $self->eccp_pins->{"P1B"}
Truth table33%1/3
A and B
A= exists $self->eccp_pins->{"P1A"}B= exists $self->eccp_pins->{"P1B"}
ABresult
0X0
100
111
188 # we ignore the @pins that comes in
189 1 @pins = qw(P1A P1B);
190 } else {
191 0 carp $self->type, " has no Enhanced PWM capabilities";
192 0 return;
193 }
194 1 my %details = $self->_pwm_details($pwm_frequency, $duty, 'half', @pins);
195 # override PWM1CON
196 1 ▶ if (defined $deadband and $deadband > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deadband and $deadband > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 1 0 1
Condition33%1/3
defined $deadband and $deadband > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
!ll&&!rl&&r
001
MC/DC0%0/2
defined $deadband and $deadband > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
defined $deadband $deadband > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
Truth table33%1/3
A and B
A= defined $deadbandB= $deadband > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
ABresult
0X0
100
111
197 1 my $fosc = $details{FOSC};
198 1 my $pwm1con = $deadband * $fosc / 4e6; # $deadband is in microseconds
199 1 $pwm1con &= 0x7F; # 6-bits only
200 1 $pwm1con |= 0x80; # clear PRSEN bit
201 1 $details{PWM1CON} = sprintf "0x%02X", $pwm1con;
202 }
203 1 return $self->_pwm_code(%details);
204 }
205  
206 sub pwm_fullbridge {
207 2 ▶ my ($self, $direction, $pwm_frequency, $duty, @pins) = @_;
Subroutine pwm_fullbridge: called CC=9 SCAR=26.7
Pod undocumented
208 2 ▶ return unless $self->doesrole('ECCP');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesrole("ECCP") 0 2 2
209 2 ▶ if (defined $direction and ref $direction eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $direction and ref $direction eq "HASH") 2 0 2
Condition33%1/3
defined $direction and ref $direction eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $direction and ref $direction eq "HASH"
defined $direction ref $direction eq "HASH"
Truth table33%1/3
A and B
A= defined $directionB= ref $direction eq "HASH"
ABresult
0X0
100
111
210 2 $direction = $direction->{string};
211 }
212 2 my $type = 'full_forward';
213 2 ▶ $type = 'full_reverse' if $direction =~ /reverse|backward|no?|0/i;
Branch: 1 branch
BranchTrueFalseTotal
if $direction =~ /reverse|backward|no?|0/i 1 1 2
214 2 ▶ if (exists $self->eccp_pins->{P1A} and exists $self->eccp_pins->{P1B} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->eccp_pins->{'P1A'} and exists $self->eccp_pins->{'P1B'} and exists $self->eccp_pins->{'P1C'} and exists $self->eccp_pins->{'P1D'}) { } 2 0 2
Condition33%3/9
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"} and exists $self->eccp_pins->{"P1C"} and exists $self->eccp_pins->{"P1D"}
!ll&&!rl&&r
002
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"} and exists $self->eccp_pins->{"P1C"}
!ll&&!rl&&r
002
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"}
!ll&&!rl&&r
002
MC/DC0%0/4
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"} and exists $self->eccp_pins->{"P1C"} and exists $self->eccp_pins->{"P1D"}
exists $self->eccp_pins->{"P1A"} exists $self->eccp_pins->{"P1B"} exists $self->eccp_pins->{"P1C"} exists $self->eccp_pins->{"P1D"}
Truth table20%1/5
A and B and C and D
A= exists $self->eccp_pins->{"P1A"}B= exists $self->eccp_pins->{"P1B"}C= exists $self->eccp_pins->{"P1C"}D= exists $self->eccp_pins->{"P1D"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
215 exists $self->eccp_pins->{P1C} and exists $self->eccp_pins->{P1D}) {
216 # we ignore the @pins that comes in
217 2 @pins = qw(P1A P1B P1C P1D);
218 } else {
219 0 carp $self->type, " has no Enhanced PWM capabilities";
220 0 return;
221 }
222 2 my %details = $self->_pwm_details($pwm_frequency, $duty, $type, @pins);
223 2 return $self->_pwm_code(%details);
224 }
225  
226 sub pwm_update {
227 1 ▶ my ($self, $pwm_frequency, $duty) = @_;
Subroutine pwm_update: called CC=8 SCAR=24.0
Pod undocumented
228 1 ▶ return unless $self->doesrole('ECCP');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->doesrole("ECCP") 0 1 1
229 # hack into the existing functions to update only what we need
230 1 my @pins = qw(CCP1);
231 1 ▶ if (exists $self->eccp_pins->{P1A} and exists $self->eccp_pins->{P1B} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"} and exists $self->eccp_pins->{"P1C"} and exists $self->eccp_pins->{"P1D"}) 1 0 1
Condition33%3/9
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"} and exists $self->eccp_pins->{"P1C"} and exists $self->eccp_pins->{"P1D"}
!ll&&!rl&&r
001
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"} and exists $self->eccp_pins->{"P1C"}
!ll&&!rl&&r
001
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"}
!ll&&!rl&&r
001
MC/DC0%0/4
exists $self->eccp_pins->{"P1A"} and exists $self->eccp_pins->{"P1B"} and exists $self->eccp_pins->{"P1C"} and exists $self->eccp_pins->{"P1D"}
exists $self->eccp_pins->{"P1A"} exists $self->eccp_pins->{"P1B"} exists $self->eccp_pins->{"P1C"} exists $self->eccp_pins->{"P1D"}
Truth table20%1/5
A and B and C and D
A= exists $self->eccp_pins->{"P1A"}B= exists $self->eccp_pins->{"P1B"}C= exists $self->eccp_pins->{"P1C"}D= exists $self->eccp_pins->{"P1D"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
232 exists $self->eccp_pins->{P1C} and exists $self->eccp_pins->{P1D}) {
233 # we ignore the @pins that comes in
234 1 @pins = qw(P1A P1B P1C P1D);
235 }
236 1 my %details = $self->_pwm_details($pwm_frequency, $duty, 'single', @pins);
237 1 my ($ccp1con5, $ccp1con4);
238 1 $ccp1con4 = $details{CCPR1L_CCP1CON54} & 0x0001;
239 1 $ccp1con5 = ($details{CCPR1L_CCP1CON54} >> 1) & 0x0001;
240 1 ▶ if ($ccp1con4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ccp1con4) { } 1 0 1
241 1 $ccp1con4 = "\tbsf CCP1CON, DC1B0";
242 } else {
243 0 $ccp1con4 = "\tbcf CCP1CON, DC1B0";
244 }
245 1 ▶ if ($ccp1con5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ccp1con5) { } 0 1 1
246 0 $ccp1con5 = "\tbsf CCP1CON, DC1B1";
247 } else {
248 1 $ccp1con5 = "\tbcf CCP1CON, DC1B1";
249 }
250 1 return << "...";
251 ;;; updating PWM duty cycle for a given frequency
252 ;;; PWM Frequency = $details{PWM_FREQUENCY} Hz
253 ;;; Duty Cycle = $details{DUTYCYCLE} / 100
254 ;;; CCPR1L:CCP1CON<5:4> = $details{CCPR1L_CCP1CON54}
255 ;;; CCPR1L = $details{CCPR1L}
256 ;;; update CCPR1L and CCP1CON<5:4> or the DC1B[01] bits
257 $ccp1con4
258 $ccp1con5
259 \tmovlw $details{CCPR1L}
260 \tmovwf CCPR1L
261 ...
262  
263 }
264  
265  
266 31 1;
267 __END__
« VIC/PIC/Functions/ADC.pm ↑ Summary VIC/PIC/Base.pm »