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

write to flash memory

Please help with writing to flash. If SoftDevice is off all ok. If SoftDevice is enabled, the sd_evt_get function always returns the value 5.

Here is the test code:

void FlashTestWrite(uint8_t TestData[4])
{
	if(nrfx_nvmc_flash_page_size_get() != 0x1000)
		return;
	
	if(nrf_sdh_is_enabled())
	{
		uint32_t p_event;
		ret_code_t err_code;
		bool flash_busy = true;
		
		err_code = sd_flash_page_erase((0x27000 / 0x1000));
		APP_ERROR_CHECK(err_code);
		
		while(flash_busy)
		{
			err_code = sd_evt_get(&p_event);
			
			if(err_code == NRF_SUCCESS)
			{
				if(p_event == NRF_EVT_FLASH_OPERATION_SUCCESS)
					flash_busy = false;
				
				if(p_event == NRF_EVT_FLASH_OPERATION_ERROR)
					return;
			}
		}
		
		uint32_t test_data[1];
		test_data[0] = (uint32_t)(TestData[3] << 24) + (uint32_t)(TestData[2] << 16) + (uint32_t)(TestData[1] << 8) + (uint32_t)TestData[0];
		
		flash_busy = true;
		
		err_code = sd_flash_write((uint32_t*)0x27000, test_data, 1);
		APP_ERROR_CHECK(err_code);
		
		while(flash_busy)
		{
			err_code = sd_evt_get(&p_event);
			
			if(err_code == NRF_SUCCESS)
			{
				if(p_event == NRF_EVT_FLASH_OPERATION_SUCCESS)
					flash_busy = false;
				
				if(p_event == NRF_EVT_FLASH_OPERATION_ERROR)
					return;
			}
		}
	}
	else
	{
		nrfx_nvmc_page_erase(0x27000);
		nrfx_nvmc_bytes_write(0x27000, TestData, 4);
		
		do
		{
			nrf_delay_ms(1);
		}while(nrfx_nvmc_write_done_check() != true);
	}
}

  • Hi jca, 

    When sd_flash_page_erase() returns NRF_SUCCESS, that only means that the erase request was successfully submitted to the SoftDevice. It does _not_ mean that the erase operation has actually been completed yet.

    When the erase operation completes, the SoftDevice will generate an event interrupt, and you must call sd_evt_get() to consume it. The event code will be NRF_EVT_FLASH_OPERATION_SUCCESS if the flash erase completes successfully, or NRF_EVT_FLASH_OPERATION_ERROR if it doesn't. You must wait until this event is generated and consume it with sd_evt_get() in order to know that the flash sector has actually been erased. This post might help.

    You also could take a look at the flash_fstorage example and the fstorage_evt_handler function (line 169).

    Please let me know what SDK version you use if you don't how to retrieve this event.

    -Amanda H.

Related