|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include "stmflash.h"
- #include "stm32f10x_flash.h"
-
-
- u16 StoreBuffer[STORE_DATA_NUM];
- u8 Save_Buffer[STORE_DATA_NUM*2];
-
- //ȡַָİ(16λ)
- //faddr:ַ(˵ַΪ2ı!!)
- //ֵ:Ӧ.
- u16 STMFLASH_ReadHalfWord(u32 faddr)
- {
- return *(vu16*)faddr;
- }
- #if STM32_FLASH_WREN //ʹд
- //д
- //WriteAddr:ʼַ
- //pBuffer:ָ
- //NumToWrite:(16λ)
- void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
- {
- u16 i;
- for(i=0;i<NumToWrite;i++)
- {
- FLASH_ProgramHalfWord(WriteAddr,pBuffer[i]);
- WriteAddr+=2;//ַ2.
- }
- }
- //ַָʼдָȵ
- //WriteAddr:ʼַ(˵ַΪ2ı!!)
- //pBuffer:ָ
- //NumToWrite:(16λ)(Ҫд16λݵĸ.)
- #if STM32_FLASH_SIZE<256
- #define STM_SECTOR_SIZE 1024 //ֽ
- #else
- #define STM_SECTOR_SIZE 2048
- #endif
- u16 STMFLASH_BUF[STM_SECTOR_SIZE/2];//2Kֽ
- void STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
- {
- u32 secpos; //ַ
- u16 secoff; //ƫƵַ(16λּ)
- u16 secremain; //ʣַ(16λּ)
- u16 i;
- u32 offaddr; //ȥ0X08000000ĵַ
- if(WriteAddr<STM32_FLASH_BASE||(WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//Ƿַ
- FLASH_Unlock(); //
- offaddr=WriteAddr-STM32_FLASH_BASE; //ʵƫƵַ.
- secpos=offaddr/STM_SECTOR_SIZE; //ַ 0~127 for STM32F103RBT6
- secoff=(offaddr%STM_SECTOR_SIZE)/2; //ڵƫ(2ֽΪλ.)
- secremain=STM_SECTOR_SIZE/2-secoff; //ʣռС
- if(NumToWrite<=secremain)secremain=NumToWrite;//ڸΧ
- while(1)
- {
- STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//
- for(i=0;i<secremain;i++)//У
- {
- if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//Ҫ
- }
- if(i<secremain)//Ҫ
- {
- FLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);//
- for(i=0;i<secremain;i++)//
- {
- STMFLASH_BUF[i+secoff]=pBuffer[i];
- }
- STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//д
- }else STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//дѾ˵,ֱдʣ.
- if(NumToWrite==secremain)break;//д
- else//дδ
- {
- secpos++; //ַ1
- secoff=0; //ƫλΪ0
- pBuffer+=secremain; //ָƫ
- WriteAddr+=secremain; //дַƫ
- NumToWrite-=secremain; //ֽ(16λ)ݼ
- if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2;//һд
- else secremain=NumToWrite;//һд
- }
- };
- FLASH_Lock();//
- }
- #endif
-
- //ַָʼָȵ
- //ReadAddr:ʼַ
- //pBuffer:ָ
- //NumToWrite:(16λ)
- void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
- {
- u16 i;
- for(i=0;i<NumToRead;i++)
- {
- pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//ȡ2ֽ.
- ReadAddr+=2;//ƫ2ֽ.
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- //WriteAddr:ʼַ
- //WriteData:Ҫд
- void Test_Write(u32 WriteAddr,u16 WriteData)
- {
- STMFLASH_Write(WriteAddr,&WriteData,1);//дһ
- }
-
-
-
-
|