Extracted functions from main.

This commit is contained in:
Daniel Beer 2010-04-29 15:56:47 +12:00
parent ab0580b99e
commit 1f98eb4339
1 changed files with 79 additions and 46 deletions

125
main.c
View File

@ -83,58 +83,50 @@ static void process_rc_file(cproc_t cp)
#define MODE_UIF_BSL 0x04 #define MODE_UIF_BSL 0x04
#define MODE_SIM 0x08 #define MODE_SIM 0x08
int main(int argc, char **argv) struct cmdline_args {
const char *uif_device;
const char *bsl_device;
int mode;
int want_jtag;
int no_rc;
int vcc_mv;
};
static int parse_cmdline_args(int argc, char **argv,
struct cmdline_args *args)
{ {
const struct fet_transport *trans;
const char *uif_device = NULL;
const char *bsl_device = NULL;
const struct device *msp430_dev = NULL;
cproc_t cp;
int opt; int opt;
int no_rc = 0;
int ret = 0;
int flags = 0;
int want_jtag = 0;
int vcc_mv = 3000;
int mode = 0;
puts(
"MSPDebug version 0.6 - debugging tool for MSP430 MCUs\n"
"Copyright (C) 2009, 2010 Daniel Beer <daniel@tortek.co.nz>\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
/* Parse arguments */
while ((opt = getopt(argc, argv, "u:jv:B:sR?n")) >= 0) while ((opt = getopt(argc, argv, "u:jv:B:sR?n")) >= 0)
switch (opt) { switch (opt) {
case 'R': case 'R':
mode |= MODE_RF2500; args->mode |= MODE_RF2500;
break; break;
case 'u': case 'u':
uif_device = optarg; args->uif_device = optarg;
mode |= MODE_UIF; args->mode |= MODE_UIF;
break; break;
case 'v': case 'v':
vcc_mv = atoi(optarg); args->vcc_mv = atoi(optarg);
break; break;
case 'j': case 'j':
want_jtag = 1; args->want_jtag = 1;
break; break;
case 'B': case 'B':
bsl_device = optarg; args->bsl_device = optarg;
mode |= MODE_UIF_BSL; args->mode |= MODE_UIF_BSL;
break; break;
case 's': case 's':
mode |= MODE_SIM; args->mode |= MODE_SIM;
break; break;
case 'n': case 'n':
no_rc = 1; args->no_rc = 1;
break; break;
case '?': case '?':
@ -148,45 +140,85 @@ int main(int argc, char **argv)
} }
/* Check for incompatible arguments */ /* Check for incompatible arguments */
if (mode & (mode - 1)) { if (args->mode & (args->mode - 1)) {
fprintf(stderr, "Multiple incompatible options specified.\n" fprintf(stderr, "Multiple incompatible options specified.\n"
"Try -? for help.\n"); "Try -? for help.\n");
return -1; return -1;
} }
if (!mode) { if (!args->mode) {
fprintf(stderr, "You need to specify an operating mode.\n" fprintf(stderr, "You need to specify an operating mode.\n"
"Try -? for help.\n"); "Try -? for help.\n");
return -1; return -1;
} }
if (stab_init() < 0) return 0;
return -1; }
const struct device *setup_device(const struct cmdline_args *args)
{
const struct device *msp430_dev = NULL;
const struct fet_transport *trans = NULL;
/* Open a device */ /* Open a device */
if (mode == MODE_SIM) { if (args->mode == MODE_SIM) {
msp430_dev = sim_open(); msp430_dev = sim_open();
} else if (mode == MODE_UIF_BSL) { } else if (args->mode == MODE_UIF_BSL) {
msp430_dev = bsl_open(bsl_device); msp430_dev = bsl_open(args->bsl_device);
} else if (mode == MODE_RF2500 || mode == MODE_UIF) { } else if (args->mode == MODE_RF2500 || args->mode == MODE_UIF) {
int flags = 0;
/* Open the appropriate transport */ /* Open the appropriate transport */
if (mode == MODE_UIF) { if (args->mode == MODE_UIF) {
trans = uif_open(uif_device); trans = uif_open(args->uif_device);
} else { } else {
trans = rf2500_open(); trans = rf2500_open();
flags |= FET_PROTO_RF2500; flags |= FET_PROTO_RF2500;
} }
if (!trans) if (!trans)
return -1; return NULL;
/* Then initialize the device */ /* Then initialize the device */
if (!want_jtag) if (!args->want_jtag)
flags |= FET_PROTO_SPYBIWIRE; flags |= FET_PROTO_SPYBIWIRE;
msp430_dev = fet_open(trans, flags, vcc_mv); msp430_dev = fet_open(trans, flags, args->vcc_mv);
} }
if (!msp430_dev) {
if (trans)
trans->close();
return NULL;
}
device_set(msp430_dev);
return msp430_dev;
}
int main(int argc, char **argv)
{
const struct device *msp430_dev;
struct cmdline_args args = {0};
cproc_t cp;
int ret = 0;
puts(
"MSPDebug version 0.6 - debugging tool for MSP430 MCUs\n"
"Copyright (C) 2009, 2010 Daniel Beer <daniel@tortek.co.nz>\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
args.vcc_mv = 3000;
if (parse_cmdline_args(argc, argv, &args) < 0)
return -1;
ctrlc_init();
if (stab_init() < 0)
return -1;
msp430_dev = setup_device(&args);
if (!msp430_dev) { if (!msp430_dev) {
stab_exit(); stab_exit();
return -1; return -1;
@ -199,14 +231,14 @@ int main(int argc, char **argv)
gdb_register(cp) < 0 || gdb_register(cp) < 0 ||
rtools_register(cp) < 0) { rtools_register(cp) < 0) {
perror("couldn't set up command parser"); perror("couldn't set up command parser");
if (cp)
cproc_destroy(cp);
msp430_dev->close();
stab_exit();
return -1; return -1;
} }
/* Initialise parsing */ if (!args.no_rc)
device_set(msp430_dev);
ctrlc_init();
if (!no_rc)
process_rc_file(cp); process_rc_file(cp);
/* Process commands */ /* Process commands */
@ -221,6 +253,7 @@ int main(int argc, char **argv)
cproc_reader_loop(cp); cproc_reader_loop(cp);
} }
cproc_destroy(cp);
msp430_dev->close(); msp430_dev->close();
stab_exit(); stab_exit();