2000-08-05 05:12:50 +00:00
|
|
|
/*
|
2000-08-06 03:53:06 +00:00
|
|
|
* Copyright 2000 Brian S. Dean <bsd@bsdhome.com>
|
2000-08-05 05:12:50 +00:00
|
|
|
* All Rights Reserved.
|
2000-08-06 03:53:06 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BRIAN S. DEAN ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRIAN S. DEAN BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
|
|
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
2000-08-05 05:12:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*
|
2000-08-06 03:53:06 +00:00
|
|
|
* Code to program an Atmel AVR AT90S device using the parallel port.
|
2000-08-05 05:12:50 +00:00
|
|
|
*
|
|
|
|
* Make the following connections:
|
|
|
|
*
|
2000-08-06 03:53:06 +00:00
|
|
|
* Parallel Port Atmel AVR
|
|
|
|
* ------------- ----------------------------
|
|
|
|
* Pin 2 -> Vcc
|
|
|
|
* Pin 3 -> PB7(SCK) CLOCK IN
|
|
|
|
* Pin 4 -> PB5(MOSI) Instruction input
|
|
|
|
* Pin 5 -> /RESET
|
|
|
|
* Pin 10 <- PB6(MISO) Data out
|
2000-12-20 02:26:31 +00:00
|
|
|
* Pin 18 <- GND
|
2000-08-05 05:12:50 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2000-12-20 02:26:31 +00:00
|
|
|
#include <stdlib.h>
|
2000-08-05 05:12:50 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include </sys/dev/ppbus/ppi.h>
|
|
|
|
|
|
|
|
#define PARALLEL "/dev/ppi0"
|
|
|
|
|
2000-08-06 18:51:32 +00:00
|
|
|
char * version = "$Id$";
|
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
char * progname;
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* bit definitions for AVR device connections
|
|
|
|
*/
|
2000-08-06 03:53:06 +00:00
|
|
|
#define AVR_POWER 0x01 /* bit 0 of data register */
|
|
|
|
#define AVR_CLOCK 0x02 /* bit 1 of data register */
|
|
|
|
#define AVR_INSTR 0x04 /* bit 2 of data register */
|
|
|
|
#define AVR_RESET 0x08 /* bit 3 of data register */
|
2000-08-05 05:12:50 +00:00
|
|
|
#define AVR_DATA 0x40 /* bit 6 of status register */
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* PPI registers
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
enum {
|
|
|
|
PPIDATA,
|
|
|
|
PPICTRL,
|
|
|
|
PPISTATUS
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* AVR memory designations
|
|
|
|
*/
|
|
|
|
typedef enum {
|
2000-08-05 05:12:50 +00:00
|
|
|
AVR_EEPROM,
|
|
|
|
AVR_FLASH,
|
|
|
|
AVR_FLASH_LO,
|
|
|
|
AVR_FLASH_HI
|
2000-08-06 19:47:03 +00:00
|
|
|
} AVRMEM;
|
|
|
|
|
|
|
|
|
2000-12-20 02:26:31 +00:00
|
|
|
struct avrpart {
|
|
|
|
char * partdesc;
|
|
|
|
char * optiontag;
|
|
|
|
int flash_size;
|
|
|
|
int eeprom_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct avrpart parts[] = {
|
|
|
|
{ "AT90S8515", "8515", 8192, 512 },
|
2000-12-31 02:24:50 +00:00
|
|
|
{ "AT90S2313", "2313", 2048, 128 },
|
|
|
|
{ "AT90S1200", "1200", 1200, 64 }
|
2000-12-20 02:26:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* set 'get' and 'set' appropriately for subsequent passage to ioctl()
|
|
|
|
* to get/set the specified PPI registers.
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int ppi_getops ( int reg, unsigned long * get, unsigned long * set )
|
|
|
|
{
|
|
|
|
switch (reg) {
|
|
|
|
case PPIDATA:
|
|
|
|
*set = PPISDATA;
|
|
|
|
*get = PPIGDATA;
|
|
|
|
break;
|
|
|
|
case PPICTRL:
|
|
|
|
*set = PPISCTRL;
|
|
|
|
*get = PPIGCTRL;
|
|
|
|
break;
|
|
|
|
case PPISTATUS:
|
|
|
|
*set = PPISSTATUS;
|
|
|
|
*get = PPIGSTATUS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf ( stderr, "%s: avr_set(): invalid register=%d\n",
|
|
|
|
progname, reg );
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* set the indicated bit of the specified register.
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int ppi_set ( int fd, int reg, int bit )
|
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
unsigned long get, set;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ppi_getops ( reg, &get, &set );
|
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ioctl(fd, get, &v);
|
|
|
|
v |= bit;
|
|
|
|
ioctl(fd, set, &v);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* clear the indicated bit of the specified register.
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int ppi_clr ( int fd, int reg, int bit )
|
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
unsigned long get, set;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ppi_getops ( reg, &get, &set );
|
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ioctl(fd, get, &v);
|
|
|
|
v &= ~bit;
|
|
|
|
ioctl(fd, set, &v);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* get the indicated bit of the specified register.
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int ppi_get ( int fd, int reg, int bit )
|
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
unsigned long get, set;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ppi_getops ( reg, &get, &set );
|
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ioctl(fd, get, &v);
|
|
|
|
v &= bit;
|
|
|
|
|
|
|
|
return (v == bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* toggle the indicated bit of the specified register.
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int ppi_toggle ( int fd, int reg, int bit )
|
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
unsigned long get, set;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ppi_getops ( reg, &get, &set );
|
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ioctl(fd, get, &v);
|
2000-08-07 01:48:53 +00:00
|
|
|
v ^= bit;
|
2000-08-05 05:12:50 +00:00
|
|
|
ioctl(fd, set, &v);
|
|
|
|
|
2000-08-07 01:48:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pulse the indicated bit of the specified register.
|
|
|
|
*/
|
|
|
|
int ppi_pulse ( int fd, int reg, int bit )
|
|
|
|
{
|
|
|
|
ppi_toggle(fd, reg, bit);
|
|
|
|
ppi_toggle(fd, reg, bit);
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* transmit and receive a bit of data to/from the AVR device
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int avr_txrx_bit ( int fd, int bit )
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2000-12-31 02:24:50 +00:00
|
|
|
/*
|
|
|
|
* read the result bit (it is either valid from a previous clock
|
|
|
|
* pulse or it is ignored in the current context)
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
r = ppi_get(fd, PPISTATUS, AVR_DATA);
|
|
|
|
|
2000-12-31 02:24:50 +00:00
|
|
|
/* set the data input line as desired */
|
2000-08-05 05:12:50 +00:00
|
|
|
if (bit)
|
|
|
|
ppi_set(fd, PPIDATA, AVR_INSTR);
|
|
|
|
else
|
|
|
|
ppi_clr(fd, PPIDATA, AVR_INSTR);
|
|
|
|
|
2000-12-31 02:24:50 +00:00
|
|
|
/*
|
|
|
|
* pulse the clock line, clocking in the MOSI data, and clocking out
|
|
|
|
* the next result bit
|
|
|
|
*/
|
2000-08-07 01:48:53 +00:00
|
|
|
ppi_pulse(fd, PPIDATA, AVR_CLOCK);
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* transmit and receive a byte of data to/from the AVR device
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
unsigned char avr_txrx ( int fd, unsigned char byte )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char r, b, rbyte;
|
|
|
|
|
|
|
|
rbyte = 0;
|
|
|
|
for (i=0; i<8; i++) {
|
|
|
|
b = (byte >> (7-i)) & 0x01;
|
|
|
|
r = avr_txrx_bit ( fd, b );
|
|
|
|
rbyte = rbyte | (r << (7-i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return rbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* transmit an AVR device command and return the results; 'cmd' and
|
|
|
|
* 'res' must point to at least a 4 byte data buffer
|
|
|
|
*/
|
|
|
|
int avr_cmd ( int fd, unsigned char cmd[4], unsigned char res[4] )
|
2000-08-05 05:12:50 +00:00
|
|
|
{
|
2000-08-06 03:53:06 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<4; i++) {
|
|
|
|
res[i] = avr_txrx(fd, cmd[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* read a byte of data from the indicated memory region
|
|
|
|
*/
|
|
|
|
unsigned char avr_read_byte ( int fd, AVRMEM memtype, unsigned short addr )
|
2000-08-06 03:53:06 +00:00
|
|
|
{
|
|
|
|
unsigned char cmd[4];
|
|
|
|
unsigned char res[4];
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
switch (memtype) {
|
|
|
|
case AVR_FLASH_LO:
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[0] = 0x20;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
case AVR_FLASH_HI:
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[0] = 0x28;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
case AVR_EEPROM:
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[0] = 0xa0;
|
2000-12-20 02:26:31 +00:00
|
|
|
/* addr &= 0x7f; */
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: avr_read_byte(); internal error: invalid memtype=%d\n",
|
|
|
|
progname, memtype);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[1] = addr >> 8; /* high order bits of address */
|
|
|
|
cmd[2] = addr & 0x0ff; /* low order bits of address */
|
|
|
|
cmd[3] = 0; /* don't care */
|
2000-08-05 05:12:50 +00:00
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
avr_cmd(fd, cmd, res);
|
|
|
|
|
|
|
|
return res[3];
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* read 'n' words of data from the indicated memory region. If the
|
|
|
|
* flash memory is being read, n*2 bytes will be read into 'buf'; if
|
|
|
|
* the eeprom is being read, 'n' bytes will be read into 'buf'.
|
|
|
|
*/
|
|
|
|
int avr_read ( int fd, AVRMEM memtype, unsigned start, unsigned n,
|
2000-08-05 05:12:50 +00:00
|
|
|
unsigned char * buf, int bufsize )
|
|
|
|
{
|
2000-08-06 03:53:06 +00:00
|
|
|
unsigned char rbyte, memt;
|
2000-08-05 05:12:50 +00:00
|
|
|
unsigned short end, i, bi;
|
|
|
|
|
|
|
|
switch (memtype) {
|
2000-08-06 03:53:06 +00:00
|
|
|
case AVR_FLASH :
|
|
|
|
memt = AVR_FLASH_LO;
|
|
|
|
break;
|
2000-08-05 05:12:50 +00:00
|
|
|
case AVR_EEPROM :
|
2000-08-06 03:53:06 +00:00
|
|
|
memt = memtype;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: avr_read(); internal error: invalid memtype=%d\n",
|
|
|
|
progname, memtype);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = start+n;
|
|
|
|
|
|
|
|
bi = 0;
|
|
|
|
|
|
|
|
for (i=start; i<end; i++) {
|
2000-08-06 03:53:06 +00:00
|
|
|
/* eeprom or low byte of flash */
|
|
|
|
rbyte = avr_read_byte(fd, memt, i);
|
|
|
|
fprintf ( stderr, " \r%4u 0x%02x", i, rbyte );
|
|
|
|
if (bi < bufsize) {
|
|
|
|
buf[bi++] = rbyte;
|
|
|
|
}
|
2000-08-05 05:12:50 +00:00
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
if (memtype == AVR_FLASH) {
|
|
|
|
/* flash high byte */
|
|
|
|
rbyte = avr_read_byte(fd, AVR_FLASH_HI, i);
|
2000-08-05 05:12:50 +00:00
|
|
|
fprintf ( stderr, " 0x%02x", rbyte );
|
|
|
|
if (bi < bufsize) {
|
|
|
|
buf[bi++] = rbyte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf ( stderr, "\n" );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* write a byte of data to the indicated memory region
|
|
|
|
*/
|
|
|
|
int avr_write_byte ( int fd, AVRMEM memtype, unsigned short addr, unsigned char data )
|
2000-08-05 05:12:50 +00:00
|
|
|
{
|
2000-08-06 03:53:06 +00:00
|
|
|
unsigned char cmd[4], res[4];
|
2000-08-05 05:12:50 +00:00
|
|
|
unsigned char r;
|
|
|
|
int ready;
|
|
|
|
int tries;
|
|
|
|
|
|
|
|
switch (memtype) {
|
|
|
|
case AVR_FLASH_LO:
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[0] = 0x40;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
case AVR_FLASH_HI:
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[0] = 0x48;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
case AVR_EEPROM:
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[0] = 0xc0;
|
2000-12-30 16:45:50 +00:00
|
|
|
/* addr &= 0x7f; */
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: avr_write_byte(); internal error: invalid memtype=%d\n",
|
|
|
|
progname, memtype);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[1] = addr >> 8; /* high order bits of address */
|
|
|
|
cmd[2] = addr & 0x0ff; /* low order bits of address */
|
|
|
|
cmd[3] = data; /* data */
|
|
|
|
|
|
|
|
avr_cmd(fd, cmd, res);
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
tries = 0;
|
|
|
|
ready = 0;
|
|
|
|
while (!ready) {
|
|
|
|
usleep(5000); /* flash write delay */
|
2000-08-06 03:53:06 +00:00
|
|
|
r = avr_read_byte(fd, memtype, addr);
|
2000-08-05 05:12:50 +00:00
|
|
|
if (data == 0x7f) {
|
2000-08-06 03:53:06 +00:00
|
|
|
usleep(20000); /* long delay for 0x7f since polling doesn't work */
|
2000-08-05 05:12:50 +00:00
|
|
|
ready = 1;
|
|
|
|
}
|
|
|
|
else if (r == data) {
|
|
|
|
ready = 1;
|
|
|
|
}
|
2000-08-06 03:53:06 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
tries++;
|
|
|
|
if (!ready && tries > 10) {
|
2000-08-06 03:53:06 +00:00
|
|
|
/*
|
|
|
|
* we couldn't write the data, indicate our displeasure by
|
|
|
|
* returning an error code
|
|
|
|
*/
|
|
|
|
return -1;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* write 'bufsize' bytes of data to the indicated memory region.
|
|
|
|
*/
|
|
|
|
int avr_write ( int fd, AVRMEM memtype, unsigned start,
|
2000-08-06 03:53:06 +00:00
|
|
|
unsigned char * buf, int bufsize )
|
2000-08-05 05:12:50 +00:00
|
|
|
{
|
2000-08-06 03:53:06 +00:00
|
|
|
unsigned char data, memt;
|
2000-08-05 05:12:50 +00:00
|
|
|
unsigned short end, i, bi;
|
2000-08-06 03:53:06 +00:00
|
|
|
int nl;
|
|
|
|
int rc;
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
switch (memtype) {
|
|
|
|
case AVR_FLASH :
|
2000-08-06 03:53:06 +00:00
|
|
|
end = start+bufsize/2;
|
|
|
|
memt = AVR_FLASH_LO;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
case AVR_EEPROM :
|
2000-08-06 03:53:06 +00:00
|
|
|
end = start+bufsize;
|
|
|
|
memt = memtype;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: avr_write(); internal error: invalid memtype=%d\n",
|
|
|
|
progname, memtype);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bi = 0;
|
|
|
|
|
|
|
|
for (i=start; i<end; i++) {
|
2000-08-06 03:53:06 +00:00
|
|
|
/* eeprom or low byte of flash */
|
|
|
|
data = buf[bi++];
|
|
|
|
nl = 0;
|
|
|
|
rc = avr_write_byte(fd, memt, i, data );
|
|
|
|
fprintf(stderr, " \r%4u 0x%02x", i, data);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, " ***failed; ");
|
|
|
|
nl = 1;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2000-08-06 03:53:06 +00:00
|
|
|
|
|
|
|
if (memtype == AVR_FLASH) {
|
|
|
|
/* high byte of flash */
|
2000-08-05 05:12:50 +00:00
|
|
|
data = buf[bi++];
|
2000-08-06 03:53:06 +00:00
|
|
|
rc = avr_write_byte(fd, AVR_FLASH_HI, i, data );
|
|
|
|
fprintf(stderr, " 0x%02x", data);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, " ***failed; " );
|
|
|
|
nl = 1;
|
|
|
|
}
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2000-08-06 03:53:06 +00:00
|
|
|
if (nl)
|
|
|
|
fprintf(stderr, "\n");
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf ( stderr, "\n" );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* issue the 'program enable' command to the AVR device
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int avr_program_enable ( int fd )
|
|
|
|
{
|
2000-08-06 03:53:06 +00:00
|
|
|
unsigned char cmd[4] = {0xac, 0x53, 0x00, 0x00};
|
|
|
|
unsigned char res[4];
|
2000-08-05 05:12:50 +00:00
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
avr_cmd(fd, cmd, res);
|
2000-08-05 05:12:50 +00:00
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
if (res[2] != cmd[1])
|
2000-08-05 05:12:50 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* issue the 'chip erase' command to the AVR device
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int avr_chip_erase ( int fd )
|
|
|
|
{
|
|
|
|
unsigned char data[4] = {0xac, 0x80, 0x00, 0x00};
|
2000-08-06 03:53:06 +00:00
|
|
|
unsigned char res[4];
|
|
|
|
|
|
|
|
avr_cmd(fd, data, res);
|
|
|
|
|
|
|
|
usleep(20000);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* read the AVR device's signature bytes
|
|
|
|
*/
|
|
|
|
int avr_signature ( int fd, unsigned char sig[4] )
|
2000-08-06 03:53:06 +00:00
|
|
|
{
|
|
|
|
unsigned char cmd[4] = {0x30, 0x00, 0x00, 0x00};
|
|
|
|
unsigned char res[4];
|
2000-08-05 05:12:50 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<4; i++) {
|
2000-08-06 03:53:06 +00:00
|
|
|
cmd[2] = i;
|
|
|
|
avr_cmd(fd, cmd, res);
|
|
|
|
sig[i] = res[3];
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* apply power to the AVR processor
|
|
|
|
*/
|
2000-08-06 03:53:06 +00:00
|
|
|
void avr_powerup ( int fd )
|
|
|
|
{
|
|
|
|
ppi_set(fd, PPIDATA, AVR_POWER); /* power up */
|
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* remove power from the AVR processor
|
|
|
|
*/
|
2000-08-06 03:53:06 +00:00
|
|
|
void avr_powerdown ( int fd )
|
|
|
|
{
|
|
|
|
ppi_clr(fd, PPIDATA, AVR_POWER); /* power down */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* initialize the AVR device and prepare it to accept commands
|
|
|
|
*/
|
2000-12-31 02:24:50 +00:00
|
|
|
int avr_initialize ( int fd, struct avrpart * p )
|
2000-08-05 05:12:50 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int tries;
|
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
avr_powerup(fd);
|
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
ppi_clr(fd, PPIDATA, AVR_CLOCK);
|
|
|
|
ppi_clr(fd, PPIDATA, AVR_RESET);
|
2000-08-07 01:48:53 +00:00
|
|
|
ppi_pulse(fd, PPIDATA, AVR_RESET);
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
usleep(20000); /* 20 ms */
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
2000-12-31 02:24:50 +00:00
|
|
|
* Enable programming mode. If we are programming an AT90S1200, we
|
|
|
|
* can only issue the command and hope it worked. If we are using
|
|
|
|
* one of the other chips, the chip will echo 0x53 when issuing the
|
|
|
|
* third byte of the command. In this case, try up to 32 times in
|
|
|
|
* order to possibly get back into sync with the chip if we are out
|
|
|
|
* of sync.
|
2000-08-06 19:47:03 +00:00
|
|
|
*/
|
2000-12-31 02:24:50 +00:00
|
|
|
if (strcmp(p->partdesc, "AT90S1200")==0) {
|
|
|
|
avr_program_enable ( fd );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tries = 0;
|
|
|
|
do {
|
|
|
|
rc = avr_program_enable ( fd );
|
|
|
|
if (rc == 0)
|
|
|
|
break;
|
|
|
|
ppi_pulse(fd, PPIDATA, AVR_CLOCK);
|
|
|
|
tries++;
|
|
|
|
} while (tries < 32);
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2000-12-31 02:24:50 +00:00
|
|
|
/*
|
|
|
|
* can't sync with the device, maybe it's not attached?
|
|
|
|
*/
|
|
|
|
if (tries == 32) {
|
|
|
|
fprintf ( stderr, "%s: AVR device not responding\n", progname );
|
|
|
|
return -1;
|
|
|
|
}
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* infinite loop, sensing on the pin that we use to read data out of
|
|
|
|
* the device; this is a debugging aid, you can insert a call to this
|
|
|
|
* function in 'main()' and can use it to determine whether your sense
|
|
|
|
* pin is actually sensing.
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int ppi_sense_test ( int fd )
|
|
|
|
{
|
|
|
|
unsigned char v, pv;
|
|
|
|
|
|
|
|
pv = 1;
|
|
|
|
do {
|
2000-08-06 19:47:03 +00:00
|
|
|
usleep(100000); /* check every 100 ms */
|
2000-08-05 05:12:50 +00:00
|
|
|
v = ppi_get(fd, PPISTATUS, AVR_DATA);
|
|
|
|
if (v != pv) {
|
2000-08-06 19:47:03 +00:00
|
|
|
fprintf ( stderr, "sense bit = %d\n", v );
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
pv = v;
|
|
|
|
} while(1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* usage message
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
void usage ( void )
|
|
|
|
{
|
2000-12-31 02:24:50 +00:00
|
|
|
int i;
|
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
fprintf ( stderr,
|
|
|
|
"\nUsage: %s [-r] [-e|-f] [-u InputFile|-o Outputfile]\n"
|
|
|
|
"\n"
|
|
|
|
" Available Options:\n"
|
|
|
|
" -r : erase the flash and eeprom (required before programming)\n"
|
|
|
|
" -e : select eeprom for reading or writing\n"
|
|
|
|
" -f : select flash for reading or writing\n"
|
2000-12-31 02:24:50 +00:00
|
|
|
" -p Part : see below for valid parts\n"
|
2000-08-05 05:12:50 +00:00
|
|
|
" -u InputFile : write data from this file\n"
|
|
|
|
" -o OutputFile : write data to this file\n"
|
2000-12-31 02:24:50 +00:00
|
|
|
" -F : override invalid device signature check\n"
|
|
|
|
" -s : read device signature bytes\n"
|
2000-08-05 05:12:50 +00:00
|
|
|
"\n",
|
|
|
|
progname );
|
2000-12-31 02:24:50 +00:00
|
|
|
|
|
|
|
fprintf(stderr, " Valid Parts for the -p option are:\n");
|
|
|
|
for (i=0; i<sizeof(parts)/sizeof(parts[0]); i++) {
|
|
|
|
fprintf(stderr, " \"%s\" = %s\n",
|
|
|
|
parts[i].optiontag, parts[i].partdesc);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* main routine
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int main ( int argc, char * argv [] )
|
|
|
|
{
|
|
|
|
int fd;
|
2000-08-07 01:48:53 +00:00
|
|
|
int rc, exitrc;
|
2000-08-06 19:47:03 +00:00
|
|
|
int i;
|
2000-12-20 02:26:31 +00:00
|
|
|
unsigned char * buf;
|
2000-08-05 05:12:50 +00:00
|
|
|
int ch;
|
|
|
|
int iofd;
|
2000-08-06 03:53:06 +00:00
|
|
|
int flash, eeprom, doread, erase, dosig;
|
2000-08-05 05:12:50 +00:00
|
|
|
int size;
|
|
|
|
char * outputf;
|
|
|
|
char * inputf;
|
2000-08-06 19:47:03 +00:00
|
|
|
char * p1, * p2;
|
2000-12-20 02:26:31 +00:00
|
|
|
struct avrpart * p;
|
2000-12-31 02:24:50 +00:00
|
|
|
unsigned char sig[4], nulldev[4];
|
|
|
|
int len;
|
|
|
|
int ovsigck;
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
iofd = -1;
|
|
|
|
outputf = NULL;
|
|
|
|
inputf = NULL;
|
|
|
|
doread = 1;
|
|
|
|
eeprom = 0;
|
|
|
|
flash = 0;
|
|
|
|
erase = 0;
|
2000-08-06 03:53:06 +00:00
|
|
|
dosig = 0;
|
2000-12-20 02:26:31 +00:00
|
|
|
p = NULL;
|
2000-12-31 02:24:50 +00:00
|
|
|
ovsigck = 0;
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
progname = rindex(argv[0],'/');
|
|
|
|
if (progname)
|
|
|
|
progname++;
|
|
|
|
else
|
|
|
|
progname = argv[0];
|
|
|
|
|
2000-08-06 18:51:32 +00:00
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* Print out an identifying string so folks can tell what version
|
|
|
|
* they are running
|
|
|
|
*/
|
|
|
|
p1 = strchr(version,',');
|
|
|
|
if (p1 == NULL)
|
|
|
|
p1 = version;
|
|
|
|
else
|
|
|
|
p1 += 3;
|
|
|
|
|
|
|
|
p2 = strrchr(p1,':');
|
|
|
|
if (p2 == NULL)
|
|
|
|
p2 = &p1[strlen(p1)];
|
|
|
|
else
|
|
|
|
p2 += 3;
|
|
|
|
|
2000-08-07 01:59:32 +00:00
|
|
|
fprintf(stderr, "\n");
|
2000-08-07 02:00:39 +00:00
|
|
|
fprintf(stderr, "AVRProg: Copyright 2000 Brian Dean, bsd@bsdhome.com\n");
|
2000-08-07 01:59:32 +00:00
|
|
|
fprintf(stderr, " Revision " );
|
2000-08-06 19:47:03 +00:00
|
|
|
for (i=0; i<p2-p1; i++)
|
|
|
|
fprintf(stderr, "%c", p1[i]);
|
2000-12-20 02:26:31 +00:00
|
|
|
fprintf(stderr, "\n\n");
|
2000-08-06 19:47:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check for no arguments
|
|
|
|
*/
|
2000-08-05 05:14:17 +00:00
|
|
|
if (argc == 1) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* process command line arguments
|
|
|
|
*/
|
2000-12-31 02:24:50 +00:00
|
|
|
while ((ch = getopt(argc,argv,"?efFo:p:rsu:")) != -1) {
|
2000-08-05 05:12:50 +00:00
|
|
|
switch (ch) {
|
2000-08-06 19:47:03 +00:00
|
|
|
case 'e': /* select eeprom memory */
|
2000-08-05 05:12:50 +00:00
|
|
|
if (flash) {
|
|
|
|
fprintf(stderr,"%s: -e and -f are incompatible\n", progname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
eeprom = 1;
|
|
|
|
break;
|
2000-08-06 19:47:03 +00:00
|
|
|
case 'f': /* select flash memory */
|
2000-08-05 05:12:50 +00:00
|
|
|
if (eeprom) {
|
|
|
|
fprintf(stderr,"%s: -e and -f are incompatible\n", progname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
flash = 1;
|
|
|
|
break;
|
2000-12-31 02:24:50 +00:00
|
|
|
case 'F': /* override invalid signature check */
|
|
|
|
ovsigck = 1;
|
|
|
|
break;
|
2000-08-06 19:47:03 +00:00
|
|
|
case 'o': /* specify output file */
|
2000-08-05 05:12:50 +00:00
|
|
|
if (inputf) {
|
|
|
|
fprintf(stderr,"%s: -o and -u are incompatible\n", progname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
doread = 1;
|
|
|
|
outputf = optarg;
|
|
|
|
if (strcmp(outputf,"-")==0) {
|
|
|
|
iofd = fileno(stdout);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iofd = open ( outputf, O_WRONLY|O_CREAT|O_TRUNC,
|
|
|
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
|
|
|
if (iofd < 0) {
|
|
|
|
fprintf(stderr, "%s: can't open output file \"%s\": %s\n",
|
|
|
|
progname, outputf, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2000-12-20 02:26:31 +00:00
|
|
|
case 'p' : /* specify AVR part */
|
|
|
|
p = NULL;
|
|
|
|
for (i=0; i<sizeof(parts)/sizeof(parts[0]); i++) {
|
|
|
|
if (strcmp(parts[i].optiontag, optarg)==0) {
|
|
|
|
p = &parts[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p == NULL) {
|
|
|
|
fprintf(stderr, "%s: AVR Part \"%s\" not found. Valid parts are:\n\n",
|
|
|
|
progname, optarg );
|
|
|
|
for (i=0; i<sizeof(parts)/sizeof(parts[0]); i++) {
|
|
|
|
fprintf(stderr, " \"%s\" = %s\n",
|
|
|
|
parts[i].optiontag, parts[i].partdesc);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2000-12-31 02:24:50 +00:00
|
|
|
case 'r': /* perform a chip erase */
|
|
|
|
erase = 1;
|
|
|
|
break;
|
|
|
|
case 's': /* read out the signature bytes */
|
|
|
|
dosig = 1;
|
|
|
|
break;
|
2000-08-06 19:47:03 +00:00
|
|
|
case 'u': /* specify input (upload) file */
|
2000-08-05 05:12:50 +00:00
|
|
|
if (outputf) {
|
|
|
|
fprintf(stderr,"%s: -o and -u are incompatible\n", progname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
doread = 0;
|
|
|
|
inputf = optarg;
|
|
|
|
iofd = open ( inputf, O_RDONLY, 0);
|
|
|
|
if (iofd < 0) {
|
|
|
|
fprintf(stderr, "%s: can't open input file \"%s\": %s\n",
|
|
|
|
progname, inputf, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2000-08-06 19:47:03 +00:00
|
|
|
case '?': /* help */
|
2000-08-05 05:12:50 +00:00
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid option -%c\n", progname, ch);
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-20 02:26:31 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: No AVR part has been specified, use \"-p Part\"\n\n"
|
|
|
|
" Valid Parts are:\n\n",
|
|
|
|
progname );
|
|
|
|
for (i=0; i<sizeof(parts)/sizeof(parts[0]); i++) {
|
|
|
|
fprintf(stderr, " \"%s\" = %s\n",
|
|
|
|
parts[i].optiontag, parts[i].partdesc);
|
|
|
|
}
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: Using AVR Part %s: flash=%d, eeprom=%d\n",
|
|
|
|
progname, p->partdesc, p->flash_size, p->eeprom_size);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
if (p->flash_size >= p->eeprom_size)
|
|
|
|
size = p->flash_size;
|
|
|
|
else
|
|
|
|
size = p->eeprom_size;
|
|
|
|
|
|
|
|
buf = (unsigned char *) malloc(size);
|
|
|
|
if (buf == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: out of memory allocating %d bytes for on-chip memory buffer\n",
|
|
|
|
progname, size);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* open the parallel port
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
fd = open ( PARALLEL, O_RDWR );
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf ( stderr, "%s: can't open device \"%s\": %s\n",
|
|
|
|
progname, PARALLEL, strerror(errno) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-08-07 01:50:37 +00:00
|
|
|
exitrc = 0;
|
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* initialize the chip in preperation for accepting commands
|
|
|
|
*/
|
2000-12-31 02:24:50 +00:00
|
|
|
rc = avr_initialize(fd,p);
|
2000-08-05 05:12:50 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
fprintf ( stderr, "%s: initialization failed, rc=%d\n", progname, rc );
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf ( stderr, "%s: AVR device initialized and ready to accept instructions\n",
|
|
|
|
progname );
|
|
|
|
|
2000-12-31 02:24:50 +00:00
|
|
|
/*
|
|
|
|
* Let's read the signature bytes to make sure there is at least a
|
|
|
|
* chip on the other end that is responding correctly. A check
|
|
|
|
* against 0xffffffff should ensure that the signature bytes are
|
|
|
|
* valid.
|
|
|
|
*/
|
|
|
|
avr_signature(fd, sig);
|
|
|
|
fprintf(stderr, "%s: Device signature = 0x", progname);
|
|
|
|
for (i=0; i<4; i++)
|
|
|
|
fprintf(stderr, "%02x", sig[i]);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
memset(nulldev,0xff,4);
|
|
|
|
if (memcmp(sig,nulldev,4)==0) {
|
|
|
|
len = strlen(progname) + 2;
|
|
|
|
for (i=0; i<len; i++)
|
|
|
|
buf[i] = ' ';
|
|
|
|
buf[i] = 0;
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Yikes! Invalid device signature.\n", progname);
|
|
|
|
if (!ovsigck) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%sDouble check connections and try again, or use -F to override\n"
|
|
|
|
"%sthis check.\n\n",
|
|
|
|
buf, buf );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
if (erase) {
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* erase the chip's flash and eeprom memories, this is required
|
|
|
|
* before the chip can accept new programming
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
fprintf(stderr, "%s: erasing chip\n", progname );
|
|
|
|
avr_chip_erase(fd);
|
2000-12-31 02:24:50 +00:00
|
|
|
avr_initialize(fd,p);
|
2000-08-05 05:12:50 +00:00
|
|
|
fprintf(stderr, "%s: done.\n", progname );
|
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
if (dosig) {
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* read out the on-chip signature bytes
|
|
|
|
*/
|
2000-08-06 03:53:06 +00:00
|
|
|
fprintf(stderr, "%s: reading signature bytes: ", progname );
|
|
|
|
avr_signature(fd, sig);
|
|
|
|
for (i=0; i<4; i++)
|
2000-12-31 02:24:50 +00:00
|
|
|
fprintf(stderr, "%02x", sig[i]);
|
2000-08-06 03:53:06 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
if (iofd < 0) {
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* Check here to see if any other operations were selected and
|
|
|
|
* generate an error message because if they were, we need either
|
|
|
|
* an input or and output file, but one was not selected.
|
|
|
|
* Otherwise, we just shut down.
|
|
|
|
*/
|
2000-08-06 03:53:06 +00:00
|
|
|
if (eeprom||flash) {
|
|
|
|
fprintf(stderr, "%s: you must specify an input or an output file\n",
|
|
|
|
progname);
|
|
|
|
}
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
if (!(eeprom||flash)) {
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* an input file or an output file was specified, but the memory
|
|
|
|
* type (eeprom or flash) was not specified.
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: please specify either the eeprom (-e) or the flash (-f) memory\n",
|
|
|
|
progname);
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
if (doread) {
|
|
|
|
/*
|
2000-08-06 19:47:03 +00:00
|
|
|
* read out the specified device memory and write it to a file
|
2000-08-05 05:12:50 +00:00
|
|
|
*/
|
|
|
|
if (flash) {
|
2000-12-20 02:26:31 +00:00
|
|
|
size = p->flash_size;
|
2000-08-05 05:12:50 +00:00
|
|
|
fprintf ( stderr, "%s: reading flash memory:\n", progname );
|
|
|
|
rc = avr_read ( fd, AVR_FLASH, 0, size/2, buf, size );
|
|
|
|
if (rc) {
|
|
|
|
fprintf ( stderr, "%s: failed to read all of flash memory, rc=%d\n",
|
|
|
|
progname, rc );
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eeprom) {
|
2000-12-20 02:26:31 +00:00
|
|
|
size = p->eeprom_size;
|
2000-08-05 05:12:50 +00:00
|
|
|
fprintf ( stderr, "%s: reading eeprom memory:\n", progname );
|
|
|
|
rc = avr_read ( fd, AVR_EEPROM, 0, size, buf, size );
|
|
|
|
if (rc) {
|
|
|
|
fprintf ( stderr, "%s: failed to read all of eeprom memory, rc=%d\n",
|
|
|
|
progname, rc );
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* write it out to the specified file
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
rc = write ( iofd, buf, size );
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: write error: %s\n", progname, strerror(errno));
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
else if (rc != size) {
|
|
|
|
fprintf(stderr, "%s: wrote only %d bytes of the expected %d\n",
|
|
|
|
progname, rc, size);
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
2000-08-06 19:47:03 +00:00
|
|
|
* write the selected device memory using data from a file
|
2000-08-05 05:12:50 +00:00
|
|
|
*/
|
|
|
|
if (flash) {
|
2000-12-20 02:26:31 +00:00
|
|
|
size = p->flash_size;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
else if (eeprom) {
|
2000-12-20 02:26:31 +00:00
|
|
|
size = p->eeprom_size;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* read in the data file that will be used to write into the chip
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
rc = read(iofd, buf, size);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: read error from \"%s\": %s\n",
|
|
|
|
progname, inputf, strerror(errno));
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
2000-08-06 03:53:06 +00:00
|
|
|
size = rc;
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* write the buffer contents to the selected memory type
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
if (flash) {
|
2000-08-06 03:53:06 +00:00
|
|
|
fprintf(stderr, "%s: writing %d bytes into flash memory:\n",
|
|
|
|
progname, size);
|
2000-08-05 05:12:50 +00:00
|
|
|
rc = avr_write ( fd, AVR_FLASH, 0, buf, size );
|
|
|
|
if (rc) {
|
|
|
|
fprintf ( stderr, "%s: failed to write flash memory, rc=%d\n",
|
|
|
|
progname, rc );
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eeprom) {
|
2000-08-06 03:53:06 +00:00
|
|
|
fprintf(stderr, "%s: writing %d bytes into eeprom memory:\n",
|
|
|
|
progname, size);
|
2000-08-05 05:12:50 +00:00
|
|
|
rc = avr_write ( fd, AVR_EEPROM, 0, buf, size );
|
|
|
|
if (rc) {
|
|
|
|
fprintf ( stderr, "%s: failed to write eeprom memory, rc=%d\n",
|
|
|
|
progname, rc );
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-07 01:48:53 +00:00
|
|
|
main_exit:
|
2000-08-06 03:53:06 +00:00
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
2000-08-07 01:48:53 +00:00
|
|
|
* program complete
|
2000-08-06 19:47:03 +00:00
|
|
|
*/
|
2000-08-07 01:48:53 +00:00
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
avr_powerdown(fd);
|
2000-08-27 16:45:17 +00:00
|
|
|
ppi_clr(fd, PPIDATA, 0xff);
|
|
|
|
ppi_set(fd, PPIDATA, AVR_RESET);
|
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
close(fd);
|
|
|
|
close(iofd);
|
|
|
|
|
2000-08-07 01:48:53 +00:00
|
|
|
fprintf(stderr, "\n" );
|
|
|
|
|
2000-08-07 01:50:37 +00:00
|
|
|
return exitrc;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|