/* flasher for HM-CFG-USB
*
- * Copyright (c) 2013-14 Michael Gernoth <michael@gernoth.net>
+ * Copyright (c) 2013-20 Michael Gernoth <michael@gernoth.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
return 1;
}
+void flash_hmcfgusb_syntax(char *prog)
+{
+ fprintf(stderr, "Syntax: %s [options] filename.enc\n\n", prog);
+ fprintf(stderr, "Possible options:\n");
+ fprintf(stderr, "\t-S serial\tuse HM-CFG-USB with given serial\n");
+ fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
+
+}
+
int main(int argc, char **argv)
{
const char twiddlie[] = { '-', '\\', '|', '/' };
struct recv_data rdata;
uint16_t len;
struct firmware *fw;
+ char *serial = NULL;
+ char *filename = NULL;
int block;
int pfd;
+ int opt;
int debug = 0;
- printf("HM-CFG-USB flasher version " VERSION "\n\n");
+ while((opt = getopt(argc, argv, "S:V")) != -1) {
+ switch (opt) {
+ case 'S':
+ serial = optarg;
+ break;
+ case 'V':
+ printf("flash-hmcfgusb " VERSION "\n");
+ printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
+ exit(EXIT_SUCCESS);
+ case 'h':
+ case ':':
+ case '?':
+ default:
+ flash_hmcfgusb_syntax(argv[0]);
+ exit(EXIT_FAILURE);
+ break;
+ }
+ }
- if (argc != 2) {
- if (argc == 1)
- fprintf(stderr, "Missing firmware filename!\n\n");
+ if (optind == argc - 1) {
+ filename = argv[optind];
+ }
- fprintf(stderr, "Syntax: %s hmusbif.enc\n\n", argv[0]);
+ printf("HM-CFG-USB flasher version " VERSION "\n\n");
+
+ if (!filename) {
+ fprintf(stderr, "Missing firmware filename!\n\n");
+ flash_hmcfgusb_syntax(argv[0]);
exit(EXIT_FAILURE);
}
- fw = firmware_read_firmware(argv[1], debug);
+ fw = firmware_read_firmware(filename, ATMEGA_UNKNOWN, debug);
if (!fw)
exit(EXIT_FAILURE);
memset(&rdata, 0, sizeof(rdata));
- dev = hmcfgusb_init(parse_hmcfgusb, &rdata);
+ dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial);
if (!dev) {
fprintf(stderr, "Can't initialize HM-CFG-USB\n");
exit(EXIT_FAILURE);
if (!dev->bootloader) {
fprintf(stderr, "\nHM-CFG-USB not in bootloader mode, entering bootloader.\n");
- hmcfgusb_enter_bootloader(dev);
fprintf(stderr, "\nWaiting for device to reappear...\n");
do {
- sleep(2);
- } while ((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL);
-
- if (!dev->bootloader) {
- fprintf(stderr, "Can't enter bootloader, giving up!\n");
- exit(EXIT_FAILURE);
- }
+ if (dev) {
+ if (!dev->bootloader)
+ hmcfgusb_enter_bootloader(dev);
+ hmcfgusb_close(dev);
+ }
+ sleep(1);
+ } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial)) == NULL) || (!dev->bootloader));
}
printf("\nHM-CFG-USB opened.\n\n");
printf("Waiting for ack...\n");
do {
errno = 0;
- pfd = hmcfgusb_poll(dev, 1);
+ pfd = hmcfgusb_poll(dev, 1000);
if ((pfd < 0) && errno) {
- perror("\n\nhmcfgusb_poll");
- exit(EXIT_FAILURE);
+ if (errno != ETIMEDOUT) {
+ perror("\n\nhmcfgusb_poll");
+ exit(EXIT_FAILURE);
+ }
}
if (rdata.ack) {
break;
firmware_free(fw);
hmcfgusb_close(dev);
+ hmcfgusb_exit();
return EXIT_SUCCESS;
}