Stm32--flash Reading and writing

Source: Internet
Author: User

Stm32--flash Reading and writingFirst, Flash Introduction

  The programming operation of Stm32 can be realized by reading and writing the Flash inside the stm32.

Stm32 's built-in programmable flash is of great importance in many situations. such as its support for ICP (in circuit programming, circuit programming, on-line programming) features enable developers to Stm32 can be alerted to debug the development, can be programmed through the JTAG and SWD interface to write stm32; support IAP (in application Programming, which is programmed in the application, allows developers to update their internal programs while Stm32 is running the program. For some requirements for data security, programmable Flash can be combined with stm32 internal unique identity to achieve a variety of anti-cracking solutions. and Stm32 Flash has a foothold in some lightweight anti-power storage solutions.

The flash of Stm32 is the main storage block and information block. The primary storage block is used to hold the specific program code and user data, and the information block is used to store the 2KB Boot program (BootLoader) and 512B User Configuration information area from the Stm32 factory.

The main memory is divided into pages. Stm32 according to the Flash main storage block capacity, different pages, system memory, divided into small capacity, medium capacity, large capacity, interconnected type, a total of four categories of products.

    • Small volume Products: primary storage block 1-32kb, 1KB per page. System memory 2KB
    • Medium capacity Products: primary storage block 64-128kb, 1KB per page. System memory 2KB
    • Large capacity Products: primary storage block 256KB or more, 2KB per page. System memory 2KB
    • Interconnect Products: primary storage block 256KB or more, 2KB per page. System memory 18KB

For a specific product category, you can check the data sheet, or according to the following simple rules to differentiate:
Stm32f101xx, stm32f102xx, stm32f103xx products, according to their main storage block capacity, must be small capacity, medium capacity, large capacity of one of the products, stm32f105xx, stm32f107xx is connected products.

Write operations to flash follow the "erase first write" principle.

Stm32 's built-in flash programming operations are written in pages, and the operation must be written in 32-bit or 16-bit half-width data units, allowing cross-page writes, and writing non-word or half-length data will result in an STM32 internal bus error.

Second, the implementation of the program

  Flash.h

1 #ifndef __flash_h2 #define__flash_h3#include"stm32f10x.h"4U8 Write_flash (U32 *buff, U8 len);5 voidRead_flash (U32 *buff, U8 len);6 voidChar_to_int (unsignedChar*buffer, unsignedint*data,unsignedintposition);7 voidInt_to_char (unsignedChar* Buffer, unsignedintdata,unsignedintposition);8 #endif

  Flash.c

#include"flash.h"#include"stm32f10x_it.h"#defineWriteflashaddress ((u32) 0x08010000)//Read and write start address (primary storage block address of internal Flash starting from 0x08000000)/******************************************************************************** Function Name: write_flash* description : Write STM32 flash* input for specified address: buff: Write Data buffer, Len: Write Data length * output: no * return value: U8: Write successful return 1, failed to return 0* description: No ******************************** ***********************************************/U8 Write_flash (u32*buff, U8 len) {        volatileFlash_status Flashstatus; U8 K=0;    U32 Address; Address=writeflashaddress; Flashstatus=Flash_complete; Flash_unlock ();//UnlockFlash_clearflag (Flash_flag_bsy | Flash_flag_eop | Flash_flag_pgerr | FLASH_FLAG_WRPRTERR);//Clear All FlagsFlashstatus = Flash_erasepage (writeflashaddress);//Sector Erase    if(Flashstatus = =flash_complete) {         for(k=0;(K<len) && (flashstatus = = Flash_complete); k++) {Flashstatus= Flash_programword (Address, buff[k]);//writes a word (32 bits) of data into the specified addressAddress = address +4;//address offset of 4 bytes} flash_lock ();//re-lock to prevent mis-writes    }    Else    {        return 0; }    if(Flashstatus = =flash_complete) {        return 1; }    return 0;}/******************************************************************************** Function Name: read_flash* description : Read STM32 flash* input for specified address: buff: Read Data buffer, Len: Read data length * output: no * return value: None * Description: No ********************************************** *********************************/voidRead_flash (U32 *buff, U8 len)    {U8 k;        U32 Address; Address=writeflashaddress;  for(k=0; k<len;k++) {Buff[k]= (* (vu32*) Address);//read the data for a word of the specified addressAddress + =4;//address offset of 4 bytes    }} 

  Main.c

intMainvoid) {u32 in_data[5]={ One, A, -, -, -};//the data to writeU32 out_data[5];//Read Storage    inti; U8 STATUS=0; Usart1_config ();//Serial 1 ConfigurationGpio_configuration ();//GPIO configuration for illuminated LEDsStatus=write_flash (In_data,5); Delay (0x02ffff); if(STATUS) {gpio_setbits (gpiod, gpio_pin_13);//Light led1.Read_flash (Out_data,5); printf ("\ r \ Five Data is: \ r \ n");  for(i=0;i<5; i++) {printf ("\ r%d \ r", Out_data[i]); }    }     while(1);}

Stm32--flash Reading and writing

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.