From b9e5db63d5b77b1f322ed8c8f5b2e1c4c700ee82 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Thu, 23 Sep 2010 15:45:06 +1200 Subject: [PATCH] device: altered erase interface to allow different erase types. --- bsl.c | 26 +++++++++++++++----------- devcmd.c | 2 +- device.h | 13 +++++++++++-- fet.c | 14 ++++++++++---- flash_bsl.c | 17 ++++++++++------- prog.c | 4 ++-- sim.c | 29 +++++++++++++++++++++++++---- 7 files changed, 74 insertions(+), 31 deletions(-) diff --git a/bsl.c b/bsl.c index 0b3ceaa..e649081 100644 --- a/bsl.c +++ b/bsl.c @@ -212,20 +212,9 @@ static void bsl_destroy(device_t dev_base) free(dev); } -static int bsl_erase(struct bsl_device *dev) -{ - /* Constants found from viewing gdbproxy's activities */ - return bsl_xfer(dev, CMD_ERASE, 0x2500, NULL, 0x0069); -} - static int bsl_ctl(device_t dev_base, device_ctl_t type) { - struct bsl_device *dev = (struct bsl_device *)dev_base; - switch (type) { - case DEVICE_CTL_ERASE: - return bsl_erase(dev); - case DEVICE_CTL_HALT: /* Ignore halt requests */ return 0; @@ -315,6 +304,20 @@ static int bsl_readmem(device_t dev_base, return 0; } +static int bsl_erase(device_t dev_base, device_erase_type_t type, + address_t addr) +{ + struct bsl_device *dev = (struct bsl_device *)dev_base; + + if (type != DEVICE_ERASE_MAIN) { + printc_err("bsl: only main erase is supported\n"); + return -1; + } + + /* Constants found from viewing gdbproxy's activities */ + return bsl_xfer(dev, CMD_ERASE, 0x2500, NULL, 0x0069); +} + static int enter_via_fet(struct bsl_device *dev) { uint8_t buf[16]; @@ -367,6 +370,7 @@ device_t bsl_open(const char *device) 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; diff --git a/devcmd.c b/devcmd.c index 24788c3..50c9ec1 100644 --- a/devcmd.c +++ b/devcmd.c @@ -142,7 +142,7 @@ int cmd_erase(char **arg) return -1; printc("Erasing...\n"); - return device_default->ctl(device_default, DEVICE_CTL_ERASE); + return device_default->erase(device_default, DEVICE_ERASE_MAIN, 0); } int cmd_step(char **arg) diff --git a/device.h b/device.h index fed2b2f..be0a890 100644 --- a/device.h +++ b/device.h @@ -31,8 +31,7 @@ typedef enum { DEVICE_CTL_RESET, DEVICE_CTL_RUN, DEVICE_CTL_HALT, - DEVICE_CTL_STEP, - DEVICE_CTL_ERASE + DEVICE_CTL_STEP } device_ctl_t; typedef enum { @@ -42,6 +41,12 @@ typedef enum { DEVICE_STATUS_ERROR } device_status_t; +typedef enum { + DEVICE_ERASE_ALL, + DEVICE_ERASE_MAIN, + DEVICE_ERASE_SEGMENT +} device_erase_type_t; + #define DEVICE_NUM_REGS 16 #define DEVICE_MAX_BREAKPOINTS 32 @@ -71,6 +76,10 @@ struct device { int (*writemem)(device_t dev, address_t addr, const uint8_t *mem, address_t len); + /* Erase memory */ + int (*erase)(device_t dev, device_erase_type_t type, + address_t address); + /* Read/write registers */ int (*getregs)(device_t dev, address_t *regs); int (*setregs)(device_t dev, const address_t *regs); diff --git a/fet.c b/fet.c index cb675b8..0ab391a 100644 --- a/fet.c +++ b/fet.c @@ -575,8 +575,16 @@ static int do_run(struct fet_device *dev, int type) return 0; } -static int do_erase(struct fet_device *dev) +static int fet_erase(device_t dev_base, device_erase_type_t type, + address_t addr) { + struct fet_device *dev = (struct fet_device *)dev_base; + + if (type != DEVICE_ERASE_MAIN) { + printc_err("fet: only main erase is supported\n"); + return -1; + } + if (xfer(dev, C_RESET, NULL, 0, 3, FET_RESET_ALL, 0, 0) < 0) { printc_err("fet: reset before erase failed\n"); return -1; @@ -686,9 +694,6 @@ static int fet_ctl(device_t dev_base, device_ctl_t action) break; } break; - - case DEVICE_CTL_ERASE: - return do_erase(dev); } return 0; @@ -921,6 +926,7 @@ device_t fet_open(transport_t transport, int proto_flags, int vcc_mv, dev->base.setregs = fet_setregs; dev->base.ctl = fet_ctl; dev->base.poll = fet_poll; + dev->base.erase = fet_erase; dev->transport = transport; dev->proto_flags = proto_flags; diff --git a/flash_bsl.c b/flash_bsl.c index 3aaa392..302d823 100644 --- a/flash_bsl.c +++ b/flash_bsl.c @@ -327,12 +327,19 @@ static int flash_bsl_readmem(device_t dev_base, return 0; } -static int flash_bsl_erase(struct flash_bsl_device *dev) +static int flash_bsl_erase(device_t dev_base, device_erase_type_t type, + address_t addr) { + struct flash_bsl_device *dev = (struct flash_bsl_device *)dev_base; const uint8_t mass_erase_cmd[] = { MASS_ERASE }; uint8_t response_buffer[16]; int ret; + if (type != DEVICE_ERASE_ALL) { + printc_err("flash_bsl_erase: only mass erase is supported\n"); + return -1; + } + if (flash_bsl_send(dev, mass_erase_cmd, sizeof(mass_erase_cmd)) < 0) { printc_err("flash_bsl_erase: failed to send erase command\n"); return -1; @@ -380,7 +387,7 @@ static int flash_bsl_unlock(struct flash_bsl_device *dev) int ret; /* mass erase (this might wipe Information Memory on some devices */ - if (flash_bsl_erase(dev) < 0) { + if (flash_bsl_erase((device_t)dev, DEVICE_ERASE_ALL, 0) < 0) { printc_err("flash_bsl_unlock: warning: erase failed\n"); } @@ -419,12 +426,7 @@ static int flash_bsl_unlock(struct flash_bsl_device *dev) static int flash_bsl_ctl(device_t dev_base, device_ctl_t type) { - struct flash_bsl_device *dev = (struct flash_bsl_device *)dev_base; - switch (type) { - case DEVICE_CTL_ERASE: - return flash_bsl_erase(dev); - case DEVICE_CTL_HALT: /* Ignore halt requests */ return 0; @@ -628,6 +630,7 @@ device_t flash_bsl_open(const char *device, int long_password) 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->serial_fd = open_serial_even_parity(device, B9600); if (dev->serial_fd < 0) { diff --git a/prog.c b/prog.c index 1b7e577..69fddfe 100644 --- a/prog.c +++ b/prog.c @@ -34,8 +34,8 @@ int prog_flush(struct prog_data *prog) if (!prog->have_erased && (prog->flags & PROG_WANT_ERASE)) { printc("Erasing...\n"); - if (device_default->ctl(device_default, - DEVICE_CTL_ERASE) < 0) + if (device_default->erase(device_default, + DEVICE_ERASE_MAIN, 0) < 0) return -1; prog->have_erased = 1; diff --git a/sim.c b/sim.c index 5fd00b8..8a42518 100644 --- a/sim.c +++ b/sim.c @@ -504,10 +504,6 @@ static int sim_ctl(device_t dev_base, device_ctl_t op) dev->regs[MSP430_REG_PC] = MEM_GETW(dev, 0xfffe); return 0; - case DEVICE_CTL_ERASE: - memset(dev->memory, 0xff, MEM_SIZE); - return 0; - case DEVICE_CTL_HALT: dev->running = 0; return 0; @@ -523,6 +519,30 @@ static int sim_ctl(device_t dev_base, device_ctl_t op) return 0; } +static int sim_erase(device_t dev_base, device_erase_type_t type, + address_t addr) +{ + struct sim_device *dev = (struct sim_device *)dev_base; + + switch (type) { + case DEVICE_ERASE_MAIN: + memset(dev->memory + 0x2000, 0xff, MEM_SIZE - 0x2000); + break; + + case DEVICE_ERASE_ALL: + memset(dev->memory + 0x1000, 0xff, 256); + break; + + case DEVICE_ERASE_SEGMENT: + addr &= ~0x3f; + addr &= (MEM_SIZE - 1); + memset(dev->memory + addr, 0xff, 64); + break; + } + + return 0; +} + static device_status_t sim_poll(device_t dev_base) { struct sim_device *dev = (struct sim_device *)dev_base; @@ -583,6 +603,7 @@ device_t sim_open(sim_fetch_func_t fetch_func, 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;