Device-BusPirate-Chip-AVR_HVSP

 view release on metacpan or  search on metacpan

bin/avr_hvsp  view on Meta::CPAN

      my $fh;
      if( $_[0] eq "-" ) {
         $fh = IO::Handle->new_from_fd( STDIN->fileno, "r" );
      }
      else {
         open $fh, "<", $_[0] or die "Cannot read $_[0] - $!\n";
      }
      return bless $fh, $class;
   }
}
package main::Fileformat::out {
   use base 'main::Fileformat';
   sub open_in { die "This format does not support being read\n" }
}

package main::Fileformat::IntelHex {
   use base 'main::Fileformat';
   sub output {
      my $self = shift;
      my ( $bytes ) = @_;
      my $addr = 0;
      foreach my $chunk ( $bytes =~ m/(.{1,16})/gs ) {
         my $clen = length $chunk;
         my $cksum = $clen + ( $addr & 0xff ) + ( $addr >> 8 );
         $self->printf( ":%02X%04X%02X", $clen, $addr, 0 );
         foreach my $byte ( split //, $chunk ) {
            $byte = ord $byte;
            $cksum += $byte;
            $self->printf( "%02X", $byte );
         }
         $self->printf( "%02X\n", ( -$cksum ) & 0xff );
         $addr += $clen;
      }
      $self->print( ":00000001FF\n" );
   }
   sub input {
      my $self = shift;
      my $bytes = "";
      while( my $line = <$self> ) {
         chomp $line;
         next unless my ( $clen, $addr, $type, $data, $cksum ) =
            $line =~ m/^:([0-9a-f]{2})([0-9a-f]{4})([0-9a-f]{2})([0-9a-f]*)([0-9a-f]{2})$/i;
         # TODO: check checksum
         $type = hex $type;
         last if $type == 1; # EOF
         next if $type != 0; # unrecognised record
         warn "Bad record length on line $.\n" and next if
            length $data != 2 * hex $clen;
         $data = pack "H*", $data;
         substr( $bytes, hex $addr, length $data ) = $data;
      }
      return $bytes;
   }
}

package main::Fileformat::Immediate {
   use base 'main::Fileformat';
   sub open_out { die "This format does not support being written\n" }
   sub open_in {
      my $class = shift;
      my $bytes = join "", map { chr( m/^0/ ? hex : $_ ) } split m/[ ,]+/, $_[0];
      return bless \$bytes, $class;
   }
   sub input {
      return ${+shift};
   }
}

package main::Fileformat::Decimal {
   use base 'main::Fileformat::out';
   sub output {
      my $self = shift;
      my ( $bytes ) = @_;
      $self->print( join ",", unpack "C*", $bytes );
      $self->print( "\n" );
   }
}

package main::Fileformat::Hex {
   use base 'main::Fileformat::out';
   sub output {
      my $self = shift;
      my ( $bytes ) = @_;
      $self->print( join ",", map { sprintf "0x%x", $_ } unpack "C*", $bytes );
      $self->print( "\n" );
   }
}

package main::Fileformat::Octal {
   use base 'main::Fileformat::out';
   sub output {
      my $self = shift;
      my ( $bytes ) = @_;
      # Annoyingly, the avrdude octal format only prepends leading 0's if the
      # value actually requires it; there isn't a sprintf() format for that
      $self->print( join ",", map { $_ > 7 ? sprintf "%#o", $_ : $_ } unpack "C*", $bytes );
      $self->print( "\n" );
   }
}

=head1 NAME

C<avr_hvsp.pl> - an F<avrdude> clone to talk HVSP to AVR chips

=head1 SYNOPSIS

 avr_hvsp.pl [-e] [-n] [-D] [-U MEMORY:OP:FILE:FORMAT] ...

=head1 DESCRIPTION

This script implements a command that behaves somewhat like F<avrdude>, using
L<Device::BusPirate::Chip::AVR_HVSP> to talk to an F<AVR> chip in HVSP mode
via a suitable circuit attached to a F<Bus Pirate>. The module provides a
detailed description of a suitable circuit.

=head1 OPTIONS

The following options are designed to be compatible with F<avrdude>

=head2 -b, --baud RATE



( run in 0.785 second using v1.01-cache-2.11-cpan-71847e10f99 )