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

pstorage_store not stores but i can read

Hello,

i am stuck with this, trying to write to flash using pstorage module. Below is my code.

static void pstore_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_ERROR_OP_CODE:
    		ble_nus_send_string( &m_nus, (uint8_t *)"PS general Error.", strlen("PS general Error.."));
    	break;
    	case PSTORAGE_STORE_OP_CODE:
    		if (result == NRF_SUCCESS)
			{
			   // Store operation successful.
				ble_nus_send_string( &m_nus, p_data, data_len);
				ble_nus_send_string( &m_nus, (uint8_t *)"STORE  Operation Done.", strlen("STORE Operation Done."));
			}
			else
			{
			   // Store operation failed.
				ble_nus_send_string( &m_nus, p_data, data_len);
				ble_nus_send_string( &m_nus, (uint8_t *)"STORE  Operation FAILED.", strlen("STORE Operation FAILED."));
			}
    	break;
    	case PSTORAGE_CLEAR_OP_CODE:
    		if (result == NRF_SUCCESS)
			{
			   // Store operation successful.
				ble_nus_send_string( &m_nus, p_data, data_len);
				ble_nus_send_string( &m_nus, (uint8_t *)"CLEAR  Operation Done.", strlen("CLEAR Operation Done."));
			}
			else
			{
			   // Store operation failed.
			}
			// Source memory can now be reused or freed.
			break;
    	break;
        case PSTORAGE_LOAD_OP_CODE:
        	if (result == NRF_SUCCESS)
			{
			   // Store operation successful.
				ble_nus_send_string( &m_nus, p_data, data_len);
				ble_nus_send_string( &m_nus, (uint8_t *)"LOAD Operation Done.", strlen("LOAD Operation Done."));
			}
			else
			{
			   // Store operation failed.
			}
			// Source memory can now be reused or freed.
			break;
    }
}
static bool pstorage_init_routine( void ){
	uint32_t retval;
	retval = pstorage_init();
	if(retval == NRF_SUCCESS)
	{
			// Module initialization successful.
		
	}
	else
	{
		 // Initialization failed, take action.
		ps_error_case = PS_INIT_FAILED;
		return false;
	}
	param.block_size  = 0x0040;
	param.block_count = 1;
	param.cb          = pstore_cb_handler;
	retval = pstorage_register(&param, &handle);
	if (retval == NRF_SUCCESS)
	{
			// Registration successful.
		pstorage_error_occured = false;
	}
	else
	{
			// Failed to register, take corrective action.
		ps_error_case = PS_REGISTRATION_FAILED;
		pstorage_error_occured = true;
		return false;
	}
	
	
	retval = pstorage_block_identifier_get(&base_handle, 0, &block_handle);
	if (retval == NRF_SUCCESS)
	{
			// Get Block Identifier successful.
		pstorage_error_occured = false;
	}
	else
	{
			// Failed to get block id, take corrective action.
		ps_error_case = PS_ID_BLOCK_GET_FAILED;
		pstorage_error_occured = true;
		return false;
	}
	
	return true;
}
  • So, having done the above, what i do is i always call the pstorage_init_routine() routine before calling pstorage_store().

Then after what is do is this below to store data.

un_g_retval = pstorage_clear(&base_handle, 64);
		if( un_g_retval == NRF_SUCCESS ){
			pstorage_error_occured = false;
		}
		else{
			pstorage_error_occured = true;
			ps_error_case = PS_CLEAR_BLOCK_FAILED;
			return;
		}
		app_store_data[0] = 'A';	//1
		app_store_data[1] = 'n';	//2
		app_store_data[2] = 'n';	//3
		app_store_data[3] = 'i';;	//4
		
		un_g_retval = pstorage_store(&block_handle, app_store_data, 4, 0);
		if (un_g_retval == NRF_SUCCESS)
		{
			pstorage_error_occured = true;
			ps_error_case = PS_DATA_WRITE_SUCCESS;
			return;
		}
		else
		{
			pstorage_error_occured = true;
			ps_error_case = PS_DATA_WRITE_FAILED;
		}

But what i am facing is, data is not been written and i do not get any error in return.

How i am confirming this, by reading data back. And i read back 0xff from the same. Yes, i have not placed delays anywhere because the event so happened in my application does not read back data immediately after writting. So what i believe is the nrf gets plenty of time after pstorage_store() routine is called.

So what is wrong with my code. Please help me out with it. If this question is duplicate, please direct me to the resolved question.

Thank you.

  • What clearly i am getting now is from sd_flash_write() which is called by pstorage_store() is NFR_ERROR_FORBIDDEN, means "Tried to write to or read from protected location". Although i have used the default pstorage_platform.h" too which comes with sdk. What is the appropriate area to read write on flash?

  • Well things NOW sorted out.

    From the link below as reference:

    devzone.nordicsemi.com/.../a00018.html

    I was following this documentation for doing pstorage, but i was carried out so much by the example so described in that documentation which confused me. i understand many of you guys have made it but i was doing mistake. And that mistake was of 'pstorage_handle_t'.

    Under the Registration example they have taken 'handle' as a 'pstorage_handle_t' variable, and under Get Block Identifier they have described the same using 'base_handle' as 'pstorage_handle_t' type variable.

    Thus , the pstorage_store() was throwing me 15 error code with respect to sd_flash_write() api, i.e Operation not permitted on protected region.

    I think this is a misleading example so described there, would like to request to nordic people to keep variable name same where it should be so any new/ starter with nordic chip can get it correctly, which is actually correct too.

    Otherwise, the code was perfect. So, now it is working.

    One more thing, i have called pstorage_apis from main routine context, as told by one of the nordic guy, that my nus_handler_() which receives data from ble , the pstorage api call have same prority. thus the pstorage call was blocked too. Thus i have to shift my whole setup to main, and setting flags in nu_data_handler() for the corresponding action.

  • I aggree with you. It is confusing. Both the code in "Registration" and "Get Block Identifier" sections should define the base handle as pstorage_handle_t base_handle. I have reported the issue for improvement.

  • the same problem.

    "the pstorage_clear() is not firing the PSTORAGE_CLEAR_OP_CODE and pstorage_store() is not firing the PSTORAGE_STORE_OP_CODE in callback handler. However, the pstorage_read() is fired from PSTORAGE_LOAD_OP_CODE inside callback handler."

Related