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
|
|
|
|
* ------------- ----------------------------
|
2000-12-31 02:35:34 +00:00
|
|
|
* Pin 2 -> Vcc (see NOTE below)
|
|
|
|
* Pin 3 -> SCK CLOCK IN
|
|
|
|
* Pin 4 -> MOSI Instruction input
|
2000-08-06 03:53:06 +00:00
|
|
|
* Pin 5 -> /RESET
|
2000-12-31 18:42:42 +00:00
|
|
|
* Pin 6,7,8,9 -> Vcc (Can be tied together with Schottky diodes)
|
2000-12-31 02:35:34 +00:00
|
|
|
* Pin 10 <- MISO Data out
|
2000-12-20 02:26:31 +00:00
|
|
|
* Pin 18 <- GND
|
2000-08-05 05:12:50 +00:00
|
|
|
*
|
2000-12-31 02:35:34 +00:00
|
|
|
* NOTE on Vcc connection: make sure your parallel port can supply an
|
|
|
|
* adequate amount of current to power your device. 6-10 mA is
|
2000-12-31 18:45:53 +00:00
|
|
|
* common for parallel port signal lines, but is not guaranteed,
|
|
|
|
* especially for notebook computers. Optionally, you can tie pins
|
|
|
|
* 6, 7, 8, and 9 also to Vcc with Schottky diodes to supply
|
|
|
|
* additional current. If in doubt, don't risk damaging your
|
|
|
|
* parallel port, use an external power supply.
|
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>
|
2001-01-13 20:00:17 +00:00
|
|
|
#include <limits.h>
|
2001-01-13 21:48:10 +00:00
|
|
|
#include <ctype.h>
|
2001-01-18 03:25:03 +00:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
2000-08-05 05:12:50 +00:00
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
#define DEFAULT_PARALLEL "/dev/ppi0"
|
2000-08-05 05:12:50 +00:00
|
|
|
|
2000-08-06 18:51:32 +00:00
|
|
|
char * version = "$Id$";
|
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
char * progname;
|
2001-01-14 21:11:18 +00:00
|
|
|
char progbuf[PATH_MAX]; /* temporary buffer of spaces the same
|
|
|
|
length as progname; used for lining up
|
|
|
|
multiline messages */
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* bit definitions for AVR device connections
|
|
|
|
*/
|
2000-12-31 18:42:42 +00:00
|
|
|
#define AVR_POWER 0xf1 /* bit 0 and 4...7 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 */
|
|
|
|
#define AVR_DATA 0x40 /* bit 6 of status register */
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2001-01-13 21:48:10 +00:00
|
|
|
typedef enum {
|
2001-01-13 20:00:17 +00:00
|
|
|
FMT_AUTO,
|
|
|
|
FMT_SREC,
|
|
|
|
FMT_IHEX,
|
|
|
|
FMT_RBIN
|
2001-01-13 21:48:10 +00:00
|
|
|
} FILEFMT;
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2000-12-20 02:26:31 +00:00
|
|
|
struct avrpart {
|
2001-01-13 20:46:25 +00:00
|
|
|
char * partdesc; /* long part name */
|
|
|
|
char * optiontag; /* short part name */
|
|
|
|
int flash_size; /* size in bytes of flash */
|
|
|
|
int eeprom_size; /* size in bytes of eeprom */
|
|
|
|
unsigned char f_readback; /* flash write polled readback value */
|
|
|
|
unsigned char e_readback[2]; /* eeprom write polled readback values */
|
|
|
|
int min_write_delay; /* microseconds */
|
|
|
|
int max_write_delay; /* microseconds */
|
|
|
|
int chip_erase_delay; /* microseconds */
|
2001-01-13 20:00:17 +00:00
|
|
|
unsigned char * flash;
|
|
|
|
unsigned char * eeprom;
|
2000-12-20 02:26:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-01-18 02:35:41 +00:00
|
|
|
|
|
|
|
/* Need to add information for 2323, 2343, and 4414 */
|
|
|
|
|
2000-12-20 02:26:31 +00:00
|
|
|
struct avrpart parts[] = {
|
2001-01-18 02:35:41 +00:00
|
|
|
{ "AT90S1200", "1200", 1024, 64, 0xff, { 0x00, 0xff },
|
2001-01-13 20:46:25 +00:00
|
|
|
9000, 20000, 20000, NULL, NULL },
|
|
|
|
|
|
|
|
{ "AT90S2313", "2313", 2048, 128, 0x7f, { 0x80, 0x7f },
|
|
|
|
9000, 20000, 20000, NULL, NULL },
|
|
|
|
|
2001-01-18 02:35:41 +00:00
|
|
|
{ "AT90S2333", "2333", 2048, 128, 0xff, { 0x00, 0xff },
|
|
|
|
9000, 20000, 20000, NULL, NULL },
|
|
|
|
|
|
|
|
{ "AT90S4433", "4433", 4096, 256, 0xff, { 0x00, 0xff },
|
|
|
|
9000, 20000, 20000, NULL, NULL },
|
|
|
|
|
|
|
|
{ "AT90S4434", "4434", 4096, 256, 0xff, { 0x00, 0xff },
|
|
|
|
9000, 20000, 20000, NULL, NULL },
|
|
|
|
|
|
|
|
{ "AT90S8515", "8515", 8192, 512, 0x7f, { 0x80, 0x7f },
|
|
|
|
9000, 20000, 20000, NULL, NULL },
|
|
|
|
|
|
|
|
{ "AT90S8535", "8535", 8192, 512, 0xff, { 0x00, 0xff },
|
|
|
|
9000, 20000, 20000, NULL, NULL },
|
2001-01-13 20:00:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define N_AVRPARTS (sizeof(parts)/sizeof(struct avrpart))
|
|
|
|
|
|
|
|
|
|
|
|
struct fioparms {
|
|
|
|
int op;
|
|
|
|
char * mode;
|
|
|
|
char * iodesc;
|
|
|
|
char * dir;
|
|
|
|
char * rw;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
FIO_READ,
|
|
|
|
FIO_WRITE
|
2000-12-20 02:26:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-01-14 21:11:18 +00:00
|
|
|
int cmd_dump(int fd, struct avrpart * p, int argc, char *argv[]);
|
|
|
|
int cmd_write(int fd, struct avrpart * p, int argc, char *argv[]);
|
2001-01-18 03:25:03 +00:00
|
|
|
int cmd_erase(int fd, struct avrpart * p, int argc, char *argv[]);
|
|
|
|
int cmd_sig(int fd, struct avrpart * p, int argc, char *argv[]);
|
|
|
|
int cmd_part(int fd, struct avrpart * p, int argc, char *argv[]);
|
|
|
|
int cmd_help(int fd, struct avrpart * p, int argc, char *argv[]);
|
2001-01-14 21:11:18 +00:00
|
|
|
int cmd_quit(int fd, struct avrpart * p, int argc, char *argv[]);
|
|
|
|
|
|
|
|
struct command {
|
|
|
|
char * name;
|
|
|
|
int (*func)(int fd, struct avrpart * p, int argc, char *argv[]);
|
2001-01-18 03:25:03 +00:00
|
|
|
char * desc;
|
2001-01-14 21:11:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct command cmd[] = {
|
2001-01-18 03:25:03 +00:00
|
|
|
{ "dump", cmd_dump, "dump memory : %s [eeprom|flash] <addr> <N-Bytes>" },
|
|
|
|
{ "write", cmd_write, "write memory : %s [eeprom|flash] <addr> <b1> <b2> ... <bN>" },
|
|
|
|
{ "erase", cmd_erase, "perform a chip erase" },
|
|
|
|
{ "sig", cmd_sig, "display device signature bytes" },
|
|
|
|
{ "part", cmd_part, "display the current part settings" },
|
|
|
|
{ "help", cmd_help, "help" },
|
|
|
|
{ "?", cmd_help, "help" },
|
|
|
|
{ "quit", cmd_quit, "quit" }
|
2001-01-14 21:11:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NCMDS (sizeof(cmd)/sizeof(struct command))
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
|
|
|
|
#define MAX_LINE_LEN 256 /* max line length for ASCII format input files */
|
|
|
|
|
|
|
|
|
|
|
|
char * usage_text =
|
|
|
|
"\n"
|
|
|
|
"Usage: avrprog [options]\n"
|
|
|
|
"\n"
|
|
|
|
" Available Options:\n"
|
|
|
|
"\n"
|
|
|
|
" -m MemType : select memory type for reading or writing\n"
|
|
|
|
" \"e\", \"eeprom\" = EEPROM\n"
|
2001-01-13 21:04:44 +00:00
|
|
|
" \"f\", \"flash\" = FLASH (default)\n"
|
2001-01-13 20:00:17 +00:00
|
|
|
"\n"
|
2001-01-13 20:50:03 +00:00
|
|
|
" -i Filename : select input file, \"-\" = stdin\n"
|
2001-01-13 20:00:17 +00:00
|
|
|
"\n"
|
|
|
|
" -o Filename : select output file, \"-\" = stdout\n"
|
|
|
|
"\n"
|
|
|
|
" -f Format : select input / output file format\n"
|
|
|
|
" \"i\" = Intel Hex\n"
|
|
|
|
" \"s\" = Motorola S-Record\n"
|
2001-01-13 21:04:44 +00:00
|
|
|
" \"r\" = Raw binary (default for output)\n"
|
|
|
|
" \"a\" = Auto detect (default for input)\n"
|
|
|
|
" (valid for input only)\n"
|
|
|
|
" \n"
|
2001-01-13 20:00:17 +00:00
|
|
|
"\n"
|
|
|
|
" -p Part : select Atmel part number (see below for valid parts)\n"
|
|
|
|
"\n"
|
|
|
|
" -P Parallel : select parallel port device name (default = /dev/ppi0)\n"
|
|
|
|
"\n"
|
|
|
|
" -F : override invalid device signature check\n"
|
|
|
|
"\n"
|
2001-01-18 02:35:41 +00:00
|
|
|
" -t : enter terminal mode (or read commands from stdin)\n"
|
2001-01-13 20:00:17 +00:00
|
|
|
"\n"
|
2001-01-19 00:49:13 +00:00
|
|
|
" -E exitspec[,...]: specify which bits to set/reset on exit\n"
|
|
|
|
" \"[no]reset\" = [don't] activate /RESET\n"
|
|
|
|
" \"[no]vcc\" = [don't] activate Vcc\n"
|
|
|
|
"\n"
|
2001-01-13 20:00:17 +00:00
|
|
|
" -e : perform a chip erase (required before programming)\n"
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-18 03:25:03 +00:00
|
|
|
int avr_txrx_bit ( int fd, int bit );
|
|
|
|
|
|
|
|
unsigned char avr_txrx ( int fd, unsigned char byte );
|
|
|
|
|
|
|
|
int avr_cmd ( int fd, unsigned char cmd[4], unsigned char res[4] );
|
|
|
|
|
|
|
|
unsigned char avr_read_byte ( int fd, struct avrpart * p,
|
|
|
|
AVRMEM memtype, unsigned short addr );
|
|
|
|
|
|
|
|
int avr_read ( int fd, struct avrpart * p, AVRMEM memtype );
|
|
|
|
|
|
|
|
int avr_write_byte ( int fd, struct avrpart * p, AVRMEM memtype,
|
|
|
|
unsigned short addr, unsigned char data );
|
|
|
|
|
|
|
|
int avr_write ( int fd, struct avrpart * p, AVRMEM memtype );
|
|
|
|
|
|
|
|
int avr_program_enable ( int fd );
|
|
|
|
|
|
|
|
int avr_chip_erase ( int fd, struct avrpart * p );
|
|
|
|
|
|
|
|
int avr_signature ( int fd, unsigned char sig[4] );
|
|
|
|
|
|
|
|
void avr_powerup ( int fd );
|
|
|
|
|
|
|
|
void avr_powerdown ( int fd );
|
|
|
|
|
|
|
|
int avr_initialize ( int fd, struct avrpart * p );
|
|
|
|
|
|
|
|
int avr_initmem ( struct avrpart * p );
|
|
|
|
|
|
|
|
void display_part ( FILE * f, struct avrpart * p, char * prefix );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
int list_valid_parts ( FILE * f, char * prefix )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<N_AVRPARTS; i++) {
|
2001-01-13 20:50:03 +00:00
|
|
|
fprintf(f, "%s%s = %s\n",
|
2001-01-13 20:00:17 +00:00
|
|
|
prefix, parts[i].optiontag, parts[i].partdesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-19 00:49:13 +00:00
|
|
|
/*
|
|
|
|
* get all bits of the specified register.
|
|
|
|
*/
|
|
|
|
int ppi_getall ( int fd, int reg )
|
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
unsigned long get, set;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ppi_getops ( reg, &get, &set );
|
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ioctl(fd, get, &v);
|
|
|
|
|
|
|
|
return (int)v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set all bits of the specified register to val.
|
|
|
|
*/
|
|
|
|
int ppi_setall ( int fd, int reg, int val )
|
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
unsigned long get, set;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ppi_getops ( reg, &get, &set );
|
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
v = val;
|
|
|
|
ioctl(fd, set, &v);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-07 01:48:53 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2001-01-13 20:46:25 +00:00
|
|
|
unsigned char avr_read_byte ( int fd, struct avrpart * p,
|
|
|
|
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-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-01-13 20:00:17 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: avr_read_byte(); internal error: invalid memtype=%d\n",
|
2000-08-05 05:12:50 +00:00
|
|
|
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
|
|
|
/*
|
2001-01-13 20:00:17 +00:00
|
|
|
* read the entirety of the specified memory type into the
|
|
|
|
* corresponding buffer of the avrpart pointed to by 'p'.
|
2000-08-06 19:47:03 +00:00
|
|
|
*/
|
2001-01-13 20:46:25 +00:00
|
|
|
int avr_read ( int fd, struct avrpart * p, AVRMEM memtype )
|
2000-08-05 05:12:50 +00:00
|
|
|
{
|
2000-08-06 03:53:06 +00:00
|
|
|
unsigned char rbyte, memt;
|
2001-01-13 20:00:17 +00:00
|
|
|
unsigned short n, start, end, i, bi;
|
|
|
|
unsigned char * buf;
|
|
|
|
int bufsize;
|
2000-08-05 05:12:50 +00:00
|
|
|
|
2001-01-15 02:28:46 +00:00
|
|
|
start = 0;
|
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
switch (memtype) {
|
2000-08-06 03:53:06 +00:00
|
|
|
case AVR_FLASH :
|
2001-01-13 20:00:17 +00:00
|
|
|
memt = AVR_FLASH_LO;
|
|
|
|
buf = p->flash;
|
|
|
|
n = p->flash_size/2;
|
|
|
|
bufsize = p->flash_size;
|
2000-08-06 03:53:06 +00:00
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
case AVR_EEPROM :
|
2001-01-13 20:00:17 +00:00
|
|
|
memt = memtype;
|
|
|
|
buf = p->eeprom;
|
|
|
|
n = p->eeprom_size;
|
|
|
|
bufsize = p->eeprom_size;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
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 */
|
2001-01-13 20:46:25 +00:00
|
|
|
rbyte = avr_read_byte(fd, p, memt, i);
|
2000-08-06 03:53:06 +00:00
|
|
|
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 */
|
2001-01-13 20:46:25 +00:00
|
|
|
rbyte = avr_read_byte(fd, p, 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
|
|
|
|
*/
|
2001-01-13 20:46:25 +00:00
|
|
|
int avr_write_byte ( int fd, struct avrpart * p, AVRMEM memtype,
|
2001-01-13 20:00:17 +00:00
|
|
|
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;
|
2001-01-14 21:32:36 +00:00
|
|
|
unsigned char b;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check to see if the write is necessary by reading the existing
|
|
|
|
* value and only write if we are changing the value
|
|
|
|
*/
|
|
|
|
b = avr_read_byte(fd, p, memtype, addr);
|
|
|
|
if (b == data) {
|
|
|
|
return 0;
|
|
|
|
}
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
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-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-01-13 20:00:17 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: avr_write_byte(); internal error: invalid memtype=%d\n",
|
2000-08-05 05:12:50 +00:00
|
|
|
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) {
|
2001-01-13 20:46:25 +00:00
|
|
|
usleep(p->min_write_delay); /* typical flash/eeprom write delay */
|
|
|
|
r = avr_read_byte(fd, p, memtype, addr);
|
|
|
|
if ((data == p->f_readback) ||
|
|
|
|
(data == p->e_readback[0]) || (data == p->e_readback[1])) {
|
|
|
|
/*
|
|
|
|
* use an extra long delay when we happen to be writing values
|
|
|
|
* used for polled data read-back. In this case, polling
|
|
|
|
* doesn't work, and we need to delay the worst case write time
|
|
|
|
* specified for the chip.
|
|
|
|
*/
|
|
|
|
usleep(p->max_write_delay);
|
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
|
|
|
/*
|
2001-01-13 20:00:17 +00:00
|
|
|
* Write the whole memory region (flash or eeprom, specified by
|
|
|
|
* 'memtype') from the corresponding buffer of the avrpart pointed to
|
|
|
|
* by 'p'. All of the memory is updated, however, input data of 0xff
|
|
|
|
* is not actually written out, because empty flash and eeprom
|
|
|
|
* contains 0xff, and you can't actually write 1's, only 0's.
|
2000-08-06 19:47:03 +00:00
|
|
|
*/
|
2001-01-13 20:46:25 +00:00
|
|
|
int avr_write ( int fd, struct avrpart * p, AVRMEM memtype )
|
2000-08-05 05:12:50 +00:00
|
|
|
{
|
2000-08-06 03:53:06 +00:00
|
|
|
unsigned char data, memt;
|
2001-01-13 20:00:17 +00:00
|
|
|
unsigned short start, end, i, bi;
|
2000-08-06 03:53:06 +00:00
|
|
|
int nl;
|
|
|
|
int rc;
|
2001-01-13 20:00:17 +00:00
|
|
|
unsigned char * buf;
|
|
|
|
int bufsize;
|
|
|
|
|
|
|
|
start = 0;
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
switch (memtype) {
|
|
|
|
case AVR_FLASH :
|
2001-01-13 20:00:17 +00:00
|
|
|
buf = p->flash;
|
|
|
|
bufsize = p->flash_size;
|
|
|
|
end = start+bufsize/2;
|
|
|
|
memt = AVR_FLASH_LO;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
case AVR_EEPROM :
|
2001-01-13 20:00:17 +00:00
|
|
|
buf = p->eeprom;
|
|
|
|
bufsize = p->eeprom_size;
|
|
|
|
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;
|
2001-01-14 21:32:36 +00:00
|
|
|
rc = avr_write_byte(fd, p, memt, i, data );
|
2000-08-06 03:53:06 +00:00
|
|
|
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++];
|
2001-01-14 21:32:36 +00:00
|
|
|
rc = avr_write_byte(fd, p, AVR_FLASH_HI, i, data );
|
2000-08-06 03:53:06 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-01-14 01:55:08 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2001-01-13 20:46:25 +00:00
|
|
|
int avr_chip_erase ( int fd, struct avrpart * p )
|
2000-08-05 05:12:50 +00:00
|
|
|
{
|
|
|
|
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);
|
2001-01-13 20:46:25 +00:00
|
|
|
usleep(p->chip_erase_delay);
|
2001-01-18 03:25:03 +00:00
|
|
|
avr_initialize(fd, p);
|
2000-08-06 03:53:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2001-01-14 01:55:08 +00:00
|
|
|
usleep(20000); /* 20 ms XXX should be a per-chip parameter */
|
2000-08-05 05:12:50 +00:00
|
|
|
|
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 )
|
|
|
|
{
|
2001-01-13 20:00:17 +00:00
|
|
|
fprintf ( stderr, "%s", usage_text );
|
2000-12-31 02:24:50 +00:00
|
|
|
|
|
|
|
fprintf(stderr, " Valid Parts for the -p option are:\n");
|
2001-01-13 20:00:17 +00:00
|
|
|
list_valid_parts(stderr, " ");
|
2000-12-31 02:24:50 +00:00
|
|
|
fprintf(stderr, "\n");
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-13 21:48:10 +00:00
|
|
|
char * fmtstr ( FILEFMT format )
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case FMT_AUTO : return "auto-detect"; break;
|
|
|
|
case FMT_SREC : return "Motorola S-Record"; break;
|
|
|
|
case FMT_IHEX : return "Intel Hex"; break;
|
|
|
|
case FMT_RBIN : return "raw binary"; break;
|
|
|
|
default : return "invalid format"; break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
|
|
|
|
int b2ihex ( unsigned char * inbuf, int bufsize,
|
|
|
|
int recsize, int startaddr,
|
|
|
|
char * outfile, FILE * outf )
|
|
|
|
{
|
|
|
|
unsigned char * buf;
|
|
|
|
unsigned int nextaddr;
|
|
|
|
int n;
|
|
|
|
int i;
|
|
|
|
unsigned char cksum;
|
|
|
|
|
|
|
|
if (recsize > 255) {
|
|
|
|
fprintf ( stderr, "%s: recsize=%d, must be < 256\n",
|
|
|
|
progname, recsize );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextaddr = startaddr;
|
|
|
|
|
|
|
|
buf = inbuf;
|
|
|
|
while (bufsize) {
|
|
|
|
n = recsize;
|
|
|
|
if (n > bufsize)
|
|
|
|
n = bufsize;
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
cksum = 0;
|
|
|
|
fprintf ( outf, ":%02X%04X00", n, nextaddr );
|
|
|
|
cksum += n + ((nextaddr >> 8) & 0x0ff) + (nextaddr & 0x0ff);
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
fprintf ( outf, "%02X", buf[i] );
|
|
|
|
cksum += buf[i];
|
|
|
|
}
|
|
|
|
cksum = -cksum;
|
|
|
|
fprintf ( outf, "%02X\n", cksum );
|
|
|
|
|
|
|
|
nextaddr += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* advance to next 'recsize' bytes */
|
|
|
|
buf += n;
|
|
|
|
bufsize -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
2001-01-14 01:55:08 +00:00
|
|
|
add the end of record data line
|
2001-01-13 20:00:17 +00:00
|
|
|
-----------------------------------------------------------------*/
|
|
|
|
cksum = 0;
|
|
|
|
n = 0;
|
2001-01-14 01:55:08 +00:00
|
|
|
nextaddr = 0;
|
|
|
|
fprintf ( outf, ":%02X%04X01", n, nextaddr );
|
2001-01-13 20:00:17 +00:00
|
|
|
cksum += n + ((nextaddr >> 8) & 0x0ff) + (nextaddr & 0x0ff);
|
|
|
|
cksum = -cksum;
|
|
|
|
fprintf ( outf, "%02X\n", cksum );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ihex2b ( char * infile, FILE * inf,
|
|
|
|
unsigned char * outbuf, int bufsize )
|
|
|
|
{
|
|
|
|
unsigned char buffer [ MAX_LINE_LEN ];
|
|
|
|
unsigned char * buf;
|
|
|
|
unsigned int prevaddr, nextaddr;
|
|
|
|
unsigned int b;
|
|
|
|
int n;
|
|
|
|
int i, j;
|
|
|
|
unsigned int cksum, rectype;
|
|
|
|
int lineno;
|
|
|
|
|
|
|
|
lineno = 0;
|
|
|
|
prevaddr = 0;
|
|
|
|
buf = outbuf;
|
|
|
|
|
|
|
|
while (fgets((char *)buffer,MAX_LINE_LEN,inf)!=NULL) {
|
|
|
|
lineno++;
|
|
|
|
if (buffer[0] != ':')
|
|
|
|
continue;
|
|
|
|
if (sscanf((char *)&buffer[1],
|
|
|
|
"%02x%04x%02x", &n, &nextaddr, &rectype) != 3) {
|
|
|
|
fprintf(stderr, "%s: invalid record at line %d of \"%s\"\n",
|
|
|
|
progname, lineno, infile);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-01-14 01:55:08 +00:00
|
|
|
if ((rectype != 0) && (rectype != 1)) {
|
2001-01-13 20:00:17 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: don't know how to deal with rectype=%d "
|
2001-01-13 20:12:08 +00:00
|
|
|
"at line %d of %s\n",
|
2001-01-13 20:00:17 +00:00
|
|
|
progname, rectype, lineno, infile);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n && ((nextaddr + n) > bufsize)) {
|
|
|
|
fprintf(stderr, "%s: address 0x%04x out of range at line %d of %s\n",
|
|
|
|
progname, nextaddr+n, lineno, infile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start computing a checksum */
|
|
|
|
cksum = n + ((nextaddr >> 8 ) & 0x0ff) + (nextaddr & 0x0ff);
|
|
|
|
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
if (sscanf((char *)&buffer[i*2+9], "%02x", &b) != 1) {
|
|
|
|
fprintf(stderr, "%s: can't scan byte number %d at line %d of %s\n",
|
|
|
|
progname, i, lineno, infile);
|
|
|
|
/* display the buffer and the position of the scan error */
|
|
|
|
fprintf(stderr, "%s", buffer);
|
|
|
|
for (j=0; j<9+2*i; j++) {
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "^\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[nextaddr + i] = b;
|
|
|
|
cksum += b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
|
|
|
read the cksum value from the record and compare it with our
|
|
|
|
computed value
|
|
|
|
-----------------------------------------------------------------*/
|
|
|
|
if (sscanf((char *)&buffer[n*2+9], "%02x", &b) != 1) {
|
|
|
|
fprintf(stderr, "%s: can't scan byte number %d at line %d of %s\n",
|
|
|
|
progname, i, lineno, infile);
|
|
|
|
/* display the buffer and the position of the scan error */
|
|
|
|
fprintf(stderr, "%s", buffer);
|
|
|
|
for (j=0; j<9+2*i; j++) {
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "^\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cksum = -cksum & 0xff;
|
|
|
|
if (cksum != b) {
|
|
|
|
fprintf(stderr,
|
2001-01-14 21:32:36 +00:00
|
|
|
"%s: WARNING: cksum error for line %d of \"%s\": computed=%02x "
|
2001-01-13 20:00:17 +00:00
|
|
|
"found=%02x\n",
|
|
|
|
progname, lineno, infile, cksum, b);
|
2001-01-14 21:32:36 +00:00
|
|
|
/* return -1; */
|
2001-01-13 20:00:17 +00:00
|
|
|
}
|
|
|
|
|
2001-01-14 01:55:08 +00:00
|
|
|
if (rectype == 1) {
|
|
|
|
/* end of record */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
prevaddr = nextaddr + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int fileio_rbin ( struct fioparms * fio,
|
|
|
|
char * filename, FILE * f, unsigned char * buf, int size )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (fio->op) {
|
|
|
|
case FIO_READ:
|
|
|
|
rc = fread(buf, 1, size, f);
|
|
|
|
break;
|
|
|
|
case FIO_WRITE:
|
|
|
|
rc = fwrite(buf, 1, size, f);
|
|
|
|
break;
|
2001-01-15 02:28:46 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: fileio: invalid operation=%d\n",
|
|
|
|
progname, fio->op);
|
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc < size) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %s error %s %s: %s; %s %d of the expected %d bytes\n",
|
|
|
|
progname, fio->iodesc, fio->dir, filename, strerror(errno),
|
|
|
|
fio->rw, rc, size);
|
2001-01-13 21:48:10 +00:00
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int fileio_ihex ( struct fioparms * fio,
|
|
|
|
char * filename, FILE * f, unsigned char * buf, int size )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (fio->op) {
|
|
|
|
case FIO_WRITE:
|
|
|
|
rc = b2ihex(buf, size, 32, 0, filename, f);
|
|
|
|
if (rc) {
|
2001-01-13 21:48:10 +00:00
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIO_READ:
|
|
|
|
rc = ihex2b(filename, f, buf, size);
|
|
|
|
if (rc)
|
2001-01-13 21:48:10 +00:00
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid Intex Hex file I/O operation=%d\n",
|
|
|
|
progname, fio->op);
|
2001-01-13 21:48:10 +00:00
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int fileio_srec ( struct fioparms * fio,
|
|
|
|
char * filename, FILE * f, unsigned char * buf, int size )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Motorola S-Record %s format not yet supported\n",
|
|
|
|
progname, fio->iodesc);
|
2001-01-13 21:48:10 +00:00
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int fileio_setparms ( int op, struct fioparms * fp )
|
|
|
|
{
|
|
|
|
fp->op = op;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case FIO_READ:
|
|
|
|
fp->mode = "r";
|
|
|
|
fp->iodesc = "input";
|
|
|
|
fp->dir = "from";
|
|
|
|
fp->rw = "read";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIO_WRITE:
|
|
|
|
fp->mode = "w";
|
|
|
|
fp->iodesc = "output";
|
|
|
|
fp->dir = "to";
|
|
|
|
fp->rw = "wrote";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid I/O operation %d\n",
|
|
|
|
progname, op);
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-13 21:48:10 +00:00
|
|
|
|
|
|
|
int fmt_autodetect ( char * fname )
|
|
|
|
{
|
|
|
|
FILE * f;
|
|
|
|
unsigned char buf[MAX_LINE_LEN];
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
int found;
|
|
|
|
|
|
|
|
f = fopen(fname, "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
fprintf(stderr, "%s: error opening %s: %s\n",
|
|
|
|
progname, fname, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets((char *)buf, MAX_LINE_LEN, f)!=NULL) {
|
|
|
|
buf[MAX_LINE_LEN-1] = 0;
|
|
|
|
len = strlen((char *)buf);
|
|
|
|
if (buf[len-1] == '\n')
|
|
|
|
buf[--len] = 0;
|
|
|
|
|
|
|
|
/* check for binary data */
|
|
|
|
found = 0;
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (buf[i] > 127) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
return FMT_RBIN;
|
|
|
|
|
|
|
|
/* check for lines that look like intel hex */
|
|
|
|
if ((buf[0] == ':') && (len >= 11)) {
|
|
|
|
found = 1;
|
|
|
|
for (i=1; i<len; i++) {
|
|
|
|
if (!isxdigit(buf[1])) {
|
|
|
|
found = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
return FMT_IHEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for lines that look like motorola s-record */
|
|
|
|
if ((buf[0] == 'S') && (len >= 10) && isdigit(buf[1])) {
|
|
|
|
found = 1;
|
|
|
|
for (i=1; i<len; i++) {
|
|
|
|
if (!isxdigit(buf[1])) {
|
|
|
|
found = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
return FMT_SREC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int fileio ( int op, char * filename, FILEFMT format,
|
2001-01-13 20:00:17 +00:00
|
|
|
struct avrpart * p, AVRMEM memtype )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
FILE * f;
|
|
|
|
char * fname;
|
|
|
|
unsigned char * buf;
|
|
|
|
int size;
|
|
|
|
struct fioparms fio;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
rc = fileio_setparms(op, &fio);
|
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strcmp(filename, "-")==0) {
|
|
|
|
if (fio.op == FIO_READ) {
|
|
|
|
fname = "<stdin>";
|
|
|
|
f = stdin;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fname = "<stdout>";
|
|
|
|
f = stdout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2001-01-13 20:12:08 +00:00
|
|
|
fname = filename;
|
|
|
|
f = fopen(fname, fio.mode);
|
2001-01-13 20:00:17 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
fprintf(stderr, "%s: can't open %s file %s: %s\n",
|
2001-01-13 20:12:08 +00:00
|
|
|
progname, fio.iodesc, fname, strerror(errno));
|
2001-01-13 21:48:10 +00:00
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (memtype) {
|
|
|
|
case AVR_EEPROM:
|
|
|
|
buf = p->eeprom;
|
|
|
|
size = p->eeprom_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVR_FLASH:
|
|
|
|
buf = p->flash;
|
|
|
|
size = p->flash_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid memory type for %s: %d\n",
|
|
|
|
progname, fio.iodesc, memtype);
|
2001-01-13 21:48:10 +00:00
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fio.op == FIO_READ) {
|
|
|
|
/* 0xff fill unspecified memory */
|
|
|
|
for (i=0; i<size; i++) {
|
|
|
|
buf[i] = 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-13 21:48:10 +00:00
|
|
|
if (format == FMT_AUTO) {
|
|
|
|
format = fmt_autodetect(fname);
|
|
|
|
if (format < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: can't determine file format for %s, specify explicitly\n",
|
|
|
|
progname, fname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: %s file %s auto detected as %s\n\n",
|
|
|
|
progname, fio.iodesc, fname, fmtstr(format));
|
|
|
|
}
|
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
switch (format) {
|
|
|
|
case FMT_IHEX:
|
|
|
|
rc = fileio_ihex(&fio, fname, f, buf, size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMT_SREC:
|
|
|
|
rc = fileio_srec(&fio, fname, f, buf, size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMT_RBIN:
|
|
|
|
rc = fileio_rbin(&fio, fname, f, buf, size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid %s file format: %d\n",
|
|
|
|
progname, fio.iodesc, format);
|
2001-01-13 21:48:10 +00:00
|
|
|
return -1;
|
2001-01-13 20:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char * memtypestr ( AVRMEM memtype )
|
|
|
|
{
|
|
|
|
switch (memtype) {
|
|
|
|
case AVR_EEPROM : return "eeprom"; break;
|
|
|
|
case AVR_FLASH : return "flash"; break;
|
|
|
|
default : return "unknown-memtype"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-14 21:11:18 +00:00
|
|
|
int nexttok ( char * buf, char ** tok, char ** next )
|
|
|
|
{
|
|
|
|
char * q, * n;
|
|
|
|
|
|
|
|
q = buf;
|
|
|
|
while (isspace(*q))
|
|
|
|
q++;
|
|
|
|
|
|
|
|
/* isolate first token */
|
|
|
|
n = q+1;
|
|
|
|
while (*n && !isspace(*n))
|
|
|
|
n++;
|
|
|
|
|
|
|
|
if (*n) {
|
|
|
|
*n = 0;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find start of next token */
|
|
|
|
while (isspace(*n))
|
|
|
|
n++;
|
|
|
|
|
|
|
|
*tok = q;
|
|
|
|
*next = n;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int hexdump_line ( char * buffer, unsigned char * p, int n, int pad )
|
|
|
|
{
|
|
|
|
char * hexdata = "0123456789abcdef";
|
|
|
|
char * b;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
b = buffer;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
if (i && ((i % 8) == 0))
|
|
|
|
b[j++] = ' ';
|
|
|
|
b[j++] = hexdata[(p[i] & 0xf0) >> 4];
|
|
|
|
b[j++] = hexdata[(p[i] & 0x0f)];
|
|
|
|
if (i < 15)
|
|
|
|
b[j++] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=j; i<pad; i++)
|
|
|
|
b[i] = ' ';
|
|
|
|
|
|
|
|
b[i] = 0;
|
|
|
|
|
|
|
|
for (i=0; i<pad; i++) {
|
|
|
|
if (!((b[i] == '0') || (b[i] == ' ')))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int chardump_line ( char * buffer, unsigned char * p, int n, int pad )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char b [ 128 ];
|
|
|
|
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
memcpy ( b, p, n );
|
|
|
|
buffer[i] = '.';
|
|
|
|
if (isalpha(b[i]) || isdigit(b[i]) || ispunct(b[i]))
|
|
|
|
buffer[i] = b[i];
|
|
|
|
else if (isspace(b[i]))
|
|
|
|
buffer[i] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=n; i<pad; i++)
|
|
|
|
buffer[i] = ' ';
|
|
|
|
|
|
|
|
buffer[i] = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int hexdump_buf ( FILE * f, int startaddr, char * buf, int len )
|
|
|
|
{
|
|
|
|
int addr;
|
|
|
|
int i, n;
|
|
|
|
unsigned char * p;
|
|
|
|
char dst1[80];
|
|
|
|
char dst2[80];
|
|
|
|
|
|
|
|
addr = startaddr;
|
|
|
|
i = 0;
|
|
|
|
p = (unsigned char *)buf;
|
|
|
|
while (len) {
|
|
|
|
n = 16;
|
|
|
|
if (n > len)
|
|
|
|
n = len;
|
|
|
|
hexdump_line(dst1, p, n, 48);
|
|
|
|
chardump_line(dst2, p, n, 16);
|
|
|
|
fprintf(stdout, "%04x %s |%s|\n", addr, dst1, dst2);
|
|
|
|
len -= n;
|
|
|
|
addr += n;
|
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int cmd_dump ( int fd, struct avrpart * p, int argc, char * argv[] )
|
|
|
|
{
|
|
|
|
char * e;
|
|
|
|
int i, j;
|
|
|
|
int len, maxsize;
|
|
|
|
AVRMEM memtype;
|
|
|
|
unsigned short addr, daddr;
|
|
|
|
char * buf;
|
|
|
|
|
|
|
|
if (argc != 4) {
|
|
|
|
fprintf(stderr, "Usage: dump flash|eeprom <addr> <len>\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1],"flash")==0) {
|
|
|
|
memtype = AVR_FLASH;
|
|
|
|
maxsize = p->flash_size;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1],"eeprom")==0) {
|
|
|
|
memtype = AVR_EEPROM;
|
|
|
|
maxsize = p->eeprom_size;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "%s (dump): invalid memory type \"%s\"\n",
|
|
|
|
progname, argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = strtoul(argv[2], &e, 0);
|
|
|
|
if (*e || (e == argv[2])) {
|
|
|
|
fprintf(stderr, "%s (dump): can't parse address \"%s\"\n",
|
|
|
|
progname, argv[2]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strtol(argv[3], &e, 0);
|
|
|
|
if (*e || (e == argv[3])) {
|
|
|
|
fprintf(stderr, "%s (dump): can't parse length \"%s\"\n",
|
|
|
|
progname, argv[3]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr > maxsize) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s (dump): address 0x%04x is out of range for %s memory\n",
|
|
|
|
progname, addr, memtypestr(memtype));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((addr + len) > maxsize) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s (dump): selected address and length exceed "
|
|
|
|
"range for %s memory\n",
|
|
|
|
progname, memtypestr(memtype));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = malloc(len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
fprintf(stderr, "%s (dump): out of memory\n", progname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
daddr = addr;
|
|
|
|
if (memtype == AVR_FLASH) {
|
|
|
|
daddr = addr / 2;
|
|
|
|
if (addr & 0x01) {
|
|
|
|
buf[j++] = avr_read_byte( fd, p, AVR_FLASH_HI, daddr);
|
|
|
|
daddr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i = daddr;
|
|
|
|
while (j < len) {
|
|
|
|
if (memtype == AVR_FLASH) {
|
|
|
|
buf[j++] = avr_read_byte( fd, p, AVR_FLASH_LO, i);
|
|
|
|
if (j < len) {
|
|
|
|
buf[j++] = avr_read_byte( fd, p, AVR_FLASH_HI, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf[j++] = avr_read_byte( fd, p, AVR_EEPROM, i);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
hexdump_buf(stdout, addr, buf, len);
|
|
|
|
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_write ( int fd, struct avrpart * p, int argc, char * argv[] )
|
|
|
|
{
|
|
|
|
char * e;
|
|
|
|
int i, j;
|
|
|
|
int len, maxsize;
|
|
|
|
AVRMEM memtype;
|
|
|
|
unsigned short addr, daddr;
|
|
|
|
char * buf;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (argc < 4) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: write flash|eeprom <addr> <byte1> <byte2> ... byteN>\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1],"flash")==0) {
|
|
|
|
memtype = AVR_FLASH;
|
|
|
|
maxsize = p->flash_size;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1],"eeprom")==0) {
|
|
|
|
memtype = AVR_EEPROM;
|
|
|
|
maxsize = p->eeprom_size;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "%s (write): invalid memory type \"%s\"\n",
|
|
|
|
progname, argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = strtoul(argv[2], &e, 0);
|
|
|
|
if (*e || (e == argv[2])) {
|
|
|
|
fprintf(stderr, "%s (write): can't parse address \"%s\"\n",
|
|
|
|
progname, argv[2]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr > maxsize) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s (write): address 0x%04x is out of range for %s memory\n",
|
|
|
|
progname, addr, memtypestr(memtype));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* number of bytes to write at the specified address */
|
|
|
|
len = argc - 3;
|
|
|
|
|
|
|
|
if ((addr + len) > maxsize) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s (write): selected address and # bytes exceed "
|
|
|
|
"range for %s memory\n",
|
|
|
|
progname, memtypestr(memtype));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = malloc(len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
fprintf(stderr, "%s (write): out of memory\n", progname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=3; i<argc; i++) {
|
|
|
|
buf[i-3] = strtoul(argv[i], &e, 0);
|
|
|
|
if (*e || (e == argv[i])) {
|
|
|
|
fprintf(stderr, "%s (write): can't parse byte \"%s\"\n",
|
|
|
|
progname, argv[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
daddr = addr;
|
|
|
|
if (memtype == AVR_FLASH) {
|
|
|
|
daddr = addr / 2;
|
|
|
|
if (addr & 0x01) {
|
|
|
|
/* handle odd numbered memory locations in the flash area */
|
|
|
|
rc = avr_write_byte(fd, p, AVR_FLASH_HI, daddr, buf[j++]);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "%s (write): error writing 0x%02x at 0x%04x\n",
|
|
|
|
progname, buf[j-1], daddr*2+1);
|
|
|
|
}
|
|
|
|
daddr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i = daddr;
|
|
|
|
while (j < len) {
|
|
|
|
if (memtype == AVR_FLASH) {
|
|
|
|
rc = avr_write_byte( fd, p, AVR_FLASH_LO, i, buf[j++]);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "%s (write): error writing 0x%02x at 0x%04x\n",
|
|
|
|
progname, buf[j-1], i*2);
|
|
|
|
}
|
|
|
|
if (j < len) {
|
|
|
|
rc = avr_write_byte( fd, p, AVR_FLASH_HI, i, buf[j++]);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "%s (write): error writing 0x%02x at 0x%04x\n",
|
|
|
|
progname, buf[j-1], i*2+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rc = avr_write_byte( fd, p, AVR_EEPROM, i, buf[j++]);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "%s (write): error writing 0x%02x at 0x%04x\n",
|
|
|
|
progname, buf[j-1], i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2001-01-14 21:43:14 +00:00
|
|
|
free(buf);
|
|
|
|
|
2001-01-14 21:11:18 +00:00
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-18 03:25:03 +00:00
|
|
|
int cmd_erase ( int fd, struct avrpart * p, int argc, char * argv[] )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: erasing chip\n", progname );
|
|
|
|
avr_chip_erase(fd,p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int cmd_part ( int fd, struct avrpart * p, int argc, char * argv[] )
|
|
|
|
{
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
display_part(stdout, p, "");
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int cmd_sig ( int fd, struct avrpart * p, int argc, char * argv[] )
|
|
|
|
{
|
|
|
|
unsigned char sig[4]; /* AVR signature bytes */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
avr_signature(fd, sig);
|
|
|
|
fprintf(stdout, "\nDevice signature = 0x");
|
|
|
|
for (i=0; i<4; i++)
|
|
|
|
fprintf(stdout, "%02x", sig[i]);
|
|
|
|
fprintf(stdout, "\n\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-14 21:11:18 +00:00
|
|
|
int cmd_quit ( int fd, struct avrpart * p, int argc, char * argv[] )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-18 03:25:03 +00:00
|
|
|
int cmd_help ( int fd, struct avrpart * p, int argc, char * argv[] )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(stdout, "Valid commands:\n\n" );
|
|
|
|
for (i=0; i<NCMDS; i++) {
|
|
|
|
fprintf(stdout, " %-6s : ", cmd[i].name );
|
|
|
|
fprintf(stdout, cmd[i].desc, cmd[i].name);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
}
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-14 21:11:18 +00:00
|
|
|
int tokenize ( char * s, char *** argv )
|
|
|
|
{
|
|
|
|
int i, n, l, nargs, offset;
|
|
|
|
int len, slen;
|
|
|
|
char * buf;
|
|
|
|
int bufsize;
|
|
|
|
char ** bufv;
|
|
|
|
char * q, * r;
|
|
|
|
char * nbuf;
|
|
|
|
char ** av;
|
|
|
|
|
|
|
|
slen = strlen(s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize allow for 20 arguments, use realloc to grow this if
|
|
|
|
* necessary
|
|
|
|
*/
|
|
|
|
nargs = 20;
|
|
|
|
bufsize = slen + 20;
|
|
|
|
buf = malloc(bufsize);
|
|
|
|
bufv = (char **) malloc(nargs*sizeof(char *));
|
|
|
|
for (i=0; i<nargs; i++) {
|
|
|
|
bufv[i] = NULL;
|
|
|
|
}
|
|
|
|
buf[0] = 0;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
l = 0;
|
|
|
|
nbuf = buf;
|
|
|
|
r = s;
|
|
|
|
while (*r) {
|
|
|
|
nexttok(r, &q, &r);
|
|
|
|
strcpy(nbuf, q);
|
|
|
|
bufv[n] = nbuf;
|
|
|
|
len = strlen(q);
|
|
|
|
l += len + 1;
|
|
|
|
nbuf += len + 1;
|
|
|
|
nbuf[0] = 0;
|
|
|
|
n++;
|
|
|
|
if ((n % 20) == 0) {
|
|
|
|
/* realloc space for another 20 args */
|
|
|
|
bufsize += 20;
|
|
|
|
nargs += 20;
|
|
|
|
buf = realloc(buf, bufsize);
|
|
|
|
bufv = realloc(bufv, nargs*sizeof(char *));
|
|
|
|
nbuf = &buf[l];
|
|
|
|
for (i=n; i<nargs; i++)
|
|
|
|
bufv[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have parsed all the args, n == argc, bufv contains an array of
|
|
|
|
* pointers to each arg, and buf points to one memory block that
|
|
|
|
* contains all the args, back to back, seperated by a nul
|
|
|
|
* terminator. Consilidate bufv and buf into one big memory block
|
|
|
|
* so that the code that calls us, will have an easy job of freeing
|
|
|
|
* this memory.
|
|
|
|
*/
|
|
|
|
av = (char **) malloc(slen + n + (n+1)*sizeof(char *));
|
|
|
|
q = (char *)&av[n+1];
|
|
|
|
memcpy(q, buf, l);
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
offset = bufv[i] - buf;
|
|
|
|
av[i] = q + offset;
|
|
|
|
}
|
|
|
|
av[i] = NULL;
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
free(bufv);
|
|
|
|
|
|
|
|
*argv = av;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int do_cmd ( int fd, struct avrpart * p, int argc, char * argv[] )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<NCMDS; i++) {
|
|
|
|
if (strcasecmp(argv[0], cmd[i].name) == 0) {
|
|
|
|
return cmd[i].func(fd, p, argc, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: invalid command \"%s\"\n",
|
|
|
|
progname, argv[0]);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int go_interactive ( int fd, struct avrpart * p )
|
|
|
|
{
|
2001-01-18 03:25:03 +00:00
|
|
|
char * cmdbuf;
|
2001-01-14 21:11:18 +00:00
|
|
|
int i, len;
|
|
|
|
char * q;
|
|
|
|
int rc;
|
|
|
|
int argc;
|
|
|
|
char ** argv;
|
|
|
|
|
2001-01-15 02:28:46 +00:00
|
|
|
rc = 0;
|
2001-01-18 03:25:03 +00:00
|
|
|
while ((cmdbuf = readline("avrprog> ")) != NULL) {
|
2001-01-14 21:11:18 +00:00
|
|
|
len = strlen(cmdbuf);
|
2001-01-19 00:49:13 +00:00
|
|
|
if (len > 1)
|
|
|
|
add_history(cmdbuf);
|
2001-01-14 21:11:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* find the start of the command, skipping any white space
|
|
|
|
*/
|
|
|
|
q = cmdbuf;
|
|
|
|
while (*q && isspace(*q))
|
|
|
|
q++;
|
|
|
|
|
|
|
|
/* skip blank lines and comments */
|
|
|
|
if (!*q || (*q == '#'))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* tokenize command line */
|
|
|
|
argc = tokenize(q, &argv);
|
|
|
|
|
|
|
|
fprintf(stdout, ">>> ");
|
|
|
|
for (i=0; i<argc; i++)
|
|
|
|
fprintf(stdout, "%s ", argv[i]);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
/* run the command */
|
|
|
|
rc = do_cmd(fd, p, argc, argv);
|
|
|
|
free(argv);
|
|
|
|
if (rc > 0) {
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
2001-01-18 03:25:03 +00:00
|
|
|
free(cmdbuf);
|
2001-01-14 21:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-15 04:18:39 +00:00
|
|
|
int avr_initmem ( struct avrpart * p )
|
|
|
|
{
|
|
|
|
p->flash = (unsigned char *) malloc(p->flash_size);
|
|
|
|
if (p->flash == NULL) {
|
|
|
|
fprintf(stderr, "%s: can't alloc buffer for flash size of %d bytes\n",
|
|
|
|
progname, p->flash_size);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
p->eeprom = (unsigned char *) malloc(p->eeprom_size);
|
|
|
|
if (p->eeprom == NULL) {
|
|
|
|
fprintf(stderr, "%s: can't alloc buffer for eeprom size of %d bytes\n",
|
|
|
|
progname, p->eeprom_size);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int verify_data(struct avrpart * p, struct avrpart * v, AVRMEM memtype)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char * buf1, * buf2;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
switch (memtype) {
|
|
|
|
case AVR_FLASH:
|
|
|
|
buf1 = p->flash;
|
|
|
|
buf2 = v->flash;
|
|
|
|
size = p->flash_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVR_EEPROM:
|
|
|
|
buf1 = p->eeprom;
|
|
|
|
buf2 = v->eeprom;
|
|
|
|
size = p->eeprom_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid memory type = %d for data verification\n",
|
|
|
|
progname, memtype);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<size; i++) {
|
|
|
|
if (buf1[i] != buf2[i]) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: verification error, first mismatch at byte %d\n"
|
|
|
|
"%s0x%02x != 0x%02x\n",
|
|
|
|
progname, i,
|
|
|
|
progbuf, buf1[i], buf2[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-18 03:25:03 +00:00
|
|
|
void display_part ( FILE * f, struct avrpart * p, char * prefix )
|
|
|
|
{
|
|
|
|
fprintf(f,
|
|
|
|
"%sAVR Part = %s\n"
|
|
|
|
"%sFlash memory size = %d bytes\n"
|
|
|
|
"%sEEPROM memory size = %d bytes\n"
|
|
|
|
"%sMin/Max program delay = %d/%d us\n"
|
|
|
|
"%sChip Erase delay = %d us\n"
|
|
|
|
"%sFlash Polled Readback = 0x%02x\n"
|
|
|
|
"%sEEPROM Polled Readback = 0x%02x, 0x%02x\n",
|
|
|
|
prefix, p->partdesc,
|
|
|
|
prefix, p->flash_size,
|
|
|
|
prefix, p->eeprom_size,
|
|
|
|
prefix, p->min_write_delay, p->max_write_delay,
|
|
|
|
prefix, p->chip_erase_delay,
|
|
|
|
prefix, p->f_readback,
|
|
|
|
prefix, p->e_readback[0], p->e_readback[1]);
|
|
|
|
}
|
|
|
|
|
2001-01-19 00:49:13 +00:00
|
|
|
/*
|
|
|
|
* parse the -E string
|
|
|
|
*/
|
|
|
|
int getexitspecs ( char *s, int *set, int *clr )
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
while ((cp = strtok(s, ","))) {
|
|
|
|
if (strcmp(cp, "reset") == 0) { *clr |= AVR_RESET; }
|
|
|
|
else if (strcmp(cp, "noreset") == 0) { *set |= AVR_RESET; }
|
|
|
|
else if (strcmp(cp, "vcc") == 0) { *set |= AVR_POWER; }
|
|
|
|
else if (strcmp(cp, "novcc") == 0) { *clr |= AVR_POWER; }
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
s = 0; /* strtok() should be called with the actual string only once */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* main routine
|
|
|
|
*/
|
2000-08-05 05:12:50 +00:00
|
|
|
int main ( int argc, char * argv [] )
|
|
|
|
{
|
2001-01-13 20:00:17 +00:00
|
|
|
int fd; /* file descriptor for parallel port */
|
|
|
|
int rc; /* general return code checking */
|
|
|
|
int exitrc; /* exit code for main() */
|
|
|
|
int i; /* general loop counter */
|
|
|
|
int ch; /* options flag */
|
|
|
|
int size; /* size of memory region */
|
|
|
|
int len; /* length for various strings */
|
|
|
|
char * p1; /* used to parse CVS Id */
|
|
|
|
char * p2; /* used to parse CVS Ed */
|
|
|
|
unsigned char sig[4]; /* AVR signature bytes */
|
|
|
|
unsigned char nulldev[4]; /* 0xff signature bytes for comparison */
|
2001-01-15 04:18:39 +00:00
|
|
|
struct avrpart * p, ap1; /* which avr part we are programming */
|
|
|
|
struct avrpart * v, ap2; /* used for verify */
|
2001-01-13 20:00:17 +00:00
|
|
|
int readorwrite; /* true if a chip read/write op was selected */
|
2001-01-19 00:49:13 +00:00
|
|
|
int ppidata; /* cached value of the ppi data register */
|
2001-01-13 20:00:17 +00:00
|
|
|
|
|
|
|
/* options / operating mode variables */
|
2001-01-13 21:48:10 +00:00
|
|
|
int memtype; /* AVR_FLASH or AVR_EEPROM */
|
|
|
|
int doread; /* 0=reading, 1=writing */
|
|
|
|
int erase; /* 1=erase chip, 0=don't */
|
|
|
|
char * outputf; /* output file name */
|
|
|
|
char * inputf; /* input file name */
|
|
|
|
int ovsigck; /* 1=override sig check, 0=don't */
|
|
|
|
char * parallel; /* parallel port device */
|
2001-01-18 02:35:41 +00:00
|
|
|
int terminal; /* 1=enter terminal mode, 0=don't */
|
2001-01-13 21:48:10 +00:00
|
|
|
FILEFMT filefmt; /* FMT_AUTO, FMT_IHEX, FMT_SREC, FMT_RBIN */
|
2001-01-14 21:32:36 +00:00
|
|
|
int nowrite; /* don't actually write anything to the chip */
|
2001-01-15 04:18:39 +00:00
|
|
|
int verify; /* perform a verify operation */
|
2001-01-19 00:49:13 +00:00
|
|
|
int ppisetbits; /* bits to set in ppi data register at exit */
|
|
|
|
int ppiclrbits; /* bits to clear in ppi data register at exit */
|
2001-01-13 20:00:17 +00:00
|
|
|
|
|
|
|
readorwrite = 0;
|
|
|
|
parallel = DEFAULT_PARALLEL;
|
|
|
|
outputf = NULL;
|
|
|
|
inputf = NULL;
|
|
|
|
doread = 1;
|
|
|
|
memtype = AVR_FLASH;
|
|
|
|
erase = 0;
|
|
|
|
p = NULL;
|
|
|
|
ovsigck = 0;
|
2001-01-18 02:35:41 +00:00
|
|
|
terminal = 0;
|
2001-01-13 20:00:17 +00:00
|
|
|
filefmt = FMT_AUTO;
|
2001-01-14 21:32:36 +00:00
|
|
|
nowrite = 0;
|
2001-01-15 04:18:39 +00:00
|
|
|
verify = 1; /* on by default; XXX can't turn it off */
|
2001-01-19 00:49:13 +00:00
|
|
|
ppisetbits = ppiclrbits = 0;
|
2000-08-05 05:12:50 +00:00
|
|
|
|
|
|
|
progname = rindex(argv[0],'/');
|
|
|
|
if (progname)
|
|
|
|
progname++;
|
|
|
|
else
|
|
|
|
progname = argv[0];
|
|
|
|
|
2001-01-13 20:46:25 +00:00
|
|
|
len = strlen(progname) + 2;
|
|
|
|
for (i=0; i<len; i++)
|
2001-01-14 21:11:18 +00:00
|
|
|
progbuf[i] = ' ';
|
|
|
|
progbuf[i] = 0;
|
2001-01-13 20:46:25 +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");
|
2001-01-13 20:46:25 +00:00
|
|
|
fprintf(stderr, "%s: Copyright 2000 Brian Dean, bsd@bsdhome.com\n"
|
2001-01-14 21:11:18 +00:00
|
|
|
"%sRevision ", progname, progbuf);
|
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
|
|
|
|
*/
|
2001-01-19 00:49:13 +00:00
|
|
|
while ((ch = getopt(argc,argv,"?eE:f:Fi:m:no:p:P:tv")) != -1) {
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
switch (ch) {
|
2001-01-13 20:00:17 +00:00
|
|
|
case 'm': /* select memory type to operate on */
|
|
|
|
if ((strcasecmp(optarg,"e")==0)||(strcasecmp(optarg,"eeprom")==0)) {
|
|
|
|
memtype = AVR_EEPROM;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2001-01-13 20:00:17 +00:00
|
|
|
else if ((strcasecmp(optarg,"f")==0)||
|
|
|
|
(strcasecmp(optarg,"flash")==0)) {
|
|
|
|
memtype = AVR_FLASH;
|
|
|
|
}
|
|
|
|
else {
|
2001-01-13 21:58:46 +00:00
|
|
|
fprintf(stderr, "%s: invalid memory type \"%s\"\n\n",
|
2001-01-13 20:00:17 +00:00
|
|
|
progname, optarg);
|
|
|
|
usage();
|
|
|
|
exit(1);
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2001-01-13 20:00:17 +00:00
|
|
|
readorwrite = 1;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2000-12-31 02:24:50 +00:00
|
|
|
case 'F': /* override invalid signature check */
|
|
|
|
ovsigck = 1;
|
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2001-01-14 21:32:36 +00:00
|
|
|
case 'n':
|
|
|
|
nowrite = 1;
|
|
|
|
break;
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
case 'o': /* specify output file */
|
2001-01-18 02:35:41 +00:00
|
|
|
if (inputf || terminal) {
|
|
|
|
fprintf(stderr,"%s: -i, -o, and -t are incompatible\n\n", progname);
|
2000-08-05 05:12:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
doread = 1;
|
|
|
|
outputf = optarg;
|
2001-01-13 20:00:17 +00:00
|
|
|
if (filefmt == FMT_AUTO)
|
2001-01-18 02:35:41 +00:00
|
|
|
filefmt = FMT_RBIN;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2000-12-20 02:26:31 +00:00
|
|
|
case 'p' : /* specify AVR part */
|
|
|
|
p = NULL;
|
2001-01-13 20:00:17 +00:00
|
|
|
for (i=0; i<N_AVRPARTS; i++) {
|
2000-12-20 02:26:31 +00:00
|
|
|
if (strcmp(parts[i].optiontag, optarg)==0) {
|
|
|
|
p = &parts[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p == NULL) {
|
2001-01-13 20:00:17 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: AVR Part \"%s\" not found. Valid parts are:\n\n",
|
2000-12-20 02:26:31 +00:00
|
|
|
progname, optarg );
|
2001-01-13 20:00:17 +00:00
|
|
|
list_valid_parts(stderr," ");
|
2000-12-20 02:26:31 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
|
|
|
case 'e': /* perform a chip erase */
|
2000-12-31 02:24:50 +00:00
|
|
|
erase = 1;
|
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2001-01-19 00:49:13 +00:00
|
|
|
case 'E':
|
|
|
|
if (getexitspecs(optarg, &ppisetbits, &ppiclrbits) < 0) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
case 'i': /* specify input file */
|
2001-01-18 02:35:41 +00:00
|
|
|
if (outputf || terminal) {
|
|
|
|
fprintf(stderr,"%s: -o, -i, and -t are incompatible\n\n", progname);
|
2000-08-05 05:12:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
doread = 0;
|
|
|
|
inputf = optarg;
|
2001-01-13 20:00:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f': /* specify file format */
|
|
|
|
if (strlen(optarg) != 1) {
|
|
|
|
fprintf(stderr, "%s: invalid file format \"%s\"\n",
|
|
|
|
progname, optarg);
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
switch (optarg[0]) {
|
|
|
|
case 'a' : filefmt = FMT_AUTO; break;
|
|
|
|
case 'i' : filefmt = FMT_IHEX; break;
|
|
|
|
case 'r' : filefmt = FMT_RBIN; break;
|
|
|
|
case 's' :
|
2001-01-13 21:58:46 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Motorola S-Record format not yet supported\n\n",
|
2001-01-13 20:00:17 +00:00
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
default :
|
2001-01-13 21:58:46 +00:00
|
|
|
fprintf(stderr, "%s: invalid file format \"%s\"\n\n",
|
2001-01-13 20:00:17 +00:00
|
|
|
progname, optarg);
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-01-18 02:35:41 +00:00
|
|
|
case 't': /* enter terminal mode */
|
2001-01-13 20:00:17 +00:00
|
|
|
if (!((inputf == NULL)||(outputf == NULL))) {
|
|
|
|
fprintf(stderr,
|
2001-01-18 02:35:41 +00:00
|
|
|
"%s: terminal mode is not compatible with -i or -o\n\n",
|
2001-01-13 20:00:17 +00:00
|
|
|
progname);
|
|
|
|
usage();
|
|
|
|
exit(1);
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2001-01-18 02:35:41 +00:00
|
|
|
terminal = 1;
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2001-01-13 20:46:25 +00:00
|
|
|
case 'P':
|
|
|
|
parallel = optarg;
|
|
|
|
break;
|
|
|
|
|
2001-01-15 04:18:39 +00:00
|
|
|
case 'v':
|
|
|
|
verify = 1;
|
|
|
|
break;
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
case '?': /* help */
|
2000-08-05 05:12:50 +00:00
|
|
|
usage();
|
2001-01-13 20:00:17 +00:00
|
|
|
exit(0);
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
default:
|
2001-01-13 21:58:46 +00:00
|
|
|
fprintf(stderr, "%s: invalid option -%c\n\n", progname, ch);
|
2000-08-05 05:12:50 +00:00
|
|
|
usage();
|
2001-01-13 20:00:17 +00:00
|
|
|
exit(1);
|
2000-08-05 05:12:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
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 );
|
2001-01-13 20:00:17 +00:00
|
|
|
list_valid_parts(stderr, " ");
|
2000-12-20 02:26:31 +00:00
|
|
|
fprintf(stderr,"\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-01-15 04:18:39 +00:00
|
|
|
/*
|
|
|
|
* set up seperate instances of the avr part, one for use in
|
|
|
|
* programming, one for use in verifying. These are separate
|
|
|
|
* because they need separate flash and eeprom buffer space
|
|
|
|
*/
|
|
|
|
ap1 = *p;
|
|
|
|
v = p;
|
|
|
|
p = &ap1;
|
|
|
|
ap2 = *v;
|
|
|
|
v = &ap2;
|
|
|
|
|
|
|
|
avr_initmem(p);
|
|
|
|
avr_initmem(v);
|
|
|
|
|
2001-01-18 03:25:03 +00:00
|
|
|
display_part(stderr, p, progbuf);
|
|
|
|
|
2000-12-20 02:26:31 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* open the parallel port
|
|
|
|
*/
|
2001-01-13 20:46:25 +00:00
|
|
|
fd = open ( parallel, O_RDWR );
|
2000-08-05 05:12:50 +00:00
|
|
|
if (fd < 0) {
|
2001-01-13 20:46:25 +00:00
|
|
|
fprintf ( stderr, "%s: can't open device \"%s\": %s\n\n",
|
|
|
|
progname, parallel, strerror(errno) );
|
2000-08-05 05:12:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-08-07 01:50:37 +00:00
|
|
|
exitrc = 0;
|
|
|
|
|
2001-01-19 00:49:13 +00:00
|
|
|
ppidata = ppi_getall(fd, PPIDATA);
|
|
|
|
if (ppidata < 0) {
|
|
|
|
fprintf ( stderr, "%s: error reading status of ppi data port\n", progname);
|
|
|
|
exitrc = 1;
|
|
|
|
ppidata = 0; /* clear all bits at exit */
|
|
|
|
goto main_exit;
|
|
|
|
}
|
|
|
|
ppidata &= ~ppiclrbits;
|
|
|
|
ppidata |= ppisetbits;
|
2000-08-07 01:50:37 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
fprintf ( stderr,
|
|
|
|
"%s: AVR device initialized and ready to accept instructions\n",
|
2000-08-05 05:12:50 +00:00
|
|
|
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) {
|
|
|
|
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",
|
2001-01-14 21:11:18 +00:00
|
|
|
progbuf, progbuf );
|
2000-12-31 02:24:50 +00:00
|
|
|
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 );
|
2001-01-13 20:46:25 +00:00
|
|
|
avr_chip_erase(fd,p);
|
2000-08-05 05:12:50 +00:00
|
|
|
fprintf(stderr, "%s: done.\n", progname );
|
|
|
|
}
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
|
2001-01-18 02:35:41 +00:00
|
|
|
if (!terminal && ((inputf==NULL) && (outputf==NULL))) {
|
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.
|
|
|
|
*/
|
2001-01-13 20:00:17 +00:00
|
|
|
if (readorwrite) {
|
2000-08-06 03:53:06 +00:00
|
|
|
fprintf(stderr, "%s: you must specify an input or an output file\n",
|
|
|
|
progname);
|
2000-12-31 18:42:42 +00:00
|
|
|
exitrc = 1;
|
2000-08-06 03:53:06 +00:00
|
|
|
}
|
2000-08-07 01:48:53 +00:00
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
2001-01-18 02:35:41 +00:00
|
|
|
if (terminal) {
|
2001-01-14 21:11:18 +00:00
|
|
|
/*
|
2001-01-18 02:35:41 +00:00
|
|
|
* terminal mode
|
2001-01-14 21:11:18 +00:00
|
|
|
*/
|
|
|
|
exitrc = go_interactive(fd, p);
|
|
|
|
}
|
|
|
|
else if (doread) {
|
2000-08-05 05:12:50 +00:00
|
|
|
/*
|
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
|
|
|
*/
|
2001-01-15 04:18:39 +00:00
|
|
|
fprintf(stderr, "%s: reading %s memory:\n",
|
|
|
|
progname, memtypestr(memtype));
|
2001-01-13 20:46:25 +00:00
|
|
|
rc = avr_read ( fd, p, memtype );
|
2001-01-13 20:00:17 +00:00
|
|
|
if (rc) {
|
2001-01-15 04:18:39 +00:00
|
|
|
fprintf(stderr, "%s: failed to read all of %s memory, rc=%d\n",
|
|
|
|
progname, memtypestr(memtype), rc);
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2001-01-15 04:18:39 +00:00
|
|
|
fprintf(stderr, "%s: writing output file \"%s\"\n",
|
|
|
|
progname, outputf);
|
2001-01-13 20:00:17 +00:00
|
|
|
rc = fileio(FIO_WRITE, outputf, filefmt, p, memtype);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: terminating\n", progname);
|
2000-08-07 01:48:53 +00:00
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2001-01-13 20:00:17 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
2001-01-13 20:00:17 +00:00
|
|
|
* write the selected device memory using data from a file; first
|
|
|
|
* read the data from the specified file
|
2000-08-05 05:12:50 +00:00
|
|
|
*/
|
2001-01-15 04:18:39 +00:00
|
|
|
fprintf(stderr, "%s: reading input file \"%s\"\n",
|
|
|
|
progname, inputf);
|
2001-01-13 20:00:17 +00:00
|
|
|
rc = fileio(FIO_READ, inputf, filefmt, p, memtype );
|
2000-08-05 05:12:50 +00:00
|
|
|
if (rc < 0) {
|
2001-01-13 20:00:17 +00:00
|
|
|
fprintf(stderr, "%s: terminating\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 03:53:06 +00:00
|
|
|
size = rc;
|
|
|
|
|
2000-08-06 19:47:03 +00:00
|
|
|
/*
|
|
|
|
* write the buffer contents to the selected memory type
|
|
|
|
*/
|
2001-01-13 20:00:17 +00:00
|
|
|
fprintf(stderr, "%s: writing %s:\n",
|
|
|
|
progname, memtypestr(memtype));
|
2001-01-14 21:32:36 +00:00
|
|
|
|
|
|
|
if (!nowrite) {
|
|
|
|
rc = avr_write ( fd, p, memtype );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* test mode, don't actually write to the chip, output the buffer
|
|
|
|
* to stdout in intel hex instead
|
|
|
|
*/
|
|
|
|
rc = fileio(FIO_WRITE, "-", FMT_IHEX, p, memtype);
|
|
|
|
}
|
|
|
|
|
2001-01-13 20:00:17 +00:00
|
|
|
if (rc) {
|
|
|
|
fprintf ( stderr, "%s: failed to write flash memory, rc=%d\n",
|
|
|
|
progname, rc );
|
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
2001-01-15 04:18:39 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
2001-01-15 04:18:39 +00:00
|
|
|
if (!doread && verify) {
|
|
|
|
/*
|
|
|
|
* verify that the in memory file (p->flash or p->eeprom) is the
|
|
|
|
* same as what is on the chip
|
|
|
|
*/
|
|
|
|
fprintf(stderr, "%s: verifying %s memory against %s:\n",
|
|
|
|
progname, memtypestr(memtype), inputf);
|
|
|
|
fprintf(stderr, "%s: reading on-chip %s data:\n",
|
|
|
|
progname, memtypestr(memtype));
|
|
|
|
rc = avr_read ( fd, v, memtype );
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "%s: failed to read all of %s memory, rc=%d\n",
|
|
|
|
progname, memtypestr(memtype), rc);
|
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: verifying\n", progname);
|
|
|
|
rc = verify_data(p, v, memtype);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "%s: verification error; content mismatch\n",
|
|
|
|
progname);
|
|
|
|
exitrc = 1;
|
|
|
|
goto main_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: data verified\n", progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2001-01-19 00:49:13 +00:00
|
|
|
ppi_setall(fd, PPIDATA, ppidata);
|
2000-08-27 16:45:17 +00:00
|
|
|
|
2000-08-05 05:12:50 +00:00
|
|
|
close(fd);
|
|
|
|
|
2001-01-14 21:11:18 +00:00
|
|
|
fprintf(stderr, "\n%s done. Thank you.\n\n", progname);
|
2000-08-07 01:48:53 +00:00
|
|
|
|
2000-08-07 01:50:37 +00:00
|
|
|
return exitrc;
|
2000-08-05 05:12:50 +00:00
|
|
|
}
|
|
|
|
|