This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Don't understand page numbering for sd_flash_write and sd_flash_erase

I want to use the last page of codespace for storing a single 32 bit word of non volatile data. I am failing so far and I think I just don't understand the page numbering scheme the soft device API is using.

So the page number I think I want to use for app data is: NRF_FICR->CODESIZE -1.

I have a simple function where I want to store three bytes in flash by stuffing them in a 32 bit word:

uint32_t write_cal_data(uint8_t I680, uint8_t I850, uint8_t I970)
{
	uint32_t retval;
	retval = sd_flash_page_erase(get_app_data_pagenum());
	if (retval != NRF_SUCCESS)
		return retval;
	uint32_t * p_dest = (uint32_t *)get_app_data_base_addr();
	uint32_t combined_currents = (I680 << 16) | (I850 << 8) | I970;
	retval = sd_flash_write(p_dest, &combined_currents, 1);
	return retval;
}

That code completes with all the return values equal to NRF_SUCCESS.

But when I try to access the stored data, I dereference address (NRF_FICR->CODEPAGESIZE * (NRF_FICR->CODESIZE - 1));, I do not get the data I previously stored.

If I write a 32 bit word to page number "NRF_FICR->CODESIZE -1", am I correct to then access that word at the address above? What am I missing? Thanks, David

Related