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?

  • I think using pstorage_update is better in your case.

  • 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.

  • Thanks nikita for your fast response. So if i want to implement a "small database" to save small blocks of data, i have to clear always the blocks before storing it, or just update a block? Thus just "overwriting" a block is just possible with update and not with store? (But can i update a block which is clear?)

    1. It is possible to store data with pstorage_store if the place where you want to write is cleared (content 0xFFFFFFFF), you can perform multiple pstorage_store to store your data in different places inside your registered pstorage module, but only once each. If you want to rewrite some values you need to clear all module allocated flash memory with pstorage_clear before you can write new value with pstorage_store. 2. pstorage_clear function can only clear all storage block in allocated module, you can't just clear some of them at a time, so if you want to have database then before rewriting some data in some block you need first to get all data in blocks in buffer, then perform pstorage_clear and then write back buffer with changed values with pstorage_store.
Related