main.c

来自「这是一个非常有价值的参考代码」· C语言 代码 · 共 1,580 行 · 第 1/3 页

C
1,580
字号
            "\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) ||      (strcmp(pgm->type, "avr910") == 0) ||      (strcmp(pgm->type, "STK500V2") == 0) ||      (strcmp(pgm->type, "JTAGMKII") == 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);  }  if (baudrate != 0) {    if (verbose) {      fprintf(stderr, "%sOverriding Baud Rate  : %d\n", progbuf, baudrate);    }    pgm->baudrate = baudrate;  }  if (bitclock != 0.0) {    if (verbose) {      fprintf(stderr, "%sSetting bit clk period: %.1f\n", progbuf, bitclock);    }    pgm->bitclock = bitclock * 1e-6;  }  rc = pgm->open(pgm, port);  if (rc < 0) {    exitrc = 1;    pgm->ppidata = 0; /* clear all bits at exit */    goto main_exit;  }  if (verbose) {    avr_display(stderr, p, progbuf, verbose);    fprintf(stderr, "\n");    programmer_display(progbuf);  }  if (quell_progress < 2) {    fprintf(stderr, "\n");  }  exitrc = 0;  /*   * handle exit specs. FIXME: this should be moved to "par.c"   */  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);    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;    }  }  /* indicate ready */  pgm->rdy_led(pgm, ON);  if (quell_progress < 2) {    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);    exitrc = 1;    goto main_exit;  }  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;    if (quell_progress < 2) {      fprintf(stderr, "%s: Device signature = 0x", progname);    }    ff = 1;    for (i=0; i<sig->size; i++) {      if (quell_progress < 2) {        fprintf(stderr, "%02x", sig->buf[i]);      }      if (sig->buf[i] != 0xff)        ff = 0;    }    if (quell_progress < 2) {      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 (safemode == 1) {    /* If safemode is enabled, go ahead and read the current low, high,       and extended fuse bytes as needed */    if (safemode_readfuses(&safemode_lfuse, &safemode_hfuse,                           &safemode_efuse, &safemode_fuse, pgm, p, verbose) != 0) {      fprintf(stderr, "%s: safemode: To protect your AVR the programming "              "will be aborted\n",              progname);      exitrc = 1;      goto main_exit;    }    //Save the fuses as default    safemode_memfuses(1, &safemode_lfuse, &safemode_hfuse, &safemode_efuse, &safemode_fuse);  }  if ((erase == 0) && (auto_erase == 1)) {    AVRMEM * m;    for (ln=lfirst(updates); ln; ln=lnext(ln)) {      upd = ldata(ln);      m = avr_locate_mem(p, upd->memtype);      if (m == NULL)        continue;      if ((strcasecmp(m->desc, "flash") == 0) && (upd->op == DEVICE_WRITE)) {        erase = 1;        if (quell_progress < 2) {          fprintf(stderr,                "%s: NOTE: FLASH memory has been specified, an erase cycle "                "will be performed\n"                "%sTo disable this feature, specify the -D option.\n",                progname, progbuf);        }        break;      }    }  }  /*   * Display cycle count, if and only if it is not set later on.   *   * The cycle count will be displayed anytime it will be changed later.   */  if ((set_cycles == -1) && ((erase == 0) || (do_cycles == 0))) {    /*     * see if the cycle count in the last four bytes of eeprom seems     * reasonable     */    rc = avr_get_cycle_count(pgm, p, &cycles);    if (quell_progress < 2) {      if ((rc >= 0) && (cycles != 0)) {        fprintf(stderr,              "%s: current erase-rewrite cycle count is %d%s\n",              progname, cycles,              do_cycles ? "" : " (if being tracked)");      }    }  }  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;      if (quell_progress < 2) {        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     */    if (quell_progress < 2) {      fprintf(stderr, "%s: erasing chip\n", progname);    }    avr_chip_erase(pgm, p);  }  if (terminal) {    /*     * terminal mode     */             exitrc = terminal_mode(pgm, p);  }  for (ln=lfirst(updates); ln; ln=lnext(ln)) {    upd = ldata(ln);    rc = do_op(pgm, p, upd, nowrite, verify);    if (rc) {      exitrc = 1;      break;    }  }  /* Right before we exit programming mode, which will make the fuse     bits active, check to make sure they are still correct */  if (safemode == 1){    /* If safemode is enabled, go ahead and read the current low,     * high, and extended fuse bytes as needed */    unsigned char safemodeafter_lfuse = 0xff;    unsigned char safemodeafter_hfuse = 0xff;    unsigned char safemodeafter_efuse = 0xff;    unsigned char safemodeafter_fuse  = 0xff;    unsigned char failures = 0;    char yes[1] = {'y'};    if (quell_progress < 2) {      fprintf(stderr, "\n");    }    //Restore the default fuse values    safemode_memfuses(0, &safemode_lfuse, &safemode_hfuse, &safemode_efuse, &safemode_fuse);    /* Try reading back fuses, make sure they are reliable to read back */    if (safemode_readfuses(&safemodeafter_lfuse, &safemodeafter_hfuse,                           &safemodeafter_efuse, &safemode_fuse, pgm, p, verbose) != 0) {      /* Uh-oh.. try once more to read back fuses */      if (safemode_readfuses(&safemodeafter_lfuse, &safemodeafter_hfuse,                             &safemodeafter_efuse, &safemodeafter_fuse, pgm, p, verbose) != 0) {         fprintf(stderr,                "%s: safemode: Sorry, reading back fuses was unreliable. "                "I have given up and exited programming mode\n",                progname);        exitrc = 1;        goto main_exit;		        }    }        /* Now check what fuses are against what they should be */    if (safemodeafter_fuse != safemode_fuse) {      fuses_updated = 1;      fprintf(stderr, "%s: safemode: fuse changed! Was %x, and is now %x\n",              progname, safemode_fuse, safemodeafter_fuse);                    /* Ask user - should we change them */              if (silentsafe == 0)            safemode_response = terminal_get_input("Would you like this fuse to be changed back? [y/n] ");       else            safemode_response = yes;              if (tolower(safemode_response[0]) == 'y') {                          /* Enough chit-chat, time to program some fuses and check them */            if (safemode_writefuse (safemode_fuse, "fuse", pgm, p,                                    10, verbose) == 0) {                fprintf(stderr, "%s: safemode: and is now rescued\n", progname);            }            else {                fprintf(stderr, "%s: and COULD NOT be changed\n", progname);                failures++;            }      }    }    /* Now check what fuses are against what they should be */    if (safemodeafter_lfuse != safemode_lfuse) {      fuses_updated = 1;      fprintf(stderr, "%s: safemode: lfuse changed! Was %x, and is now %x\n",              progname, safemode_lfuse, safemodeafter_lfuse);                    /* Ask user - should we change them */              if (silentsafe == 0)            safemode_response = terminal_get_input("Would you like this fuse to be changed back? [y/n] ");       else            safemode_response = yes;              if (tolower(safemode_response[0]) == 'y') {                          /* Enough chit-chat, time to program some fuses and check them */            if (safemode_writefuse (safemode_lfuse, "lfuse", pgm, p,                                    10, verbose) == 0) {                fprintf(stderr, "%s: safemode: and is now rescued\n", progname);            }            else {                fprintf(stderr, "%s: and COULD NOT be changed\n", progname);                failures++;            }      }    }    /* Now check what fuses are against what they should be */    if (safemodeafter_hfuse != safemode_hfuse) {      fuses_updated = 1;      fprintf(stderr, "%s: safemode: hfuse changed! Was %x, and is now %x\n",              progname, safemode_hfuse, safemodeafter_hfuse);                    /* Ask user - should we change them */       if (silentsafe == 0)            safemode_response = terminal_get_input("Would you like this fuse to be changed back? [y/n] ");       else            safemode_response = yes;       if (tolower(safemode_response[0]) == 'y') {            /* Enough chit-chat, time to program some fuses and check them */            if (safemode_writefuse(safemode_hfuse, "hfuse", pgm, p,                                    10, verbose) == 0) {                fprintf(stderr, "%s: safemode: and is now rescued\n", progname);            }            else {                fprintf(stderr, "%s: and COULD NOT be changed\n", progname);                failures++;            }      }    }    /* Now check what fuses are against what they should be */    if (safemodeafter_efuse != safemode_efuse) {      fuses_updated = 1;      fprintf(stderr, "%s: safemode: efuse changed! Was %x, and is now %x\n",              progname, safemode_efuse, safemodeafter_efuse);      /* Ask user - should we change them */       if (silentsafe == 0)            safemode_response = terminal_get_input("Would you like this fuse to be changed back? [y/n] ");       else            safemode_response = yes;       if (tolower(safemode_response[0]) == 'y') {                          /* Enough chit-chat, time to program some fuses and check them */            if (safemode_writefuse (safemode_efuse, "efuse", pgm, p,                                    10, verbose) == 0) {                fprintf(stderr, "%s: safemode: and is now rescued\n", progname);            }            else {                fprintf(stderr, "%s: and COULD NOT be changed\n", progname);                failures++;            }       }    }    if (quell_progress < 2) {      fprintf(stderr, "%s: safemode: ", progname);      if (failures == 0) {        fprintf(stderr, "Fuses OK\n");      }      else {        fprintf(stderr, "Fuses not recovered, sorry\n");      }    }    if (fuses_updated && fuses_specified) {      exitrc = 1;    }  }main_exit:  /*   * program complete   */  pgm->powerdown(pgm);  pgm->disable(pgm);  pgm->rdy_led(pgm, OFF);  pgm->close(pgm);  if (quell_progress < 2) {    fprintf(stderr, "\n%s done.  Thank you.\n\n", progname);  }  return exitrc;}

⌨️ 快捷键说明

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