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

write struct to flash

Hi guys,

I just want to ask you is it possible to write a struct into flash using sd_flash_write() function. I can successfully write and ready INT values, but the problem occurs when I want to write and read a structure. I have already tried to define a pointer as struct structName *pointer_name, but there is always a warning message that informs me the pointer is not compatible with sd_flash_write(). Can you please help me to solve this problem.

Br,

Adnan.

Parents Reply Children
  • Is it possible to use softdevice APIs in Mesh, as I tried in my example: 

    Write function inside which I call sd_flash_page_erase that automatically goes inside sys callback function:

    void write_Tasks(){
    
        if(flash_status != FLASH_IDLE){
        
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Flash is busy in status %d. Try again later! \n", flash_status); 
            return 1;
        }else{
            
            uint32_t err_code = sd_flash_page_erase(pg_num_task);
            if (err_code != 0){
                
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Fatal error in task list erase function: err_code is %d \n", err_code);
            }
            flash_status = LIST_WRITE; 
        }
    }

    This is the way how I use sd_flash_write function, but I think here is an issue:

    if(flash_status == LIST_WRITE){
     
                uint32_t err_code = sd_flash_write(pointer_task, task_list, 12);
                nrf_delay_ms(100);
                if(err_code != 0){
                
                    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Fatal error in task list write function: err_code is %d \n", err_code);
                }
                flash_status = LIST_FINISHED;

    And finally this is the way how I define a free space: 

    uint 32_t pg_size_task = NRF_FICR->CODEPAGESIZE;
    uint32_t pg_num_task = NRF_FICR->CODESIZE-4;
    struct TaskInstance *pointer_task = (struct TaskInstance *)(pg_size_task * pg_num_task);
    

    Is it possible to finish my code in this way, keeping these functions? Thanks in advance!

    Br,

    Adnan.

  • And also one extra question, my struct looks like this: 

    struct name1{
    
        (enum) enum1 enum_name;
        struct name2 xyz[2];
        int int_value;
    };

    Is it possible to store it in the flash if it looks like this with int, enum and another struct in itself?

    Br,

    Adnan.

  • Hi,

    It's perfectly fine to save a struct in flash. Regarding your choice to use the Softdevice API directly, this is not recommended, I strongly suggest that you use the flash manager instead.

    regards

    Jared 

Related