Tuesday, 6 March 2018

ARM Programming 5 - Placing key variable in custom Flash Memory Location

How to Place a variable in custom location of MCU internal Flash Memory?

Now, it is getting interesting. We were able to place the RAM variables into the loctions we neded. We took help of scatter file which was generated by default. We then edited it. We informed Keil to use our own scater file. We partioned RAM into several blocks. We assigned RO data, ZI data of differect pbject files into each block of RAM. We tested it to make sure it works.

Now, I am trying to place a variable in custom Flash memory location. I want to know the exact location of the value in the flash memory. This has several appliaitons.
  1. I can take help of this information of the variable in the flash memory to decide what is the type of the software loaded
  2. Bootloader can make use of this information to understand whether there is a valid application code or not
  3. Bootloader can also use this information to decide which part of application to load ( ex.: if the MCU has too many applications in same package)
  4. I can also use this as a key to verify whether some operation was done on this product already or not. if the operation was done the code will erase the key and write a new value. 
  5. and some more applications which are not listed here :)
I have declared two load region in the scatter file. The new load region is called LR_SECRET_IROM1.

I have declared the variable in a separate C file. That C file contains only the declaration of the variable. nothing else. Below pictures should help to understand what i am trying to do.

1000 ARMS programming
Notice that in this new scatter file, i have declared all RAM variables in one block alone. Two load regions are visible. (LR_  ) Also notice the address of the RO objects of project3 file which contains our secret KEY. 
1000 ARMS programming
Secret key variable declared in separate file. Also note the point in comments. the attribute is needed to tell the compiler not remove the variable during linking thinking it is not used anywhere. It is our secret key after all

1000 ARMS programming
Header file: Project3.h

1000 ARMS programming
Flash variable with the programmed address location and also the value now visible


So, i was able t successfully place the variable in Internal flash memory in the region i wanted. We also placed the variables in RAM. That is different story. Placing variables in Flash was little challenging. 

So, what next?

In the coming posts, i want to access this location in the code and try to read the value. If the value matches expected value, then i will write another value in another location. This is to learn run time flash memory access. it si helpful in the future for boot loader application.

I think, until now, scatter file has helped a lot. We will dig into accessing flash memory in coming posts. Also, we will look at MAP file once more with a simple MAP file viewer.



ARM Programming 5 - Placing key variable in custom Flash Memory Location

How to Place a variable in custom location of MCU internal Flash Memory? Now, it is getting interesting. We were able to place the RAM v...