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 page size

I try to optmize my code so it can run on the 128k chip. My issue is with pstorage: I have 7 variables (from 4 bytes to 20 bytes) that I store in individual blocks. But it seems that on a page, there is only one block ? So for each block, I need PSTORAGE_FLASH_PAGE_SIZE. if PSTORAGE_FLASH_PAGE_SIZE = 1024, I need 7k to store my 7 variables ?

So I try to reduce PSTORAGE_FLASH_PAGE_SIZE, but I have strange behaviour when I do this. Should it be possible to have PSTORAGE_FLASH_PAGE_SIZE to 20 bytes ?

Parents
  • PSTORAGE_FLASH_PAGE_SIZE is the actual size of one page in flash, so you shouldn't change that.

    You should be able to use a single flash page for this.

    It is recommended that the block size is a multiple of the flash word size (4B), so if your largest variable is 20B(5*4B), you can use that as block size. It is also recommended that the block size is a divisor of the flash page size (1024B), that flash page size % block size = 0. This doesn't matter if you use less than 1 page of flash.

    I'm assuming you are developing on one of our S110 BLE projects. Have you increased PSTORAGE_MAX_APPLICATIONS to 2? Your application + the device manager. This also increases the number of flash pages available to pstorage to 2 (actually 3, if you include the swap page). One for your application, one for the device manager.

    Some variables:

    static pstorage_handle_t        m_store_variables_base_handle;
    static pstorage_handle_t        m_store_variables_curr_handle;
    static int block_number = 0;
    static uint8_t block0[4]  __attribute__((aligned(4)));
    

    You must register your application in the pstorage module with something like this:

    static void store_variables_init(void)
    {
        uint32_t err_code;
        
        pstorage_module_param_t param;
    
        param.block_size  = 20;
        param.block_count = 7;
        param.cb          = store_variables_cb_handler;
    
        err_code = pstorage_register(&param, &m_store_variables_base_handle);
        APP_ERROR_CHECK(err_code);
    }
    

    You must define the pstorage event handler for your application:

    static void store_variables_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_STORE_OP_CODE:
               if (result == NRF_SUCCESS)
               {
                  rdy_for_next = true; // Ready for next store operation
               }
               else
               {
                   // Store operation failed. This must be handled
               }
               break;       
        }
    }
    

    rdy_for_next is a flag that is checked in the main loop:

    // Enter main loop.
    for (;;)
    {
        if(rdy_for_next)
        {
            store_next_block();
        }
        err_code = sd_app_evt_wait();
        APP_ERROR_CHECK(err_code);
    }
    

    The store_next_block() can look like this:

    static void store_next_block(void)
    {
        rdy_for_next = false;
        uint32_t err_code;
    
        err_code = pstorage_block_identifier_get(&m_store_variables_base_handle, block_number, &m_store_variables_curr_handle);
        APP_ERROR_CHECK(err_code);
        err_code = pstorage_store(&m_store_variables_curr_handle, block0, 4,0);
        APP_ERROR_CHECK(err_code);
        block_number++;            
    }
    

    This should get you started, please add a comment if anything is unclear or if anything requires a better explanation.

  • Hi , Can I register for more than 1 page size ??, say i have block size of 16 and I need to reserver 2K memory. So I used count = 128. its returning a 07 error Thanks & Regards, DIbin

Reply Children
No Data
Related