自用的,你试试。STM32Flash.h
- #ifndef __STM32FLASH_H__
- #define __STM32FLASH_H__
- #define FLASHSTART_ADDR 0x08000000 //128K 最后一页 1KB每页 可存512个16位数 仅用于小于512K产品
- #define FLASH_BLOCK_USE 1 //使用倒数1个block
- #define STM32_FLASH_SIZE 128 //128K Flash
- #if STM32_FLASH_SIZE<256
- #define STM_SECTOR_SIZE 1024
- #else
- #define STM_SECTOR_SIZE 2048
- #endif
- #define USER_ADDR (FLASHSTART_ADDR+STM32_FLASH_SIZE*1024-FLASH_BLOCK_USE*STM_SECTOR_SIZE)
- #include "GloablVar.h"
- void STMFlash_Init(void);
- u8 STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite);
- void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead);
- #endif //for __STM32FLASH_H__
复制代码
STM32Flash.c
- #include "main.h"
- #include "STM32Flash.h"
- u8 FLASH_Unlock(void) {
- if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
- {
- /* Authorize the FLASH Registers access */
- WRITE_REG(FLASH->KEYR, FLASH_KEY1);
- WRITE_REG(FLASH->KEYR, FLASH_KEY2);
- /* Verify Flash is unlocked */
- if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
- {
- return 1;
- }
- }
-
- return 0;
- }
- void FLASH_Lock(void) {
- SET_BIT(FLASH->CR, FLASH_CR_LOCK);
- }
- void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
- {
- /* Proceed to program the new data */
- SET_BIT(FLASH->CR, FLASH_CR_PG);
- /* Write data in the address */
- *(__IO uint16_t*)Address = Data;
-
- while (READ_BIT(FLASH->SR, FLASH_SR_BSY));
- CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
- SET_BIT(FLASH->SR, FLASH_SR_EOP);
- }
- void STMFlash_Init(void) {
- // FLASH->ACR=0; //CPU= 0M-24M
- // FLASH->ACR=1; //CPU= 24M-48M
- FLASH->ACR=2; //CPU= 48M-72M
- }
-
- void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
- {
- u16 i;
- for(i=0;i<NumToWrite;i++)
- {
- FLASH_Program_HalfWord(WriteAddr+i*2,pBuffer[i]);
- }
- }
- void STMFLASH_PageErase(uint32_t EraseAddress)
- {
- /* Proceed to erase the page */
- SET_BIT(FLASH->SR, FLASH_SR_EOP);
- SET_BIT(FLASH->CR, FLASH_CR_PER);
- WRITE_REG(FLASH->AR, EraseAddress);
- SET_BIT(FLASH->CR, FLASH_CR_STRT);
- while (READ_BIT(FLASH->SR, FLASH_SR_EOP)==0);
- SET_BIT(FLASH->SR, FLASH_SR_EOP);
- CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
- }
- u16 STMFLASH_ReadHalfWord(u32 faddr)
- {
- return *(vu16*)faddr;
- }
- u16 STMFLASH_BUF[STM_SECTOR_SIZE/2];
- void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
- {
- u16 i;
- for(i=0;i<NumToRead;i++)
- {
- pBuffer[i]=STMFLASH_ReadHalfWord(USER_ADDR+ReadAddr+i*2);
- }
-
- }
- u8 STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
- {
- u32 secpos;
- u16 secoff;
- u16 rdLen;
- u16 i;
- u16 wrEnd;
-
- if(WriteAddr+NumToWrite*2>FLASH_BLOCK_USE*STM_SECTOR_SIZE) return 1; //写入地址超限
- FLASH_Unlock();
- secpos = WriteAddr / STM_SECTOR_SIZE;
- secoff = WriteAddr % STM_SECTOR_SIZE;
- wrEnd=0;
- while (wrEnd<NumToWrite) {
- rdLen=(secoff+(NumToWrite-wrEnd)*2<=STM_SECTOR_SIZE)?NumToWrite-wrEnd:(STM_SECTOR_SIZE-secoff)/2;
- STMFLASH_Read(secpos*STM_SECTOR_SIZE,STMFLASH_BUF,STM_SECTOR_SIZE/2);
- for(i=0;i<rdLen;i++) {
- if(STMFLASH_BUF[secoff/2+i]!=0XFFFF) break;
- }
- if (i<rdLen) {
- STMFLASH_PageErase(secpos*STM_SECTOR_SIZE+USER_ADDR);
- for(i=0;i<rdLen;i++)
- {
- STMFLASH_BUF[i+secoff/2]=pBuffer[i+wrEnd];
- }
- STMFLASH_Write_NoCheck(USER_ADDR+secpos*STM_SECTOR_SIZE,STMFLASH_BUF,STM_SECTOR_SIZE/2);
- }
- else
- STMFLASH_Write_NoCheck(USER_ADDR+secpos*STM_SECTOR_SIZE+secoff,pBuffer+wrEnd,rdLen);
-
- secoff = 0;
- secpos++;
- wrEnd+=rdLen;
- }
- FLASH_Lock();
- return 0;
- }
复制代码
|