Info.pm

filter: Statementstmt 21.0% 27 / 128 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 18.4% 7 / 38 Podpod 100% 16 / 16 totaltotal 22.5% 50 / 222 CC 25 SCAR 20.2
CC
25
Cov
17%
property_group
20.0
_module_by_name
12.0
interface
12.0
CRAP 7.5
« Info/Module.pm ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2014-2021 -- leonerd@leonerd.org.uk
5  
6 package Device::AVR::Info;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.03';
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use XML::Smart;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Struct::Dumb 'readonly_struct';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Device::AVR::Info::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 C<Device::AVR::Info> - load data from F<Atmel> F<AVR Studio> device files
24  
25 =head1 SYNOPSIS
26  
27 use Device::AVR::Info;
28  
29 my $avr = Device::AVR::Info->new_from_file( "devices/ATtiny84.xml" );
30  
31 printf "The signature of %s is %s\n",
32 $avr->name, $avr->signature;
33  
34 =head1 DESCRIPTION
35  
36 This module loads an parses "part info" XML files as supplied with F<Atmel>'s
37 F<AVR Studio>, and provides convenient access to the data stored inside them.
38  
39 =cut
40  
41 =head1 CONSTRUCTORS
42  
43 =cut
44  
45 =head2 new_from_file
46  
47 $avr = Device::AVR::Info->new_from_file( $filename )
48  
49 Loads the device information from the given XML file.
50  
51 =cut
52  
53 sub new_from_file
54 0 ▶{
Subroutine new_from_file: not called CC=2 SCAR=17.9
Pod documented
55 0 my $class = shift;
56 0 my ( $filename ) = @_;
57  
58 0 my $root = XML::Smart->new( $filename );
59  
60 0 my $devices = $root->{"avr-tools-device-file"}{devices};
61 0 ▶ @$devices == 1 or
Branch: 1 branch
BranchTrueFalseTotal
@$devices == 1 or croak('Expected only one device, found ' . scalar(@$devices)) 0 0 0
62 croak "Expected only one device, found " . scalar(@$devices);
63  
64 bless {
65 _device => $devices->[0]{device},
66 _modules => $root->{"avr-tools-device-file"}{modules}{module},
67 0 }, $class;
68 }
69  
70 sub _module_by_name
71 0 ▶{
Subroutine _module_by_name: not called CC=3 SCAR=24.8
72 0 my $self = shift;
73 0 my ( $name ) = @_;
74  
75 0 foreach ( @{ $self->{_modules} } ) {
76 0 ▶ return Device::AVR::Info::Module->_new( $_ ) if $_->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"name"} eq $name 0 0 0
77 }
78  
79 0 croak "No module named '$name' is defined";
80 }
81  
82 =head1 ACCESSORS
83  
84 =cut
85  
86 =head2 name
87  
88 $name = $avr->name
89  
90 The device name (e.g. "ATtiny84")
91  
92 =head2 architecture
93  
94 $architecture = $avr->architecture
95  
96 The device architecture (e.g. "AVR8")
97  
98 =head2 family
99  
100 $family = $avr->family
101  
102 The device family (e.g. "tinyAVR")
103  
104 =cut
105  
106 0 ▶sub name { shift->{_device}{name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
107 0 ▶sub architecture { shift->{_device}{architecture} }
Subroutine architecture: not called CC=1 SCAR=6.9
Pod documented
108 0 ▶sub family { shift->{_device}{family} }
Subroutine family: not called CC=1 SCAR=6.9
Pod documented
109  
110 =head2 interfaces
111  
112 =head2 interface
113  
114 @ifaces = $avr->interfaces
115  
116 $iface = $avr->interface( $name )
117  
118 Returns a list of interface instances, or a single one having the given name,
119 representing the programming interfaces supported by the device.
120  
121 Each is a structure of the following fields.
122  
123 $iface->name
124 $iface->type
125  
126 =cut
127  
128 1 readonly_struct Interface => [qw( type name )];
129  
130 sub interfaces
131 0 ▶{
Subroutine interfaces: not called CC=2 SCAR=17.9
Pod documented
132 0 my $self = shift;
133 0 return @{ $self->{interfaces} //= [ map {
134 0 Interface( "$_->{type}", "$_->{name}" )
135 0 ▶ } @{ $self->{_device}{interfaces}{interface} } ] };
Condition0%0/2
$self->{"interfaces"} //= [map({Interface(qq[$_->{"type"}], qq[$_->{"name"}]);} @{$$self{"_device"}{"interfaces"}{"interface"};})]
l!l
00
MC/DC0%0/1
$self->{"interfaces"} //= [map({Interface(qq[$_->{"type"}], qq[$_->{"name"}]);} @{$$self{"_device"}{"interfaces"}{"interface"};})]
$self->{"interfaces"}
Truth table0%0/2
A
A= $self->{"interfaces"}
Aresult
11
00
136 }
137  
138 sub interface
139 0 ▶{
Subroutine interface: not called CC=3 SCAR=24.8
Pod documented
140 0 my $self = shift;
141 0 my ( $name ) = @_;
142 0 ▶ $_->name eq $name and return $_ for $self->interfaces;
Branch: 1 branch
BranchTrueFalseTotal
$_->name eq $name and return $_ 0 0 0
143 0 return;
144 }
145  
146 =head2 memories
147  
148 =head2 memory
149  
150 @memories = $avr->memories
151  
152 $memory = $avr->memory( $name )
153  
154 Returns a list of memory instances, or a single one having the given name,
155 representing the available memories on the device.
156  
157 Each is a structure of the following fields.
158  
159 $memory->name
160 $memory->id
161 $memory->endianness
162 $memory->start # in bytes
163 $memory->size # in bytes
164 @segments = $memory->segments
165 $segment = $memory->segment($name)
166  
167 The C<segments> field returns a list of structures of the following fields:
168  
169 $seg->start
170 $seg->size
171 $seg->name
172 $seg->type
173 $seg->can_read
174 $seg->can_write
175 $seg->can_exec
176 $seg->pagesize
177  
178 Note that all sizes are given in bytes; for memories of 16-bit word-size,
179 divide this by 2 to obtain the size in words.
180  
181 =cut
182  
183 {
184 # Can't quite use Struct::Dumb because of list return inflation of 'segments'
185 package
186 Device::AVR::Info::_Memory;
187  
188 0 ▶ sub name { shift->[0] }
Subroutine name: not called CC=1 SCAR=6.9
189 0 ▶ sub id { shift->[1] }
Subroutine id: not called CC=1 SCAR=6.9
190 0 ▶ sub endianness { shift->[2] }
Subroutine endianness: not called CC=1 SCAR=6.9
191 0 ▶ sub start { shift->[3] }
Subroutine start: not called CC=1 SCAR=6.9
192 0 ▶ sub size { shift->[4] }
Subroutine size: not called CC=1 SCAR=6.9
193 0 ▶ sub segments { @{ shift->[5] } }
Subroutine segments: not called CC=1 SCAR=6.9
194 0 ▶ sub segment { $_->name eq $_[1] and return $_ for $_[0]->segments; return }
Subroutine segment: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_->name eq $_[1] and return $_ 0 0 0
195 }
196  
197 1 readonly_struct MemorySegment => [qw( start size name type can_read can_write can_exec pagesize )];
198  
199 sub memories
200 0 ▶{
Subroutine memories: not called CC=3 SCAR=24.8
Pod documented
201 0 my $self = shift;
202 0 return @{ $self->{memories} //= [ map {
203 my @segments = exists $_->{"memory-segment"} ? map {
204 0 my $rw = $_->{rw};
205 0 MemorySegment( hex "$_->{start}", hex "$_->{size}", "$_->{name}", "$_->{type}",
206 scalar $rw =~ m/R/, scalar $rw =~ m/W/, !!"$_->{exec}", hex "$_->{pagesize}" );
207 0 ▶ } @{ $_->{"memory-segment"} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{'memory-segment'} ? : 0 0 0
208  
209 0 bless [ "$_->{name}", "$_->{id}", "$_->{endianness}",
210 hex "$_->{start}", hex "$_->{size}", \@segments ], "Device::AVR::Info::_Memory";
211 0 ▶ } @{ $self->{_device}{"address-spaces"}{"address-space"} } ] };
Condition0%0/2
$self->{"memories"} //= [map({my(@segments) = exists $_->{"memory-segment"} ? map({my $rw = $_->{"rw"}; MemorySegment(hex qq[$_->{"start"}], hex qq[$_->{"size"}], qq[$_->{"name"}], qq[$_->{"type"}], scalar $rw =~ /R/, scalar $rw =~ /W/, !!qq[$_->{"exec"}], hex qq[$_->{"pagesize"}]);} @{$$_{"memory-segment"};}) : (); bless [qq[$_->{"name"}], qq[$_->{"id"}], qq[$_->{"endianness"}], hex qq[$_->{"start"}], hex qq[$_->{"size"}], \@segments], "Device::AVR::Info::_Memory";} @{$$self{"_device"}{"address-spaces"}{"address-space"};})]
l!l
00
MC/DC0%0/1
$self->{"memories"} //= [map({my(@segments) = exists $_->{"memory-segment"} ? map({my $rw = $_->{"rw"}; MemorySegment(hex qq[$_->{"start"}], hex qq[$_->{"size"}], qq[$_->{"name"}], qq[$_->{"type"}], scalar $rw =~ /R/, scalar $rw =~ /W/, !!qq[$_->{"exec"}], hex qq[$_->{"pagesize"}]);} @{$$_{"memory-segment"};}) : (); bless [qq[$_->{"name"}], qq[$_->{"id"}], qq[$_->{"endianness"}], hex qq[$_->{"start"}], hex qq[$_->{"size"}], \@segments], "Device::AVR::Info::_Memory";} @{$$self{"_device"}{"address-spaces"}{"address-space"};})]
$self->{"memories"}
Truth table0%0/2
A
A= $self->{"memories"}
Aresult
11
00
212 }
213  
214 sub memory
215 0 ▶{
Subroutine memory: not called CC=3 SCAR=24.8
Pod documented
216 0 my $self = shift;
217 0 my ( $name ) = @_;
218 0 ▶ $_->name eq $name and return $_ for $self->memories;
Branch: 1 branch
BranchTrueFalseTotal
$_->name eq $name and return $_ 0 0 0
219 0 return;
220 }
221  
222 sub _memory_by_id
223 0 ▶{
Subroutine _memory_by_id: not called CC=3 SCAR=24.8
224 0 my $self = shift;
225 0 my ( $id ) = @_;
226 0 ▶ $_->id eq $id and return $_ for $self->memories;
Branch: 1 branch
BranchTrueFalseTotal
$_->id eq $id and return $_ 0 0 0
227 0 return;
228 }
229  
230 =head2 interrupts
231  
232 =head2 interrupt
233  
234 @ints = $avr->interrupts
235  
236 $int = $avr->interrupt( $name )
237  
238 Returns a list of interrupt instances, or a single one having the given name,
239 representing the interrupt sources available on the device.
240  
241 Each is a structure of the following fields.
242  
243 $int->name
244 $int->index
245 $int->caption
246  
247 =cut
248  
249 1 readonly_struct Interrupt => [qw( name index caption )];
250  
251 sub interrupts
252 0 ▶{
Subroutine interrupts: not called CC=2 SCAR=17.9
Pod documented
253 0 my $self = shift;
254 0 return @{ $self->{interrupts} //= [ map {
255 0 Interrupt( "$_->{name}", "$_->{index}", "$_->{caption}" )
256 0 ▶ } @{ $self->{_device}{interrupts}{interrupt} } ] };
Condition0%0/2
$self->{"interrupts"} //= [map({Interrupt(qq[$_->{"name"}], qq[$_->{"index"}], qq[$_->{"caption"}]);} @{$$self{"_device"}{"interrupts"}{"interrupt"};})]
l!l
00
MC/DC0%0/1
$self->{"interrupts"} //= [map({Interrupt(qq[$_->{"name"}], qq[$_->{"index"}], qq[$_->{"caption"}]);} @{$$self{"_device"}{"interrupts"}{"interrupt"};})]
$self->{"interrupts"}
Truth table0%0/2
A
A= $self->{"interrupts"}
Aresult
11
00
257 }
258  
259 sub interrupt
260 0 ▶{
Subroutine interrupt: not called CC=3 SCAR=24.8
Pod documented
261 0 my $self = shift;
262 0 my ( $name ) = @_;
263 0 ▶ $_->name eq $name and return $_ for $self->interrupts;
Branch: 1 branch
BranchTrueFalseTotal
$_->name eq $name and return $_ 0 0 0
264 0 return;
265 }
266  
267 =head2 peripherals
268  
269 =head2 peripheral
270  
271 @periphs = $avr->peripherals
272  
273 $periph = $avr->peripheral( $name )
274  
275 Returns a list of peripheral instances, or a single one having the given name,
276 representing the peripherals or other miscellaneous information available on
277 the device.
278  
279 Each is a structure of the following fields.
280  
281 $periph->name
282 $periph->module # instance of Device::AVR::Info::Module
283 $periph->regname
284 $periph->regspace # instance of $memory
285  
286 @registers = $periph->registers
287 # instances of $register from Device::AVR::Info::Module
288  
289 =cut
290  
291 {
292 package
293 Device::AVR::Info::_Peripheral;
294  
295 0 ▶ sub name { shift->[0] }
Subroutine name: not called CC=1 SCAR=6.9
296 0 ▶ sub module { shift->[1] }
Subroutine module: not called CC=1 SCAR=6.9
297 0 ▶ sub regname { shift->[2] }
Subroutine regname: not called CC=1 SCAR=6.9
298 0 ▶ sub regoffset { shift->[3] }
Subroutine regoffset: not called CC=1 SCAR=6.9
299 0 ▶ sub regspace { shift->[4] }
Subroutine regspace: not called CC=1 SCAR=6.9
300  
301 0 ▶ sub registers {
Subroutine registers: not called CC=1 SCAR=6.9
302 0 my $self = shift;
303 0 $self->module->_registers_offset( $self->regname, $self->regoffset );
304 }
305 }
306  
307 sub peripherals
308 1 ▶{
Subroutine peripherals: not called CC=2 SCAR=16.3
Pod documented
309 0 my $self = shift;
310 0 return @{ $self->{peripherals} //= [ map {
311 0 my $module = $self->_module_by_name( "$_->{name}" );
312 map {
313 0 my $reggroup = $_->{"register-group"}[0];
314 bless [ "$_->{name}", $module,
315 "$reggroup->{'name-in-module'}", hex "$reggroup->{offset}",
316 0 $self->_memory_by_id( $reggroup->{"address-space"} ) ], "Device::AVR::Info::_Peripheral";
317 0 } @{ $_->{instance} };
318 0 ▶ } @{ $self->{_device}{peripherals}{module} } ] };
Condition0%0/2
$self->{"peripherals"} //= [map({my $module = $self->_module_by_name(qq[$_->{"name"}]); map {my $reggroup = $_->{"register-group"}[0]; bless [qq[$_->{"name"}], $module, qq[$reggroup->{"name-in-module"}], hex qq[$reggroup->{"offset"}], $self->_memory_by_id($reggroup->{"address-space"})], "Device::AVR::Info::_Peripheral";} @{$$_{"instance"};};} @{$$self{"_device"}{"peripherals"}{"module"};})]
l!l
00
MC/DC0%0/1
$self->{"peripherals"} //= [map({my $module = $self->_module_by_name(qq[$_->{"name"}]); map {my $reggroup = $_->{"register-group"}[0]; bless [qq[$_->{"name"}], $module, qq[$reggroup->{"name-in-module"}], hex qq[$reggroup->{"offset"}], $self->_memory_by_id($reggroup->{"address-space"})], "Device::AVR::Info::_Peripheral";} @{$$_{"instance"};};} @{$$self{"_device"}{"peripherals"}{"module"};})]
$self->{"peripherals"}
Truth table0%0/2
A
A= $self->{"peripherals"}
Aresult
11
00
319 }
320  
321 sub peripheral
322 0 ▶{
Subroutine peripheral: not called CC=3 SCAR=24.8
Pod documented
323 0 my $self = shift;
324 0 my ( $name ) = @_;
325  
326 0 $self->peripherals;
327  
328 0 ▶ $_->name eq $name and return $_ for @{ $self->{peripherals} };
Branch: 1 branch
BranchTrueFalseTotal
$_->name eq $name and return $_ 0 0 0
329 0 return;
330 }
331  
332 =head2 property_groups
333  
334 @group_names = $avr->property_groups
335  
336 Returns (in no particular order) the names of the defined property groups.
337  
338 =head2 property_group
339  
340 \%values = $avr->property_group( $group_name )
341  
342 Returns a HASH reference of all the properties in the given property group.
343  
344 =head2 property
345  
346 $value = $avr->property( $group_name, $prop_name )
347  
348 Returns a single value of a property in the given property group.
349  
350 Any value given in the XML file in the form of a single hexadecimal numerical
351 constant is automatically converted into the appropriate integer. Strings of
352 multiple numbers (such as the HVSP and HVPP control stacks) are not converted.
353  
354 =cut
355  
356 sub property_groups
357 0 ▶{
Subroutine property_groups: not called CC=2 SCAR=17.9
Pod documented
358 0 my $self = shift;
359 $self->{property_groups} //= { map {
360 +( "$_->{name}", $_->{property} )
361 0 ▶ } @{ $self->{_device}{"property-groups"}{"property-group"} } };
Condition0%0/2
$self->{"property_groups"} //= {map({qq[$_->{"name"}], $_->{"property"};} @{$$self{"_device"}{"property-groups"}{"property-group"};})}
l!l
00
MC/DC0%0/1
$self->{"property_groups"} //= {map({qq[$_->{"name"}], $_->{"property"};} @{$$self{"_device"}{"property-groups"}{"property-group"};})}
$self->{"property_groups"}
Truth table0%0/2
A
A= $self->{"property_groups"}
Aresult
11
00
362 0 return keys %{ $self->{property_groups} };
363 }
364  
365 sub property_group
366 0 ▶{
Subroutine property_group: not called CC=4 SCAR=30.0
Pod documented
367 0 my $self = shift;
368 0 my ( $group ) = @_;
369  
370 0 $self->property_groups;
371 0 ▶ my $properties = $self->{property_groups}{$group} or
Branch: 1 branch
BranchTrueFalseTotal
my $properties = $self->{"property_groups"}{$group} or croak("No such property group '${group}'") 0 0 0
372 croak "No such property group '$group'";
373  
374 return $self->{properties}{$group} //= { map {
375 0 ▶ my $value = $_->{value};
Condition0%0/2
$self->{"properties"}{$group} //= {map({my $value = $_->{"value"}; $value = hex $value if $value =~ /^0x[[:xdigit:]]+$/; qq[$_->{"name"}], "$value";} @$properties)}
l!l
00
MC/DC0%0/1
$self->{"properties"}{$group} //= {map({my $value = $_->{"value"}; $value = hex $value if $value =~ /^0x[[:xdigit:]]+$/; qq[$_->{"name"}], "$value";} @$properties)}
$self->{"properties"}{$group}
Truth table0%0/2
A
A= $self->{"properties"}{$group}
Aresult
11
00
376 0 ▶ $value = hex $value if $value =~ m/^0x[[:xdigit:]]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^0x[[:xdigit:]]+$/ 0 0 0
377  
378 0 +( "$_->{name}", "$value" )
379 } @$properties };
380 }
381  
382 sub property
383 0 ▶{
Subroutine property: not called CC=1 SCAR=6.9
Pod documented
384 0 my $self = shift;
385 0 my ( $group, $prop ) = @_;
386  
387 0 return $self->property_group( $group )->{$prop};
388 }
389  
390 =head1 DERIVED METHODS
391  
392 These methods wrap information provided by the basic accessors.
393  
394 =cut
395  
396 =head2 signature
397  
398 $sig = $avr->signature
399  
400 Returns a 6-character hexadecimal string consisting of the three bytes of the
401 device signature.
402  
403 =cut
404  
405 sub signature
406 0 ▶{
Subroutine signature: not called CC=1 SCAR=6.9
Pod documented
407 0 my $self = shift;
408 return sprintf "%02x%02x%02x",
409 0 map { $self->property( SIGNATURES => "SIGNATURE$_" ) } 0 .. 2;
410 }
411  
412 =head1 AUTHOR
413  
414 Paul Evans <leonerd@leonerd.org.uk>
415  
416 =cut
417  
418 1 0x55AA;
« Info/Module.pm ↑ Summary