main.c
来自「这是一个非常有价值的参考代码」· C语言 代码 · 共 1,580 行 · 第 1/3 页
C
1,580 行
int rc; mem = avr_locate_mem(p, upd->memtype); if (mem == NULL) { fprintf(stderr, "\"%s\" memory type not defined for part \"%s\"\n", upd->memtype, p->desc); return -1; } if (upd->op == DEVICE_READ) { /* * read out the specified device memory and write it to a file */ if (quell_progress < 2) { fprintf(stderr, "%s: reading %s memory:\n", progname, mem->desc); } report_progress(0,1,"Reading"); rc = avr_read(pgm, p, upd->memtype, 0, 1); if (rc < 0) { fprintf(stderr, "%s: failed to read all of %s memory, rc=%d\n", progname, mem->desc, rc); return -1; } report_progress(1,1,NULL); size = rc; if (quell_progress < 2) { fprintf(stderr, "%s: writing output file \"%s\"\n", progname, strcmp(upd->filename, "-")==0 ? "<stdout>" : upd->filename); } rc = fileio(FIO_WRITE, upd->filename, upd->format, p, upd->memtype, size); if (rc < 0) { fprintf(stderr, "%s: write to file '%s' failed\n", progname, upd->filename); return -1; } } else if (upd->op == DEVICE_WRITE) { /* * write the selected device memory using data from a file; first * read the data from the specified file */ if (quell_progress < 2) { fprintf(stderr, "%s: reading input file \"%s\"\n", progname, strcmp(upd->filename, "-")==0 ? "<stdin>" : upd->filename); } rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memtype, -1); if (rc < 0) { fprintf(stderr, "%s: write to file '%s' failed\n", progname, upd->filename); return -1; } size = rc; /* * write the buffer contents to the selected memory type */ if (quell_progress < 2) { fprintf(stderr, "%s: writing %s (%d bytes):\n", progname, upd->memtype, size); } if (!nowrite) { report_progress(0,1,"Writing"); rc = avr_write(pgm, p, upd->memtype, size, 1); report_progress(1,1,NULL); } 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, upd->memtype, size); } if (rc < 0) { fprintf(stderr, "%s: failed to write %s memory, rc=%d\n", progname, mem->desc, rc); return -1; } vsize = rc; if (quell_progress < 2) { fprintf(stderr, "%s: %d bytes of %s written\n", progname, vsize, upd->memtype); } } else if (upd->op == DEVICE_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); v = avr_dup_part(p); if (quell_progress < 2) { fprintf(stderr, "%s: verifying %s memory against %s:\n", progname, mem->desc, upd->filename); fprintf(stderr, "%s: load data %s data from input file %s:\n", progname, mem->desc, upd->filename); } rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memtype, -1); if (rc < 0) { fprintf(stderr, "%s: read from file '%s' failed\n", progname, upd->filename); return -1; } size = rc; if (quell_progress < 2) { fprintf(stderr, "%s: input file %s contains %d bytes\n", progname, upd->filename, size); fprintf(stderr, "%s: reading on-chip %s data:\n", progname, upd->memtype); } report_progress (0,1,"Reading"); rc = avr_read(pgm, v, upd->memtype, size, 1); if (rc < 0) { fprintf(stderr, "%s: failed to read all of %s memory, rc=%d\n", progname, mem->desc, rc); pgm->err_led(pgm, ON); return -1; } report_progress (1,1,NULL); if (quell_progress < 2) { fprintf(stderr, "%s: verifying ...\n", progname); } rc = avr_verify(p, v, upd->memtype, size); if (rc < 0) { fprintf(stderr, "%s: verification error; content mismatch\n", progname); pgm->err_led(pgm, ON); return -1; } if (quell_progress < 2) { fprintf(stderr, "%s: %d bytes of %s verified\n", progname, rc, mem->desc); } pgm->vfy_led(pgm, OFF); } else { fprintf(stderr, "%s: invalid update operation (%d) requested\n", progname, upd->op); return -1; } return 0;}/* * main routine */int main(int argc, char * argv []){ int rc; /* general return code checking */ int exitrc; /* exit code for main() */ int i; /* general loop counter */ int ch; /* options flag */ int len; /* length for various strings */ struct avrpart * p; /* which avr part we are programming */ struct avrpart * v; /* used for verify */ AVRMEM * sig; /* signature data */ struct stat sb; UPDATE * upd; LNODEID * ln; /* options / operating mode variables */ int erase; /* 1=erase chip, 0=don't */ int auto_erase; /* 0=never erase unless explicity told to do so, 1=erase if we are going to program flash */ int ovsigck; /* 1=override sig check, 0=don't */ char * port; /* device port (/dev/xxx) */ int terminal; /* 1=enter terminal mode, 0=don't */ int nowrite; /* don't actually write anything to the chip */ int verify; /* perform a verify operation */ int ppisetbits; /* bits to set in ppi data register at exit */ int ppiclrbits; /* bits to clear in ppi data register at exit */ char * exitspecs; /* exit specs string from command line */ char * programmer; /* programmer id */ char * partdesc; /* part id */ char sys_config[PATH_MAX]; /* system wide config file */ char usr_config[PATH_MAX]; /* per-user config file */ int cycles; /* erase-rewrite cycles */ int set_cycles; /* value to set the erase-rewrite cycles to */ char * e; /* for strtol() error checking */ int baudrate; /* override default programmer baud rate */ double bitclock; /* Specify programmer bit clock (JTAG ICE) */ int safemode; /* Enable safemode, 1=safemode on, 0=normal */ int silentsafe; /* Don't ask about fuses, 1=silent, 0=normal */ unsigned char safemode_lfuse = 0xff; unsigned char safemode_hfuse = 0xff; unsigned char safemode_efuse = 0xff; unsigned char safemode_fuse = 0xff; char * safemode_response; int fuses_specified = 0; int fuses_updated = 0;#if !defined(WIN32NATIVE) char * homedir;#endif progname = rindex(argv[0],'/');#if defined (WIN32NATIVE) /* take care of backslash as dir sep in W32 */ if (!progname) progname = rindex(argv[0],'\\');#endif /* WIN32NATIVE */ if (progname) progname++; else progname = argv[0]; default_parallel[0] = 0; default_serial[0] = 0; init_config(); updates = lcreat(NULL, 0); if (updates == NULL) { fprintf(stderr, "%s: cannot initialize updater list\n", progname); exit(1); } partdesc = NULL; port = default_parallel; erase = 0; auto_erase = 1; p = NULL; ovsigck = 0; terminal = 0; nowrite = 0; verify = 1; /* on by default */ quell_progress = 0; ppisetbits = 0; ppiclrbits = 0; exitspecs = NULL; pgm = NULL; programmer = default_programmer; verbose = 0; do_cycles = 0; set_cycles = -1; baudrate = 0; bitclock = 0.0; safemode = 1; /* Safemode on by default */ silentsafe = 0; /* Ask by default */ if (isatty(STDIN_FILENO) == 0) safemode = 0; /* Turn off safemode if this isn't a terminal */#if defined(WIN32NATIVE) win_sys_config_set(sys_config); win_usr_config_set(usr_config);#else strcpy(sys_config, CONFIG_DIR); i = strlen(sys_config); if (i && (sys_config[i-1] != '/')) strcat(sys_config, "/"); strcat(sys_config, "avrdude.conf"); usr_config[0] = 0; homedir = getenv("HOME"); if (homedir != NULL) { strcpy(usr_config, homedir); i = strlen(usr_config); if (i && (usr_config[i-1] != '/')) strcat(usr_config, "/"); strcat(usr_config, ".avrduderc"); }#endif len = strlen(progname) + 2; for (i=0; i<len; i++) progbuf[i] = ' '; progbuf[i] = 0; /* * check for no arguments */ if (argc == 1) { usage(); return 0; } /* * process command line arguments */ while ((ch = getopt(argc,argv,"?b:B:c:C:DeE:Fnp:P:qstU:uvVyY:")) != -1) { switch (ch) { case 'b': /* override default programmer baud rate */ baudrate = strtol(optarg, &e, 0); if ((e == optarg) || (*e != 0)) { fprintf(stderr, "%s: invalid baud rate specified '%s'\n", progname, optarg); exit(1); } break; case 'B': /* specify JTAG ICE bit clock period */ bitclock = strtod(optarg, &e); if ((e == optarg) || (*e != 0) || bitclock == 0.0) { fprintf(stderr, "%s: invalid bit clock period specified '%s'\n", progname, optarg); exit(1); } break; case 'c': /* programmer id */ programmer = optarg; break; case 'C': /* system wide configuration file */ strncpy(sys_config, optarg, PATH_MAX); sys_config[PATH_MAX-1] = 0; break; case 'D': /* disable auto erase */ auto_erase = 0; break; case 'e': /* perform a chip erase */ erase = 1; break; case 'E': exitspecs = optarg; break; case 'F': /* override invalid signature check */ ovsigck = 1; break; case 'n': nowrite = 1; break; case 'p' : /* specify AVR part */ partdesc = optarg; break; case 'P': port = optarg; break; case 'q' : /* Quell progress output */ quell_progress++ ; break; case 's' : /* Silent safemode */ silentsafe = 1; safemode = 1; break; case 't': /* enter terminal mode */ terminal = 1; break; case 'u' : /* Disable safemode */ safemode = 0; break; case 'U': upd = parse_op(optarg); if (upd == NULL) { fprintf(stderr, "%s: error parsing update operation '%s'\n", progname, optarg); exit(1); } ladd(updates, upd); if (verify && upd->op == DEVICE_WRITE) { upd = dup_update(upd); upd->op = DEVICE_VERIFY; ladd(updates, upd); } break; case 'v': verbose++; break; case 'V': verify = 0; break; case 'y': do_cycles = 1; break; case 'Y': set_cycles = strtol(optarg, &e, 0); if ((e == optarg) || (*e != 0)) { fprintf(stderr, "%s: invalid cycle count '%s'\n", progname, optarg); exit(1); } 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 (quell_progress == 0) { if (isatty (STDERR_FILENO)) update_progress = update_progress_tty; else { update_progress = update_progress_no_tty; /* disable all buffering of stderr for compatibility with software that captures and redirects output to a GUI i.e. Programmers Notepad */ setvbuf( stderr, NULL, _IONBF, 0 ); setvbuf( stdout, NULL, _IONBF, 0 ); } } 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-2005 Brian Dean, http://www.bdmicro.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,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?