WouoUI/Third-party-design/platform-test/User/flash.c

211 lines
5.3 KiB
C
Raw Normal View History

#include "flash.h"
uint16_t STMFLASH_BUF[STM_SECTOR_SIZE/2];//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2K<32>ֽ<EFBFBD>
/**
* @brief Unlock the FLASH control register access
* @retval LL Status
*/
LL_StatusTypeDef LL_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 LL_ERROR;
}
}
else
return LL_ERROR;
return LL_OK;
}
/**
* @brief Locks the FLASH control register access
* @retval LL Status
*/
LL_StatusTypeDef LL_FLASH_Lock(void)
{
/* Set the LOCK Bit to lock the FLASH Registers access */
SET_BIT(FLASH->CR, FLASH_CR_LOCK);
return LL_OK;
}
/**
* @brief Get the FLASH Status
* @retval LL Status
*/
LL_StatusTypeDef LL_FLASH_GetStatus(void)
{
uint32_t res;
res=FLASH->SR;
if(res&(1<<0))return LL_BUSY; //æ
else if(res&(1<<2))return LL_ERROR; //<2F><><EFBFBD>̴<EFBFBD><CCB4><EFBFBD>
else if(res&(1<<4))return LL_PROTECT; //д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return LL_OK; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
LL_StatusTypeDef LL_FLASH_Wait(void)
{
LL_StatusTypeDef res;
uint16_t time = 50000;
res = LL_FLASH_GetStatus();
while(res == LL_BUSY)
{
res = LL_FLASH_GetStatus();
if(time==0)
{
res = LL_TIMEOUT;
break;
}
time--;
}
return res;
}
LL_StatusTypeDef LL_FLASH_ErasePage(uint32_t PageAddress)
{
LL_StatusTypeDef res;
res = LL_FLASH_Wait();
if(res == LL_OK)
{
SET_BIT(FLASH->CR, FLASH_CR_PER); //ҳ<><D2B3><EFBFBD><EFBFBD>
WRITE_REG(FLASH->AR, PageAddress); //<2F><><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3>ַ
SET_BIT(FLASH->CR, FLASH_CR_STRT); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
res = LL_FLASH_Wait(); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(res != LL_BUSY)
FLASH->CR &= ~FLASH_CR_PER; //<2F><><EFBFBD><EFBFBD>PERλ
}
return res;
}
LL_StatusTypeDef LL_FLASH_ProgramHalfWord(uint32_t Address,uint16_t Data)
{
LL_StatusTypeDef res;
res=LL_FLASH_Wait();
if(res == LL_OK)
{
SET_BIT(FLASH->CR, FLASH_CR_PG); //<2F><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
*(__IO uint16_t*)Address = Data; //д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
res=LL_FLASH_Wait(); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
if(res != LL_BUSY) //<2F><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
FLASH->CR &= ~FLASH_CR_PG; //<2F><><EFBFBD><EFBFBD>PGλ
}
return res;
}
#if STM32_FLASH_WREN //<2F><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>д
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
//WriteAddr:<3A><>ʼ<EFBFBD><CABC>ַ
//pBuffer:<3A><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
//NumToWrite:<3A><><EFBFBD><EFBFBD>(16λ)<29><>
void STMFLASH_Write_NoCheck(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite)
{
uint16_t i;
for(i=0;i<NumToWrite;i++)
{
LL_FLASH_ProgramHalfWord(WriteAddr,pBuffer[i]);
WriteAddr+=2;//<2F><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>2
}
}
//<2F><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
//ReadAddr:<3A><>ʼ<EFBFBD><CABC>ַ
//pBuffer:<3A><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
//NumToWrite:<3A><><EFBFBD><EFBFBD>(16λ)<29><>
void STMFLASH_Read(uint32_t ReadAddr,uint16_t *pBuffer,uint16_t NumToRead)
{
uint16_t i;
for(i=0;i<NumToRead;i++)
{
pBuffer[i]=LL_FLASH_ReadHalfWord(ReadAddr);//<2F><>ȡ2<C8A1><32><EFBFBD>ֽ<EFBFBD>.
ReadAddr+=2;//ƫ<><C6AB>2<EFBFBD><32><EFBFBD>ֽ<EFBFBD>.
}
}
//<2F><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ʼд<CABC><D0B4>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
//WriteAddr:<3A><>ʼ<EFBFBD><CABC>ַ(<28>˵<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ϊ2<CEAA>ı<EFBFBD><C4B1><EFBFBD>!!)
//pBuffer:<3A><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
//NumToWrite:<3A><><EFBFBD><EFBFBD>(16λ)<29><>(<28><><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4><EFBFBD><EFBFBD>16λ<36><CEBB><EFBFBD>ݵĸ<DDB5><C4B8><EFBFBD>.)
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>ַ
LL_Flash_Unlock(); //<2F><><EFBFBD><EFBFBD>
offaddr=WriteAddr-STM32_FLASH_BASE; //ʵ<><CAB5>ƫ<EFBFBD>Ƶ<EFBFBD>ַ.
secpos=offaddr/STM_SECTOR_SIZE; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ 0~127 for STM32F103RBT6
secoff=(offaddr%STM_SECTOR_SIZE)/2; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ƫ<EFBFBD><C6AB>(2<><32><EFBFBD>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ.)
secremain=STM_SECTOR_SIZE/2-secoff; //<2F><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD>С
if(NumToWrite<=secremain)secremain=NumToWrite;//<2F><><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
while(1)
{
STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=0;i<secremain;i++) //У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//<2F><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
}
if(i<secremain) //<2F><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
{
LL_FLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=0;i<secremain;i++)//<2F><><EFBFBD><EFBFBD>
{
STMFLASH_BUF[i+secoff]=pBuffer[i];
}
STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}else STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//д<>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><><D6B1>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
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*2; //д<><D0B4>ַƫ<D6B7><C6AB>(16λ<36><CEBB><EFBFBD>ݵ<EFBFBD>ַ,<2C><>Ҫ*2)
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>
}
}
LL_FLASH_Lock();//<2F><><EFBFBD><EFBFBD>
}
#endif
//<2F><>ȡָ<C8A1><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD>İ<EFBFBD><C4B0><EFBFBD>(16λ<36><CEBB><EFBFBD><EFBFBD>)
//faddr:<3A><><EFBFBD><EFBFBD>ַ
//<2F><><EFBFBD><EFBFBD>ֵ:<3A><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>.
uint16_t LL_FLASH_ReadHalfWord(uint32_t faddr)
{
return *(__IO uint16_t*)faddr;
}
uint32_t LL_FLASH_ReadWord(uint32_t faddr)
{
uint32_t i = 0;
i = LL_FLASH_ReadHalfWord(faddr+2);
i = i<<16;
i |= LL_FLASH_ReadHalfWord(faddr);
return i;
}
void LL_FLASH_WriteHalfWord(uint32_t WriteAddr,uint16_t data)
{
STMFLASH_Write(WriteAddr,&data,1);
}
void LL_FLASH_WriteWord(uint32_t WriteAddr,uint32_t data)
{
uint16_t buffer[2] = {data,data>>16};
STMFLASH_Write(WriteAddr,buffer,2);
}