target/lpc: flash read write return bool

This commit is contained in:
Rafael Silva 2022-08-24 12:37:42 +01:00 committed by Rachel Mant
parent 50057d3f55
commit f0c955432b
5 changed files with 27 additions and 28 deletions

View File

@ -196,5 +196,5 @@ static bool lmi_flash_write(target_flash_s *f, target_addr_t dest, const void *s
static bool lmi_mass_erase(target *t) static bool lmi_mass_erase(target *t)
{ {
return lmi_flash_erase(t->flash, t->flash->start, t->flash->length) == 0; return lmi_flash_erase(t->flash, t->flash->start, t->flash->length);
} }

View File

@ -48,7 +48,7 @@
static bool lpc43xx_cmd_reset(target *t, int argc, const char *argv[]); static bool lpc43xx_cmd_reset(target *t, int argc, const char *argv[]);
static bool lpc43xx_cmd_mkboot(target *t, int argc, const char *argv[]); static bool lpc43xx_cmd_mkboot(target *t, int argc, const char *argv[]);
static int lpc43xx_flash_init(target *t); static int lpc43xx_flash_init(target *t);
static int lpc43xx_flash_erase(target_flash_s *f, target_addr_t addr, size_t len); static bool lpc43xx_flash_erase(target_flash_s *f, target_addr_t addr, size_t len);
static bool lpc43xx_mass_erase(target *t); static bool lpc43xx_mass_erase(target *t);
static void lpc43xx_set_internal_clock(target *t); static void lpc43xx_set_internal_clock(target *t);
static void lpc43xx_wdt_set_period(target *t); static void lpc43xx_wdt_set_period(target *t);
@ -186,10 +186,10 @@ static int lpc43xx_flash_init(target *t)
return 0; return 0;
} }
static int lpc43xx_flash_erase(target_flash_s *f, target_addr_t addr, size_t len) static bool lpc43xx_flash_erase(target_flash_s *f, target_addr_t addr, size_t len)
{ {
if (lpc43xx_flash_init(f->t)) if (lpc43xx_flash_init(f->t))
return -1; return false;
return lpc_flash_erase(f, addr, len); return lpc_flash_erase(f, addr, len);
} }

View File

@ -54,7 +54,7 @@ static bool lpc546xx_cmd_write_sector(target *t, int argc, const char *argv[]);
static void lpc546xx_reset_attach(target *t); static void lpc546xx_reset_attach(target *t);
static int lpc546xx_flash_init(target *t); static int lpc546xx_flash_init(target *t);
static int lpc546xx_flash_erase(target_flash_s *f, target_addr_t addr, size_t len); static bool lpc546xx_flash_erase(target_flash_s *f, target_addr_t addr, size_t len);
static bool lpc546xx_mass_erase(target *t); static bool lpc546xx_mass_erase(target *t);
static void lpc546xx_wdt_set_period(target *t); static void lpc546xx_wdt_set_period(target *t);
static void lpc546xx_wdt_pet(target *t); static void lpc546xx_wdt_pet(target *t);
@ -267,14 +267,13 @@ static bool lpc546xx_cmd_write_sector(target *t, int argc, const char *argv[])
buf[i] = i & 0xff; buf[i] = i & 0xff;
} }
retval = lpc_flash_write_magic_vect(t->flash, sector_addr, buf, retval = !lpc_flash_write_magic_vect(t->flash, sector_addr, buf, sector_size);
sector_size);
free(buf); free(buf);
return retval == 0; return retval;
} }
return -1; return true;
} }
static int lpc546xx_flash_init(target *t) static int lpc546xx_flash_init(target *t)
@ -291,10 +290,10 @@ static int lpc546xx_flash_init(target *t)
return 0; return 0;
} }
static int lpc546xx_flash_erase(target_flash_s *tf, target_addr_t addr, size_t len) static bool lpc546xx_flash_erase(target_flash_s *tf, target_addr_t addr, size_t len)
{ {
if (lpc546xx_flash_init(tf->t)) if (lpc546xx_flash_init(tf->t))
return -1; return false;
return lpc_flash_erase(tf, addr, len); return lpc_flash_erase(tf, addr, len);
} }

View File

