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.

Related