From 5ea01030e2d3a585cd0076d2652cade69b67297c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Mellstr=C3=B6m?= Date: Thu, 3 Jun 2021 12:58:05 +0200 Subject: [PATCH] Add nRF51 command for (only) erasing the UICR registers Original implementation by: Benjamin Vedder --- src/target/nrf51.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/target/nrf51.c b/src/target/nrf51.c index 038e8a8b..4021c0a9 100644 --- a/src/target/nrf51.c +++ b/src/target/nrf51.c @@ -31,6 +31,7 @@ static int nrf51_flash_write(struct target_flash *f, target_addr dest, const void *src, size_t len); static bool nrf51_cmd_erase_all(target *t, int argc, const char **argv); +static bool nrf51_cmd_erase_uicr(target *t, int argc, const char **argv); static bool nrf51_cmd_read_hwid(target *t, int argc, const char **argv); static bool nrf51_cmd_read_fwid(target *t, int argc, const char **argv); static bool nrf51_cmd_read_deviceid(target *t, int argc, const char **argv); @@ -41,6 +42,7 @@ static bool nrf51_cmd_read(target *t, int argc, const char **argv); const struct command_s nrf51_cmd_list[] = { {"erase_mass", (cmd_handler)nrf51_cmd_erase_all, "Erase entire flash memory"}, + {"erase_uicr", (cmd_handler)nrf51_cmd_erase_uicr, "Erase UICR registers"}, {"read", (cmd_handler)nrf51_cmd_read, "Read device parameters"}, {NULL, NULL, NULL} }; @@ -244,6 +246,31 @@ static bool nrf51_cmd_erase_all(target *t, int argc, const char **argv) return true; } +static bool nrf51_cmd_erase_uicr(target *t, int argc, const char **argv) +{ + (void)argc; + (void)argv; + tc_printf(t, "erase..\n"); + + /* Enable erase */ + target_mem_write32(t, NRF51_NVMC_CONFIG, NRF51_NVMC_CONFIG_EEN); + + /* Poll for NVMC_READY */ + while (target_mem_read32(t, NRF51_NVMC_READY) == 0) + if(target_check_error(t)) + return false; + + /* Erase UICR */ + target_mem_write32(t, NRF51_NVMC_ERASEUICR, 1); + + /* Poll for NVMC_READY */ + while (target_mem_read32(t, NRF51_NVMC_READY) == 0) + if(target_check_error(t)) + return false; + + return true; +} + static bool nrf51_cmd_read_hwid(target *t, int argc, const char **argv) { (void)argc;