⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 AVR的USB文件
💻 C
📖 第 1 页 / 共 2 页
字号:
        do_cycles = 1;        break;      case '?': /* help */        usage();        exit(0);        break;      default:        fprintf(stderr, "%s: invalid option -%c\n\n", progname, ch);        usage();        exit(1);        break;    }  }  if (verbose) {    /*     * Print out an identifying string so folks can tell what version     * they are running     */    fprintf(stderr,             "\n%s: Version %s\n"            "%sCopyright (c) 2000-2003 Brian Dean, bsd@bsdhome.com\n\n",             progname, version, progbuf);  }  if (verbose) {    fprintf(stderr, "%sSystem wide configuration file is \"%s\"\n",            progbuf, sys_config);  }  rc = read_config(sys_config);  if (rc) {    fprintf(stderr,             "%s: error reading system wide configuration file \"%s\"\n",            progname, sys_config);    exit(1);  }  if (usr_config[0] != 0) {    if (verbose) {      fprintf(stderr, "%sUser configuration file is \"%s\"\n",              progbuf, usr_config);    }    rc = stat(usr_config, &sb);    if ((rc < 0) || ((sb.st_mode & S_IFREG) == 0)) {      if (verbose) {        fprintf(stderr,                "%sUser configuration file does not exist or is not a "                "regular file, skipping\n",                progbuf);      }    }    else {      rc = read_config(usr_config);      if (rc) {        fprintf(stderr, "%s: error reading user configuration file \"%s\"\n",                progname, usr_config);        exit(1);      }    }  }  if (verbose) {    fprintf(stderr, "\n");  }  if (partdesc) {    if (strcmp(partdesc, "?") == 0) {      fprintf(stderr, "\n");      fprintf(stderr,"Valid parts are:\n");      list_parts(stderr, "  ", part_list);      fprintf(stderr, "\n");      exit(1);    }  }  if (programmer) {    if (strcmp(programmer, "?") == 0) {      fprintf(stderr, "\n");      fprintf(stderr,"Valid programmers are:\n");      list_programmers(stderr, "  ", programmers);      fprintf(stderr,"\n");      exit(1);    }  }  if (programmer[0] == 0) {    fprintf(stderr,             "\n%s: no programmer has been specified on the command line "            "or the config file\n",             progname);    fprintf(stderr,             "%sSpecify a programmer using the -c option and try again\n\n",            progbuf);    exit(1);  }  pgm = locate_programmer(programmers, programmer);  if (pgm == NULL) {    fprintf(stderr,"\n");    fprintf(stderr,             "%s: Can't find programmer id \"%s\"\n",            progname, programmer);    fprintf(stderr,"\nValid programmers are:\n");    list_programmers(stderr, "  ", programmers);    fprintf(stderr,"\n");    exit(1);  }  if (strcmp(pgm->type, "STK500") == 0) {    if (port == default_parallel) {      port = default_serial;    }  }  if (partdesc == NULL) {    fprintf(stderr,             "%s: No AVR part has been specified, use \"-p Part\"\n\n",            progname);    fprintf(stderr,"Valid parts are:\n");    list_parts(stderr, "  ", part_list);    fprintf(stderr, "\n");    exit(1);  }  p = locate_part(part_list, partdesc);  if (p == NULL) {    fprintf(stderr,             "%s: AVR Part \"%s\" not found.\n\n",            progname, partdesc);    fprintf(stderr,"Valid parts are:\n");    list_parts(stderr, "  ", part_list);    fprintf(stderr, "\n");    exit(1);  }  if (exitspecs != NULL) {    if (strcmp(pgm->type, "PPI") != 0) {      fprintf(stderr,               "%s: WARNING: -E option is only valid with \"PPI\" "              "programmer types\n",              progname);      exitspecs = NULL;    }    else if (getexitspecs(exitspecs, &ppisetbits, &ppiclrbits) < 0) {      usage();      exit(1);    }  }  /*    * 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    */  p = avr_dup_part(p);  v = avr_dup_part(p);  if (strcmp(pgm->type, "PPI") == 0) {    verify_pin_assigned(PIN_AVR_RESET, "AVR RESET");    verify_pin_assigned(PIN_AVR_SCK,   "AVR SCK");    verify_pin_assigned(PIN_AVR_MISO,  "AVR MISO");    verify_pin_assigned(PIN_AVR_MOSI,  "AVR MOSI");  }  /*   * open the programmer   */  if (port[0] == 0) {    fprintf(stderr, "\n%s: no port has been specified on the command line "            "or the config file\n",             progname);    fprintf(stderr, "%sSpecify a port using the -P option and try again\n\n",            progbuf);    exit(1);  }  if (verbose) {    fprintf(stderr, "%sUsing Port            : %s\n", progbuf, port);    fprintf(stderr, "%sUsing Programmer      : %s\n", progbuf, programmer);  }  pgm->open(pgm, port);  if (verbose) {    avr_display(stderr, p, progbuf, verbose);    fprintf(stderr, "\n");    programmer_display(progbuf);  }  fprintf(stderr, "\n");  exitrc = 0;  /*   * allow the programmer to save its state   */  rc = pgm->save(pgm);  if (rc < 0) {    exitrc = 1;    ppidata = 0; /* clear all bits at exit */    goto main_exit;  }  if (strcmp(pgm->type, "PPI") == 0) {    pgm->ppidata &= ~ppiclrbits;    pgm->ppidata |= ppisetbits;  }  /*   * enable the programmer   */  pgm->enable(pgm);  /*   * turn off all the status leds   */  pgm->rdy_led(pgm, OFF);  pgm->err_led(pgm, OFF);  pgm->pgm_led(pgm, OFF);  pgm->vfy_led(pgm, OFF);  /*   * initialize the chip in preperation for accepting commands   */  rc = pgm->initialize(pgm, p);  if (rc < 0) {    fprintf(stderr, "%s: initialization failed, rc=%d\n", progname, rc);    exitrc = 1;    goto main_exit;  }  /* indicate ready */  pgm->rdy_led(pgm, ON);  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.     */  rc = avr_signature(pgm, p);  if (rc != 0) {    fprintf(stderr, "%s: error reading signature data, rc=%d\n",            progname, rc);    exit(1);  }  sig = avr_locate_mem(p, "signature");  if (sig == NULL) {    fprintf(stderr,            "%s: WARNING: signature data not defined for device \"%s\"\n",            progname, p->desc);  }  if (sig != NULL) {    int ff;    fprintf(stderr, "%s: Device signature = 0x", progname);    ff = 1;    for (i=0; i<sig->size; i++) {      fprintf(stderr, "%02x", sig->buf[i]);      if (sig->buf[i] != 0xff)        ff = 0;    }    fprintf(stderr, "\n");    if (ff) {      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",                progbuf, progbuf);        exitrc = 1;        goto main_exit;      }    }  }  if (set_cycles != -1) {    rc = avr_get_cycle_count(pgm, p, &cycles);    if (rc == 0) {      /*       * only attempt to update the cycle counter if we can actually       * read the old value       */      cycles = set_cycles;      fprintf(stderr, "%s: setting erase-rewrite cycle count to %d\n",               progname, cycles);      rc = avr_put_cycle_count(pgm, p, cycles);      if (rc < 0) {        fprintf(stderr,                 "%s: WARNING: failed to update the erase-rewrite cycle "                "counter\n",                progname);      }    }  }  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);    pgm->chip_erase(pgm, p);    fprintf(stderr, "%s: done.\n", progname);  }  else if (set_cycles == -1) {    /*     * The erase routine displays this same information, so don't     * repeat it if an erase was done.  Also, don't display this if we     * set the cycle count (due to -Y).     *     * see if the cycle count in the last four bytes of eeprom seems     * reasonable      */    rc = avr_get_cycle_count(pgm, p, &cycles);    if ((rc >= 0) && (cycles != 0xffffffff)) {      fprintf(stderr,              "%s: current erase-rewrite cycle count is %d%s\n",              progname, cycles,               do_cycles ? "" : " (if being tracked)");    }  }  if (!terminal && ((inputf==NULL) && (outputf==NULL))) {    /*     * 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 an output file, but one was not selected.     * Otherwise, we just shut down.       */    if (readorwrite) {      fprintf(stderr, "%s: you must specify an input or an output file\n",              progname);      exitrc = 1;    }    goto main_exit;  }  if (terminal) {    /*     * terminal mode     */    exitrc = terminal_mode(pgm, p);  }  else if (doread) {    /*     * read out the specified device memory and write it to a file      */    fprintf(stderr, "%s: reading %s memory:\n",             progname, memtype);    rc = avr_read(pgm, p, memtype, 0, 1);    if (rc < 0) {      fprintf(stderr, "%s: failed to read all of %s memory, rc=%d\n",               progname, memtype, rc);      exitrc = 1;      goto main_exit;    }    size = rc;    fprintf(stderr, "%s: writing output file \"%s\"\n",            progname, outputf);    rc = fileio(FIO_WRITE, outputf, filefmt, p, memtype, size);    if (rc < 0) {      fprintf(stderr, "%s: terminating\n", progname);      exitrc = 1;      goto main_exit;    }  }  else {    /*     * write the selected device memory using data from a file; first     * read the data from the specified file     */    fprintf(stderr, "%s: reading input file \"%s\"\n",            progname, inputf);    rc = fileio(FIO_READ, inputf, filefmt, p, memtype, -1);    if (rc < 0) {      fprintf(stderr, "%s: terminating\n", progname);      exitrc = 1;      goto main_exit;    }    size = rc;    /*     * write the buffer contents to the selected memory type     */    fprintf(stderr, "%s: writing %s (%d bytes):\n",             progname, memtype, size);    if (!nowrite) {      rc = avr_write(pgm, p, memtype, size, 1);    }    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, size);    }    if (rc < 0) {      fprintf(stderr, "%s: failed to write %s memory, rc=%d\n",                 progname, memtype, rc);      exitrc = 1;      goto main_exit;    }    vsize = rc;    fprintf(stderr, "%s: %d bytes of %s written\n", progname,             vsize, memtype);  }  if (!doread && verify) {    /*      * verify that the in memory file (p->mem[AVR_M_FLASH|AVR_M_EEPROM])     * is the same as what is on the chip      */    pgm->vfy_led(pgm, ON);    fprintf(stderr, "%s: verifying %s memory against %s:\n",             progname, memtype, inputf);    fprintf(stderr, "%s: reading on-chip %s data:\n",             progname, memtype);    rc = avr_read(pgm, v, memtype, vsize, 1);    if (rc < 0) {      fprintf(stderr, "%s: failed to read all of %s memory, rc=%d\n",               progname, memtype, rc);      pgm->err_led(pgm, ON);      exitrc = 1;      goto main_exit;    }    fprintf(stderr, "%s: verifying ...\n", progname);    rc = avr_verify(p, v, memtype, vsize);    if (rc < 0) {      fprintf(stderr, "%s: verification error; content mismatch\n",               progname);      pgm->err_led(pgm, ON);      exitrc = 1;      goto main_exit;    }        fprintf(stderr, "%s: %d bytes of %s verified\n",             progname, rc, memtype);    pgm->vfy_led(pgm, OFF);  } main_exit:  /*   * program complete   */  pgm->powerdown(pgm);  /*   * restore programmer state   */  pgm->restore(pgm);  pgm->disable(pgm);  pgm->rdy_led(pgm, OFF);  pgm->close(pgm);  fprintf(stderr, "\n%s done.  Thank you.\n\n", progname);  return exitrc;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -