rem: ppi_set(fd, PPIDATA, AVR_RESET);
add: ppi_clr(fd, PPIDATA, AVR_RESET);
context: int main ( int argc, char * argv [] ){  int fd;  int rc, exitrc;  int i;  unsigned char * buf;  int ch;  int iofd;  int flash, eeprom, doread, erase, dosig;  int size;  char * outputf;  char * inputf;  char * p1, * p2;  struct avrpart * p;  unsigned char sig[4], nulldev[4];  int len;  int ovsigck;  iofd    = -1;  outputf = NULL;  inputf  = NULL;  doread  = 1;  eeprom  = 0;  flash   = 0;  erase   = 0;  dosig   = 0;  p       = NULL;  ovsigck = 0;  progname = rindex(argv[0],'/');  if (progname)    progname++;  else    progname = argv[0];  /*   * 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;  fprintf(stderr, "\n");  fprintf(stderr, "AVRProg: Copyright 2000 Brian Dean, bsd@bsdhome.com\n");  fprintf(stderr, "         Revision " );  for (i=0; i<p2-p1; i++)    fprintf(stderr, "%c", p1[i]);  fprintf(stderr, "\n\n");  /*   * check for no arguments   */  if (argc == 1) {    usage();    return 0;  }  /*   * process command line arguments   */  while ((ch = getopt(argc,argv,"?efFo:p:rsu:")) != -1) {    switch (ch) {      case 'e': /* select eeprom memory */        if (flash) {          fprintf(stderr,"%s: -e and -f are incompatible\n", progname);          return 1;        }        eeprom = 1;        break;      case 'f': /* select flash memory */        if (eeprom) {          fprintf(stderr,"%s: -e and -f are incompatible\n", progname);          return 1;        }        flash = 1;        break;      case 'F': /* override invalid signature check */        ovsigck = 1;        break;      case 'o': /* specify output file */        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;      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;      case 'r': /* perform a chip erase */        erase = 1;        break;      case 's': /* read out the signature bytes */        dosig = 1;        break;      case 'u': /* specify input (upload) file */        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;      case '?': /* help */        usage();        return 1;        break;      default:        fprintf(stderr, "%s: invalid option -%c\n", progname, ch);        usage();        return 1;        break;    }  }  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;  }  /*   * open the parallel port   */  fd = open ( PARALLEL, O_RDWR );  if (fd < 0) {    fprintf ( stderr, "%s: can't open device \"%s\": %s\n",              progname, PARALLEL, strerror(errno) );    return 1;  }  exitrc = 0;  /*   * initialize the chip in preperation for accepting commands   */  rc = avr_initialize(fd,p);  if (rc < 0) {    fprintf ( stderr, "%s: initialization failed, rc=%d\n", progname, rc );    exitrc = 1;    goto main_exit;  }  fprintf ( stderr, "%s: AVR device initialized and ready to accept instructions\n",            progname );  /*   * 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");  if (erase) {    /*     * erase the chip's flash and eeprom memories, this is required     * before the chip can accept new programming     */    fprintf(stderr, "%s: erasing chip\n", progname );    avr_chip_erase(fd);    avr_initialize(fd,p);    fprintf(stderr, "%s: done.\n", progname );  }  if (dosig) {    /*     * read out the on-chip signature bytes     */    fprintf(stderr, "%s: reading signature bytes: ", progname );    avr_signature(fd, sig);    for (i=0; i<4; i++)      fprintf(stderr, "%02x", sig[i]);    fprintf(stderr, "\n");  }  if (iofd < 0) {    /*     * 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.       */    if (eeprom||flash) {      fprintf(stderr, "%s: you must specify an input or an output file\n",              progname);    }    exitrc = 1;    goto main_exit;  }  if (!(eeprom||flash)) {    /*     * an input file or an output file was specified, but the memory     * type (eeprom or flash) was not specified.       */    fprintf(stderr,             "%s: please specify either the eeprom (-e) or the flash (-f) memory\n",            progname);    exitrc = 1;    goto main_exit;  }  if (doread) {    /*     * read out the specified device memory and write it to a file      */    if (flash) {      size = p->flash_size;      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 );        exitrc = 1;        goto main_exit;      }    }    else if (eeprom) {      size = p->eeprom_size;      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 );        exitrc = 1;        goto main_exit;      }    }    /*     * write it out to the specified file     */    rc = write ( iofd, buf, size );    if (rc < 0) {      fprintf(stderr, "%s: write error: %s\n", progname, strerror(errno));      exitrc = 1;      goto main_exit;    }    else if (rc != size) {      fprintf(stderr, "%s: wrote only %d bytes of the expected %d\n",               progname, rc, size);      exitrc = 1;      goto main_exit;    }  }  else {    /*     * write the selected device memory using data from a file     */    if (flash) {      size = p->flash_size;    }    else if (eeprom) {      size = p->eeprom_size;    }    /*     * read in the data file that will be used to write into the chip     */    rc = read(iofd, buf, size);    if (rc < 0) {      fprintf(stderr, "%s: read error from \"%s\": %s\n",               progname, inputf, strerror(errno));      exitrc = 1;      goto main_exit;    }    size = rc;    /*     * write the buffer contents to the selected memory type     */    if (flash) {      fprintf(stderr, "%s: writing %d bytes into flash memory:\n",               progname, size);      rc = avr_write ( fd, AVR_FLASH, 0, buf, size );      if (rc) {        fprintf ( stderr, "%s: failed to write flash memory, rc=%d\n",                   progname, rc );        exitrc = 1;        goto main_exit;      }    }    else if (eeprom) {      fprintf(stderr, "%s: writing %d bytes into eeprom memory:\n",               progname, size);      rc = avr_write ( fd, AVR_EEPROM, 0, buf, size );      if (rc) {        fprintf ( stderr, "%s: failed to write eeprom memory, rc=%d\n",                   progname, rc );        exitrc = 1;        goto main_exit;      }    }  } main_exit:  /*   * program complete   */  avr_powerdown(fd);  ppi_clr(fd, PPIDATA, 0xff);  ppi_set(fd, PPIDATA, AVR_RESET);  close(fd);  close(iofd);  fprintf(stderr, "\n" );  return exitrc;}