nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: nuclear@0: enum { nuclear@0: OP_NONE, nuclear@0: OP_WRITE, nuclear@0: OP_READ, nuclear@0: OP_RESET nuclear@0: }; nuclear@0: nuclear@0: int op_write(FILE *dev, FILE *fp); nuclear@0: int op_read(FILE *dev, FILE *fp); nuclear@0: int op_reset(FILE *dev); nuclear@0: int parse_args(int argc, char **argv); nuclear@0: nuclear@0: int op, verify = 0; nuclear@0: const char *devname = "/dev/ttyUSB0"; nuclear@0: char *fname = 0; nuclear@0: uint32_t start_addr; nuclear@0: int read_size = 128 * 1024; nuclear@0: nuclear@0: int main(int argc, char **argv) nuclear@0: { nuclear@0: FILE *fp, *devfp; nuclear@0: int dev; nuclear@0: struct termios term; nuclear@0: nuclear@0: if(parse_args(argc, argv) == -1) { nuclear@0: return 1; nuclear@0: } nuclear@0: nuclear@0: if((dev = open(devname, O_RDWR)) == -1) { nuclear@0: fprintf(stderr, "failed to open device %s: %s\n", devname, strerror(errno)); nuclear@0: return 1; nuclear@0: } nuclear@0: if(tcgetattr(dev, &term) == -1) { nuclear@0: fprintf(stderr, "failed to get terminal attributes for %s: %s\n", devname, strerror(errno)); nuclear@0: return 1; nuclear@0: } nuclear@0: term.c_cflag = CS8 | CLOCAL; nuclear@0: term.c_iflag &= ~(IXON | IXOFF); nuclear@0: term.c_lflag = 0; nuclear@0: nuclear@0: cfsetispeed(&term, B38400); nuclear@0: cfsetospeed(&term, B38400); nuclear@0: nuclear@0: if(tcsetattr(dev, TCSANOW, &term) == -1) { nuclear@0: fprintf(stderr, "failed to set terminal attributes for %s: %s\n", devname, strerror(errno)); nuclear@0: return 1; nuclear@0: } nuclear@0: nuclear@0: if(!(devfp = fdopen(dev, "r+"))) { nuclear@0: perror("failed to attach I/O stream to the device file\n"); nuclear@0: return 1; nuclear@0: } nuclear@0: setvbuf(devfp, 0, _IONBF, 0); nuclear@0: nuclear@0: if(fname && !(fp = fopen(fname, op == OP_READ ? "w" : "r"))) { nuclear@0: fprintf(stderr, "failed to open %s: %s\n", fname, strerror(errno)); nuclear@0: return 1; nuclear@0: } nuclear@0: nuclear@0: switch(op) { nuclear@0: case OP_WRITE: nuclear@0: if(op_write(devfp, fp) == -1) { nuclear@0: fprintf(stderr, "write failed\n"); nuclear@0: return 1; nuclear@0: } nuclear@0: break; nuclear@0: nuclear@0: case OP_READ: nuclear@0: if(op_read(devfp, fp) == -1) { nuclear@0: fprintf(stderr, "read failed\n"); nuclear@0: return 1; nuclear@0: } nuclear@0: break; nuclear@0: nuclear@0: case OP_RESET: nuclear@0: if(op_reset(devfp) == -1) { nuclear@0: fprintf(stderr, "reset failed\n"); nuclear@0: return 1; nuclear@0: } nuclear@0: break; nuclear@0: nuclear@0: default: nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: fclose(devfp); nuclear@0: close(dev); nuclear@0: if(fp) { nuclear@0: fclose(fp); nuclear@0: } nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: #define RET_IF_FAIL(x) \ nuclear@0: if(!fgets(buf, sizeof buf, dev) || buf[0] != 'O') return x nuclear@0: nuclear@0: int op_write(FILE *dev, FILE *fp) nuclear@0: { nuclear@0: char buf[128]; nuclear@0: int c; nuclear@0: nuclear@0: fprintf(dev, "p\n"); nuclear@0: RET_IF_FAIL(-1); nuclear@0: fprintf(dev, "a%d\n", start_addr); nuclear@0: RET_IF_FAIL(-1); nuclear@0: nuclear@0: while((c = fgetc(fp)) != -1) { nuclear@0: fprintf(dev, "w%d\n", c); nuclear@0: RET_IF_FAIL(-1); nuclear@0: } nuclear@0: nuclear@0: fprintf(dev, "b\n"); nuclear@0: RET_IF_FAIL(-1); nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: int op_read(FILE *dev, FILE *fp) nuclear@0: { nuclear@0: char buf[128]; nuclear@0: int i, val; nuclear@0: nuclear@0: fprintf(dev, "p\n"); nuclear@0: RET_IF_FAIL(-1); nuclear@0: fprintf(dev, "a%d\n", start_addr); nuclear@0: RET_IF_FAIL(-1); nuclear@0: nuclear@0: for(i=0; i\n"); nuclear@0: printf(" -r|-read \n"); nuclear@0: printf(" -a|-addr
: start address for read/write operations\n"); nuclear@0: printf(" -R|-reset: reboot machine\n"); nuclear@0: printf(" -v|-verify: do verification after writing\n"); nuclear@0: printf(" -s|-size: read size in bytes\n"); nuclear@0: printf(" -d|-device : serial device (eg /dev/ttyUSB0)\n"); nuclear@0: printf(" -h|-help: print usage and exit\n"); nuclear@0: return 0; nuclear@0: nuclear@0: } else { nuclear@0: fprintf(stderr, "invalid argument: %s\n", argv[i]); nuclear@0: return -1; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if(!op) { nuclear@0: fprintf(stderr, "you must specify an operation\n"); nuclear@0: return -1; nuclear@0: } nuclear@0: return 0; nuclear@0: }