one. Stm32 Flash is divided into1. Primary storage block: Used to save the specific program code and user data, the primary storage block is divided into pages,a page size of 1KB. The range is within 128KB beginning with address 0x08000000. 2. Information BLOCK: For the User configuration information area that is responsible for the 2KB Boot program (Bootloader) and 512B that is shipped by Stm32 factory
two. Operating Principleswrite operation to Flash to " Erase first write " principle;Stm32 's built-in flash programming operations are written in pages, and the operation must be written in 16-bit half-width data bit units, allowing cross-page writes, and writing non-16-bit data will cause stm32 internal bus errors . the internal RC oscillator must be turned on when the built-in flash reads and writes.
Header file:
#ifndef __bsp_flash_h#define__bsp_flash_h#include"stm32f10x.h"typedefenum{FLASH_WRITE_OK=1, Flash_wrike_no=0, Flash_read_ok=1, Flash_read_no=0}flash_flag;externUnsignedCharFlash_write (unsigned Short int*memory_data);externUnsignedCharFlash_read (unsigned Short int* memory_data,unsigned Short intn);#endif/* __led_h */
Read and Write functions:
#include"bsp_flash.h"Flash_flag flash_status;StaticU32 count =0;/**---------------------------------------------------------------------------------* @brief Flash Write essential Steps * @ param input Data Start address * @retval function has completed-------------------------------------------------------------------------------- */unsignedCharFlash_write (unsigned Short int*memory_data) { Static intT; Count=0; /*enables or disables the Internal high speed oscillator (HSI).*/Rcc_hsicmd (ENABLE); /*Open Flash erasable Controller*/Flash_unlock (); /*Flash three flag bits fully clear*/Flash_clearflag (Flash_flag_eop| flash_flag_pgerr|Flash_flag_wrprterr); Flash_erasepage (0x8002000); T=sizeof((Const Char*) memory_data); while(Count <=t) {/*Flash for a byte storage, 16-bit data must address plus 2*/Flash_programhalfword ((0x8002000+count*2), * (memory_data+count)); Count++; if(Count >t) {flash_status=Flash_wrike_no; returnFlash_status; } } /*Locks the FLASH program Erase Controller.*/Flash_lock (); Rcc_hsicmd (DISABLE); Flash_status=FLASH_WRITE_OK; returnFlash_status;}/**---------------------------------------------------------------------------------* @brief Flash readout function * @param m Emory_data: Output storage address, n output number * @retval function has not been executed----------------------------------------------------------------------- ---------*/unsignedCharFlash_read (unsigned Short int* memory_data,unsigned Short intN) {Count=0; while(Count <N) {* (Memory_data+count) = * (U16 *) (0x8002000+ count*2); printf ("\ r%d \ r", * (Memory_data+count));//Readcount++; if(Count >N) {flash_status=Flash_read_no; returnFlash_status; } } //flash_erasepage (0x8002000);Flash_status =FLASH_READ_OK; returnFlash_status;}/*********************************************end of file**********************/
Stm32 Flash read-write standalone function [library function]