AVR_HVSP.pm

filter: Statementstmt 90.5% 363 / 401 Branchbran 58.9% 33 / 56 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 77.7% 42 / 54 Podpod 92.3% 24 / 26 totaltotal 83.1% 468 / 563 CC 45 SCAR 12.4
CC
45
Cov
85%
memory_info
12.0
read_eeprom
7.4
read_flash
7.3
CRAP 3.5
↑ Summary AVR_HVSP/FuseInfo.pm »
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-2023 -- leonerd@leonerd.org.uk
5  
6 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Device::Chip::AVR_HVSP 0.08;
11 class Device::Chip::AVR_HVSP
12 1 ▶ :isa(Device::Chip);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Object::Pad::ClassAttr::Struct 0.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use constant PROTOCOL => "GPIO";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 C<Device::Chip::AVR_HVSP> - high-voltage serial programming for F<AVR> chips
25  
26 =head1 DESCRIPTION
27  
28 This L<Device::Chip> subclass allows interaction with an F<AVR>
29 microcontroller of the F<ATtiny> family in high-voltage serial programming
30 (HVSP) mode. It is particularly useful for configuring fuses or working with a
31 chip with the C<RSTDISBL> fuse programmed, because in such cases a regular ISP
32 programmer cannot be used.
33  
34 =head2 CONNECTIONS
35  
36 To use this module you will need to make connections to the pins of the
37 F<ATtiny> chip:
38  
39 ATtiny | tiny84 | tiny85
40 -------+--------+-------
41 SDO | 9 | 7
42 SII | 8 | 6
43 SDI | 7 | 5
44 SCI | 2 | 2
45 RESET | 4 | 1
46 Vcc | 1 | 8
47 GND | 14 | 4
48  
49 This module recognises the following kinds of adapter and automatically
50 assigns default pin connections for likely configurations:
51  
52 Bus Pirate | Sparkfun | Seeed |:| ATtiny
53 | cable | cable |:|
54 -----------+----------+----------+-+-------
55 MISO | brown | black |:| SDO
56 CS | red | white |:| SII
57 MOSI | orange | grey |:| SDI
58 CLK | yellow | purple |:| SCI
59 AUX | green | blue |:| HV control
60 +5V | grey | orange |:| Vcc
61 GND | black | brown |:| GND
62  
63 Z<>
64  
65 FTDI |:| ATtiny
66 -----+-+-------
67 D0 |:| SCI
68 D1 |:| SDI
69 D2 |:| SDO
70 D3 |:| SII
71 D4 |:| HV control
72  
73 For other kinds of adapter, use the named parameters to the L</mount> method
74 to tell the chip driver which F<ATtiny> pin is connected to what GPIO line.
75  
76 The C<HV control> line from the adapter will need to be able to control a +12V
77 supply to the C<RESET> pin of the F<ATtiny> chip. It should be active-high,
78 and can be achieved by a two-stage NPN-then-PNP transistor arrangement.
79  
80 Additionally, the C<SDO> pin and the C<PA0> to C<PA2> pins of 14-pin devices
81 will need a pull-down to ground of around 100Ohm to 1kOhm.
82  
83 =cut
84  
85 =head1 MOUNT PARAMETERS
86  
87 =head2 sdi, sii, sci, sdo
88  
89 The names of GPIO lines on the adapter that are connected to the HVSP signal
90 pins of the F<ATtiny> chip.
91  
92 =head2 hv
93  
94 The name of the GPIO line on the adapter that is connected to the 12V power
95 supply control.
96  
97 =head1 METHODS
98  
99 The following methods documented in an C<await> expression return L<Future>
100 instances.
101  
102 =cut
103  
104 my %DEFAULT_PINS = (
105 'Device::Chip::Adapter::BusPirate' => {
106 sdi => "MOSI",
107 sii => "CS",
108 sci => "CLK",
109 sdo => "MISO",
110 hv => "AUX",
111 },
112  
113 'Device::Chip::Adapter::FTDI' => {
114 sdi => "D1",
115 sii => "D3",
116 sci => "D0",
117 sdo => "D2",
118 hv => "D4",
119 },
120  
121 # For unit testing this is convenient
122 'Test::Device::Chip::Adapter' => {
123 3 map { $_ => $_ } qw( sdi sii sci sdo hv )
124 },
125 );
126  
127 3 field %_pins;
128  
129 2 ▶async method mount ( $adapter, %params )
Subroutine mount: called CC=4 SCAR=13.9
Pod documented
130 2 {
131 2 ▶ if( my $pins = $DEFAULT_PINS{ref $adapter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pins = $DEFAULT_PINS{ref $adapter}) 2 0 2
132 2 %params = ( %$pins, %params );
133 }
134  
135 2 foreach my $pin (qw( sdi sii sci sdo hv )) {
136 10 ▶ defined $params{$pin} or croak "Require a pin assignment for '$pin'" ;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{$pin} or croak("Require a pin assignment for '${pin}'") 10 0 10
137  
138 10 $_pins{$pin} = $params{$pin};
139 }
140  
141 2 await $self->SUPER::mount( $adapter, %params );
142  
143 await $self->protocol->write_gpios( {
144 $_pins{sdi} => 0,
145 $_pins{sii} => 0,
146 2 $_pins{sci} => 0,
147 });
148  
149 # Set input
150 2 await $self->protocol->tris_gpios( [ $_pins{sdo} ] );
151 3 }
152  
153 =head2 start
154  
155 await $chip->start;
156  
157 Powers up the device, reads and checks the signature, ensuring it is a
158 recognised chip.
159  
160 This method leaves the chip powered up with +5V on Vcc and +12V on RESET. Use
161 the C<power>, C<hv_power> or C<all_power> methods to turn these off if it is
162 not required again immediately.
163  
164 =cut
165  
166 class Device::Chip::AVR_HVSP::_PartInfo :Struct(readonly)
167 {
168 11 ▶ field $signature;
Subroutine signature: called CC=1 SCAR=0.0
169 2 ▶ field $flash_words;
Subroutine flash_words: called CC=1 SCAR=0.0
170 66 ▶ field $flash_pagesize;
Subroutine flash_pagesize: called CC=1 SCAR=0.0
171 2 ▶ field $eeprom_words;
Subroutine eeprom_words: called CC=1 SCAR=0.0
172 2 ▶ field $eeprom_pagesize;
Subroutine eeprom_pagesize: called CC=1 SCAR=0.0
173 1 ▶ field $has_efuse;
Subroutine has_efuse: called CC=1 SCAR=0.0
174 }
175 51 ▶sub PartInfo { Device::Chip::AVR_HVSP::_PartInfo->new_values( @_ ) }
Subroutine PartInfo: called CC=1 SCAR=0.0
Pod undocumented
176  
177 class Device::Chip::AVR_HVSP::_MemoryInfo :Struct(readonly)
178 {
179 0 ▶ field $wordsize;
Subroutine wordsize: not called CC=1 SCAR=2.6
180 0 ▶ field $pagesize;
Subroutine pagesize: not called CC=1 SCAR=2.6
181 0 ▶ field $words;
Subroutine words: not called CC=1 SCAR=2.6
182 0 ▶ field $can_write;
Subroutine can_write: not called CC=1 SCAR=2.6
183 }
184 8 ▶sub MemoryInfo { Device::Chip::AVR_HVSP::_MemoryInfo->new_values( @_ ) }
Subroutine MemoryInfo: called CC=1 SCAR=0.0
Pod undocumented
185  
186 3 my %PARTS;
187 {
188 1 local $_;
189 3 ▶ while( <DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 54 3 57
190 54 ▶ my ( $name, $data ) = m/^(\S+)\s*=\s*(.*?)\s*$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = /^(\S+)\s*=\s*(.*?)\s*$/u or next 51 3 54
191 51 $PARTS{$name} = PartInfo( split /\s+/, $data );
192 }
193 }
194  
195 1 ▶field $_partname :reader;
Subroutine partname: called CC=1 SCAR=0.0
Pod documented
196 3 field $_partinfo;
197 3 field @_memories;
198  
199 1 ▶async method start ()
Subroutine start: called CC=5 SCAR=16.2
Pod documented
200 1 {
201 1 await $self->all_power(1);
202  
203 1 my $sig = uc unpack "H*", await $self->read_signature;
204  
205 1 my $partinfo;
206 my $part;
207 ( $partinfo = $PARTS{$_} )->signature eq $sig and $part = $_, last
208 1 ▶ for keys %PARTS;
Branch: 1 branch
BranchTrueFalseTotal
($partinfo = $PARTS{$_})->signature eq $sig and $part = $_, last 1 10 11
209  
210 1 ▶ defined $part or die "Unrecognised signature $sig\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $part or die "Unrecognised signature $sig\n" 1 0 1
211  
212 1 $_partname = $part;
213 1 $_partinfo = $partinfo;
214  
215 1 ▶ @_memories = (
Branch: 1 branch
BranchTrueFalseTotal
$partinfo->has_efuse ? : 1 0 1
216 # ws ps nw wr
217 signature => MemoryInfo( 8, 3, 3, 0 ),
218 calibration => MemoryInfo( 8, 1, 1, 0 ),
219 lock => MemoryInfo( 8, 1, 1, 1 ),
220 lfuse => MemoryInfo( 8, 1, 1, 1 ),
221 hfuse => MemoryInfo( 8, 1, 1, 1 ),
222 ( $partinfo->has_efuse ?
223 ( efuse => MemoryInfo( 8, 1, 1, 1 ) ) :
224 () ),
225 flash => MemoryInfo( 16, $partinfo->flash_pagesize, $partinfo->flash_words, 1 ),
226 eeprom => MemoryInfo( 8, $partinfo->eeprom_pagesize, $partinfo->eeprom_words, 1 ),
227 );
228  
229 1 return $self;
230 3 }
231  
232 =head2 stop
233  
234 await $chip->stop;
235  
236 Shut down power to the device.
237  
238 =cut
239  
240 0 ▶async method stop ()
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
241 0 {
242 0 await $self->all_power(0);
243 3 }
244  
245 =head2 power
246  
247 await $chip->power( $on );
248  
249 Controls +5V to the Vcc pin of the F<ATtiny> chip.
250  
251 =cut
252  
253 1 ▶async method power ( $on )
Subroutine power: called CC=1 SCAR=0.0
Pod documented
254 1 {
255 1 await $self->protocol->power( $on );
256 3 }
257  
258 =head2 hv_power
259  
260 await $chip->hv_power( $on );
261  
262 Controls +12V to the RESET pin of the F<ATtiny> chip.
263  
264 =cut
265  
266 1 ▶async method hv_power ( $on )
Subroutine hv_power: called CC=1 SCAR=0.0
Pod documented
267 1 {
268 1 await $self->protocol->write_gpios( { $_pins{hv} => $on } );
269 3 }
270  
271 =head2 all_power
272  
273 await $chip->all_power( $on );
274  
275 Controls both +5V and +12V supplies at once. The +12V supply is turned on last
276 but off first, ensuring the correct HVSP-RESET sequence is applied to the
277 chip.
278  
279 =cut
280  
281 1 ▶async method all_power ( $on )
Subroutine all_power: called CC=2 SCAR=7.2
Pod documented
282 1 {
283 # Allow power to settle before turning on +12V on AUX
284 # Normal serial line overheads should allow enough time here
285  
286 1 ▶ if( $on ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on) { } 1 0 1
287 1 await $self->power(1);
288 1 await $self->hv_power(1);
289 }
290 else {
291 0 await $self->hv_power(0);
292 0 await $self->power(0);
293 }
294 3 }
295  
296 =head2 $name = $chip->partname
297  
298 Returns the name of the chip whose signature was detected by the C<start>
299 method.
300  
301 =cut
302  
303 # :reader
304  
305 =head2 $memory = $avr->memory_info( $name )
306  
307 Returns a memory info structure giving details about the named memory for the
308 attached part. The following memory names are recognised:
309  
310 signature calibration lock lfuse hfuse efuse flash eeprom
311  
312 (Note that the F<ATtiny13> has no C<efuse> memory).
313  
314 The structure will respond to the following methods:
315  
316 =over 4
317  
318 =item * wordsize
319  
320 Returns number of bits per word. This will be 8 for the byte-oriented
321 memories, but 16 for the main program flash.
322  
323 =item * pagesize
324  
325 Returns the number of words per page; the smallest amount that can be
326 written in one go.
327  
328 =item * words
329  
330 Returns the total number of words that are available.
331  
332 =item * can_write
333  
334 Returns true if the memory type can be written (in general; this does not take
335 into account the lock bits that might futher restrict a particular chip).
336  
337 =back
338  
339 =cut
340  
341 0 ▶method memory_info ( $name )
Subroutine memory_info: not called CC=3 SCAR=24.8
Pod documented
342 {
343 $_memories[$_*2] eq $name and return $_memories[$_*2 + 1]
344 0 ▶ for 0 .. $#_memories/2;
Branch: 1 branch
BranchTrueFalseTotal
$_memories[$_ * 2] eq $name and return $_memories[$_ * 2 + 1] 0 0 0
345  
346 0 die "$_partname does not have a $name memory";
347 3 }
348  
349 =head2 %memories = $avr->memory_infos
350  
351 Returns a key/value list of all the known device memories.
352  
353 =cut
354  
355 0 ▶method memory_infos ()
Subroutine memory_infos: not called CC=1 SCAR=6.9
Pod documented
356 {
357 0 return @_memories;
358 3 }
359  
360 =head2 $fuseinfo = $avr->fuseinfo
361  
362 Returns a L<Device::Chip::AVR_HVSP::FuseInfo> instance containing information
363 on the fuses in the attached device type.
364  
365 =cut
366  
367 0 ▶method fuseinfo ()
Subroutine fuseinfo: not called CC=1 SCAR=6.9
Pod documented
368 {
369 0 require Device::Chip::AVR_HVSP::FuseInfo;
370 0 return Device::Chip::AVR_HVSP::FuseInfo->for_part( $self->partname );
371 3 }
372  
373 1 ▶async method _transfer ( $sdi, $sii )
Subroutine _transfer: called CC=4 SCAR=13.9
374 1 {
375 1 my $SCI = $_pins{sci};
376 1 my $SDI = $_pins{sdi};
377 1 my $SII = $_pins{sii};
378 1 my $SDO = $_pins{sdo};
379  
380 1 my $sdo = 0;
381 1 my $proto = $self->protocol;
382  
383 # A "byte" transfer consists of 11 clock transitions; idle low. Each bit is
384 # clocked in from SDO on the falling edge of clocks 0 to 7, but clocked out
385 # of SDI and SII on clocks 1 to 8.
386 # We'll therefore toggle the clock 11 times; on each of the first 8 clocks
387 # we raise it, then simultaneously lower it, writing out the next out bits
388 # and reading in the input.
389 # Serial transfer is MSB first in both directions
390 #
391 # We cheat massively here and rely on pipeline ordering of the actual
392 # ->write calls, by writing all 22 of the underlying bit transitions to the
393 # underlying device, then waiting on all 11 reads to come back.
394  
395 1 my @f;
396 1 foreach my $i ( 0 .. 10 ) {
397 11 ▶ my $mask = $i < 8 ? (1 << 7-$i) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i < 8 ? : 8 3 11
398  
399 11 push @f, $proto->write_gpios( { $SCI => 1 } );
400  
401 11 ▶ if( !$mask ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mask) 3 8 11
402 3 push @f, $proto->write_gpios( { $SCI => 0 } );
403 3 next;
404 }
405  
406 # TODO: this used to be
407 # $mode->writeread
408 # on the BusPirate version
409 push @f,
410 $proto->write_gpios( {
411 $SDI => ( $sdi & $mask ),
412 $SII => ( $sii & $mask ),
413 $SCI => 0
414 } ),
415  
416 8 ▶ $proto->read_gpios( [ $SDO ] )->on_done( sub ( $v ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
417 8 ▶ $sdo |= $mask if $v->{$SDO};
Branch: 1 branch
BranchTrueFalseTotal
if $v->{$SDO} 4 4 8
418 8 });
419 }
420  
421 1 await Future->needs_all( @f );
422  
423 1 return $sdo;
424 3 }
425  
426 99 ▶async method _await_SDO_high ()
Subroutine _await_SDO_high: called CC=3 SCAR=11.1
427 99 {
428 99 my $SDO = $_pins{sdo};
429  
430 99 my $proto = $self->protocol;
431  
432 99 my $count = 50;
433 99 while(1) {
434 99 ▶ $count-- or die "Timeout waiting for device to ACK";
Branch: 1 branch
BranchTrueFalseTotal
$count-- or die "Timeout waiting for device to ACK" 99 0 99
435  
436 99 ▶ last if ( await $proto->read_gpios( [ $SDO ] ) )->{$SDO};
Branch: 1 branch
BranchTrueFalseTotal
if <custom op await>->{$SDO} 99 0 99
437 }
438 3 }
439  
440 # The AVR datasheet on HVSP does not name any of these operations, only
441 # giving them bit patterns. We'll use the names invented by RikusW. See also
442 # https://sites.google.com/site/megau2s/
443  
444 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
445 # SII values
446 3 HVSP_CMD => 0x4C, # Command
447 HVSP_LLA => 0x0C, # Load Lo Address
448 HVSP_LHA => 0x1C, # Load Hi Address
449 HVSP_LLD => 0x2C, # Load Lo Data
450 HVSP_LHD => 0x3C, # Load Hi Data
451 HVSP_WLB => 0x64, # Write Lo Byte = WRL = WFU0
452 HVSP_WHB => 0x74, # Write Hi Byte = WRH = WFU1
453 HVSP_WFU2 => 0x66, # Write Extended Fuse
454 HVSP_RLB => 0x68, # Read Lo Byte
455 HVSP_RHB => 0x78, # Read Hi Byte
456 HVSP_RSIG => 0x68, # Read Signature
457 HVSP_RFU0 => 0x68, # Read Low Fuse
458 HVSP_RFU1 => 0x7A, # Read High Fuse
459 HVSP_RFU2 => 0x6A, # Read Extended Fuse
460 HVSP_REEP => 0x68, # Read EEPROM
461 HVSP_ROSC => 0x78, # Read Oscillator calibration
462 HVSP_RLCK => 0x78, # Read Lock
463 HVSP_PLH => 0x7D, # Page Latch Hi
464 HVSP_PLL => 0x6D, # Page Latch Lo
465 HVSP_ORM => 0x0C, # OR mask for SII to pulse actual read/write operation
466  
467 # HVSP_CMD Commands
468 CMD_CE => 0x80, # Chip Erase
469 CMD_WFUSE => 0x40, # Write Fuse
470 CMD_WLOCK => 0x20, # Write Lock
471 CMD_WFLASH => 0x10, # Write FLASH
472 CMD_WEEP => 0x11, # Write EEPROM
473 CMD_RSIG => 0x08, # Read Signature
474 CMD_RFUSE => 0x04, # Read Fuse
475 CMD_RFLASH => 0x02, # Read FLASH
476 CMD_REEP => 0x03, # Read EEPROM
477 CMD_ROSC => 0x08, # Read Oscillator calibration
478 CMD_RLOCK => 0x04, # Read Lock
479 3 };
480 # Some synonyms not found in the AVR ctrlstack software
481 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
482 3 HVSP_WLCK => HVSP_WLB, # Write Lock
483 HVSP_WFU0 => HVSP_WLB, # Write Low Fuse
484 HVSP_WFU1 => HVSP_WHB, # Write High Fuse
485 3 };
486  
487 =head2 chip_erase
488  
489 await $avr->chip_erase;
490  
491 Performs an entire chip erase. This will clear the flash and EEPROM memories,
492 before resetting the lock bits. It does not affect the fuses.
493  
494 =cut
495  
496 1 ▶async method chip_erase ()
Subroutine chip_erase: called CC=1 SCAR=0.0
Pod documented
497 1 {
498 1 await $self->_transfer( CMD_CE, HVSP_CMD );
499  
500 1 await $self->_transfer( 0, HVSP_WLB );
501 1 await $self->_transfer( 0, HVSP_WLB|HVSP_ORM );
502  
503 1 await $self->_await_SDO_high;
504 3 }
505  
506 =head2 read_signature
507  
508 $bytes = await $avr->read_signature;
509  
510 Reads the three device signature bytes and returns them in as a single binary
511 string.
512  
513 =cut
514  
515 2 ▶async method read_signature ()
Subroutine read_signature: called CC=2 SCAR=6.9
Pod documented
516 2 {
517 2 await $self->_transfer( CMD_RSIG, HVSP_CMD );
518  
519 2 my @sig;
520 2 foreach my $byte ( 0 .. 2 ) {
521 6 await $self->_transfer( $byte, HVSP_LLA );
522 6 await $self->_transfer( 0, HVSP_RSIG );
523 6 push @sig, await $self->_transfer( 0, HVSP_RSIG|HVSP_ORM );
524 }
525  
526 2 return pack "C*", @sig;
527 3 }
528  
529 =head2 read_calibration
530  
531 $byte = await $avr->read_calibration;
532  
533 Reads the calibration byte.
534  
535 =cut
536  
537 1 ▶async method read_calibration ()
Subroutine read_calibration: called CC=1 SCAR=0.0
Pod documented
538 1 {
539 1 await $self->_transfer( CMD_ROSC, HVSP_CMD );
540  
541 1 await $self->_transfer( 0, HVSP_LLA );
542 1 await $self->_transfer( 0, HVSP_ROSC );
543 1 my $val = await $self->_transfer( 0, HVSP_ROSC|HVSP_ORM );
544  
545 1 return chr $val;
546 3 }
547  
548 =head2 read_lock
549  
550 $byte = await $avr->read_lock;
551  
552 Reads the lock byte.
553  
554 =cut
555  
556 1 ▶async method read_lock ()
Subroutine read_lock: called CC=1 SCAR=0.0
Pod documented
557 1 {
558 1 await $self->_transfer( CMD_RLOCK, HVSP_CMD );
559  
560 1 await $self->_transfer( 0, HVSP_RLCK );
561 1 my $val = await $self->_transfer( 0, HVSP_RLCK|HVSP_ORM );
562  
563 1 return chr( $val & 3 );
564 3 }
565  
566 =head2 write_lock
567  
568 await $avr->write_lock( $byte );
569  
570 Writes the lock byte.
571  
572 =cut
573  
574 1 ▶async method write_lock ( $byte )
Subroutine write_lock: called CC=1 SCAR=0.0
Pod documented
575 1 {
576 1 await $self->_transfer( CMD_WLOCK, HVSP_CMD );
577  
578 1 await $self->_transfer( ( ord $byte ) & 3, HVSP_LLD );
579 1 await $self->_transfer( 0, HVSP_WLCK );
580 1 await $self->_transfer( 0, HVSP_WLCK|HVSP_ORM );
581  
582 1 await $self->_await_SDO_high;
583 3 }
584  
585 =head2 read_fuse_byte
586  
587 $int = await $avr->read_fuse_byte( $fuse );
588  
589 Reads one of the fuse bytes C<lfuse>, C<hfuse>, C<efuse>, returning an
590 integer.
591  
592 =cut
593  
594 3 my %SII_FOR_FUSE_READ = (
595 lfuse => HVSP_RFU0,
596 hfuse => HVSP_RFU1,
597 efuse => HVSP_RFU2,
598 );
599  
600 1 ▶async method read_fuse_byte ( $fuse )
Subroutine read_fuse_byte: called CC=4 SCAR=14.4
Pod documented
601 1 {
602 1 ▶ my $sii = $SII_FOR_FUSE_READ{$fuse} or croak "Unrecognised fuse type '$fuse'";
Branch: 1 branch
BranchTrueFalseTotal
my $sii = $SII_FOR_FUSE_READ{$fuse} or croak("Unrecognised fuse type '${fuse}'") 1 0 1
603  
604 1 ▶ $fuse eq "efuse" and !$_partinfo->has_efuse and
Branch: 1 branch
BranchTrueFalseTotal
$fuse eq "efuse" and not $_partinfo->has_efuse and croak("This part does not have an 'efuse'") 0 1 1
Condition33%1/3
$fuse eq "efuse" and not $_partinfo->has_efuse
!ll&&!rl&&r
100
MC/DC0%0/2
$fuse eq "efuse" and not $_partinfo->has_efuse
$fuse eq "efuse" not $_partinfo->has_efuse
Truth table33%1/3
A and not(B)
A= $fuse eq "efuse"B= not $_partinfo->has_efuse
ABresult
0X0
100
111
605 croak "This part does not have an 'efuse'";
606  
607 1 await $self->_transfer( CMD_RFUSE, HVSP_CMD );
608  
609 1 await $self->_transfer( 0, $sii );
610 1 return await $self->_transfer( 0, $sii|HVSP_ORM );
611 3 }
612  
613 =head2 write_fuse_byte
614  
615 await $avr->write_fuse_byte( $fuse, $byte );
616  
617 Writes one of the fuse bytes C<lfuse>, C<hfuse>, C<efuse> from an integer.
618  
619 =cut
620  
621 3 my %SII_FOR_FUSE_WRITE = (
622 lfuse => HVSP_WFU0,
623 hfuse => HVSP_WFU1,
624 efuse => HVSP_WFU2,
625 );
626  
627 1 ▶async method write_fuse_byte ( $fuse, $byte )
Subroutine write_fuse_byte: called CC=4 SCAR=14.2
Pod documented
628 1 {
629 1 ▶ my $sii = $SII_FOR_FUSE_WRITE{$fuse} or croak "Unrecognised fuse type '$fuse'";
Branch: 1 branch
BranchTrueFalseTotal
my $sii = $SII_FOR_FUSE_WRITE{$fuse} or croak("Unrecognised fuse type '${fuse}'") 1 0 1
630  
631 1 ▶ $fuse eq "efuse" and !$_partinfo->has_efuse and
Branch: 1 branch
BranchTrueFalseTotal
$fuse eq "efuse" and not $_partinfo->has_efuse and croak("This part does not have an 'efuse'") 0 1 1
Condition33%1/3
$fuse eq "efuse" and not $_partinfo->has_efuse
!ll&&!rl&&r
100
MC/DC0%0/2
$fuse eq "efuse" and not $_partinfo->has_efuse
$fuse eq "efuse" not $_partinfo->has_efuse
Truth table33%1/3
A and not(B)
A= $fuse eq "efuse"B= not $_partinfo->has_efuse
ABresult
0X0
100
111
632 croak "This part does not have an 'efuse'";
633  
634 1 await $self->_transfer( CMD_WFUSE, HVSP_CMD );
635  
636 1 await $self->_transfer( $byte, HVSP_LLD );
637 1 await $self->_transfer( 0, $sii );
638 1 await $self->_transfer( 0, $sii|HVSP_ORM );
639  
640 1 await $self->_await_SDO_high;
641 3 }
642  
643 =head2 read_lfuse
644  
645 =head2 read_hfuse
646  
647 =head2 read_efuse
648  
649 $byte = await $avr->read_lfuse;
650  
651 $byte = await $avr->read_hfuse;
652  
653 $byte = await $avr->read_efuse;
654  
655 Convenient shortcuts to reading the low, high and extended fuses directly,
656 returning a byte.
657  
658 =head2 write_lfuse
659  
660 =head2 write_hfuse
661  
662 =head2 write_efuse
663  
664 await $avr->write_lfuse( $byte );
665  
666 await $avr->write_hfuse( $byte );
667  
668 await $avr->write_efuse( $byte );
669  
670 Convenient shortcuts for writing the low, high and extended fuses directly,
671 from a byte.
672  
673 =cut
674  
675 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.6
676 3 ▶ use Object::Pad 0.800 ':experimental(mop)';
Subroutine BEGIN: called CC=1 SCAR=0.0
677 3 my $meta = Object::Pad::MOP::Class->for_caller;
678  
679 3 foreach my $fuse (qw( lfuse hfuse efuse )) {
680 0 ▶ $meta->add_method( "read_$fuse" => async method {
Subroutine read_hfuse: not called CC=1 SCAR=6.9
Subroutine read_lfuse: not called CC=1 SCAR=6.9
Pod documented
Pod documented
681 0 return chr await $self->read_fuse_byte( $fuse );
682 9 } );
683 0 ▶ $meta->add_method( "write_$fuse" => async method {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine write_efuse: not called CC=1 SCAR=6.9
Pod documented
684 0 await $self->write_fuse_byte( $fuse, ord $_[0] );
685 9 } );
686 }
687 }
688  
689 =head2 read_flash
690  
691 $bytes = await $avr->read_flash( %args );
692  
693 Reads a range of the flash memory and returns it as a binary string.
694  
695 Takes the following optional arguments:
696  
697 =over 4
698  
699 =item start => INT
700  
701 =item stop => INT
702  
703 Address range to read. If omitted, reads the entire memory.
704  
705 =item bytes => INT
706  
707 Alternative to C<stop>; gives the nubmer of bytes (i.e. not words of flash)
708 to read.
709  
710 =back
711  
712 =cut
713  
714 1 ▶async method read_flash ( %opts )
Subroutine read_flash: called CC=7 SCAR=19.9
Pod documented
715 1 {
716 1 ▶ my $partinfo = $_partinfo or croak "Cannot ->read_flash of an unrecognised part";
Branch: 1 branch
BranchTrueFalseTotal
my $partinfo = $_partinfo or croak("Cannot ->read_flash of an unrecognised part") 1 0 1
717  
718 1 ▶ my $start = $opts{start} // 0;
Condition50%1/2
$opts{"start"} // 0
l!l
10
MC/DC0%0/1
$opts{"start"} // 0
$opts{"start"}
Truth table50%1/2
A
A= $opts{"start"}
Aresult
11
00
719 my $stop = $opts{stop} //
720 1 ▶ $opts{bytes} ? $start + ( $opts{bytes}/2 ) : $partinfo->flash_words;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'stop'} // $opts{'bytes'} ? : 1 0 1
Condition33%1/3
$opts{"stop"} // $opts{"bytes"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{"stop"} // $opts{"bytes"}
$opts{"stop"} $opts{"bytes"}
Truth table33%1/3
A // B
A= $opts{"stop"}B= $opts{"bytes"}
ABresult
1X1
011
000
721  
722 1 my $bytes = "";
723  
724 1 await $self->_transfer( CMD_RFLASH, HVSP_CMD );
725 1 my $cur_ahi = -1;
726  
727 1 foreach my $addr ( $start .. $stop - 1 ) {
728 1 my $alo = $addr & 0xff;
729 1 my $ahi = $addr >> 8;
730  
731 1 await $self->_transfer( $alo, HVSP_LLA );
732  
733 1 ▶ await $self->_transfer( $cur_ahi = $ahi, HVSP_LHA ) if $cur_ahi != $ahi;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_ahi != $ahi 1 0 1
734  
735 1 await $self->_transfer( 0, HVSP_RLB );
736 1 $bytes .= chr await $self->_transfer( 0, HVSP_RLB|HVSP_ORM );
737  
738 1 await $self->_transfer( 0, HVSP_RHB );
739 1 $bytes .= chr await $self->_transfer( 0, HVSP_RHB|HVSP_ORM );
740 }
741  
742 1 return $bytes;
743 3 }
744  
745 =head2 write_flash
746  
747 await $avr->write_flash( $bytes );
748  
749 Writes the flash memory from the binary string.
750  
751 =cut
752  
753 1 ▶async method write_flash ( $bytes )
Subroutine write_flash: called CC=4 SCAR=13.9
Pod documented
754 1 {
755 1 ▶ my $partinfo = $_partinfo or croak "Cannot ->write_flash of an unrecognised part";
Branch: 1 branch
BranchTrueFalseTotal
my $partinfo = $_partinfo or croak("Cannot ->write_flash of an unrecognised part") 1 0 1
756 1 my $nbytes_page = $partinfo->flash_pagesize * 2; # words are 2 bytes
757  
758 1 ▶ croak "Cannot write - too large" if length $bytes > $partinfo->flash_words * 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes > $partinfo->flash_words * 2 0 1 1
759  
760 1 await $self->_transfer( CMD_WFLASH, HVSP_CMD );
761  
762 1 my @chunks = $bytes =~ m/(.{1,$nbytes_page})/gs;
763 1 my $addr = 0;
764  
765 1 foreach my $chunk ( @chunks ) {
766 64 my $thisaddr = $addr;
767 64 $addr += $partinfo->flash_pagesize;
768  
769 64 await $self->_write_flash_page( $chunk, $thisaddr );
770 }
771  
772 1 await $self->_transfer( 0, HVSP_CMD );
773 3 }
774  
775 64 ▶async method _write_flash_page ( $bytes, $baseaddr )
Subroutine _write_flash_page: called CC=2 SCAR=6.9
776 64 {
777 64 foreach my $idx ( 0 .. length($bytes)/2 - 1 ) {
778 1024 my $addr = $baseaddr + $idx;
779 1024 my $byte_lo = substr $bytes, $idx*2, 1;
780 1024 my $byte_hi = substr $bytes, $idx*2 + 1, 1;
781  
782 # Datasheet disagrees with the byte value written in the final
783 # instruction. Datasheet says 6C even though the OR mask would yield
784 # the value 6E. It turns out emperically that either value works fine
785 # so for neatness of following other code patterns, we use 6E here.
786  
787 1024 await $self->_transfer( $addr & 0xff, HVSP_LLA );
788 1024 await $self->_transfer( ord $byte_lo, HVSP_LLD );
789 1024 await $self->_transfer( 0, HVSP_PLL );
790 1024 await $self->_transfer( 0, HVSP_PLL|HVSP_ORM );
791 1024 await $self->_transfer( ord $byte_hi, HVSP_LHD );
792 1024 await $self->_transfer( 0, HVSP_PLH );
793 1024 await $self->_transfer( 0, HVSP_PLH|HVSP_ORM );
794 }
795  
796 64 await $self->_transfer( $baseaddr >> 8, HVSP_LHA );
797 64 await $self->_transfer( 0, HVSP_WLB );
798 64 await $self->_transfer( 0, HVSP_WLB|HVSP_ORM );
799 64 await $self->_await_SDO_high;
800 3 }
801  
802 =head2 read_eeprom
803  
804 $bytes = await $avr->read_eeprom( %args );
805  
806 Reads a range of the EEPROM memory and returns it as a binary string.
807  
808 Takes the following optional arguments:
809  
810 =over 4
811  
812 =item start => INT
813  
814 =item stop => INT
815  
816 Address range to read. If omitted, reads the entire memory.
817  
818 =item bytes => INT
819  
820 Alternative to C<stop>; gives the nubmer of bytes to read.
821  
822 =back
823  
824 =cut
825  
826 1 ▶async method read_eeprom ( %opts )
Subroutine read_eeprom: called CC=7 SCAR=20.0
Pod documented
827 1 {
828 1 ▶ my $partinfo = $_partinfo or croak "Cannot ->read_eeprom of an unrecognised part";
Branch: 1 branch
BranchTrueFalseTotal
my $partinfo = $_partinfo or croak("Cannot ->read_eeprom of an unrecognised part") 1 0 1
829  
830 1 ▶ my $start = $opts{start} // 0;
Condition50%1/2
$opts{"start"} // 0
l!l
10
MC/DC0%0/1
$opts{"start"} // 0
$opts{"start"}
Truth table50%1/2
A
A= $opts{"start"}
Aresult
11
00
831 my $stop = $opts{stop} //
832 1 ▶ $opts{bytes} ? $start + $opts{bytes} : $partinfo->eeprom_words;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'stop'} // $opts{'bytes'} ? : 1 0 1
Condition33%1/3
$opts{"stop"} // $opts{"bytes"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{"stop"} // $opts{"bytes"}
$opts{"stop"} $opts{"bytes"}
Truth table33%1/3
A // B
A= $opts{"stop"}B= $opts{"bytes"}
ABresult
1X1
011
000
833  
834 1 my $bytes = "";
835  
836 1 await $self->_transfer( CMD_REEP, HVSP_CMD );
837  
838 1 my $cur_ahi = -1;
839  
840 1 foreach my $addr ( $start .. $stop - 1 ) {
841 1 my $alo = $addr & 0xff;
842 1 my $ahi = $addr >> 8;
843  
844 1 await $self->_transfer( $alo, HVSP_LLA );
845  
846 1 ▶ await $self->_transfer( $cur_ahi = $ahi, HVSP_LHA ) if $cur_ahi != $ahi;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_ahi != $ahi 1 0 1
847  
848 1 await $self->_transfer( 0, HVSP_REEP );
849 1 $bytes .= chr await $self->_transfer( 0, HVSP_REEP|HVSP_ORM );
850 }
851  
852 1 return $bytes;
853 3 }
854  
855 =head2 write_eeprom
856  
857 await $avr->write_eeprom( $bytes );
858  
859 Writes the EEPROM memory from the binary string.
860  
861 =cut
862  
863 1 ▶async method write_eeprom ( $bytes )
Subroutine write_eeprom: called CC=4 SCAR=13.9
Pod documented
864 1 {
865 1 ▶ my $partinfo = $_partinfo or croak "Cannot ->write_eeprom of an unrecognised part";
Branch: 1 branch
BranchTrueFalseTotal
my $partinfo = $_partinfo or croak("Cannot ->write_eeprom of an unrecognised part") 1 0 1
866  
867 1 ▶ croak "Cannot write - too large" if length $bytes > $partinfo->eeprom_words;
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes > $partinfo->eeprom_words 0 1 1
868  
869 1 my $nwords_page = $partinfo->eeprom_pagesize;
870  
871 1 await $self->_transfer( CMD_WEEP, HVSP_CMD );
872  
873 1 my @chunks = $bytes =~ m/(.{1,$nwords_page})/gs;
874 1 my $addr = 0;
875  
876 1 foreach my $chunk ( @chunks ) {
877 32 my $thisaddr = $addr;
878 32 $addr += $nwords_page;
879  
880 32 await $self->_write_eeprom_page( $chunk, $thisaddr )
881 }
882  
883 1 await $self->_transfer( 0, HVSP_CMD );
884 3 }
885  
886 32 ▶async method _write_eeprom_page ( $bytes, $baseaddr )
Subroutine _write_eeprom_page: called CC=2 SCAR=6.9
887 32 {
888 32 foreach my $idx ( 0 .. length($bytes) - 1 ) {
889 128 my $addr = $baseaddr + $idx;
890 128 my $byte = substr $bytes, $idx, 1;
891  
892 # Datasheet disagrees with the byte value written in the final
893 # instruction. Datasheet says 6C even though the OR mask would yield
894 # the value 6E. It turns out emperically that either value works fine
895 # so for neatness of following other code patterns, we use 6E here.
896  
897 128 await $self->_transfer( $addr & 0xff, HVSP_LLA );
898 128 await $self->_transfer( $addr >> 8, HVSP_LHA );
899 128 await $self->_transfer( ord $byte, HVSP_LLD );
900 128 await $self->_transfer( 0, HVSP_PLL );
901 128 await $self->_transfer( 0, HVSP_PLL|HVSP_ORM );
902 }
903  
904 32 await $self->_transfer( 0, HVSP_WLB );
905 32 await $self->_transfer( 0, HVSP_WLB|HVSP_ORM );
906 32 await $self->_await_SDO_high;
907 3 }
908  
909 =head1 SEE ALSO
910  
911 =over 4
912  
913 =item *
914  
915 L<http://dangerousprototypes.com/2014/10/27/high-voltage-serial-programming-for-avr-chips-with-the-bus-pirate/> -
916 High voltage serial programming for AVR chips with the Bus Pirate.
917  
918 =back
919  
920 =head1 AUTHOR
921  
922 Paul Evans <leonerd@leonerd.org.uk>
923  
924 =cut
925  
926 3 0x55AA;
927  
928 __DATA__
↑ Summary AVR_HVSP/FuseInfo.pm »