@ -70,7 +70,7 @@ char *iap_error[] = {
"Page is invalid", "Page is invalid",
}; };
static int lpc_flash_write(target_flash_s *tf, target_addr_t dest, const void *src, size_t len); static bool lpc_flash_write(target_flash_s *tf, target_addr_t dest, const void *src, size_t len);
struct lpc_flash *lpc_add_flash(target *t, target_addr_t addr, size_t length) struct lpc_flash *lpc_add_flash(target *t, target_addr_t addr, size_t length)
{ {
@ -182,7 +182,7 @@ enum iap_status lpc_iap_call(struct lpc_flash *f, void *result, enum iap_cmd cmd
#define LPX80X_SECTOR_SIZE 0x400 #define LPX80X_SECTOR_SIZE 0x400
#define LPX80X_PAGE_SIZE 0x40 #define LPX80X_PAGE_SIZE 0x40
int lpc_flash_erase(target_flash_s *tf, target_addr_t addr, size_t len) bool lpc_flash_erase(target_flash_s *tf, target_addr_t addr, size_t len)
{ {
struct lpc_flash *f = (struct lpc_flash *)tf; struct lpc_flash *f = (struct lpc_flash *)tf;
const uint32_t start = lpc_sector_for_addr(f, addr); const uint32_t start = lpc_sector_for_addr(f, addr);
@ -190,7 +190,7 @@ int lpc_flash_erase(target_flash_s *tf, target_addr_t addr, size_t len)
uint32_t last_full_sector = end; uint32_t last_full_sector = end;
if (lpc_iap_call(f, NULL, IAP_CMD_PREPARE, start, end, f->bank)) if (lpc_iap_call(f, NULL, IAP_CMD_PREPARE, start, end, f->bank))
return -1; return false;
/* Only LPC80x has reserved pages!*/ /* Only LPC80x has reserved pages!*/
if (f->reserved_pages && addr + len >= tf->length - 0x400U) if (f->reserved_pages && addr + len >= tf->length - 0x400U)
@ -199,11 +199,11 @@ int lpc_flash_erase(target_flash_s *tf, target_addr_t addr, size_t len)
if (start <= last_full_sector) { if (start <= last_full_sector) {
/* Sector erase */ /* Sector erase */
if (lpc_iap_call(f, NULL, IAP_CMD_ERASE, start, last_full_sector, CPU_CLK_KHZ, f->bank)) if (lpc_iap_call(f, NULL, IAP_CMD_ERASE, start, last_full_sector, CPU_CLK_KHZ, f->bank))
return -2; return false;
/* check erase ok */ /* check erase ok */
if (lpc_iap_call(f, NULL, IAP_CMD_BLANKCHECK, start, last_full_sector, f->bank)) if (lpc_iap_call(f, NULL, IAP_CMD_BLANKCHECK, start, last_full_sector, f->bank))
return -3; return false;
} }
if (last_full_sector != end) { if (last_full_sector != end) {
@ -211,23 +211,23 @@ int lpc_flash_erase(target_flash_s *tf, target_addr_t addr, size_t len)
const uint32_t page_end = page_start + LPX80X_SECTOR_SIZE / LPX80X_PAGE_SIZE - 1 - f->reserved_pages; const uint32_t page_end = page_start + LPX80X_SECTOR_SIZE / LPX80X_PAGE_SIZE - 1 - f->reserved_pages;
if (lpc_iap_call(f, NULL, IAP_CMD_PREPARE, end, end, f->bank)) if (lpc_iap_call(f, NULL, IAP_CMD_PREPARE, end, end, f->bank))
return -1; return false;
if (lpc_iap_call(f, NULL, IAP_CMD_ERASE_PAGE, page_start, page_end, CPU_CLK_KHZ, f->bank)) if (lpc_iap_call(f, NULL, IAP_CMD_ERASE_PAGE, page_start, page_end, CPU_CLK_KHZ, f->bank))
return -2; return false;
/* Blank check omitted!*/ /* Blank check omitted!*/
} }
return 0; return true;
} }
static int lpc_flash_write(target_flash_s *tf, target_addr_t dest, const void *src, size_t len) static bool lpc_flash_write(target_flash_s *tf, target_addr_t dest, const void *src, size_t len)
{ {
struct lpc_flash *f = (struct lpc_flash *)tf; struct lpc_flash *f = (struct lpc_flash *)tf;
/* prepare... */ /* prepare... */
uint32_t sector = lpc_sector_for_addr(f, dest); uint32_t sector = lpc_sector_for_addr(f, dest);
if (lpc_iap_call(f, NULL, IAP_CMD_PREPARE, sector, sector, f->bank)) { if (lpc_iap_call(f, NULL, IAP_CMD_PREPARE, sector, sector, f->bank)) {
DEBUG_WARN("Prepare failed\n"); DEBUG_WARN("Prepare failed\n");
return -1; return false;
} }
uint32_t bufaddr = ALIGN(f->iap_ram + sizeof(struct flash_param), 4); uint32_t bufaddr = ALIGN(f->iap_ram + sizeof(struct flash_param), 4);
target_mem_write(f->f.t, bufaddr, src, len); target_mem_write(f->f.t, bufaddr, src, len);
@ -236,7 +236,7 @@ static int lpc_flash_write(target_flash_s *tf, target_addr_t dest, const void *s
/* Write payload to target ram */ /* Write payload to target ram */
/* set the destination address and program */ /* set the destination address and program */
if (lpc_iap_call(f, NULL, IAP_CMD_PROGRAM, dest, bufaddr, len, CPU_CLK_KHZ)) if (lpc_iap_call(f, NULL, IAP_CMD_PROGRAM, dest, bufaddr, len, CPU_CLK_KHZ))
return -2; return false;
} else { } else {
/* On LPC80x, write top sector in pages. /* On LPC80x, write top sector in pages.
* Silently ignore write to the 2 reserved pages at top!*/ * Silently ignore write to the 2 reserved pages at top!*/
@ -244,20 +244,20 @@ static int lpc_flash_write(target_flash_s *tf, target_addr_t dest, const void *s
while (len) { while (len) {
if (lpc_iap_call(f, NULL, IAP_CMD_PREPARE, sector, sector, f->bank)) { if (lpc_iap_call(f, NULL, IAP_CMD_PREPARE, sector, sector, f->bank)) {
DEBUG_WARN("Prepare failed\n"); DEBUG_WARN("Prepare failed\n");
return -1; return false;
} }
/* set the destination address and program */ /* set the destination address and program */
if (lpc_iap_call(f, NULL, IAP_CMD_PROGRAM, dest, bufaddr, LPX80X_PAGE_SIZE, CPU_CLK_KHZ)) if (lpc_iap_call(f, NULL, IAP_CMD_PROGRAM, dest, bufaddr, LPX80X_PAGE_SIZE, CPU_CLK_KHZ))
return -2; return false;
dest += LPX80X_PAGE_SIZE; dest += LPX80X_PAGE_SIZE;
bufaddr += LPX80X_PAGE_SIZE; bufaddr += LPX80X_PAGE_SIZE;
len -= LPX80X_PAGE_SIZE; len -= LPX80X_PAGE_SIZE;
} }
} }
return 0; return true;
} }
int lpc_flash_write_magic_vect(target_flash_s *f, target_addr_t dest, const void *src, size_t len) bool lpc_flash_write_magic_vect(target_flash_s *f, target_addr_t dest, const void *src, size_t len)
{ {
if (dest == 0) { if (dest == 0) {
/* Fill in the magic vector to allow booting the flash */ /* Fill in the magic vector to allow booting the flash */

View File

@ -82,7 +82,7 @@ struct lpc_flash {
struct lpc_flash *lpc_add_flash(target *t, target_addr_t addr, size_t length); struct lpc_flash *lpc_add_flash(target *t, target_addr_t addr, size_t length);
enum iap_status lpc_iap_call(struct lpc_flash *f, void *result, enum iap_cmd cmd, ...); enum iap_status lpc_iap_call(struct lpc_flash *f, void *result, enum iap_cmd cmd, ...);
int lpc_flash_erase(target_flash_s *f, target_addr_t addr, size_t len); bool lpc_flash_erase(target_flash_s *f, target_addr_t addr, size_t len);
int lpc_flash_write_magic_vect(target_flash_s *f, target_addr_t dest, const void *src, size_t len); bool lpc_flash_write_magic_vect(target_flash_s *f, target_addr_t dest, const void *src, size_t len);
#endif /* TARGET_LPC_COMMON_H */ #endif /* TARGET_LPC_COMMON_H */