From 99a23a7e1ace245bdb668efc2b06ffa7b4fec47e Mon Sep 17 00:00:00 2001 From: mirage Date: Sun, 13 Jan 2013 09:50:23 +0800 Subject: [PATCH 1/2] As per the lpc11xx manual the stack pointer should be set before IAP calls are made. In lpc11xx_flash_prepare() and lpc11xx_flash_erase() bug in calculating the ending sector number. --- src/nxp_tgt.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/nxp_tgt.c b/src/nxp_tgt.c index ca6a81f..a9f1c92 100644 --- a/src/nxp_tgt.c +++ b/src/nxp_tgt.c @@ -22,6 +22,10 @@ struct flash_program { static struct flash_program flash_pgm; +#define MSP 17 // Main stack pointer register number +#define MIN_RAM_SIZE_FOR_LPC1xxx 2048 +#define RAM_USAGE_FOR_IAP_ROUTINES 32 // IAP routines use 32 bytes at top of ram + #define IAP_ENTRYPOINT 0x1fff1ff1 #define IAP_RAM_BASE 0x10000000 @@ -122,6 +126,7 @@ lpc11x_iap_call(struct target_s *target, struct flash_param *param, unsigned par regs[0] = IAP_RAM_BASE + offsetof(struct flash_param, command); regs[1] = IAP_RAM_BASE + offsetof(struct flash_param, result); + regs[MSP] = IAP_RAM_BASE + MIN_RAM_SIZE_FOR_LPC1xxx - RAM_USAGE_FOR_IAP_ROUTINES;// stack pointer - top of the smallest ram less 32 for IAP usage regs[14] = IAP_RAM_BASE | 1; regs[15] = IAP_ENTRYPOINT; target_regs_write(target, regs); @@ -141,7 +146,7 @@ lpc11xx_flash_prepare(struct target_s *target, uint32_t addr, int len) memset(&flash_pgm.p, 0, sizeof(flash_pgm.p)); flash_pgm.p.command[0] = IAP_CMD_PREPARE; flash_pgm.p.command[1] = addr / 4096; - flash_pgm.p.command[2] = flash_pgm.p.command[1] + ((len + 4095) / 4096) - 1; + flash_pgm.p.command[2] = (addr + len) / 4096; lpc11x_iap_call(target, &flash_pgm.p, sizeof(flash_pgm.p)); if (flash_pgm.p.result[0] != IAP_STATUS_CMD_SUCCESS) { @@ -165,7 +170,7 @@ lpc11xx_flash_erase(struct target_s *target, uint32_t addr, int len) /* and now erase them */ flash_pgm.p.command[0] = IAP_CMD_ERASE; flash_pgm.p.command[1] = addr / 4096; - flash_pgm.p.command[2] = flash_pgm.p.command[1] + ((len + 4095) / 4096) - 1; + flash_pgm.p.command[2] = (addr + len) / 4096; flash_pgm.p.command[3] = 12000; /* XXX safe to assume this? */ lpc11x_iap_call(target, &flash_pgm.p, sizeof(flash_pgm.p)); if (flash_pgm.p.result[0] != IAP_STATUS_CMD_SUCCESS) { From b50b3ffa861e2fb522c52d887844c7f3952f226b Mon Sep 17 00:00:00 2001 From: mirage Date: Sat, 26 Jan 2013 11:38:27 +0800 Subject: [PATCH 2/2] Error in calculating ending sector number. --- src/nxp_tgt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nxp_tgt.c b/src/nxp_tgt.c index a9f1c92..a22eda8 100644 --- a/src/nxp_tgt.c +++ b/src/nxp_tgt.c @@ -146,7 +146,7 @@ lpc11xx_flash_prepare(struct target_s *target, uint32_t addr, int len) memset(&flash_pgm.p, 0, sizeof(flash_pgm.p)); flash_pgm.p.command[0] = IAP_CMD_PREPARE; flash_pgm.p.command[1] = addr / 4096; - flash_pgm.p.command[2] = (addr + len) / 4096; + flash_pgm.p.command[2] = (addr + len - 1) / 4096; lpc11x_iap_call(target, &flash_pgm.p, sizeof(flash_pgm.p)); if (flash_pgm.p.result[0] != IAP_STATUS_CMD_SUCCESS) { @@ -170,7 +170,7 @@ lpc11xx_flash_erase(struct target_s *target, uint32_t addr, int len) /* and now erase them */ flash_pgm.p.command[0] = IAP_CMD_ERASE; flash_pgm.p.command[1] = addr / 4096; - flash_pgm.p.command[2] = (addr + len) / 4096; + flash_pgm.p.command[2] = (addr + len - 1) / 4096; flash_pgm.p.command[3] = 12000; /* XXX safe to assume this? */ lpc11x_iap_call(target, &flash_pgm.p, sizeof(flash_pgm.p)); if (flash_pgm.p.result[0] != IAP_STATUS_CMD_SUCCESS) {