FreeCalypso > hg > freecalypso-hwlab
comparison ee2232/ee2232-prog.c @ 4:02ea5dbdf84b
ee2232-prog: input parsing implemented
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Thu, 05 Apr 2018 22:30:38 +0000 |
| parents | |
| children | a85b6b7398bc |
comparison
equal
deleted
inserted
replaced
| 3:ef43bbfa8d16 | 4:02ea5dbdf84b |
|---|---|
| 1 #include <sys/types.h> | |
| 2 #include <ctype.h> | |
| 3 #include <string.h> | |
| 4 #include <strings.h> | |
| 5 #include <stdio.h> | |
| 6 #include <stdlib.h> | |
| 7 #include <unistd.h> | |
| 8 #include <ftdi.h> | |
| 9 | |
| 10 char *device_selector = "i:0x0403:0x6010"; | |
| 11 unsigned eeprom_size = 64; | |
| 12 u_short eeprom[256]; | |
| 13 int erase; | |
| 14 | |
| 15 process_cmdline(argc, argv) | |
| 16 char **argv; | |
| 17 { | |
| 18 int c; | |
| 19 extern char *optarg; | |
| 20 | |
| 21 while ((c = getopt(argc, argv, "d:et:")) != EOF) { | |
| 22 switch (c) { | |
| 23 case 'd': | |
| 24 device_selector = optarg; | |
| 25 continue; | |
| 26 case 'e': | |
| 27 erase = 1; | |
| 28 continue; | |
| 29 case 't': | |
| 30 if (!strcmp(optarg, "46")) | |
| 31 eeprom_size = 64; | |
| 32 else if (!strcmp(optarg, "56")) | |
| 33 eeprom_size = 128; | |
| 34 else if (!strcmp(optarg, "66")) | |
| 35 eeprom_size = 256; | |
| 36 else { | |
| 37 fprintf(stderr, | |
| 38 "error: -t option invalid value \"%s\"\n", | |
| 39 optarg); | |
| 40 exit(1); | |
| 41 } | |
| 42 continue; | |
| 43 default: | |
| 44 /* error msg already printed */ | |
| 45 exit(1); | |
| 46 } | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 read_eeprom_from_stdin() | |
| 51 { | |
| 52 unsigned n, off; | |
| 53 int rc; | |
| 54 | |
| 55 for (n = 0; n < eeprom_size; n += 8) { | |
| 56 rc = scanf("%x: %hx %hx %hx %hx %hx %hx %hx %hx", &off, | |
| 57 eeprom + n, eeprom + n + 1, eeprom + n + 2, | |
| 58 eeprom + n + 3, eeprom + n + 4, eeprom + n + 5, | |
| 59 eeprom + n + 6, eeprom + n + 7); | |
| 60 if (rc != 9 || off != n * 2) { | |
| 61 fprintf(stderr, | |
| 62 "ee2232-prog error: invalid input on stdin\n"); | |
| 63 exit(1); | |
| 64 } | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 test_output() | |
| 69 { | |
| 70 unsigned n, col; | |
| 71 | |
| 72 for (n = 0; n < eeprom_size; n++) { | |
| 73 col = n & 7; | |
| 74 if (col == 0) | |
| 75 printf("%02X:", n * 2); | |
| 76 printf(" %04X", eeprom[n]); | |
| 77 if (col == 7) | |
| 78 putchar('\n'); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 main(argc, argv) | |
| 83 char **argv; | |
| 84 { | |
| 85 struct ftdi_context ftdi; | |
| 86 | |
| 87 process_cmdline(argc, argv); | |
| 88 if (erase) | |
| 89 memset(eeprom, 0xFF, eeprom_size * 2); | |
| 90 else | |
| 91 read_eeprom_from_stdin(); | |
| 92 test_output(); | |
| 93 exit(0); | |
| 94 | |
| 95 } |
