cancel
Showing results for 
Search instead for 
Did you mean: 

How to swap two memory regions of FLASH memory in STM32L475?

Shetu Raj
Associate II

Hi,

I am working on STM32L475 ARM Cortex M4 board and writing code for it on STM32Cube IDE. I want to swap two memory regions of FLASH memory in my BootLoader program when some conditions are met. I already tried swapping using the normal method but it was not successful. Here is my code for swapping:-

#define APPLICATION_ADDRESS 0x0800C000

#define APPLICATION2_ADDRESS 0x08076400

boolean swap(void)

{

   char *app_1=( char*) APPLICATION_ADDRESS; //points to the 1st address of application1

char *app_2=(char*) APPLICATION2_ADDRESS ;//points to the 1st address of application2

int mem1= getMemorySize((unsigned char*)APPLICATION_ADDRESS);//returns the number of bytes in Application1

int mem2= getMemorySize((unsigned char*)APPLICATION2_ADDRESS);//returns the number of bytes in Application2

int limit;

if(mem1>mem2)

limit= mem1;

else

limit= mem2;

char swap;

for(int i=1; i<=limit; i++,app_1++,app_2++)

{

swap = *app_1;

*app_1 = *app_2;

*app_2 = swap

}

return TRUE;

}

Do I have to unlock or enable FLASH read/write using some functions or is there some other way to do it?

Thanks,

Shetu

1 ACCEPTED SOLUTION

Accepted Solutions

You have to erase blocks of FLASH before you can write to them. It does not act like RAM.

You would do better getting the code to run in place, or use a dual bank device supporting physical memory swapping.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5
Uwe Bonnes
Principal III

No Flash registers need to be set. However I do not understand what your for loop is good for. And befor jumping to either of the apps, you need to set VTOR.

> However I do not understand what your for loop is good for.

I suspect it stems from a lack of understanding in Flash technology, and the meaning of Flash sectors and banks.

Shetu Raj
Associate II

I have already set the VTOR. So basically I get the size of the both Applications(in terms of number of bytes) in mem1 and mem2. Then I also have pointers pointing to the first addresses of both the Applications respectively in app1 and app2. The variable limit contains the size of the bigger memory in terms of bytes so that there is no error while swapping in case one application is smaller than the other.For loop is for swapping both the memory regions because both of them are quite big (384 KB each) and as you can see in the code I am swapping byte by byte. I read in a documentation that you can only swap 2KB(a page) of memory at once and also you have to set a register called FLASH STATUS REGISTER(FSR) to a value to erase or read/write the FLASH Memory.here is that document:- (page 103, section 3.3.5) https://www.st.com/content/ccc/resource/technical/document/reference_manual/02/35/09/0c/4f/f7/40/03/DM00083560.pdf/files/DM00083560.pdf/jcr:content/translations/en.DM00083560.pdf . Yes I don't have much understanding of the FLASH stuff. If you could help me then it would be really nice of you.

You have to erase blocks of FLASH before you can write to them. It does not act like RAM.

You would do better getting the code to run in place, or use a dual bank device supporting physical memory swapping.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

> Yes I don't have much understanding of the FLASH stuff. If you could help me then it would be really nice of you.

The regarding section in the Reference Manual and the datasheet are a highly recommended read.

As mentioned, Flash is very unlike RAM. For technological reasons.