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

Persistent storage manager writing problem for one block

Hello Everyone ,

I want to use pstorage for my project.i get the example code from here devzone.nordicsemi.com/.../ and its working properly(i.e read and write operation). but when i modified it for my requirement as given below its not to read the data what i have write .what mistake i am doing.

//Get block identifiers
 pstorage_block_identifier_get(&handle, 0, &block_0_handle);
 pstorage_clear(&block_0_handle, 32);                       //Clear 32 bytes
 pstorage_wait_handle = block_0_handle.block_id;      //Specify which pstorage handle to wait for
 pstorage_wait_flag = 1;                                    //Set the wait flag. Cleared in the example_cb_handler
//Store data to block one. Wait for the last store operation to finish before reading out the data.
pstorage_store(&block_0_handle, source_data_0, 16, 0);     //Write to flash, only one block is allowed for each pstorage_store command
 while(pstorage_wait_flag) {
     power_manage(); 
 }              //Sleep until store operation is finished.

 pstorage_wait_handle = block_0_handle.block_id;            //Specify which pstorage handle to wait for
 pstorage_wait_flag = 1;                      //Set the wait flag. Cleared in the example_cb_handler        
 pstorage_load(dest_data_0, &block_0_handle, 16, 0);   //Read from flash, only one block is allowed for each pstorage_load command
 while(pstorage_wait_flag) { power_manage(); }              //Sleep until store operation is finished.

        if(dest_data_0[1] == source_data_0[1])
        {
          nrf_gpio_pin_clear(LED_3);
        }

while debugging in keil, if i put the breakpoint on the the pstorage_load(dest_data_0, &block_0_handle, 16, 0); line and then at if(dest_data_0[1] == source_data_0[1]) the read value is same as what is written.

        but when i put breakpoint directly on
        
        if(dest_data_0[1] == source_data_0[1])
        the read value is 0xFF.

Why it is so.
Please let me know what mistake i am doing...

Thanks & Regards,
Parents
  • Hi (updated post. I replaced my earlier answer with the following)

    It looks like both the pstorage_clear and the pstorage_store are queued. You should wait for each to complete before proceeding to the next since they are working on the same block. That is, do:

        pstorage_block_identifier_get(&handle, 0, &block_0_handle);
    
        pstorage_wait_handle = block_0_handle.block_id;        // Specify which pstorage handle to wait for
    
        pstorage_wait_flag = 1;                                // Set the wait flag.
        pstorage_clear(&block_0_handle, 16);                   // Clear 16 bytes
        while(pstorage_wait_flag)  power_manage();             // Wait for clear
    
        pstorage_wait_flag = 1;                                // Set the wait flag. 
        pstorage_store(&block_0_handle, source_data_0, 16, 0); // Write to flash
        while(pstorage_wait_flag) power_manage();              // Wait for store
    
        pstorage_load(dest_data_0, &block_0_handle, 16, 0);    // Read from flash, 
    

    You should probably also change:

    static uint8_t pstorage_wait_flag = 0;
    

    to:

     static volatile uint8_t pstorage_wait_flag = 0;
    

    In your earlier version:

    1. clear was queued
    2. write was queued
    3. you wait for a command on the block to complete. You get an acknowledgement when the CLEAR completes.
    4. you do the load, which loads the cleared values. The write is still in the queue.

    I believe when you used the additional breakpoint the extra delay allowed the write to complete.

Reply
  • Hi (updated post. I replaced my earlier answer with the following)

    It looks like both the pstorage_clear and the pstorage_store are queued. You should wait for each to complete before proceeding to the next since they are working on the same block. That is, do:

        pstorage_block_identifier_get(&handle, 0, &block_0_handle);
    
        pstorage_wait_handle = block_0_handle.block_id;        // Specify which pstorage handle to wait for
    
        pstorage_wait_flag = 1;                                // Set the wait flag.
        pstorage_clear(&block_0_handle, 16);                   // Clear 16 bytes
        while(pstorage_wait_flag)  power_manage();             // Wait for clear
    
        pstorage_wait_flag = 1;                                // Set the wait flag. 
        pstorage_store(&block_0_handle, source_data_0, 16, 0); // Write to flash
        while(pstorage_wait_flag) power_manage();              // Wait for store
    
        pstorage_load(dest_data_0, &block_0_handle, 16, 0);    // Read from flash, 
    

    You should probably also change:

    static uint8_t pstorage_wait_flag = 0;
    

    to:

     static volatile uint8_t pstorage_wait_flag = 0;
    

    In your earlier version:

    1. clear was queued
    2. write was queued
    3. you wait for a command on the block to complete. You get an acknowledgement when the CLEAR completes.
    4. you do the load, which loads the cleared values. The write is still in the queue.

    I believe when you used the additional breakpoint the extra delay allowed the write to complete.

Children
Related