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

pstorage_store problem

Hi, I'm using nrf51822 board with SDK 10 and S110 softdevice. My application is based on uart over ble. It take 20 bytes data from a android device using nus, store that data and send a reply to the android that data is stored. First time when I store some data using pstorage_store(), It is stored successfully. Now second time I want to clear that data and store some new in place of that, so I first call pstorage_clear() and then again pstorage_store(). Now the problem arrived here, second time first 4 bytes becomes 0x0 and then remaining is my data. for example if my data is 12345678, it becomes like 00005678. I dont know what is going here. I'm using pstorage function like this and my function is defined in a pstorage_lock.c which is attached here. Please help me in this.

if(!pstorage_erase(0, 32))
{
	if(!pstorage_write(my_data, 0, 32))
	{
	 // some operation to do here
	}
}

my_data is an 20 bytes array in which data comes from android using nus.

Thanks. Amit

pstorage_lock.c

  • Could you edit your question and include some description of how you are using pstorage, or maybe some code?

  • I edited my question and attached some code with it. please review this and tell me what is the problem.

  • Hi Amit,

    Thank you for providing more information! That made it much easier to see what you are doing and what may solve the issues.

    Putting in an nrf_delay_ms(100) is not enough to be sure the clear operation in pstorage_erase() completes successfully. Rather, you must wait for the event callback and check the result of the operation (i.e. that result is NRF_SUCCESS in the PSTORAGE_CLEAR_OP_CODE case. of pstorage_cb_handler())

    The same goes for your other pstorage functions.

    I see that you have commented out a previous attempt using pstorage_wait_flag to signal that the operation is finished. You should do two more things in order for this to work: First, you should also signal whether the operation was successful, and handle the "unsuccessful" case. Second, you should make sure that pstorage_wait_flag is declared using the volatile keyword, to ensure that it is read from and written to memory each time it is used. If not declared volatile the value may be stored only in CPU registers, and in the worst case it is optimised away entirely.

    Regards, Terje

  • I commented pstorage_wait_flag because I did not receive the callback.

  • Here is some part of my application. I'm using nrF Uart App in android side. First I connect to the board then send some data from the android to the board. I received that data in nus_data_handler(), then to store this data, I just passed the data in StoreData() function from nus_data_handler(). I received NRF_SUCCESS from pstorage_erase() as well as from pstorage_write(), but on seeking memory I found starting 4 bytes becomes zero. I edited a line of pstorage.c which is m_next_page_addr, so that I know where the data is stored in memory.

    uint32_t pstorage_init(void){
    cmd_queue_init();
    
    m_next_app_instance = 0;
    **m_next_page_addr    = 0x0003e0c0;//PSTORAGE_DATA_START_ADDR;**
    m_current_page_id   = 0;
    
    for (uint32_t index = 0; index < PSTORAGE_NUM_OF_PAGES; index++)
    {
        m_app_table[index].cb           = NULL;
        m_app_table[index].block_size   = 0;
        m_app_table[index].block_count  = 0;
    }
    

    I did not receive any callback from pstorage_cb_handler that's why I just commented out pstorage_wait_flag in pstorage_lock.c and put a nrf_delay_ms(100) as asked by Terje Schjelderup.

    please go through this project and tell me what is missing in the code.

    Thanks.

    ble_app_uart.rar

Related