clean up --freq parser

This commit is contained in:
Martin Pittermann 2020-05-25 10:31:37 +02:00
parent 92888956f3
commit e93034db69
1 changed files with 46 additions and 34 deletions

View File

@ -43,7 +43,7 @@ struct arguments {
string bit_file;
string device;
string cable;
string speed;
uint32_t freq;
string board;
bool list_cables;
bool list_boards;
@ -57,6 +57,7 @@ struct arguments {
#define LIST_BOARD 2
#define LIST_FPGA 3
#define DETECT 4
#define FREQUENCY 5
const char *argp_program_version = "openFPGALoader 1.0";
const char *argp_program_bug_address = "<gwenhael.goavec-merou@trabucayre.com>";
@ -65,7 +66,7 @@ static char args_doc[] = "BIT_FILE";
static error_t parse_opt(int key, char *arg, struct argp_state *state);
static struct argp_option options[] = {
{"cable", 'c', "CABLE", 0, "jtag interface"},
{"speed", 's', "SPEED", 0, "jtag frequency (Hz)"},
{"freq", FREQUENCY, "FREQ", 0, "jtag frequency (Hz)"},
{"list-cables", LIST_CABLE, 0, 0, "list all supported cables"},
{"board", 'b', "BOARD", 0, "board name, may be used instead of cable"},
{"list-boards", LIST_BOARD, 0, 0, "list all supported boards"},
@ -93,7 +94,7 @@ int main(int argc, char **argv)
jtag_pins_conf_t *pins_config = NULL;
/* command line args. */
struct arguments args = {false, false, false, 0, "", "-", "-", "6M", "-",
struct arguments args = {false, false, false, 0, "", "-", "-", 6000000, "-",
false, false, false, false, true, false};
/* parse arguments */
argp_parse(&argp, argc, argv, 0, 0, &args);
@ -129,39 +130,12 @@ int main(int argc, char **argv)
}
cable = select_cable->second;
uint32_t speed;
try {
size_t end;
float speed_base = stof(args.speed, &end);
if (end == args.speed.size()) {
speed = (uint32_t)speed_base;
} else if (end == (args.speed.size() - 1)) {
switch (args.speed.back()) {
case 'k': case 'K':
speed = (uint32_t)(1e3 * speed_base);
break;
case 'm': case 'M':
speed = (uint32_t)(1e6 * speed_base);
break;
default:
cerr << "error : speed: invaild postfix \"" << args.speed.back() << "\"" << endl;
return EXIT_FAILURE;
}
} else {
cerr << "error : speed: invaild postfix \"" << args.speed.substr(end) << "\"" << endl;
return EXIT_FAILURE;
}
} catch (...) {
cerr << "error : speed: invaild format" << endl;
return EXIT_FAILURE;
}
/* jtag base */
Jtag *jtag;
if (args.device == "-")
jtag = new Jtag(cable, pins_config, speed, false);
jtag = new Jtag(cable, pins_config, args.freq, false);
else
jtag = new Jtag(cable, pins_config, args.device, speed, false);
jtag = new Jtag(cable, pins_config, args.device, args.freq, false);
/* chain detection */
vector<int> listDev;
@ -231,6 +205,35 @@ int main(int argc, char **argv)
delete(jtag);
}
// parse double from string in enginerring notation
// can deal with postfixes k and m, add more when required
static error_t parse_eng(string arg, double *dst) {
try {
size_t end;
double base = stod(arg, &end);
if (end == arg.size()) {
*dst = base;
return 0;
} else if (end == (arg.size() - 1)) {
switch (arg.back()) {
case 'k': case 'K':
*dst = (uint32_t)(1e3 * base);
return 0;
case 'm': case 'M':
*dst = (uint32_t)(1e6 * base);
return 0;
default:
return EINVAL;
}
} else {
return EINVAL;
}
} catch (...) {
cerr << "error : speed: invaild format" << endl;
return EINVAL;
}
}
/* arguments parser */
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
@ -264,8 +267,17 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
case 'b':
arguments->board = arg;
break;
case 's':
arguments->speed = arg;
case FREQUENCY:
double freq;
if (parse_eng(string(arg), &freq)) {
cerr << "Error: invalid format for --freq" << endl;
exit(1);
}
if (freq < 1) {
cerr << "Error: --freq must be positive" << endl;
exit(1);
}
arguments->freq = freq;
break;
case ARGP_KEY_ARG:
arguments->bit_file = arg;