FLASH Programming in STM32
Every microcontroller have some memory allocated for the user flash. Today We will use this memory to store some data in it. The benefit of using this flash memory is that, even after the power disconnect, the data remains safe in the flash memory.
This tutorial is devided into two halves. The first Half will cover those microcontrollers, whose memory is divided into pages. For eg- Cortex M3, and M0 series. The second Half is for those microcontrollers, whose memory is divided into Sectors
FLASH PAGE TYPE
For the first half, I am using STM32F103 microcontroller, and you can see the memory distribution in the picture below
As you can see above that the main memory (Flash memory) is distributed in 128 pages.
Each page is of 1 KiloByte, thus making the total memory of 128 KiloByte
Now always remember that we should start programming as lower as possible in the flash memory. This is because the start of the flash is already allocated to the program, that is being executed on your controller right now. I have explained it properly in the video, you can check that out in the end.
Each page can hold 1024 Bytes of data, and i don’t have much data to write, so I will choose the last page of the flash memory, i.e. 0x0801FC00 – 0x0801FFFF
Some insight into the CODE
FLASH_PAGE.c Library file contains 2 functions. One is for writing data to the flash, and another is for reading from it
Flash_Write_Data takes the following parameters
StartPageAddress is the Start address of the page, or memory in the page, from where you want to start writing the data
DATA_32 is the address of the 32 bit data, that you want to write into the flash
- It will first count the number of words, that you want to write into the flash
- Flash will be Unlocked to make modifications
- Based on the number of words, it will calculate the number of pages required to store that data
- Next, the required number of pages will be erased, and the new data will be written into them
- Flash will be Locked again
Flash_Read_Data reads the data from the flash. It takes the following parameters
- StartPageAddress is the Start address of the page, from where you want to start reading the data
- DATA_32 is the address of the 32 bit variable, where you want to store the data
Above, we are writing the string “Hello World” to the memory location 0x0801FC00, and than we will read data from the same address and store it in the Rx_Data.
FLASH SECTOR TYPE
Some microcontrollers have memory distributed in Sectors.
In my STM32F446RE, there are 7 sectors and the size is varying between them. Similarly, some microcontrollers can have 11, 15 or 23 sectors. This code will cover all these types, irrespective of the number of sectors the controller have.
If the memory for the controller is arranged in dual banks, like Bank1 or Bank2, by defaults these Banks are disabled and the memory uses only single Bank addressing. Check the sector addresses in the single Bank distribution.
Also, if the sector sizes are different than as shown in the picture above, All you need to do is, define new sectors as according to your device datasheet in the function static uint32_t GetSector(uint32_t Address)
Some insight into the CODE
Flash_Write_Data will write data to the given memory location.
1.) It will first calculate the number of words, that needs to be written
2.) next, it will calculate the start sector number based on the address entered in the parameter
3.) than, the sector address of the sector where the data is going to end
4.) and finally the sector number of the last sector
5.) It will than erase the required number of sectors and program it with the new data
Flash_Read_Data will read the data from the entered memory location, and save it in the DATA_32
Convert_To_Str converts the data to the string
Above, I am writing the data to the memory location, 0x800FFF4, which is somewhere towards the end of SECTOR 3. And this data will partially come in SECTOR 4 also.
As you can see above the data is written in both sectors i.e. Sector 3 and Sector 4
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020
- STM32 Clock Setup using Registers November 17, 2020
- USB CDC Device and HOST in STM32 November 14, 2020
- ST7735 1.8″ TFT Display with STM32 November 6, 2020
- How to communicate between HC-12 using STM32 October 5, 2020
- How to use STM32 as a KEYBOARD September 28, 2020