clean up --freq parser
This commit is contained in:
parent
92888956f3
commit
e93034db69
80
src/main.cpp
80
src/main.cpp
|
|
@ -43,7 +43,7 @@ struct arguments {
|
||||||
string bit_file;
|
string bit_file;
|
||||||
string device;
|
string device;
|
||||||
string cable;
|
string cable;
|
||||||
string speed;
|
uint32_t freq;
|
||||||
string board;
|
string board;
|
||||||
bool list_cables;
|
bool list_cables;
|
||||||
bool list_boards;
|
bool list_boards;
|
||||||
|
|
@ -57,6 +57,7 @@ struct arguments {
|
||||||
#define LIST_BOARD 2
|
#define LIST_BOARD 2
|
||||||
#define LIST_FPGA 3
|
#define LIST_FPGA 3
|
||||||
#define DETECT 4
|
#define DETECT 4
|
||||||
|
#define FREQUENCY 5
|
||||||
|
|
||||||
const char *argp_program_version = "openFPGALoader 1.0";
|
const char *argp_program_version = "openFPGALoader 1.0";
|
||||||
const char *argp_program_bug_address = "<gwenhael.goavec-merou@trabucayre.com>";
|
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 error_t parse_opt(int key, char *arg, struct argp_state *state);
|
||||||
static struct argp_option options[] = {
|
static struct argp_option options[] = {
|
||||||
{"cable", 'c', "CABLE", 0, "jtag interface"},
|
{"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"},
|
{"list-cables", LIST_CABLE, 0, 0, "list all supported cables"},
|
||||||
{"board", 'b', "BOARD", 0, "board name, may be used instead of cable"},
|
{"board", 'b', "BOARD", 0, "board name, may be used instead of cable"},
|
||||||
{"list-boards", LIST_BOARD, 0, 0, "list all supported boards"},
|
{"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;
|
jtag_pins_conf_t *pins_config = NULL;
|
||||||
|
|
||||||
/* command line args. */
|
/* 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};
|
false, false, false, false, true, false};
|
||||||
/* parse arguments */
|
/* parse arguments */
|
||||||
argp_parse(&argp, argc, argv, 0, 0, &args);
|
argp_parse(&argp, argc, argv, 0, 0, &args);
|
||||||
|
|
@ -129,39 +130,12 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
cable = select_cable->second;
|
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 base */
|
||||||
Jtag *jtag;
|
Jtag *jtag;
|
||||||
if (args.device == "-")
|
if (args.device == "-")
|
||||||
jtag = new Jtag(cable, pins_config, speed, false);
|
jtag = new Jtag(cable, pins_config, args.freq, false);
|
||||||
else
|
else
|
||||||
jtag = new Jtag(cable, pins_config, args.device, speed, false);
|
jtag = new Jtag(cable, pins_config, args.device, args.freq, false);
|
||||||
|
|
||||||
/* chain detection */
|
/* chain detection */
|
||||||
vector<int> listDev;
|
vector<int> listDev;
|
||||||
|
|
@ -231,6 +205,35 @@ int main(int argc, char **argv)
|
||||||
delete(jtag);
|
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 */
|
/* arguments parser */
|
||||||
static error_t parse_opt(int key, char *arg, struct argp_state *state)
|
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':
|
case 'b':
|
||||||
arguments->board = arg;
|
arguments->board = arg;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case FREQUENCY:
|
||||||
arguments->speed = arg;
|
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;
|
break;
|
||||||
case ARGP_KEY_ARG:
|
case ARGP_KEY_ARG:
|
||||||
arguments->bit_file = arg;
|
arguments->bit_file = arg;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue