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

How can i write 10kB of data to internal flash?

Hi,

I use nRF51822 with 256k flash and external sensor, and i want to write the data i receive from it via SPI to internal flash?

After the data received i want to send it to iOS application with s110 uart_app.

How can i do it?

P.S. I tried pstorage option but it always give me one page of 0x400 bytes. How to move to next page?

Thanks

intFlash - Copy.c intFlash - Copy.h

Parents
  • Hi,

    Could you help me figure out the bug in the code below.

    I have a code to test writing and loading using pstorage (I use nrf51, SDK11).

    When power on, it will load from flash to check:

    • If data is not existed (=0xFF) it will save the default data to flash
    • Else (data is exist) it will change data then update to flash again. So after each reset, the data loaded must be changed.

    But with my code, the data loaded does not change.

    Help me. Thanks.

    My code:

    	pstorage_handle_t       block_handle;
    	pstorage_module_param_t param;
    	uint8_t					dest_data[4];
    	uint8_t					default_data[4];
    	uint32_t				retval;
    	
    	param.block_size  		= 16;
    	param.block_count 		= 1;
    	param.cb				= app_cb_handler;
    
    	default_data[0] 		= 1;
    	default_data[1] 		= 2;
    	default_data[2] 		= 3;
    	default_data[3] 		= 4;
    	
    	retval = pstorage_init();							// Initialization
    	if(retval == NRF_SUCCESS)
    			printf("\r\nPstorage init success");
    	else
    			printf("\r\nPstorage init failed");
    	retval = pstorage_register(&param, &block_handle);	// Register
    	if(retval == NRF_SUCCESS)
    			printf("\r\nPstorage regis success");
    	else
    			printf("\r\nPstorage regis failed");
    	
    	// Request to read 4 bytes from block at an offset of 0 bytes.
    	retval = pstorage_load(dest_data, &block_handle, 4, 0);
    	if(retval == NRF_SUCCESS)
    			printf("\r\nLoad success");
    	else
    			printf("\r\nLoad failed");
    	if(dest_data[1] == 0xFF)
    	{
    			printf("\r\nData is not exist, save data...");
    			retval = pstorage_clear(&block_handle, 4);
    			if(retval == NRF_SUCCESS)
    					printf("\r\nClear success");
    			else
    					printf("\r\nClear failed");
    			retval = pstorage_store(&block_handle, default_data, 4, 0);
    			if(retval == NRF_SUCCESS)
    					printf("\r\nStore success");
    			else
    					printf("\r\nStore failed");
    	}
    	else
    	{
    			printf("\r\ndata0 = %d", dest_data[0]);
    			printf("\r\ndata1 = %d", dest_data[1]);
    			printf("\r\ndata2 = %d", dest_data[2]);
    			printf("\r\ndata3 = %d", dest_data[3]);
    			dest_data[0] += 1;
    			dest_data[1] += 1;
    			dest_data[2] += 1;
    			dest_data[3] += 1;			
    			retval = pstorage_update(&block_handle, dest_data, 4, 0);
    			if(retval == NRF_SUCCESS)
    					printf("\r\nUpdate success");
    			else
    					printf("\r\nUpdate failed");
    	}
    

    On termial:

    Pstorage init success
    Pstorage regis success
    Load success
    Data is not exist, save data...
    Clear failed
    Store success
    ---Reset---
    Pstorage init success
    Pstorage regis success
    Load success
    data0 = 1
    data1 = 2
    data2 = 3
    data3 = 4
    Update success
    ---Reset---
    Pstorage init success
    Pstorage regis success
    Load success
    data0 = 1
    data1 = 2
    data2 = 3
    data3 = 4
    Update success
    
Reply
  • Hi,

    Could you help me figure out the bug in the code below.

    I have a code to test writing and loading using pstorage (I use nrf51, SDK11).

    When power on, it will load from flash to check:

    • If data is not existed (=0xFF) it will save the default data to flash
    • Else (data is exist) it will change data then update to flash again. So after each reset, the data loaded must be changed.

    But with my code, the data loaded does not change.

    Help me. Thanks.

    My code:

    	pstorage_handle_t       block_handle;
    	pstorage_module_param_t param;
    	uint8_t					dest_data[4];
    	uint8_t					default_data[4];
    	uint32_t				retval;
    	
    	param.block_size  		= 16;
    	param.block_count 		= 1;
    	param.cb				= app_cb_handler;
    
    	default_data[0] 		= 1;
    	default_data[1] 		= 2;
    	default_data[2] 		= 3;
    	default_data[3] 		= 4;
    	
    	retval = pstorage_init();							// Initialization
    	if(retval == NRF_SUCCESS)
    			printf("\r\nPstorage init success");
    	else
    			printf("\r\nPstorage init failed");
    	retval = pstorage_register(&param, &block_handle);	// Register
    	if(retval == NRF_SUCCESS)
    			printf("\r\nPstorage regis success");
    	else
    			printf("\r\nPstorage regis failed");
    	
    	// Request to read 4 bytes from block at an offset of 0 bytes.
    	retval = pstorage_load(dest_data, &block_handle, 4, 0);
    	if(retval == NRF_SUCCESS)
    			printf("\r\nLoad success");
    	else
    			printf("\r\nLoad failed");
    	if(dest_data[1] == 0xFF)
    	{
    			printf("\r\nData is not exist, save data...");
    			retval = pstorage_clear(&block_handle, 4);
    			if(retval == NRF_SUCCESS)
    					printf("\r\nClear success");
    			else
    					printf("\r\nClear failed");
    			retval = pstorage_store(&block_handle, default_data, 4, 0);
    			if(retval == NRF_SUCCESS)
    					printf("\r\nStore success");
    			else
    					printf("\r\nStore failed");
    	}
    	else
    	{
    			printf("\r\ndata0 = %d", dest_data[0]);
    			printf("\r\ndata1 = %d", dest_data[1]);
    			printf("\r\ndata2 = %d", dest_data[2]);
    			printf("\r\ndata3 = %d", dest_data[3]);
    			dest_data[0] += 1;
    			dest_data[1] += 1;
    			dest_data[2] += 1;
    			dest_data[3] += 1;			
    			retval = pstorage_update(&block_handle, dest_data, 4, 0);
    			if(retval == NRF_SUCCESS)
    					printf("\r\nUpdate success");
    			else
    					printf("\r\nUpdate failed");
    	}
    

    On termial:

    Pstorage init success
    Pstorage regis success
    Load success
    Data is not exist, save data...
    Clear failed
    Store success
    ---Reset---
    Pstorage init success
    Pstorage regis success
    Load success
    data0 = 1
    data1 = 2
    data2 = 3
    data3 = 4
    Update success
    ---Reset---
    Pstorage init success
    Pstorage regis success
    Load success
    data0 = 1
    data1 = 2
    data2 = 3
    data3 = 4
    Update success
    
Children
  • Where is your pstorage callback function. You first know that update is successful when you get a pstorage_callback_success event. My guess is the you reset the device before the pstorage update operation is complete. The pstorage update function is in fact combination of several flash operation, including 2x pstorage clear operations and pstorage clear takes around 22ms each. There are some flash storage examples here, including pstorage example where pstorage callback handler is implemented. It is in fact the same example as posted on this thread for SDK 11.0.0

    Your question is in fact very good and it would be relevant to make a new thread for it in order for others to find the question and answer.

  • I already had a pstorage callback function. But when I use printing in callback function I just receive callback of pstorage_load function, I don't know why I can't see callback of pstorage_store and pstorage_update.

    I sure the device does not reset before storing or updating complete, because after this test pstorage code it run into bluetooth app (with a blinking led for indicator) and I reset board by pressing the "IF BOOT/RESET" button on board so I think there is enough time for pstorage_store and pstorage_update function.

    I will try my code on your example project.

    Thanks

  • @Thanh Doung Please add a new question and link to this one if it is relevant.

Related