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

data saved different to data read

Hi, I want to save settings in the memory. In my case, the softdevice is enabled, there is no ble advertising. I use pstorage functions. Problem: I stored 0x12, 0x23, 0x45,0x67. But I read 0xFF, 0xFF, 0xFF, 0xFF.

Initialization

  void init_app_data_flash_manager(void)
{
		pstorage_module_param_t param;
		uint32_t retval1;
		uint32_t retval;
		

		// Initialize persistent storage module.
		retval1 = pstorage_init();
		APP_ERROR_CHECK(retval1);
	
		param.block_size = 0x10;
		param.block_count = 8;
		param.cb = app_data_flash_manager_cb_handler;
	
		retval = pstorage_register(&param, &app_data_flash_manager_handle);
	
		if (retval == NRF_SUCCESS)
		{
			
			 UART_TXT("successful register..\r\n\0");
		}
		else
		{
			// Failed to register, take corrective action.
			simple_uart_put((uint8_t)retval1);
			simple_uart_put((uint8_t)retval);
		
		}

}

Data saving

  void save_settings_to_flash()
{
	
	pstorage_handle_t block_handle;
	uint32_t retval1;
	uint32_t retval;
	
	retval1 = pstorage_block_identifier_get(&app_data_flash_manager_handle, 4, &block_handle);
	   	

 	dataToStore[0] = 0x12;
	dataToStore[1] = 0x23;
	dataToStore[2] = 0x45;	
	dataToStore[3] = 0x67;

	
	retval = pstorage_update(&block_handle, dataToStore, 4, 0);
	
	if (retval == NRF_SUCCESS)
	{
	// Store successfully requested. Wait for operation result.
		UART_TXT("stored\n");
		}
	else
	{
	// Failed to request store, take corrective action.
		
		simple_uart_put((uint8_t)retval);
		
	}
}

data reading

    void read_settings_from_flash(void)
{
	pstorage_handle_t block_handle;
	uint32_t retval;
	
	uint8_t p_encode_data_read[4];

	retval = pstorage_block_identifier_get(&app_data_flash_manager_handle, 4, &block_handle);
	
  	retval = pstorage_load(p_encode_data_read, &block_handle, 4, 0);
	
	if (retval == NRF_SUCCESS)
	{
		UART_TXT("Loaded\n");
		simple_uart_put(0xAA);
		simple_uart_put(p_encode_data_read[0]);
		simple_uart_put(p_encode_data_read[1]);
		simple_uart_put(p_encode_data_read[2]);
		simple_uart_put(p_encode_data_read[3]);
		simple_uart_put(0xBB);
	// Load successful. Consume data.
	}
	else
	{
		UART_TXT("Loading failed");
	// Failed to load, take corrective action.
	}
}




static pstorage_handle_t 					app_data_flash_manager_handle;
static uint8_t dataToStore[4] __attribute__((aligned(4)));
Parents
  • Thank you for your help! I did not work, because I forgot this part:

    static void sys_evt_dispatch(uint32_t sys_evt)
    {
        pstorage_sys_event_handler(sys_evt);
    }
    
    void body_ble_stack_init(void) {
    	uint32_t err_code;
    	// Initialize the SoftDevice handler module.
    	SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM , false);
    	err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
        APP_ERROR_CHECK(err_code);
    	
    }
    
Reply
  • Thank you for your help! I did not work, because I forgot this part:

    static void sys_evt_dispatch(uint32_t sys_evt)
    {
        pstorage_sys_event_handler(sys_evt);
    }
    
    void body_ble_stack_init(void) {
    	uint32_t err_code;
    	// Initialize the SoftDevice handler module.
    	SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM , false);
    	err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
        APP_ERROR_CHECK(err_code);
    	
    }
    
Children
Related