GeekIMU/2.Firmware/STM32/Firmware/driver/stmflash.c

138 lines
5.0 KiB
C
Raw Normal View History

2024-11-09 21:39:20 +08:00
/******************** (C) COPYRIGHT 2020 GEEKIMU *******************************
* File Name : stmflash.c
* Current Version : V2.0 & ST 3.5.0
* Author : zhanli 719901725@qq.com & JustFeng.
* Date of Issued : 2017.1.11 zhanli : Create
* Comments : STM32 ģ<EFBFBD><EFBFBD>Flash<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
********************************************************************************/
#include "stmflash.h"
#include "delay.h"
#include "usart.h"
#include "stm32f10x_flash.h"
#if STM32_FLASH_SIZE<256
#define STM_SECTOR_SIZE 1024 //<2F>ֽ<EFBFBD>
#else
#define STM_SECTOR_SIZE 2048
#endif
uint16_t STMFLASH_BUF[STM_SECTOR_SIZE / 2];//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2K<32>ֽ<EFBFBD>
/**----------------------------------------------------------------------
* Function : STMFLASH_ReadHalfWord
* Description : <EFBFBD><EFBFBD>ȡָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>(16λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* Input : faddr:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ(<EFBFBD>˵<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ2<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>)
* Output : <EFBFBD><EFBFBD>Ӧ16λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com & JustFeng.
* Date : 2015/10/3 zhanli
*---------------------------------------------------------------------**/
uint16_t STMFLASH_ReadHalfWord(uint32_t faddr)
{
return *(uint16_t*)faddr;
}
/**----------------------------------------------------------------------
* Function : STMFLASH_Write_NoCheck
* Description : <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>
* Input : WriteAddr : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
pBuffer : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
NumToWrite : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>(16λ)<EFBFBD><EFBFBD>
* Output : <EFBFBD><EFBFBD>Ӧ16λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com & JustFeng.
* Date : 2015/10/3 zhanli
*---------------------------------------------------------------------**/
void STMFLASH_Write_NoCheck(uint32_t WriteAddr,uint16_t *pBuffer,
uint16_t NumToWrite)
{
uint16_t i;
for(i = 0;i < NumToWrite;i++)
{
FLASH_ProgramHalfWord(WriteAddr, pBuffer[i]);
// <20><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>2.
WriteAddr += 2;
}
}
/**----------------------------------------------------------------------
* Function : STMFLASH_Write
* Description : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>׷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ʼд<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
WriteAddr:<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ(<EFBFBD>˵<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ2<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>)
pBuffer:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ʼд<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com & JustFeng.
* Date : 2015/2/13 zhanli
*---------------------------------------------------------------------**/
void STMFLASH_Write(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite)
{
uint32_t secpos; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
uint16_t secoff; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƫ<EFBFBD>Ƶ<EFBFBD>ַ(16λ<36>ּ<EFBFBD><D6BC><EFBFBD>)
uint16_t secremain; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD>ַ(16λ<36>ּ<EFBFBD><D6BC><EFBFBD>)
uint16_t i;
uint32_t offaddr; //ȥ<><C8A5>0X08000000<30><30><EFBFBD>ĵ<EFBFBD>ַ
if(WriteAddr<STM32_FLASH_BASE||(WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ַ
FLASH_Unlock(); // <20><><EFBFBD><EFBFBD>
offaddr=WriteAddr-STM32_FLASH_BASE; // ʵ<><CAB5>ƫ<EFBFBD>Ƶ<EFBFBD>ַ.
secpos=offaddr/STM_SECTOR_SIZE; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ 0~127 for STM32F103RBT6
secoff=(offaddr%STM_SECTOR_SIZE)/2; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ƫ<EFBFBD><C6AB>(2<><32><EFBFBD>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ.)
secremain=STM_SECTOR_SIZE/2-secoff; // <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD>С
if(NumToWrite<=secremain)secremain=NumToWrite; // <20><><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
while(1){
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);
// У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i = 0;i < secremain; i++){
if(STMFLASH_BUF[secoff+i] != 0XFFFF)break;//<2F><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
}
// <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
if(i < secremain){
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
FLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);
// <20><><EFBFBD><EFBFBD>
for(i = 0;i < secremain; i++){
STMFLASH_BUF[i+secoff]=pBuffer[i];
}
// д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);
}else{
// д<>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><><D6B1>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);
}
if(NumToWrite == secremain)break;//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else//д<><D0B4>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>
{
secpos++; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>1
secoff = 0; //ƫ<><C6AB>λ<EFBFBD><CEBB>Ϊ0
pBuffer += secremain; //ָ<><D6B8>ƫ<EFBFBD><C6AB>
WriteAddr += secremain; //д<><D0B4>ַƫ<D6B7><C6AB>
NumToWrite -= secremain; //<2F>ֽ<EFBFBD>(16λ)<29><><EFBFBD>ݼ<EFBFBD>
if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2; //<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
else secremain = NumToWrite; //<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
}
};
FLASH_Lock();//<2F><><EFBFBD><EFBFBD>
}
/**----------------------------------------------------------------------
* Function : STMFLASH_Read
* Description : <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ReadAddr<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
pBuffer : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
NumToWrite<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com & JustFeng.
* Date : 2015/2/13 zhanli
*---------------------------------------------------------------------**/
void STMFLASH_Read(uint32_t ReadAddr,uint16_t *pBuffer,uint16_t NumToRead)
{
uint16_t i;
for(i = 0;i < NumToRead; i++){
// <20><>ȡ2<C8A1><32><EFBFBD>ֽ<EFBFBD>.
pBuffer[i] = STMFLASH_ReadHalfWord(ReadAddr);
// <20><>ȡ<EFBFBD><C8A1>ַƫ<D6B7><C6AB>2<EFBFBD><32><EFBFBD>ֽ<EFBFBD>.
ReadAddr += 2;
}
}