This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

pstorage reading other values than writing

Hi!

Maybe I'm doing something wrong (very possible), but for example, when I want to store array of 3 uint8_t values I use:

uint8_t dataToStore[3];
dataToStore[0] = 11;
dataToStore[1] = 22;
dataToStore[2] = 33;

retval = pstorage_store(&block_handle, dataToStore, 3 , 0);
	if (retval == NRF_SUCCESS)
	{
			// Store successfully requested. Wait for operation result.
	}
	else
	{
			// Failed to request store, take corrective action.
	}

and when I want to read it from the memory to variable I use:

   retval = pstorage_block_identifier_get(&persistant_memory_handle, 0, &block_handle);
	if (retval == NRF_SUCCESS)
	{
		uint8_t           dest_data[3];
		 //Request to read 3 bytes from block at an offset of 0 bytes.
		retval = pstorage_load(dest_data, &block_handle, 3, 0);
		if (retval == NRF_SUCCESS)
		{
                         // here dest_data should be: 11, 22, 33 but it gets different values.
		}
		else
		{
				// Failed to load, take corrective action.
		}

Received value is always different than stored. What can be the reason?

Parents
  • Ok, I'd changed it to:

        retval = pstorage_load(dest_data, &block_handle, 4, 0);
    

    and

        retval = pstorage_store(&block_handle, dataToStore, 4 , 0);
    

    but now I get retval = NRF_ERROR_INVALID_ADDR when storing value.

    I think that I don't exactly understand how to specify size and offset. Can you explain it a little bit?

    Thanks!

    Variables

    static 	uint8_t receivedData[4] __attribute__((aligned(4)));
    static pstorage_handle_t persistant_memory_handle;
    static uint32_t retval;
    static pstorage_handle_t block_handle;
    

    Pstorage initialization

    static void init_memory(void)
    {
    uint32_t retval;
    pstorage_module_param_t param;
    	
    retval = pstorage_init();
    if(retval == NRF_SUCCESS)
    {
    	
    }
    else 
    {
    }
    
    param.block_size = 0x10;
    param.block_count = 10;
    param.cb = storage_cb_handler;
    
    retval = pstorage_register(&param, &persistant_memory_handle);
    if(retval == NRF_SUCCESS)
    {
    	
    }
    else 
    {
    }
    
    }
    

    Event Notification Handler

    static void storage_cb_handler(pstorage_handle_t  * handle,
                               uint8_t              op_code,
                               uint32_t             result,
                               uint8_t            * p_data,
                               uint32_t             data_len)
    
    {
    switch(op_code)
    {
       case PSTORAGE_LOAD_OP_CODE:
           if (result == NRF_SUCCESS)
           {
               // Store operation successful.
           }
           else
           {
               // Store operation failed.
           }
           // Source memory can now be reused or freed.
           break;       
        case PSTORAGE_UPDATE_OP_CODE:
           if (result == NRF_SUCCESS)
           {
               // Update operation successful.
           }
           else
           {
               // Update operation failed.
           }
           break;
       case PSTORAGE_CLEAR_OP_CODE:
           if (result == NRF_SUCCESS)
           {
               // Clear operation successful.
           }
           else
           {
               // Clear operation failed.
           }
           break;
    
    	}
    }
    

    Writing data

    retval = pstorage_store(&block_handle, receivedData, 4 , 0);
    	if (retval == NRF_SUCCESS)
    	{
    			// Store successfully requested. Wait for operation result.
    	}
    	else
    	{
    			// Failed to request store, take corrective action.
    	}
    

    Reading data

    	retval = pstorage_block_identifier_get(&persistant_memory_handle, 0, &block_handle);
    	if (retval == NRF_SUCCESS)
    	{
    		uint8_t           dest_data[4];
    		 //Request to read 4 bytes from block at an offset of 12 bytes.
    		retval = pstorage_load(dest_data, &block_handle, 4, 0);
    		if (retval == NRF_SUCCESS)
    		{
    				 parse_data(dest_data);
    		}
    		else
    		{
    				// Failed to load, take corrective action.
    		}
    	}
    	else
    	{
    			// Failed to get block id, take corrective action.
    	}
    
Reply
  • Ok, I'd changed it to:

        retval = pstorage_load(dest_data, &block_handle, 4, 0);
    

    and

        retval = pstorage_store(&block_handle, dataToStore, 4 , 0);
    

    but now I get retval = NRF_ERROR_INVALID_ADDR when storing value.

    I think that I don't exactly understand how to specify size and offset. Can you explain it a little bit?

    Thanks!

    Variables

    static 	uint8_t receivedData[4] __attribute__((aligned(4)));
    static pstorage_handle_t persistant_memory_handle;
    static uint32_t retval;
    static pstorage_handle_t block_handle;
    

    Pstorage initialization

    static void init_memory(void)
    {
    uint32_t retval;
    pstorage_module_param_t param;
    	
    retval = pstorage_init();
    if(retval == NRF_SUCCESS)
    {
    	
    }
    else 
    {
    }
    
    param.block_size = 0x10;
    param.block_count = 10;
    param.cb = storage_cb_handler;
    
    retval = pstorage_register(&param, &persistant_memory_handle);
    if(retval == NRF_SUCCESS)
    {
    	
    }
    else 
    {
    }
    
    }
    

    Event Notification Handler

    static void storage_cb_handler(pstorage_handle_t  * handle,
                               uint8_t              op_code,
                               uint32_t             result,
                               uint8_t            * p_data,
                               uint32_t             data_len)
    
    {
    switch(op_code)
    {
       case PSTORAGE_LOAD_OP_CODE:
           if (result == NRF_SUCCESS)
           {
               // Store operation successful.
           }
           else
           {
               // Store operation failed.
           }
           // Source memory can now be reused or freed.
           break;       
        case PSTORAGE_UPDATE_OP_CODE:
           if (result == NRF_SUCCESS)
           {
               // Update operation successful.
           }
           else
           {
               // Update operation failed.
           }
           break;
       case PSTORAGE_CLEAR_OP_CODE:
           if (result == NRF_SUCCESS)
           {
               // Clear operation successful.
           }
           else
           {
               // Clear operation failed.
           }
           break;
    
    	}
    }
    

    Writing data

    retval = pstorage_store(&block_handle, receivedData, 4 , 0);
    	if (retval == NRF_SUCCESS)
    	{
    			// Store successfully requested. Wait for operation result.
    	}
    	else
    	{
    			// Failed to request store, take corrective action.
    	}
    

    Reading data

    	retval = pstorage_block_identifier_get(&persistant_memory_handle, 0, &block_handle);
    	if (retval == NRF_SUCCESS)
    	{
    		uint8_t           dest_data[4];
    		 //Request to read 4 bytes from block at an offset of 12 bytes.
    		retval = pstorage_load(dest_data, &block_handle, 4, 0);
    		if (retval == NRF_SUCCESS)
    		{
    				 parse_data(dest_data);
    		}
    		else
    		{
    				// Failed to load, take corrective action.
    		}
    	}
    	else
    	{
    			// Failed to get block id, take corrective action.
    	}
    
Children
  • You need to call pstorage_block_identifier_get before calling pstorage_store.

  • It was called before (app starts from reading values, store is performed only when configuration is changed), but now I added it directly before:

    	retval = pstorage_block_identifier_get(&persistant_memory_handle, 0, &block_handle);
    	if (retval == NRF_SUCCESS)
    	{
    		retval = pstorage_store(&block_handle, receivedData, 4 , 0);
    		if (retval == NRF_SUCCESS)
    		{
    				// Store successfully requested. Wait for operation result.
    		}
    		else
    		{
    				// Failed to request store, take corrective action.
    		}
    	}
    	else
    	{
    			// Failed to get block id, take corrective action.
    	}
    

    but still read value is 0x00,0x00,0x00,0x00

  • Ok, so I should perform pstorage_clear(&base_handle, 16) before writing? But now I assume that pstorage_write should be placed in event handler (I should also store somewhere identifier of "write position", to support multiple variables storage) or can I put it in if(retval == NRF_SUCCESS) { } clause?

    Or maybe using pstorage_update should solve this problem?

  • Hi nikita, could you explain please, whats the difference between calling "pstorage_store" and "pstorage_update" on a already used block?

  • You can read it here: devzone.nordicsemi.com/.../a00018.html In short pstorage_store is to write data only in previously cleared place in flash, but pstorage_update can update only part of the data in all allocated flash memory, it's reading all content of this memory in buffer, then updates needed data in bufer with new values, then clears all allocated flash memory and store all bufer data to the flash.

Related