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

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
User11041
Level 1
Level 1
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 -> and . 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


#include
#include
#include
#include

#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 page=0;

//copy the target data word by word into the buffer
for (uint32_t i=0; i page= *(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);
}
0 Likes
2 Replies
lock attach
Attachments are accessible only for community members.
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
Hi,

I got this software project from DAVE3. Do give it a try (if you have DAVE3) or use it as a reference for DAVE4 which I believe should be very similar.
0 Likes
lock attach
Attachments are accessible only for community members.
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
Hi,

I did this Flash write and erase project on DAVE4. Pls give this a try.

1735.attach
0 Likes