37 #define NRF51_FICR_REG(offset) (NRF51_FICR_BASE + offset) 81 #define NRF51_UICR_REG(offset) (NRF51_UICR_BASE + offset) 93 #define NRF51_NVMC_REG(offset) (NRF51_NVMC_BASE + offset) 116 const uint8_t *
buffer, uint32_t offset, uint32_t
count);
147 .build_code =
"CA/C0",
148 .flash_size_kb = 256,
155 .flash_size_kb = 128,
162 .flash_size_kb = 128,
169 .flash_size_kb = 256,
176 .flash_size_kb = 256,
185 .flash_size_kb = 256,
192 .flash_size_kb = 256,
199 .flash_size_kb = 256,
206 .flash_size_kb = 256,
213 .flash_size_kb = 256,
220 .flash_size_kb = 128,
227 .flash_size_kb = 256,
234 .flash_size_kb = 256,
241 .flash_size_kb = 256,
250 .flash_size_kb = 256,
257 .flash_size_kb = 128,
264 .flash_size_kb = 256,
271 .flash_size_kb = 256,
278 .flash_size_kb = 128,
285 .flash_size_kb = 256,
292 .flash_size_kb = 256,
301 .flash_size_kb = 256,
308 .flash_size_kb = 256,
315 .flash_size_kb = 256,
324 .flash_size_kb = 256,
331 .flash_size_kb = 256,
338 .flash_size_kb = 128,
345 .flash_size_kb = 256,
354 .flash_size_kb = 256,
361 .flash_size_kb = 128,
368 .flash_size_kb = 256,
375 .flash_size_kb = 256,
382 .flash_size_kb = 128,
389 .flash_size_kb = 256,
396 .flash_size_kb = 256,
407 .flash_size_kb = 256,
415 assert(chip !=
NULL);
448 LOG_ERROR(
"Couldn't read NVMC_READY register");
452 if (ready == 0x00000001)
458 LOG_DEBUG(
"Timed out waiting for NVMC_READY");
470 LOG_ERROR(
"Failed to enable erase operation");
480 LOG_ERROR(
"Erase enable did not complete");
493 LOG_ERROR(
"Failed to enable write operation");
503 LOG_ERROR(
"Write enable did not complete");
516 LOG_ERROR(
"Failed to enable read-only operation");
525 LOG_ERROR(
"Read only enable did not complete");
531 uint32_t erase_register, uint32_t erase_value)
554 LOG_ERROR(
"Failed to erase reg: 0x%08"PRIx32
" val: 0x%08"PRIx32,
555 erase_register, erase_value);
570 assert(chip !=
NULL);
575 LOG_ERROR(
"Couldn't read code region 0 size[FICR]");
579 if (clenr0 == 0xFFFFFFFF) {
583 LOG_ERROR(
"Couldn't read code region 0 size[UICR]");
598 uint32_t clenr0, ppfc;
610 LOG_ERROR(
"Code region 0 must start at the begining of the bank");
617 LOG_ERROR(
"Couldn't read PPFC register");
621 if ((ppfc & 0xFF) == 0x00) {
622 LOG_ERROR(
"Code region 0 size was pre-programmed at the factory, can't change flash protection settings");
629 LOG_ERROR(
"Couldn't read code region 0 size[UICR]");
633 if (clenr0 == 0xFFFFFFFF) {
637 LOG_ERROR(
"Couldn't write code region 0 size[UICR]");
642 LOG_ERROR(
"You need to perform chip erase before changing the protection settings");
658 LOG_ERROR(
"Couldn't read CONFIGID register");
666 for (
size_t i = 0; i <
ARRAY_SIZE(nrf51_known_devices_table); i++) {
667 if (hwid == nrf51_known_devices_table[i].hwid) {
668 spec = &nrf51_known_devices_table[i];
675 LOG_INFO(
"nRF%s-%s(build code: %s) %ukB Flash",
679 LOG_WARNING(
"Unknown device (HWID 0x%08" PRIx32
")", hwid);
687 LOG_ERROR(
"Couldn't read code page size");
696 LOG_ERROR(
"Couldn't read code memory size");
703 LOG_WARNING(
"Chip's reported Flash capacity does not match expected one");
770 LOG_DEBUG(
"Erasing all non-volatile memory");
784 LOG_ERROR(
"Cannot erase protected sector at 0x%" PRIx32, sector->
offset);
793 LOG_ERROR(
"Couldn't read PPFC register");
797 if ((ppfc & 0xFF) == 0xFF) {
804 LOG_ERROR(
"The chip was not pre-programmed with SoftDevice stack and UICR cannot be erased separately. Please issue mass erase before trying to write to this region");
853 uint32_t buffer_size = 8192;
862 LOG_DEBUG(
"Writing buffer to flash offset=0x%"PRIx32
" bytes=0x%"PRIx32, offset, bytes);
863 assert(bytes % 4 == 0);
868 LOG_WARNING(
"no working area available, falling back to slow memory writes");
870 for (; bytes > 0; bytes -= 4) {
886 LOG_WARNING(
"using fast async flash loader. This is currently supported");
887 LOG_WARNING(
"only with ST-Link and CMSIS-DAP. If you have issues, add");
888 LOG_WARNING(
"\"set WORKAREASIZE 0\" before sourcing nrf51.cfg to disable it");
900 if (buffer_size <= 256) {
904 LOG_WARNING(
"No large enough working area available, can't do block memory writes");
954 for (offset = start; offset < end; offset += chip->
code_page_size) {
957 LOG_ERROR(
"Invalid sector @ 0x%08"PRIx32, offset);
962 LOG_ERROR(
"Can't erase protected sector @ 0x%08"PRIx32, offset);
989 LOG_ERROR(
"Failed to write to nrf51 flash");
1003 for (
int s = first; s <= last && res ==
ERROR_OK; s++)
1011 const uint8_t *
buffer, uint32_t offset, uint32_t
count)
1023 LOG_DEBUG(
"Padding write from 0x%08"PRIx32
"-0x%08"PRIx32
" as 0x%08"PRIx32
"-0x%08"PRIx32,
1024 offset, offset+count, first_page_offset, last_page_offset);
1026 uint32_t page_cnt = last_page - first_page;
1030 uint32_t pre = offset - first_page_offset;
1042 memcpy(buffer_to_flash+pre, buffer, count);
1045 uint32_t post = last_page_offset - (offset+
count);
1052 buffer_to_flash+pre+count);
1057 return nrf51_write_pages(bank, first_page_offset, last_page_offset, buffer_to_flash);
1062 const uint8_t *
buffer, uint32_t offset, uint32_t
count)
1090 memcpy(&uicr[offset], buffer, count);
1120 switch (bank->
base) {
1134 chip = calloc(1,
sizeof(*chip));
1141 switch (bank->
base) {
1166 assert(bank !=
NULL);
1179 LOG_ERROR(
"Couldn't read PPFC register");
1183 if ((ppfc & 0xFF) == 0x00) {
1184 LOG_ERROR(
"Code region 0 size was pre-programmed at the factory, " 1185 "mass erase command won't work.");
1201 LOG_ERROR(
"Failed to check chip's write protection");
1225 const uint32_t address;
1269 for (
size_t i = 0; i <
ARRAY_SIZE(ficr); i++) {
1273 LOG_ERROR(
"Couldn't read %" PRIx32, ficr[i].address);
1278 for (
size_t i = 0; i <
ARRAY_SIZE(uicr); i++) {
1282 LOG_ERROR(
"Couldn't read %" PRIx32, uicr[i].address);
1287 snprintf(buf, buf_size,
1288 "\n[factory information control block]\n\n" 1289 "code page size: %"PRIu32
"B\n" 1290 "code memory size: %"PRIu32
"kB\n" 1291 "code region 0 size: %"PRIu32
"kB\n" 1292 "pre-programmed code: %s\n" 1293 "number of ram blocks: %"PRIu32
"\n" 1294 "ram block 0 size: %"PRIu32
"B\n" 1295 "ram block 1 size: %"PRIu32
"B\n" 1296 "ram block 2 size: %"PRIu32
"B\n" 1297 "ram block 3 size: %"PRIu32
"B\n" 1298 "config id: %" PRIx32
"\n" 1299 "device id: 0x%"PRIx32
"%08"PRIx32
"\n" 1300 "encryption root: 0x%08"PRIx32
"%08"PRIx32
"%08"PRIx32
"%08"PRIx32
"\n" 1301 "identity root: 0x%08"PRIx32
"%08"PRIx32
"%08"PRIx32
"%08"PRIx32
"\n" 1302 "device address type: 0x%"PRIx32
"\n" 1303 "device address: 0x%"PRIx32
"%08"PRIx32
"\n" 1304 "override enable: %"PRIx32
"\n" 1305 "NRF_1MBIT values: %"PRIx32
" %"PRIx32
" %"PRIx32
" %"PRIx32
" %"PRIx32
"\n" 1306 "BLE_1MBIT values: %"PRIx32
" %"PRIx32
" %"PRIx32
" %"PRIx32
" %"PRIx32
"\n" 1307 "\n[user information control block]\n\n" 1308 "code region 0 size: %"PRIu32
"kB\n" 1309 "read back protection configuration: %"PRIx32
"\n" 1310 "reset value for XTALFREQ: %"PRIx32
"\n" 1311 "firmware id: 0x%04"PRIx32,
1313 (ficr[1].value * ficr[0].value) / 1024,
1314 (ficr[2].value == 0xFFFFFFFF) ? 0 : ficr[2].value / 1024,
1315 ((ficr[3].value & 0xFF) == 0x00) ?
"present" :
"not present",
1318 (ficr[6].value == 0xFFFFFFFF) ? 0 : ficr[6].value,
1319 (ficr[7].value == 0xFFFFFFFF) ? 0 : ficr[7].value,
1320 (ficr[8].value == 0xFFFFFFFF) ? 0 : ficr[8].value,
1322 ficr[10].value, ficr[11].value,
1323 ficr[12].value, ficr[13].value, ficr[14].value, ficr[15].value,
1324 ficr[16].value, ficr[17].value, ficr[18].value, ficr[19].value,
1326 ficr[21].value, ficr[22].value,
1328 ficr[24].value, ficr[25].value, ficr[26].value, ficr[27].value, ficr[28].value,
1329 ficr[29].value, ficr[30].value, ficr[31].value, ficr[32].value, ficr[33].value,
1330 (uicr[0].value == 0xFFFFFFFF) ? 0 : uicr[0].value / 1024,
1331 uicr[1].value & 0xFFFF,
1332 uicr[2].value & 0xFF,
1333 uicr[3].value & 0xFFFF);
1340 .
name =
"mass_erase",
1341 .handler = nrf51_handle_mass_erase_command,
1343 .help =
"Erase all flash contents of the chip.",
1352 .help =
"nrf51 flash command group",
1362 .flash_bank_command = nrf51_flash_bank_command,
static int nrf51_get_probed_chip_if_halted(struct flash_bank *bank, struct nrf51_info **chip)
static int nrf51_code_flash_write(struct flash_bank *bank, struct nrf51_info *chip, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int nrf51_erase(struct flash_bank *bank, int first, int last)
#define NRF51_NVMC_REG(offset)
static struct flash_sector * nrf51_find_sector_by_address(struct flash_bank *bank, uint32_t address)
#define LOG_DEBUG(expr...)
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
uint32_t offset
Bus offset from start of the flash chip (in bytes).
uint32_t size
The size of this chip bank, in bytes.
static int nrf51_uicr_flash_write(struct flash_bank *bank, struct nrf51_info *chip, const uint8_t *buffer, uint32_t offset, uint32_t count)
int target_write_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
static const uint8_t nrf51_flash_write_code[]
#define LOG_INFO(expr...)
static int nrf51_wait_for_nvmc(struct nrf51_info *chip)
#define ERROR_TARGET_NOT_HALTED
uint32_t size
Number of bytes in this flash sector.
static int nrf51_nvmc_write_enable(struct nrf51_info *chip)
void * driver_priv
Private driver storage pointer.
static int nrf51_erase_page(struct flash_bank *bank, struct nrf51_info *chip, struct flash_sector *sector)
#define ERROR_FLASH_SECTOR_INVALID
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
static int nrf51_ll_flash_write(struct nrf51_info *chip, uint32_t offset, const uint8_t *buffer, uint32_t bytes)
static int nrf51_protect_check(struct flash_bank *bank)
void init_reg_param(struct reg_param *param, char *reg_name, uint32_t size, enum param_direction direction)
static int nrf51_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
static int nrf51_probe(struct flash_bank *bank)
#define NRF51_UICR_REG(offset)
int target_free_working_area(struct target *target, struct working_area *area)
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
#define NRF51_FICR_REG(offset)
static int nrf51_auto_probe(struct flash_bank *bank)
int target_read_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
static int nrf51_info(struct flash_bank *bank, char *buf, int buf_size)
#define ERROR_FLASH_BANK_NOT_PROBED
int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
FLASH_BANK_COMMAND_HANDLER(nrf51_flash_bank_command)
static int nrf51_write_pages(struct flash_bank *bank, uint32_t start, uint32_t end, const uint8_t *buffer)
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
#define ARMV7M_COMMON_MAGIC
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
#define LOG_ERROR(expr...)
static const struct command_registration nrf51_exec_command_handlers[]
static int nrf51_nvmc_erase_enable(struct nrf51_info *chip)
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
struct target * get_current_target(struct command_context *cmd_ctx)
struct nrf51_info::@9 bank[2]
struct target * target
Target to which this bank belongs.
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
static const struct command_registration nrf51_command_handlers[]
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
int target_run_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Executes a target-specific native code algorithm in the target.
static int nrf51_bank_is_probed(struct flash_bank *bank)
int get_flash_bank_by_addr(struct target *target, uint32_t addr, bool check, struct flash_bank **result_bank)
Returns the flash bank located at a specified address.
int num_sectors
The number of sectors on this chip.
Provides details of a flash bank, available either on-chip or through a major interface.
static int nrf51_erase_all(struct nrf51_info *chip)
static int nrf51_nvmc_read_only(struct nrf51_info *chip)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
void destroy_reg_param(struct reg_param *param)
int target_write_u32(struct target *target, uint32_t address, uint32_t value)
void alive_sleep(uint64_t ms)
COMMAND_HANDLER(nrf51_handle_mass_erase_command)
struct flash_driver nrf51_flash
static int nrf51_nvmc_generic_erase(struct nrf51_info *chip, uint32_t erase_register, uint32_t erase_value)
static const struct nrf51_device_spec nrf51_known_devices_table[]
int(* write)(struct flash_bank *bank, struct nrf51_info *chip, const uint8_t *buffer, uint32_t offset, uint32_t count)
int bank_number
The 'bank' (or chip number) of this instance.
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown...
Describes the geometry and status of a single flash sector within a flash bank.
static int nrf51_protect(struct flash_bank *bank, int set, int first, int last)
#define LOG_WARNING(expr...)
unsigned int flash_size_kb
uint32_t base
The base address of this bank.