blib/lib/PINE64/MCP3208.pm

filter: Statementstmt 29.5% 21 / 71 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 60.0% 3 / 5 Podpod 100% 2 / 2 totaltotal 23.8% 26 / 109 CC 12 SCAR 47.6
CC
12
Cov
22%
read3208
156.0
new
2.0
BEGIN
1.0
CRAP 117.3
↑ Summary
1 #!/usr/bin/perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use PINE64::GPIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package PINE64::MCP3208;
7  
8 1 our $VERSION = '0.901';
9  
10 #SPI / bitbang varables
11 1 my ($clk, $din, $dout, $chpsel);
12  
13 #channels: first bit is set to single, not differential
14 #5 bits because the first is the start bit
15 1 my @ch0 = (1,1,0,0,0);
16 1 my @ch1 = (1,1,0,0,1);
17 1 my @ch2 = (1,1,0,1,0);
18 1 my @ch3 = (1,1,0,1,1);
19 1 my @ch4 = (1,1,1,0,0);
20 1 my @ch5 = (1,1,1,0,1);
21 1 my @ch6 = (1,1,1,1,0);
22 1 my @ch7 = (1,1,1,1,1);
23  
24 #instantiate PINE64 gpio device
25 1 my $p64 = PINE64::GPIO->new();
26  
27 0 ▶sub new{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
28 0 my $class = shift;
29 0 my $self = bless {}, $class;
30  
31 #
32 0 $clk = $_[0]; #clock
33 0 $din = $_[1]; #instructions to adc
34 0 $dout = $_[2]; #10-bit output from adc
35 0 $chpsel = $_[3]; #latch / load to init comm to adc
36  
37 #init gpio lines
38 0 $p64->gpio_enable($clk, 'out');
39 0 $p64->gpio_enable($din, 'out');
40 0 $p64->gpio_enable($dout, 'in');
41 0 $p64->gpio_enable($chpsel, 'out');
42
43 #init chpsel high, comm begins when
44 #chipsel goes from high to low
45 0 $p64->gpio_write($chpsel, 1);
46  
47 0 return $self;
48  
49 }#end new
50  
51 0 ▶sub read3208{
Subroutine read3208: not called CC=12 SCAR=50.5
Pod documented
52 #init empty array that will
53 #contain 10-bit reading from ADC
54 0 my @reading = ();
55 #get reading from the MCP3004 ADC
56 #starts comm when cs goes from high to low
57
58 #ADC channel number
59 #channel is an array reference
60 0 my $channel = $_[1];
61  
62 #delay between clock pulses
63 #needs to operate at 10KHz so
64 #sample is accurate, so min usleep(100)
65 0 my $delay = $_[2];
66  
67 #reference voltate, used only for calculations
68 #can be omitted
69 0 my $vref = $_[3];
70  
71 #main clock variable
72 0 my $i=0;
73
74 #high flag for data gpio line
75 0 my $hf = 0;
76  
77 #number of clock pulses,
78 0 my $ncp = 38; #for 20 clock pulses
79  
80 #high or low state of clock pulse
81 0 my $state = 0;
82 0 my $seed = 3; #seed used to determine high or low, start 3 so first pulse is high
83
84 #toggles CS line to init communication with the adc
85 #start low, go high, then low. comm begins when CS
86 #brought from high to low
87 0 $p64->gpio_write($chpsel, 0);
88 0 Time::HiRes::usleep($delay);
89
90 #main loop
91 0 ▶ while($i<$ncp){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ncp) 0 0 0
92 0 $state = $seed%2;#toggles between 1 and 0
93 0 $seed++;
94  
95 #clock pulse high
96 0 ▶ if(($i%2) eq 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2 eq 0) 0 0 0
97 #print "i: $i\tcp high\tstate: $state\n";
98  
99 0 ▶ if($channel->[$i/2] eq 1 && $i <=8){
Branch: 1 branch
BranchTrueFalseTotal
if ($$channel[$i / 2] eq 1 and $i <= 8) 0 0 0
Condition0%0/3
$$channel[$i / 2] eq 1 and $i <= 8
!ll&&!rl&&r
000
MC/DC0%0/2
$$channel[$i / 2] eq 1 and $i <= 8
$$channel[$i / 2] eq 1 $i <= 8
Truth table0%0/3
A and B
A= $$channel[$i / 2] eq 1B= $i <= 8
ABresult
0X0
100
111
100 0 $p64->gpio_write($din, 1);
101 0 $hf = 1;#set high flag
102 #print "ch[".($i/2)."]: ".$channel->[$i/2]."\n";
103 }#end if
104 0 ▶ if($channel->[$i/2] eq 0 && $i <=8){
Branch: 1 branch
BranchTrueFalseTotal
if ($$channel[$i / 2] eq 0 and $i <= 8) 0 0 0
Condition0%0/3
$$channel[$i / 2] eq 0 and $i <= 8
!ll&&!rl&&r
000
MC/DC0%0/2
$$channel[$i / 2] eq 0 and $i <= 8
$$channel[$i / 2] eq 0 $i <= 8
Truth table0%0/3
A and B
A= $$channel[$i / 2] eq 0B= $i <= 8
ABresult
0X0
100
111
105 0 $p64->gpio_write($din,0);
106 }#end if zero on data line
107 }#end if high
108  
109 #if($i>8){#data line
110 #everything after D0 bit is don't care,
111 #set to state
112 #gpio_write($din, $state);
113 #}#end else
114  
115 #clock pulse
116 0 $p64->gpio_write($clk, $state);
117  
118 #read data out
119 #data clocked out on falling
120 #edge of clk
121 0 ▶ if(($i%2) == 1 && $i >12){
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2 == 1 and $i > 12) 0 0 0
Condition0%0/3
$i % 2 == 1 and $i > 12
!ll&&!rl&&r
000
MC/DC0%0/2
$i % 2 == 1 and $i > 12
$i % 2 == 1 $i > 12
Truth table0%0/3
A and B
A= $i % 2 == 1B= $i > 12
ABresult
0X0
100
111
122 #read state of gpio pin connected
123 #to data out of ADC
124 0 push @reading, $p64->gpio_read($dout);
125 #print "i[$i]: ".$reading[($i-14)/2]."\n"
126 }#end read data out
127  
128 #lower data if high flag set
129 0 ▶ if($hf eq 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($hf eq 1) 0 0 0
130 #gpio_write($din,0);
131 0 $hf = 0;#reset high flag
132 }#end if
133
134 #pause between clk pls
135 0 Time::HiRes::usleep($delay);
136  
137 #increment counter
138 0 $i++;
139 }#end while
140
141 #make din low
142 0 $p64->gpio_write($din, 0);
143  
144 #make chip select high
145 0 $p64->gpio_write($chpsel, 1);
146  
147 #perform calcuations, return a voltage
148 #based of 12-bit reading, and vref val
149 0 my $bindig = 2048;
150 0 my $rdgval = 0;
151 0 my $voltage = 0;
152 0 ▶ for(my $x=0; $x<12;$x++){
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 12) 0 0 0
153 0 ▶ if(@reading[$x] == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@reading[$x] == 1) 0 0 0
154 0 $rdgval = $rdgval + $bindig;
155 }#end if
156 0 $bindig = $bindig / 2;
157 }#end for
158  
159 #voltage calculation
160 0 $voltage = ($rdgval*$vref)/4096;
161  
162 0 return (\@reading, $rdgval, $voltage);
163 }#end read3208
164  
165 1 1;
166 __END__
↑ Summary