X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fflash%2Fnor%2Fnrf51.c;h=711d6fbaadee713529410b5ef5820380c3d1d401;hp=88c318c9d4cca8ac2047020657da78040b43d0e4;hb=36bc83b174e4ac9741f325fc20fa01885e10f85e;hpb=ba66b4c594e12e4b1dd37168376ded95a8ae4e89 diff --git a/src/flash/nor/nrf51.c b/src/flash/nor/nrf51.c index 88c318c9d4..711d6fbaad 100644 --- a/src/flash/nor/nrf51.c +++ b/src/flash/nor/nrf51.c @@ -203,7 +203,7 @@ static const struct nrf51_device_spec nrf51_known_devices_table[] = { static int nrf51_bank_is_probed(struct flash_bank *bank) { - struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv; + struct nrf51_info *chip = bank->driver_priv; assert(chip != NULL); @@ -218,7 +218,7 @@ static int nrf51_get_probed_chip_if_halted(struct flash_bank *bank, struct nrf51 return ERROR_TARGET_NOT_HALTED; } - *chip = (struct nrf51_info *)bank->driver_priv; + *chip = bank->driver_priv; int probed = nrf51_bank_is_probed(bank); if (probed < 0) @@ -357,7 +357,7 @@ static int nrf51_protect_check(struct flash_bank *bank) if (bank->base == NRF51_UICR_BASE) return ERROR_OK; - struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv; + struct nrf51_info *chip = bank->driver_priv; assert(chip != NULL); @@ -443,7 +443,7 @@ static int nrf51_probe(struct flash_bank *bank) { uint32_t hwid; int res; - struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv; + struct nrf51_info *chip = bank->driver_priv; res = target_read_u32(chip->target, NRF51_FICR_CONFIGID, &hwid); if (res != ERROR_OK) { @@ -546,7 +546,7 @@ static int nrf51_auto_probe(struct flash_bank *bank) static struct flash_sector *nrf51_find_sector_by_address(struct flash_bank *bank, uint32_t address) { - struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv; + struct nrf51_info *chip = bank->driver_priv; for (int i = 0; i < bank->num_sectors; i++) if (bank->sectors[i].offset <= address && @@ -600,16 +600,36 @@ static int nrf51_erase_page(struct nrf51_info *chip, struct flash_sector *sector return res; } +static int nrf51_ll_flash_write(struct nrf51_info *chip, uint32_t offset, const uint8_t *buffer, uint32_t buffer_size) +{ + int res; + assert(buffer_size % 4 == 0); + + for (; buffer_size > 0; buffer_size -= 4) { + res = target_write_memory(chip->target, offset, 4, 1, buffer); + if (res != ERROR_OK) + return res; + + res = nrf51_wait_for_nvmc(chip); + if (res != ERROR_OK) + return res; + + offset += 4; + buffer += 4; + } + + return ERROR_OK; +} + static int nrf51_write_page(struct flash_bank *bank, uint32_t offset, const uint8_t *buffer) { assert(offset % 4 == 0); - int res = ERROR_FAIL; - struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv; + struct nrf51_info *chip = bank->driver_priv; struct flash_sector *sector = nrf51_find_sector_by_address(bank, offset); if (!sector) - goto error; + return ERROR_FLASH_SECTOR_INVALID; if (sector->is_protected) goto error; @@ -627,8 +647,8 @@ static int nrf51_write_page(struct flash_bank *bank, uint32_t offset, const uint goto error; sector->is_erased = 0; - res = target_write_memory(bank->target, offset, 4, - chip->code_page_size / 4, buffer); + + res = nrf51_ll_flash_write(chip, offset, buffer, chip->code_page_size); if (res != ERROR_OK) goto set_read_only; @@ -768,11 +788,7 @@ static int nrf51_uicr_flash_write(struct flash_bank *bank, memcpy(&uicr[offset], buffer, count); - res = target_write_memory(bank->target, - NRF51_UICR_BASE, - 4, - NRF51_UICR_SIZE / 4, - uicr); + res = nrf51_ll_flash_write(chip, NRF51_UICR_BASE, uicr, NRF51_UICR_SIZE); if (res != ERROR_OK) { nrf51_nvmc_read_only(chip); return res; @@ -904,8 +920,9 @@ static int nrf51_info(struct flash_bank *bank, char *buf, int buf_size) if (res != ERROR_OK) return res; - struct { - uint32_t address, value; + static struct { + const uint32_t address; + uint32_t value; } ficr[] = { { .address = NRF51_FICR_CODEPAGESIZE }, { .address = NRF51_FICR_CODESIZE },