This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Save struct to flash memory

Hello guys.

I am able to save an array to flash memory unfortunately I can not save a struct in flash. I am referring to this question Save struct to flash . However I am not able to save it using that example. Where I am doing a mistake in here?

This is my structure:

    __align(4)	 struct 
    		{
    		uint8_t sleep_time_min;
    		uint8_t advertise_time_sec;	
    		}default_commands;

This is pstorage initialization function:

     void my_pstorage_init(void)
    
    {
    	 uint32_t               err_code;
    
            pstorage_module_param_t    param;
        	param.block_size  = 40;  										//Select block size of 32 bytes
            param.block_count = 1;   										//Select 2 blocks, total of 64 bytes
        		param.cb          = my_pstorage_cb_handler; //Set the pstorage callback handler
        	
        	 err_code = pstorage_init();
           APP_ERROR_CHECK(err_code);
        	 err_code = pstorage_register(&param, &base_handle);
        		APP_ERROR_CHECK(err_code);
        	
        }

This is clearing function:

    void my_pstorage_clear(void)
    {
    		pstorage_handle_t       block_handle;
    		uint8_t 							index;
    		uint32_t               err_code;
    		
    		index = 0; //we will write to the second block
    		err_code = pstorage_block_identifier_get(&base_handle, index,&block_handle);
    		APP_ERROR_CHECK(err_code);
    		
    		err_code = pstorage_clear(&block_handle,40); 
    		APP_ERROR_CHECK(err_code);
    		pstorage_wait_handle = block_handle.block_id;            //Specify which pstorage handle to wait for 
    		pstorage_wait_flag = 1;                                  //Set the wait flag. Cleared in the my_pstorage_cb_handler
    	  while(pstorage_wait_flag) { power_manage(); }              //Sleep until clear operation is finished.	
    	
    	
    }

Write function:

    void my_pstorage_write(void)
    {
    		pstorage_handle_t       block_handle;
    		uint8_t 							index;
    		uint32_t               err_code;
    		
    		index = 0; //we will write to the first block
    		//Store data to blocks or just block. Wait for the last store operation to finish before reading out the data.
    		err_code = pstorage_block_identifier_get(&base_handle, index,&block_handle); 
    		APP_ERROR_CHECK(err_code);
    		
    		err_code = pstorage_store(&block_handle, (uint8_t*)&default_commands,sizeof(default_commands),0); //Write to flash, only one block is allowed for each pstorage_store command
    		APP_ERROR_CHECK(err_code);
    		pstorage_wait_handle = block_handle.block_id;                     //Specify which pstorage handle to wait for
    		pstorage_wait_flag = 1;                                    				//Set the wait flag. Cleared in the my_pstorage_cb_handler
    		while(pstorage_wait_flag) { power_manage(); }             		    //Sleep until store operation is finished.
    }

My main function:

        int main(void)
        {

	default_commands.sleep_time_min = 1;
	default_commands.advertise_time_sec = 30;
	
        my_pstorage_init();
	my_pstorage_clear();
	
	my_pstorage_write();
	my_pstorage_read();
	my_pstorage_clear();
        }

EDIT______________________________________________________________ So the problem is that microcontroler becomes unresponsive in this case PSTORAGE_STORE_OP_CODE when reaches line retval = sd_flash_write();

case PSTORAGE_STORE_OP_CODE:
        {
            uint32_t  size;
            uint32_t  offset;
            uint8_t * p_data_addr = p_cmd->p_data_addr;

            offset        = (m_round_val * SOC_MAX_WRITE_SIZE);
            size          = p_cmd->size - offset;
            p_data_addr  += offset;
            storage_addr += (p_cmd->offset + offset);

            if (size < SOC_MAX_WRITE_SIZE)
            {
                retval = sd_flash_write(((uint32_t *)storage_addr),
                                        (uint32_t *)p_data_addr,
                                        size / sizeof(uint32_t));
            }
  • nowhere in that whole mess of code did you actually say what it is makes you believe you were unable to save the struct. Did you get an error, were you unable to read it back? What was the actual issue.

Related