My problem is similar to this
except for one major difference. I am using only SoftDevice and I disable SoftDevice before I do the writes. I follow the requirements by writing in small hunks. The code that does the write is as follows
// Now we have to write the data in hunks into flash // Each page is 1024 bytes, and a write is in 4-byte hunks // So we will find the number of 1024 byte pages to write, // and then the number of 4-byte hunks left over. // Disable soft device so we don't have to deal with events sd_softdevice_disable(); // Buffer to write to flash. Need to write it in four-byte hunks uint32_t *ptr32 = (uint32_t *)keysDataBuffer; while (true) { // Where to write addr = (uint32_t *)(pg_size * pg_num); // Erase page: while(true) { err_code = sd_flash_page_erase(pg_num); if (err_code == NRF_SUCCESS) { break; } if (err_code != NRF_ERROR_BUSY) { NRF_LOG_DEBUG("Erasing data returned error %u.", err_code); APP_ERROR_CHECK(err_code); } } i = (size >= pg_size) ? (pg_size >> 2) : (size >> 2); // four-byte hunks to write; size is evenly divisible by four while(true) { err_code = sd_flash_write(addr, ptr32, i); if (err_code == NRF_SUCCESS) { break; } if (err_code != NRF_ERROR_BUSY) { NRF_LOG_DEBUG("Writing data returned error %u.", err_code); APP_ERROR_CHECK(err_code); } } size = size - pg_size; // Subtract a page size from the total size if (size <= 0) // if zero or less, all data has been written { break; } pg_num++; ptr32 = (uint32_t *)(keysDataBuffer + pg_size); } free(keysDataBuffer); NRF_LOG_DEBUG("Flash written");
The code works fine when running on the nRF52840 DK. What do I need to do to write to flash on the dongle? Clearly I cannot use NRF_LOG statements so I have used LED buttons to see how far I get and it does in the while loop.