File Coverage

blib/lib/Device/Chip/AVR_HVSP.pm
Criterion Covered Total %
statement 293 319 91.8
branch 27 48 56.2
condition 8 21 38.1
subroutine 32 38 84.2
pod 21 21 100.0
total 381 447 85.2


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