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

pstorage block size

What exactly is block size in pstorage?

Desired block size for persistent memory storage, for example, if a module has a table with 10 entries, each entry is size 64 bytes, it can request 10 blocks with block size 64 bytes. On the other hand, the module can also request one block of size 640 based on how it would like to access or alter memory in persistent memory. First option is preferred when single entries that need to be updated often when having no impact on the other entries. While second option is preferred when entries of table are not changed on individually but have common point of loading and storing data.

Does that mean if I set block size to 10, I get blocks of 64 bytes, and if block size is 1 I get 640? This doesn't make much sense. Is block size instead the number of bytes in a single block? Or is it words per block?

Is block size somehow tied to block count? Or is block count the number of blocks I get of size block_size?

Thanks

  • The total allocated space by the pstorage is set by: block_size * block_count = total flash space

    for instance:

    static pstorage_handle_t m_p_example_id;
    ...
    static void example_pstorage_init()
    {
    	uint32_t err_code;         
    	pstorage_module_param_t p_example_param;	
    	
    	// Setup pstorage with 60blocks of 16byte in each.
    	p_example_param.block_size  = 0x10; // recommended to be >= 4 byte
    	p_example_param.block_count = 60;
    	p_example_param.cb          = example_cb_handler;
    
    	err_code = pstorage_init();
    	APP_ERROR_CHECK(err_code);  
    	
    	err_code = pstorage_register (&p_example_param, &m_p_example_id);
    	APP_ERROR_CHECK(err_code);  
    }
    

    This will allocate 960 bytes in flash, divided into 60 blocks of 16 bytes each. The minimum block-size is set by define PSTORAGE_MIN_BLOCK_SIZE in pstorage_platform.h

    To store data into one of these blocks, you can do like this:

    // Init pstorage for application storage
    example_pstorage_init();
    
    // Store some data to first block
    {
    	uint32_t err_code;         
    	
    	uint16_t dummy_block = 0;
        // Must be static as the pstorage module processes the queue asynchronously
    	static uint8_t dummy_data[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    	uint16_t dummy_size = 16;
    	uint16_t dummy_offset = 0;
    	pstorage_handle_t p_block_id;
    	
    	err_code = pstorage_block_identifier_get(&m_p_example_id, dummy_block, &p_block_id);
    	APP_ERROR_CHECK(err_code);
    	
    	err_code = pstorage_store(&p_block_id, dummy_data, dummy_size, dummy_offset);
    	APP_ERROR_CHECK(err_code);
    }
    

    The pstorage_block_identifier_get will specify which instance/ID is trying to access it's chunk of data (ID is written into m_p_example_id, similar to what app_timer/app_gpiote also does) and set p_block_id to the right address corresponding to your block.

    Then you store the data using pstorage_store(...).

    Since this module uses the softdevice flash API, the events will be processed through the function given into "softdevice_sys_evt_handler_set()".

    You can read back the data using this function (after you've gotten the event in the above handler):

    err_code = pstorage_load(my_buffer, &p_block_id, dummy_size, dummy_offset);
    
  • @Håkon Alseth : Your example is not working because the data to store "dummy_data" are defined in a context potentially no longer available when the pstorage queue (asynchronous) will process the task .

  • That's true! thanks for pointing that out. Will fix that ASAP.

  • Hi,Håkon Alseth! I want to ask you how do you creat and initialize your example_cb_hanlder? Because I did as what you said and return a NRF_ERROR_NULL when running NULL_PARAM_CHECK(p_module_param->cb);

  • Here is the signature of the handler :

    void pstorage_cb_handler(pstorage_handle_t * handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len)

Related