STM32F412 Application Development Note VI: Using on-chip flash storage parameters

Source: Internet
Author: User

Some of the system configuration parameters need to be saved in our project, which is characterized by a small number and no need for constant modification, but cannot be defined as constants, because each device may be different and may be modified at a later stage. This requires consideration of the problem of saving these parameters. It is convenient to have this type of data in the specified location and modify the value of the storage location directly when it needs to be modified. Given that these data volumes are relatively small, the use of specialized storage units is neither economical nor necessary, just that some MCUs have larger flash, and using a small amount to store these parameters is both convenient and economical. The STM32F4 flash architecture is as follows:

The STM32F412ZG on the NUCLEO-F412ZG test Board has 1 m of flash, so we can try to save the parameters to their on-chip flash. The organization mode of its flash is as follows:

According to the above Flash organization mode, we can make the corresponding definition according to our own use convenience. From Sector5 to Sector11 size is 128K, so we do the following macro definition:

#define SECTOR_SIZE 1024*128//Byte

Although the library function of St is quite comprehensive, it is the basic operation, in order to use the aspect, according to our own need to encapsulate it again.

For a relatively simple read operation, the built-in flash module can be addressed directly in the common address space, just like reading variables.

Reads multiple data from the specified address

void Flash_readmoredata (uint32_t startaddress,uint16_t *readdata,uint16_t counttoread)

{

uint16_t Dataindex;

for (dataindex=0;dataindex<counttoread;dataindex++)

{

Readdata[dataindex]=flash_readhalfword (startaddress+dataindex*2);

}

}

Reads the half word (16-bit data) of the specified address

uint16_t Flash_readhalfword (uint32_t address)

{

return * (__io uint16_t*) address;

}

Reads the entire word of the specified address (32-bit data)

uint32_t Flash_readword (uint32_t address)

{

uint32_t TEMP1,TEMP2;

temp1=* (__io uint16_t*) address;

temp2=* (__io uint16_t*) (address+2);

Return (temp2<<16) +temp1;

}

The write operation involves writing and erasing user data, which is relatively more complex for writes. To prevent misoperation, write protection locks are also available. But these basic operations St's library functions have been written for us, we just need to call.

After the STM32 is reset, the Fpec module is protected and only after the write protection is released can we operate the relevant registers. STM32 Flash programming must be written to 16 bits at a time, and any operation that is not half-word will cause an error. As the process of flash writing:

STM32 Flash when programming, also must require its write address Flash is erased (that is, its value must be 0XFFFF), otherwise cannot write. Flash erase requires a full page erase, so you must also write the entire page, or you may lose data. As the Flash page erase process:

The following is the flash full erase process,

We write the data to write the function as follows:

Write multiple data from the specified address

void Flash_writemoredata (uint32_t startaddress,uint16_t *writedata,uint16_t counttowrite)

{

if (startaddress<flash_base| | ((startaddress+counttowrite*2) >= (flash_base+1024*flash_size)))

{

return;//illegal Address

}

Flash_unlock (); Unlock write protection

uint32_t offsetaddress=startaddress-flash_base; Calculates the actual offset address after removing the 0x08000000

uint32_t sectorposition=offsetaddress/sector_size; Calculate sector address,

uint32_t sectorstartaddress=sectorposition*sector_size+flash_base; The first address of the corresponding sector

Flash_erasepage (sectorstartaddress);//Erase this sector

uint16_t Dataindex;

for (dataindex=0;dataindex<counttowrite;dataindex++)

{

Flash_programhalfword (Startaddress+dataindex*2,writedata[dataindex]);

}

Flash_lock ();//Lock write protection

}

The data on the page should be read out and merged with the data to be written, and then written after erasing, but this data volume is very large, so considering a small amount of data storage, each time the whole data is written at the same time, simplifying the operation and reducing the amount of data processing. After testing the above program to write and read the data are correct, you can achieve internal flash read and write operations.

Reference to St's programming manual PM0081--STM32F40XXX and STM32F41XXX Flash programming Manual.

STM32F412 Application Development Note VI: Using on-chip flash storage parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.