| 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, 2019 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package App::AVR::Fuses; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use feature qw( say ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 3 | ▶ | use File::ShareDir qw( module_dir ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 3 | ▶ | use Getopt::Long qw( GetOptionsFromArray ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 3 | ▶ | use List::Util qw( first ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 3 | ▶ | use YAML (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 3 | my $SHAREDIR = module_dir( __PACKAGE__ ); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | C<App::AVR::Fuses> - support module for F<avr-fuses> | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | This module contains the support code for the F<avr-fuses> command. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | sub usage | ||||||||||||||||||||
| 34 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
usage: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 35 | 0 | my ( $err ) = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | my ( $basename ) = $0 =~ m{/([^/]+)$}; | |||||||||||||||||||
| 38 | 0 | ▶ | ( $err ? \*STDERR : \*STDOUT )->print( <<"EOF" ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | Usage: $basename [YAML-FILE] FUSES... | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | Options: | ||||||||||||||||||||
| 42 | -h, -help - display this help | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | -v, --verbose - print more verbose messages | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | -p, --part NAME - specify AVR part name as an alternative to giving | ||||||||||||||||||||
| 47 | the YAML-FILE path | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | -f, --fuse FUSE=VALUE - preset the given fuse value | ||||||||||||||||||||
| 50 | FUSE: lfuse | hfuse | efuse | ||||||||||||||||||||
| 51 | VALUE: 123 | 0456 | 0x78 | ||||||||||||||||||||
| 52 | EOF | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | exit $err; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | sub run | ||||||||||||||||||||
| 58 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
run: called CC=12 SCAR=25.4
Pod
undocumented
| |||||||||||||||||||||
| 59 | 9 | my $class = shift; | |||||||||||||||||||
| 60 | 9 | my @argv = @_; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 9 | my $yamlpath; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | my %current; | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | GetOptionsFromArray( | ||||||||||||||||||||
| 67 | \@argv, | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | 'h|help' => sub { usage(0) }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 'v|verbose' => \my $VERBOSE, | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 9 | ▶ | 'p|part=s' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||
| 74 | 9 | my $partname = $_[1]; | |||||||||||||||||||
| 75 | 9 | ▶ | $partname = "ATmega\U$1" if $partname =~ m/^(?:atmega|m)(.*)$/i; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 9 | ▶ | $partname = "ATtiny\U$1" if $partname =~ m/^(?:attiny|t)(.*)$/i; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 9 | $yamlpath = "$SHAREDIR/$partname.yaml"; | |||||||||||||||||||
| 79 | 9 | ▶ | unless( -f $yamlpath ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 0 | print STDERR "No YAML file found at $yamlpath\n"; | |||||||||||||||||||
| 81 | 0 | exit 1; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 1 | ▶ | 'f|fuse=s' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||||||||||
| 86 | 1 | ▶ | my ( $fuse, $val ) = $_[1] =~ m/^(.*?)=(.*)$/ or die "Unable to parse --fuse\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 1 | ▶ | $val = oct $val if $val =~ m/^0/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 1 | $current{lc substr $fuse, 0, 1} = $val; | |||||||||||||||||||
| 89 | }, | ||||||||||||||||||||
| 90 | 9 | ▶ | ) or usage(1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 9 | ▶ | $yamlpath //= shift @argv; | ||||||||||||||||||
|
Condition33%1/3
$yamlpath //= shift @argv
MC/DC0%0/2
$yamlpath //= shift @argv
$yamlpath
shift @argv
Truth table33%1/3
A //= B
A= $yamlpathB= shift @argv
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 9 | ▶ | defined $yamlpath | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | or usage(1); | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 9 | my $self = $class->new( | |||||||||||||||||||
| 98 | yamlpath => $yamlpath, | ||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 9 | $self->set_fusereg( $_ => $current{$_} ) for keys %current; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 9 | foreach my $arg ( @argv ) { | |||||||||||||||||||
| 104 | 3 | ▶ | my ( $name, $val ) = $arg =~ m/^(\w+)=(.*)$/ or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | die "Unable to parse '$arg'\n"; | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 3 | ▶ | if( $val eq "?" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 1 | say join "\n", | |||||||||||||||||||
| 109 | "Possible values for $name are:", | ||||||||||||||||||||
| 110 | $self->list_values_for_fuse( $name ); | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 1 | return 1; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 2 | $self->set_fuse( uc $name, $val ); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 8 | ▶ | if( $VERBOSE ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | # Explain the current fuse values | ||||||||||||||||||||
| 120 | 2 | foreach my $name ( $self->{fuses}->@* ) { | |||||||||||||||||||
| 121 | 21 | my $fuseinfo = $self->{fusemap}{$name}; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 21 | my $mask = $fuseinfo->{mask} + 0; # force number because YAML loading | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 21 | my $val = $self->{regs}{ $fuseinfo->{reg} } & $mask; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 21 | ▶ | if( $fuseinfo->{values} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 5 | ▶ | my $chosen = first { $_->{value} == $val } $fuseinfo->{values}->@*; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 129 | 5 | $val = "$chosen->{name} - $chosen->{caption}"; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||
| 132 | 16 | ▶ | $val = $val ? "1" : "0"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 21 | say "using $name=$val"; | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 8 | say join " ", $self->gen_fuses_avrdude; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | sub new | ||||||||||||||||||||
| 143 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 144 | 9 | my $class = shift; | |||||||||||||||||||
| 145 | 9 | my %opts = @_; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 9 | my $data = YAML::LoadFile( $opts{yamlpath} ); | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 9 | my %regs; | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | my $self = bless { | ||||||||||||||||||||
| 152 | 9 | ( map { $_ => $data->{$_} } qw( reginfos fuses fusemap ) ), | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | regs => \%regs, | ||||||||||||||||||||
| 155 | }, $class; | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | # Initialise defaults | ||||||||||||||||||||
| 158 | 9 | $regs{ $_->{name} } = $_->{default} for $self->{reginfos}->@*; | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 9 | return $self; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | sub set_fusereg | ||||||||||||||||||||
| 164 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
set_fusereg: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 165 | 1 | my $self = shift; | |||||||||||||||||||
| 166 | 1 | my ( $name, $value ) = @_; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 1 | ▶ | exists $self->{regs}{$name} or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | die "Unrecognised fuse register name $name\n"; | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 1 | $self->{regs}{$name} = $value; | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | sub set_fuse | ||||||||||||||||||||
| 175 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
set_fuse: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 176 | 2 | my $self = shift; | |||||||||||||||||||
| 177 | 2 | my ( $name, $val ) = @_; | |||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 2 | ▶ | my $fuseinfo = $self->{fusemap}{$name} or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 180 | die "No such fuse $name\n"; | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 2 | my $reg = $fuseinfo->{reg}; | |||||||||||||||||||
| 183 | 2 | my $mask = $fuseinfo->{mask} + 0; # force number because YAML loading | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 2 | my $regval = $self->{regs}{$reg}; | |||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 2 | $regval &= ~$mask; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 2 | ▶ | if( $fuseinfo->{values} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | 1 | ▶ | my $chosen = first { $_->{name} eq $val } $fuseinfo->{values}->@*; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 191 | 1 | ▶ | defined $chosen or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | die "Unrecognised value for $name\n"; | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 1 | $regval |= $chosen->{value}; | |||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | else { | ||||||||||||||||||||
| 197 | 1 | ▶ | $regval |= $mask if $val; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 2 | $self->{regs}{$reg} = $regval; | |||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | sub list_values_for_fuse | ||||||||||||||||||||
| 204 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
list_values_for_fuse: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||
| 205 | 1 | my $self = shift; | |||||||||||||||||||
| 206 | 1 | my ( $name ) = @_; | |||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 1 | ▶ | my $fuseinfo = $self->{fusemap}{$name} or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 209 | die "No such fuse $name\n"; | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 1 | ▶ | if( $fuseinfo->{values} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 212 | return map { | ||||||||||||||||||||
| 213 | 4 | " $_->{name} - $_->{caption}" | |||||||||||||||||||
| 214 | 1 | } $fuseinfo->{values}->@*; | |||||||||||||||||||
| 215 | } | ||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||
| 217 | return ( | ||||||||||||||||||||
| 218 | 0 | " 1", | |||||||||||||||||||
| 219 | " 0", | ||||||||||||||||||||
| 220 | ); | ||||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | } | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | sub gen_fuses_avrdude | ||||||||||||||||||||
| 225 | 8 | ▶ | { | ||||||||||||||||||
|
Subroutine
gen_fuses_avrdude: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 226 | 8 | my $self = shift; | |||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 8 | my @output; | |||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 8 | foreach my $reginfo ( $self->{reginfos}->@* ) { | |||||||||||||||||||
| 231 | 24 | my $regname = $reginfo->{name}; | |||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | # avrdude format -U lfuse:w:0x62:m | ||||||||||||||||||||
| 234 | 24 | push @output, "-U", sprintf "%sfuse:w:0x%02X:m", $regname, $self->{regs}{$regname}; | |||||||||||||||||||
| 235 | } | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 8 | return @output; | |||||||||||||||||||
| 238 | } | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =head1 AUTHOR | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | 3 | 0x55AA; | |||||||||||||||||||