From ed2c841da937d77b9e1c69a6fed4ce63ea8986d6 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Tue, 15 Mar 2011 14:56:07 +1300 Subject: [PATCH] Separated device vtables/class information. --- bsl.c | 25 +++++++++------ bsl.h | 2 +- device.h | 44 ++++++++++++++++---------- fet.c | 91 ++++++++++++++++++++++++++++++++++++++++++----------- fet.h | 9 +++--- flash_bsl.c | 26 +++++++++------ flash_bsl.h | 2 +- main.c | 55 +++++++------------------------- sim.c | 25 +++++++++------ sim.h | 2 +- 10 files changed, 165 insertions(+), 116 deletions(-) diff --git a/bsl.c b/bsl.c index 1d27847..d77c588 100644 --- a/bsl.c +++ b/bsl.c @@ -361,7 +361,7 @@ static int enter_via_fet(struct bsl_device *dev) return 0; } -device_t bsl_open(const struct device_args *args) +static device_t bsl_open(const struct device_args *args) { struct bsl_device *dev; @@ -378,14 +378,7 @@ device_t bsl_open(const struct device_args *args) memset(dev, 0, sizeof(*dev)); - dev->base.destroy = bsl_destroy; - dev->base.readmem = bsl_readmem; - dev->base.writemem = bsl_writemem; - dev->base.erase = bsl_erase; - dev->base.getregs = bsl_getregs; - dev->base.setregs = bsl_setregs; - dev->base.ctl = bsl_ctl; - dev->base.poll = bsl_poll; + dev->base.type = &device_bsl; dev->serial_fd = open_serial(args->path, B460800); if (dev->serial_fd < 0) { @@ -423,3 +416,17 @@ device_t bsl_open(const struct device_args *args) free(dev); return NULL; } + +const struct device_class device_bsl = { + .name = "uif-bsl", + .help = "TI FET430UIF bootloader.", + .open = bsl_open, + .destroy = bsl_destroy, + .readmem = bsl_readmem, + .writemem = bsl_writemem, + .erase = bsl_erase, + .getregs = bsl_getregs, + .setregs = bsl_setregs, + .ctl = bsl_ctl, + .poll = bsl_poll +}; diff --git a/bsl.h b/bsl.h index b5bd67f..39932e2 100644 --- a/bsl.h +++ b/bsl.h @@ -22,6 +22,6 @@ #include "device.h" /* MSP430 FET Bootloader implementation. */ -device_t bsl_open(const struct device_args *args); +extern const struct device_class device_bsl; #endif diff --git a/device.h b/device.h index 983d920..f2cc8c5 100644 --- a/device.h +++ b/device.h @@ -67,14 +67,12 @@ struct device_args { const char *forced_chip_id; }; -struct device { - /* Breakpoint table. This should not be modified directly. - * Instead, you should use the device_setbrk() helper function. This - * will set the appropriate flags and ensure that the breakpoint is - * reloaded before the next run. - */ - int max_breakpoints; - struct device_breakpoint breakpoints[DEVICE_MAX_BREAKPOINTS]; +struct device_class { + const char *name; + const char *help; + + /* Create a new device */ + device_t (*open)(const struct device_args *args); /* Close the connection to the device and destroy the driver object */ void (*destroy)(device_t dev); @@ -100,6 +98,18 @@ struct device { device_status_t (*poll)(device_t dev); }; +struct device { + const struct device_class *type; + + /* Breakpoint table. This should not be modified directly. + * Instead, you should use the device_setbrk() helper function. This + * will set the appropriate flags and ensure that the breakpoint is + * reloaded before the next run. + */ + int max_breakpoints; + struct device_breakpoint breakpoints[DEVICE_MAX_BREAKPOINTS]; +}; + /* Set or clear a breakpoint. The index of the modified entry is * returned, or -1 if no free entries were available. The modified * entry is flagged so that it will be reloaded on the next run. @@ -113,20 +123,20 @@ int device_setbrk(device_t dev, int which, int enabled, address_t address); extern device_t device_default; /* Helper macros for operating on the default device */ -#define device_destroy() device_default->destroy(device_default) +#define device_destroy() device_default->type->destroy(device_default) #define device_readmem(addr, mem, len) \ - device_default->readmem(device_default, addr, mem, len) + device_default->type->readmem(device_default, addr, mem, len) #define device_writemem(addr, mem, len) \ - device_default->writemem(device_default, addr, mem, len) -#define device_erase(type, addr) \ - device_default->erase(device_default, type, addr) + device_default->type->writemem(device_default, addr, mem, len) +#define device_erase(et, addr) \ + device_default->type->erase(device_default, et, addr) #define device_getregs(regs) \ - device_default->getregs(device_default, regs) + device_default->type->getregs(device_default, regs) #define device_setregs(regs) \ - device_default->setregs(device_default, regs) + device_default->type->setregs(device_default, regs) #define device_ctl(op) \ - device_default->ctl(device_default, op) + device_default->type->ctl(device_default, op) #define device_poll() \ - device_default->poll(device_default) + device_default->type->poll(device_default) #endif diff --git a/fet.c b/fet.c index c371a3f..23ae560 100644 --- a/fet.c +++ b/fet.c @@ -993,8 +993,9 @@ int try_open(struct fet_device *dev, const struct device_args *args, return 0; } -device_t fet_open(const struct device_args *args, - protocol_t protocol, transport_t transport) +static device_t fet_open(const struct device_args *args, + protocol_t protocol, transport_t transport, + const struct device_class *type) { struct fet_device *dev = malloc(sizeof(*dev)); int i; @@ -1006,15 +1007,7 @@ device_t fet_open(const struct device_args *args, memset(dev, 0, sizeof(*dev)); - dev->base.destroy = fet_destroy; - dev->base.readmem = fet_readmem; - dev->base.writemem = fet_writemem; - dev->base.getregs = fet_getregs; - dev->base.setregs = fet_setregs; - dev->base.ctl = fet_ctl; - dev->base.poll = fet_poll; - dev->base.erase = fet_erase; - + dev->base.type = type; dev->transport = transport; dev->protocol = protocol; @@ -1039,7 +1032,7 @@ device_t fet_open(const struct device_args *args, return NULL; } -device_t fet_open_rf2500(const struct device_args *args) +static device_t fet_open_rf2500(const struct device_args *args) { transport_t trans; @@ -1052,10 +1045,25 @@ device_t fet_open_rf2500(const struct device_args *args) if (!trans) return NULL; - return fet_open(args, PROTOCOL_RF2500, trans); + return fet_open(args, PROTOCOL_RF2500, trans, &device_rf2500); } -device_t fet_open_olimex(const struct device_args *args) +const struct device_class device_rf2500 = { + .name = "rf2500", + .help = +"ez430-RF2500 devices. Only USB connection is supported.", + .open = fet_open_rf2500, + .destroy = fet_destroy, + .readmem = fet_readmem, + .writemem = fet_writemem, + .erase = fet_erase, + .getregs = fet_getregs, + .setregs = fet_setregs, + .ctl = fet_ctl, + .poll = fet_poll +}; + +static device_t fet_open_olimex(const struct device_args *args) { transport_t trans; @@ -1067,10 +1075,25 @@ device_t fet_open_olimex(const struct device_args *args) if (!trans) return NULL; - return fet_open(args, PROTOCOL_OLIMEX, trans); + return fet_open(args, PROTOCOL_OLIMEX, trans, &device_olimex); } -device_t fet_open_olimex_iso(const struct device_args *args) +const struct device_class device_olimex = { + .name = "olimex", + .help = +"Olimex MSP-JTAG-TINY.", + .open = fet_open_olimex, + .destroy = fet_destroy, + .readmem = fet_readmem, + .writemem = fet_writemem, + .erase = fet_erase, + .getregs = fet_getregs, + .setregs = fet_setregs, + .ctl = fet_ctl, + .poll = fet_poll +}; + +static device_t fet_open_olimex_iso(const struct device_args *args) { transport_t trans; @@ -1083,10 +1106,25 @@ device_t fet_open_olimex_iso(const struct device_args *args) if (!trans) return NULL; - return fet_open(args, PROTOCOL_OLIMEX, trans); + return fet_open(args, PROTOCOL_OLIMEX, trans, &device_olimex_iso); } -device_t fet_open_uif(const struct device_args *args) +const struct device_class device_olimex_iso = { + .name = "olimex-iso", + .help = +"Olimex MSP-JTAG-ISO.", + .open = fet_open_olimex_iso, + .destroy = fet_destroy, + .readmem = fet_readmem, + .writemem = fet_writemem, + .erase = fet_erase, + .getregs = fet_getregs, + .setregs = fet_setregs, + .ctl = fet_ctl, + .poll = fet_poll +}; + +static device_t fet_open_uif(const struct device_args *args) { transport_t trans; @@ -1099,5 +1137,20 @@ device_t fet_open_uif(const struct device_args *args) if (!trans) return NULL; - return fet_open(args, PROTOCOL_UIF, trans); + return fet_open(args, PROTOCOL_UIF, trans, &device_uif); } + +const struct device_class device_uif = { + .name = "olimex-iso", + .help = +"TI FET430UIF and compatible devices (e.g. eZ430).", + .open = fet_open_uif, + .destroy = fet_destroy, + .readmem = fet_readmem, + .writemem = fet_writemem, + .erase = fet_erase, + .getregs = fet_getregs, + .setregs = fet_setregs, + .ctl = fet_ctl, + .poll = fet_poll +}; diff --git a/fet.h b/fet.h index 52d64a9..db702f0 100644 --- a/fet.h +++ b/fet.h @@ -21,10 +21,9 @@ #include "device.h" -/* Open UIF devices and variants */ -device_t fet_open_rf2500(const struct device_args *args); -device_t fet_open_olimex(const struct device_args *args); -device_t fet_open_uif(const struct device_args *args); -device_t fet_open_olimex_iso(const struct device_args *args); +extern const struct device_class device_rf2500; +extern const struct device_class device_olimex; +extern const struct device_class device_olimex_iso; +extern const struct device_class device_uif; #endif diff --git a/flash_bsl.c b/flash_bsl.c index c71fb69..8cc227a 100644 --- a/flash_bsl.c +++ b/flash_bsl.c @@ -621,7 +621,7 @@ static void flash_bsl_destroy(device_t dev_base) free(dev); } -device_t flash_bsl_open(const struct device_args *args) +static device_t flash_bsl_open(const struct device_args *args) { struct flash_bsl_device *dev; uint8_t tx_bsl_version_command[] = { TX_BSL_VERSION }; @@ -642,15 +642,7 @@ device_t flash_bsl_open(const struct device_args *args) crc_selftest( ); memset(dev, 0, sizeof(*dev)); - - dev->base.destroy = flash_bsl_destroy; - dev->base.readmem = flash_bsl_readmem; - dev->base.writemem = flash_bsl_writemem; - dev->base.getregs = flash_bsl_getregs; - dev->base.setregs = flash_bsl_setregs; - dev->base.ctl = flash_bsl_ctl; - dev->base.poll = flash_bsl_poll; - dev->base.erase = flash_bsl_erase; + dev->base.type = &device_flash_bsl; dev->serial_fd = open_serial_even_parity(args->path, B9600); if (dev->serial_fd < 0) { @@ -697,3 +689,17 @@ device_t flash_bsl_open(const struct device_args *args) free(dev); return NULL; } + +const struct device_class device_flash_bsl = { + .name = "flash-bsl", + .help = "TI generic flash-based bootloader via RS-232", + .open = flash_bsl_open, + .destroy = flash_bsl_destroy, + .readmem = flash_bsl_readmem, + .writemem = flash_bsl_writemem, + .getregs = flash_bsl_getregs, + .setregs = flash_bsl_setregs, + .ctl = flash_bsl_ctl, + .poll = flash_bsl_poll, + .erase = flash_bsl_erase +}; diff --git a/flash_bsl.h b/flash_bsl.h index 9883a23..ee214db 100644 --- a/flash_bsl.h +++ b/flash_bsl.h @@ -23,6 +23,6 @@ #include "device.h" /* MSP430 Flash bootloader implementation. */ -device_t flash_bsl_open(const struct device_args *args); +extern const struct device_class device_flash_bsl; #endif diff --git a/main.c b/main.c index 55c6c96..0158f31 100644 --- a/main.c +++ b/main.c @@ -57,47 +57,14 @@ struct cmdline_args { struct device_args devarg; }; -struct driver { - const char *name; - const char *help; - device_t (*func)(const struct device_args *args); -}; - -static const struct driver driver_table[] = { - { - .name = "rf2500", - .help = "eZ430-RF2500 devices. Only USB connection is " - "supported.", - .func = fet_open_rf2500 - }, - { .name = "olimex", - .help = "Olimex MSP-JTAG-TINY.", - .func = fet_open_olimex - }, - { .name = "olimex-iso", - .help = "Olimex MSP-JTAG-ISO.", - .func = fet_open_olimex_iso - }, - { - .name = "sim", - .help = "Simulation mode.", - .func = sim_open - }, - { - .name = "uif", - .help = "TI FET430UIF and compatible devices (e.g. eZ430).", - .func = fet_open_uif - }, - { - .name = "uif-bsl", - .help = "TI FET430UIF bootloader.", - .func = bsl_open - }, - { - .name = "flash-bsl", - .help = "TI generic flash-based bootloader via RS-232", - .func = flash_bsl_open - } +static const struct device_class *const driver_table[] = { + &device_rf2500, + &device_olimex, + &device_olimex_iso, + &device_sim, + &device_uif, + &device_bsl, + &device_flash_bsl }; static const char *version_text = @@ -147,7 +114,7 @@ static void usage(const char *progname) printc("Available drivers are:\n"); for (i = 0; i < ARRAY_LEN(driver_table); i++) { - const struct driver *drv = &driver_table[i]; + const struct device_class *drv = driver_table[i]; printc(" %s\n %s\n", drv->name, drv->help); } @@ -296,7 +263,7 @@ int setup_driver(struct cmdline_args *args) i = 0; while (i < ARRAY_LEN(driver_table) && - strcasecmp(driver_table[i].name, args->driver_name)) + strcasecmp(driver_table[i]->name, args->driver_name)) i++; if (i >= ARRAY_LEN(driver_table)) { printc_err("Unknown driver: %s. Try --help for a list.\n", @@ -308,7 +275,7 @@ int setup_driver(struct cmdline_args *args) if (!stab_default) return -1; - device_default = driver_table[i].func(&args->devarg); + device_default = driver_table[i]->open(&args->devarg); if (!device_default) { stab_destroy(stab_default); return -1; diff --git a/sim.c b/sim.c index ce9a9c4..313af49 100644 --- a/sim.c +++ b/sim.c @@ -722,7 +722,7 @@ static device_status_t sim_poll(device_t dev_base) return DEVICE_STATUS_RUNNING; } -device_t sim_open(const struct device_args *args) +static device_t sim_open(const struct device_args *args) { struct sim_device *dev = malloc(sizeof(*dev)); @@ -733,15 +733,8 @@ device_t sim_open(const struct device_args *args) memset(dev, 0, sizeof(*dev)); + dev->base.type = &device_sim; dev->base.max_breakpoints = DEVICE_MAX_BREAKPOINTS; - dev->base.destroy = sim_destroy; - dev->base.readmem = sim_readmem; - dev->base.writemem = sim_writemem; - dev->base.erase = sim_erase; - dev->base.getregs = sim_getregs; - dev->base.setregs = sim_setregs; - dev->base.ctl = sim_ctl; - dev->base.poll = sim_poll; memset(dev->memory, 0xff, sizeof(dev->memory)); memset(dev->regs, 0xff, sizeof(dev->regs)); @@ -752,3 +745,17 @@ device_t sim_open(const struct device_args *args) printc_dbg("Simulation started, 0x%x bytes of RAM\n", MEM_SIZE); return (device_t)dev; } + +const struct device_class device_sim = { + .name = "sim", + .help = "Simulation mode.", + .open = sim_open, + .destroy = sim_destroy, + .readmem = sim_readmem, + .writemem = sim_writemem, + .erase = sim_erase, + .getregs = sim_getregs, + .setregs = sim_setregs, + .ctl = sim_ctl, + .poll = sim_poll +}; diff --git a/sim.h b/sim.h index 8484411..8b5fdfe 100644 --- a/sim.h +++ b/sim.h @@ -22,6 +22,6 @@ #include "device.h" /* Dummy/simulation implementation. */ -device_t sim_open(const struct device_args *args); +extern const struct device_class device_sim; #endif