44. Flash operation program

Publisher:美好梦想Latest update time:2017-11-10 Source: eefocusKeywords:Flash Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Read half word (16 bits) function

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

//faddr: read address (this address must be a multiple of 2!!)

//Return value: corresponding data.

u16 STMFLASH_ReadHalfWord(u32 faddr)

{

return *(vu16*)faddr; 

}

2. Direct writing without checking

// Write without checking

//WriteAddr: starting address

//pBuffer: data pointer

//NumToWrite: half-word (16-bit) number   

void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)   

{  

u16 i;

for(i=0;i

{

FLASH_ProgramHalfWord(WriteAddr,pBuffer[i]);

   WriteAddr+=2; //Address increases by 2. Write one half word at a time (16-bit number)

}  

3. Read data of specified length from specified address

//Read data of specified length starting from the specified address

//ReadAddr: starting address

//pBuffer: data pointer

//NumToWrite: half-word (16-bit) number

void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)  

{

u16 i;

for(i=0;i

{

pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr); //Read 2 bytes.

ReadAddr+=2; //Offset 2 bytes.

}

}

4. Write data of specified length starting from the specified address

Program ideas

44. Flash operation program

44. Flash operation program

//Write data of specified length starting from the specified address

//WriteAddr: starting address (this address must be a multiple of 2!!)

//pBuffer: data pointer

//NumToWrite: half-word (16-bit) number (that is, the number of 16-bit data to be written.)

#if STM32_FLASH_SIZE<256

#define STM_SECTOR_SIZE 1024 // bytes

#else 

#define STM_SECTOR_SIZE 2048

#endif  

u16 STMFLASH_BUF[STM_SECTOR_SIZE/2]; // up to 2K bytes

void STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)

{

u32 secpos; //sector address

u16 secoff; //Inside sector offset address (16-bit word calculation)

u16 secremain; // Remaining addresses in the sector (16-bit word calculation)   

  u16 i;    

u32 offaddr; // remove the address after 0X08000000

if(WriteAddr=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//Illegal address, first determine whether the address to be written is an illegal address

FLASH_Unlock(); //Unlock


offaddr=WriteAddr-STM32_FLASH_BASE; //actual offset address.

secpos=offaddr/STM_SECTOR_SIZE; //sector address 0~127 for STM32F103RBT6, determine which page or sector

secoff=(offaddr%STM_SECTOR_SIZE)/2; //Offset within the sector (2 bytes as the basic unit.)

secremain=STM_SECTOR_SIZE/2-secoff; //Sector remaining space size   

if(NumToWrite<=secremain)secremain=NumToWrite; //Not larger than the sector range

while(1) 

{

STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2); //Read the contents of the entire sector and read it into STMFLASH_BUF

for(i=0;i

{

if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//need to erase    

}

If (i) the check above is aborted midway, i must be less than secretmain, otherwise if all checks are successful, i should be equal to secretmain.

{

FLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);//Erase this sector

                        for(i=0;i

{

STMFLASH_BUF[i+secoff]=pBuffer[i]; //Write the data to be updated to the array

}

STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//Write the entire sector  

}

               else STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//Write what has been erased, directly write to the remaining area of ​​the sector.   

if(NumToWrite==secremain)break;//Writing is finished

                else //Writing is not finished

{

secpos++; //Increase the sector address by 1

secoff=0; //Offset position is 0  

  pBuffer+=secremain; //Pointer offset

WriteAddr+=secremain; //write address offset   

  NumToWrite-=secremain; //Number of bytes (16 bits) decreases

if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2; //The next sector cannot be written completely

else secremain=NumToWrite; //The next sector can be written

}  

};

FLASH_Lock(); //Lock

}


Keywords:Flash Reference address:44. Flash operation program

Previous article:42.485 Communication Experiment
Next article:43. Flash memory programming principles and steps

Recommended ReadingLatest update time:2024-11-16 22:24

About the read and write operation method of MSP430-Flash exceeding 64K
About the read and write operation method of MSP430-Flash exceeding 64K #include "msp430xG46x.h"   void WriteFlashErrorNum(void);   //----------------------------------------------------------------------   //Function: Read data from flash, just need to give the first address and the number of data to be retrieved
[Microcontroller]
Samsung Electronics: NAND Flash production will not be reduced
Samsung Electronics has denied media reports that the company plans to cut NAND Flash production as early as this month, saying the reports are groundless, according to Sina Technology.   Recently, as NAND Flash suppliers have gradually tightened their supply, coupled with Japan's embargo on three key semiconductor ma
[Embedded]
FLASH debugging_HCS12 study notes (6)
      I stopped debugging the FLASH program for a while and tried debugging the CAN program, but to no avail. The CAN program was also very troublesome, but I finally debugged the FLASH program this morning. Actually, there should be no problem with the program itself, but because the program is not segmented, the FLAS
[Microcontroller]
Analysis and transplantation of FLASH driver based on eCos
0 Preface Embedded systems need to support a wide variety of external devices. How to construct well-functioning drivers for embedded devices is of great significance to the practical application of embedded operating systems. eCos is a real-time embedded operating system with open source code. It has good support fo
[Microcontroller]
Analysis and transplantation of FLASH driver based on eCos
s3c2440 bare metal-NorFlash1-principle
1. Flash types and characteristics: Flash is generally divided into NAND flash and NOR flash, and their respective characteristics are as follows: - Nor NAND XIP (Execute in Place) yes no Performance(Erase) Very slow (5s, chunks are too big)
[Microcontroller]
Born out of necessity! GigaDevice launches breakthrough 1.2V ultra-low power SPI NOR Flash product series
Born out of necessity! GigaDevice launches breakthrough 1.2V ultra-low power SPI NOR Flash product series Beijing, China (August 19, 2022) — GigaDevice, an industry-leading semiconductor device supplier, announced the launch of a breakthrough 1.2V ultra-low power SPI NOR Flash product, the GD25UF
[Embedded]
Born out of necessity! GigaDevice launches breakthrough 1.2V ultra-low power SPI NOR Flash product series
STM32's protection measures for internal Flash
1. STM32 protection measures for internal Flash       All STM32 chips provide protection for Flash to prevent illegal access to Flash - write protection and read protection.      1) Read protection is commonly referred to as "encryption", which acts on the entire Flash storage area. Once the Flash read protection is
[Microcontroller]
Free port communication solution between Hollysys PLC and Flash storage device
1. Introduction Hollysys LM series PLC is a small integrated PLC product independently developed by Hangzhou Hollysys Company. LM series PLC has the characteristics of high reliability, rich instructions, and strong communication capabilities. It supports six programming languages, including LD,
[Industrial Control]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号