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

Writing to flash.

Hello,

I want to store some values to flash. What is the best method to store data to flash?

I'm trying to get pstorage working but i have no luck, so i'm wondering if there are any other ways...

my code for pstorage:

static void example_cb_handler(pstorage_handle_t  * handle,
														 uint8_t              op_code,
                           uint32_t             result,
                           uint8_t            * p_data,
                           uint32_t             data_len)
{
		if(handle->block_id == pstorage_wait_handle) { pstorage_wait_flag = 0; }  //If we are waiting for this callback, clear the wait flag.
	
			switch(op_code)
		{
		case PSTORAGE_LOAD_OP_CODE:
			 if (result == NRF_SUCCESS)
			 {		
					LOG_DEBUG("LOAD");
			 }
			 else
			 {
				 LOG_DEBUG("LOAD error");
			 }
			 break;
		case PSTORAGE_STORE_OP_CODE:
			 if (result == NRF_SUCCESS)
			 {
				 LOG_DEBUG("STORE");
			 }
			 else
			 {
				 LOG_DEBUG("STORE error");
			 }
			 break;				 
		case PSTORAGE_UPDATE_OP_CODE:
			 if (result == NRF_SUCCESS)
			 {
			 }
			 else
			 {
			 }
			 break;
		case PSTORAGE_CLEAR_OP_CODE:
			 if (result == NRF_SUCCESS)
			 {
			 }
			 else
			 {
			 }
	

		 break;
			case PSTORAGE_ERROR_OP_CODE:
				 break;				 
		}
}

pstorage_module_param_t param;
pstorage_handle_t       handle;
pstorage_handle_t				block_0_handle;
uint8_t							dest_data[5];
static uint8_t block0[4]  __attribute__((aligned(4)));


static void pstorage_init_register(void)
{
	uint32_t	error_code;
	if(pstorage_init()!=NRF_SUCCESS)
		LOG_DEBUG("PSTORAGE INIT NI USPEL");
	     
	param.block_size  = 16;                 //Select block size of 16 bytes
	param.block_count = 1;                   //Select 10 blocks, total of 160 bytes


	param.cb          = example_cb_handler;   //Set the pstorage callback handler
			
		if((error_code=pstorage_register(&param, &handle))!=NRF_SUCCESS)
			LOG_DEBUG("PSTORAGE REGISTER NI USPEL. ERROR_CODE: %x",error_code);
		else
			LOG_DEBUG("PSTORAGE INIT IN REGISTER USPEL");
}

static void pstorage_store_data(uint8_t* source_data, int i)
{
	uint32_t	error_code;
	pstorage_block_identifier_get(&handle, 0, &block_0_handle);
	pstorage_clear(&block_0_handle, 16);
	if(error_code!=NRF_SUCCESS)
		LOG_DEBUG("IDENTETY ERROR CODE store: %x",error_code);
	error_code=pstorage_store(&block_0_handle, source_data, 4, 0); 
	if(error_code!=NRF_SUCCESS)
		LOG_DEBUG("PSTORAGE LOAD ERROR CODE: %x",error_code);
	LOG_DEBUG("ERROR CODE %x",error_code);
	
}

static void pstorage_read_data(void)
{
	uint32_t error_code;
	error_code=pstorage_block_identifier_get(&handle, 0, &block_0_handle);
	if(error_code!=NRF_SUCCESS)
		LOG_DEBUG("IDENTETY ERROR CODE read: %x",error_code);
	error_code=pstorage_load(dest_data, &block_0_handle, 4, 0);
	if(error_code!=NRF_SUCCESS)
		LOG_DEBUG("PSTORAGE LOAD ERROR CODE: %x",error_code);
	LOG_DEBUG("DEST_DATA: %x",*dest_data);
}

The example_cb_handler only seems to get called when i call pstorage_read (and even then, the returned values are only 0xFF). If i call pstorage_store, the example_cb_handler doesn't get called.

What could cause this problem?

regards.

Related