Quantcast
Channel: Infineon Forums
Viewing all articles
Browse latest Browse all 9892

How to write data of arbitrary type to FLASH of XMC4400?

$
0
0
Hi all,

I am trying to save a single instance of a typedeffed data type to the flash of the XMC4400 for later use (only an example here, in reality it is a larger configuration struct, but not larger than the page size of 256 bytes). As a target, I chose the first page of the user configurable block 0 (UCB0) of the flash of XMC4400, starting address is 0xc000000.

Here is a portion of sample code that I tried - loading the data is not a problem as the flash can be accessed as RAM, but saving the data does not work - though I'm using the latest version of the XMClib -> <xmc_flash.h> and <xmc4_flash.h>. The codes executes without exceptions, the flash status indicates programming mode after save_data(..), but if I look at 0xc000000 with the debugger I notice nothing has happened. Consequently, after load_data() the variable reads random data.

Can anyone help? Provide some code that will succeed to write this variable to the flash?

Thanks,
Niklas

Code:

#include <DAVE.h>
#include <XMC4400.h>
#include <core_cm4.h>
#include <xmc_flash.h>

#define FLASH_ADDRESS XMC_FLASH_UCB0

typedef struct my_type {
        bool foo;
        void (*foo_handler)(void);
        float bar;
        //etc...
}TYPE_t;

TYPE_t data;

void save_data(uint32_t* address)
{
        uint32_t page[XMC_FLASH_WORDS_PER_PAGE];
        TYPE_t* data_ptr = &data;

        //initialize a buffer of the size of one flash page
        for (uint32_t i=0; i<XMC_FLASH_WORDS_PER_PAGE; ++i)
                page[i]=0;

        //copy the target data word by word into the buffer
        for (uint32_t i=0; i<sizeof(TYPE_t)/4; ++i)
                page[i]= *(uint32_t*)data_ptr++;

       
        XMC_FLASH_ClearStatus();
       
        //attempt to write the buffer (=one page) to the flash -> executes but does not do anything???
        XMC_FLASH_ProgramPage(FLASH_ADDRESS, page);
}

void load_data(void)
{
        data = *((TYPE_t*)FLASH_ADDRESS);
}

int main(void)
{
        SystemInit();
       
        data.foo = true;
        data.foo_handler = NULL;
        data.bar = 100e-9;

        save_data(FLASH_ADDRESS);
       
        data.foo = false;
        data.bar = 0;

        load_data();

        while(1U);
}


Viewing all articles
Browse latest Browse all 9892


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>