Optimize functions related to flash operation

This commit is contained in:
wgzAIIT 2023-05-31 18:11:57 +08:00
parent 938d46d5d9
commit 5faa3e7b5a
5 changed files with 214 additions and 232 deletions

View File

@ -507,7 +507,7 @@ static status_t flexspi_config_mcr1(uint32_t instance, flexspi_mem_config_t *con
// Configure MCR1
FLEXSPI->MCR1 = FLEXSPI_MCR1_SEQWAIT(seqWaitTicks) | FLEXSPI_MCR1_AHBBUSWAIT(ahbBusWaitTicks);
return kStatus_Success;
return (status_t)kStatus_Success;
}
@ -647,14 +647,14 @@ uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len)
/*******************************************************************************
* : FLASH_Read
* : FLASH_ReadBuf
* : Flash内容
* : addr:
buf:
len:
* : kStatus_Success
*******************************************************************************/
status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len)
status_t FLASH_ReadBuf(uint32_t addr, uint32_t *buf, uint32_t len)
{
status_t status;
flexspi_xfer_t flashXfer;
@ -678,206 +678,28 @@ status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len)
}
/*******************************************************************************
* : flash_erase
* : Flash指定长度的空间
* : addr:
byte_cnt:,4k字节为最小擦除单位
* : kStatus_Success
* : 4k字节的4k字节
*******************************************************************************/
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt)
{
uint32_t addr;
status_t status;
addr = start_addr;
while(addr < (byte_cnt + start_addr))
{
status = FLASH_EraseSector(addr);
if(status != kStatus_Success)
{
return status;
}
addr += FLASH_GetSectorSize();
}
return status;
}
/*******************************************************************************
* : flash_write
* : flash起始地址写入指定长度的数据
* : addr:
buf:
byte_cnt:
* : kStatus_Success
*******************************************************************************/
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt)
{
uint32_t size;
status_t status;
while(byte_cnt > 0)
{
size = byte_cnt > FLASH_PAGE_SIZE ? FLASH_PAGE_SIZE : byte_cnt;
status = FLASH_WritePage(start_addr, (void *)buf, size);
if(status != kStatus_Success)
{
return status;
}
start_addr += size;
buf += size;
byte_cnt -= size;
}
return kStatus_Success;
}
/*******************************************************************************
* : flash_read
* : Flash内容
* : addr:
buf:
len:
* : kStatus_Success
*******************************************************************************/
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len)
{
/* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */
if((addr >= 0x60000000) && (addr < 0x61000000))
{
return FLASH_Read(addr, (void *)buf, len);
}
else
{
void* result = memcpy(buf, (void*)addr, len);
if(result == NULL)
{
return (status_t)kStatus_Fail;
}
else
{
return (status_t)kStatus_Success;
}
}
}
/*******************************************************************************
* : flash_copy
* : flash数据在分区之间的拷贝
* : srcAddr:flash的起始地址
dstAddr:flash的起始地址;
imageSize:flash空间大小,
* : kStatus_Success
*******************************************************************************/
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
{
uint32_t PageNum, Remain, i;
status_t status;
if((srcAddr == dstAddr) || imageSize > APP_FLASH_SIZE)
{
return (status_t)kStatus_Fail;
}
status = flash_erase(dstAddr,imageSize);
if(status != kStatus_Success)
{
KPrintf("Erase flash 0x%08x failure !\r\n",dstAddr);
return status;
}
PageNum = imageSize/FLASH_PAGE_SIZE;
Remain = imageSize%FLASH_PAGE_SIZE;
for(i=0;i<PageNum;i++)
{
memset(buffer, 0, sizeof(buffer));
status = flash_read(srcAddr + i*FLASH_PAGE_SIZE, buffer, sizeof(buffer));
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = flash_write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, FLASH_PAGE_SIZE);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
if(Remain)
{
memset(buffer, 0, sizeof(buffer));
status = flash_read(srcAddr + i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = flash_write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : NOR_FLASH_Erase
* : Flash指定长度的空间,imageSize
* : addr:
imageSize:
* : None
*******************************************************************************/
status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize)
{
uint16_t i;
uint32_t sectorNum = (imageSize%SECTOR_SIZE != 0)? (imageSize/SECTOR_SIZE + 1):(imageSize/SECTOR_SIZE);
for(i=0;i<sectorNum;i++)
{
status_t status = FLASH_EraseSector(app_base_addr+i*SECTOR_SIZE);
if (status != kStatus_Success)
{
KPrintf("Erase_Sector 0x%x faild!\r\n",i*SECTOR_SIZE);
return status;
}
}
return kStatus_Success;
}
/*******************************************************************************
* : NorFlash_Write_PageProgram
* : Flash指定长度的数据
* : pBuffer:
WriteAddr:
NumByteToWrite:(256)
* : kStatus_Success
* : , kStatus_Success,
* : 256
*******************************************************************************/
void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
status_t NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint8_t temp_data[256] = {0xff};
memcpy(temp_data,pBuffer,NumByteToWrite);
status_t status = FLASH_WritePage(WriteAddr,(void *)temp_data,FLASH_PAGE_SIZE);
if (status != kStatus_Success)
if(status != kStatus_Success)
{
KPrintf("Write_PageProgram 0x%x faild!\r\n",WriteAddr);
}
return (status_t)kStatus_Success;
}
@ -887,13 +709,14 @@ void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t Num
* : pBuffer:
WriteAddr:(24bit)
NumByteToWrite:(65535)
* :
* : , kStatus_Success,
* : 0XFF,0XFF处写入的数据将失败!
,,!
*******************************************************************************/
void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
status_t NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint16_t pageRemain;
uint16_t pageRemain;
status_t status;
pageRemain = 256 - WriteAddr%256;//单页剩余的字节数
@ -904,7 +727,12 @@ void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByte
while(1)
{
NorFlash_Write_PageProgram(pBuffer,WriteAddr,pageRemain);
status = NorFlash_Write_PageProgram(pBuffer,WriteAddr,pageRemain);
if(status != kStatus_Success)
{
KPrintf("Write_PageProgram 0x%x faild!\r\n",WriteAddr);
return status;
}
if(NumByteToWrite == pageRemain)
{
break;//写入结束了
@ -925,25 +753,54 @@ void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByte
}
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : NorFlash_Write
* : Flash_Erase
* : Flash指定长度的空间,imageSize
* : start_addr:
imageSize:
* : , kStatus_Success,
*******************************************************************************/
status_t Flash_Erase(uint32_t start_addr, uint32_t imageSize)
{
uint16_t i;
status_t status;
uint32_t sectorNum = (imageSize%SECTOR_SIZE != 0)? (imageSize/SECTOR_SIZE + 1):(imageSize/SECTOR_SIZE);
for(i=0;i<sectorNum;i++)
{
status = FLASH_EraseSector(start_addr+i*SECTOR_SIZE);
if (status != kStatus_Success)
{
KPrintf("Erase_Sector 0x%x faild!\r\n",i*SECTOR_SIZE);
return status;
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : Flash_Write
* : W25QXX在指定地址开始写入指定长度的数据
* : pBuffer:
WriteAddr:(24bit)
WriteAddr:
NumByteToWrite:(65535)
* : None
* : , kStatus_Success,
* :
*******************************************************************************/
void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWrite)
{
uint32_t secPos;
uint16_t secOff;
uint16_t secRemain;
uint16_t i;
uint8_t *NorFlash_BUF = 0;
status_t status;
NorFlash_BUF = NorFlash_BUFFER;//RAM缓冲区4K
@ -959,7 +816,11 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
}
while(1)
{
FLASH_Read(CHIP_FLAH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
status = FLASH_ReadBuf(CHIP_FLAH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
if (status != kStatus_Success)
{
return status;
}
for(i=0;i<secRemain;i++)//校验数据
{
if(NorFlash_BUF[secOff+i] != 0xFF)
@ -969,16 +830,28 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
}
if(i < secRemain)//需要擦除
{
FLASH_EraseSector(CHIP_FLAH_BASE + secPos*SECTOR_SIZE);
status = FLASH_EraseSector(CHIP_FLAH_BASE + secPos*SECTOR_SIZE);
if (status != kStatus_Success)
{
return status;
}
for(i=0;i<secRemain;i++)//复制
{
NorFlash_BUF[i+secOff] = pBuffer[i];
}
NorFlash_Write_NoCheck(NorFlash_BUF,CHIP_FLAH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区
status = NorFlash_Write_NoCheck(NorFlash_BUF,CHIP_FLAH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区
if (status != kStatus_Success)
{
return status;
}
}
else
{
NorFlash_Write_NoCheck(pBuffer,CHIP_FLAH_BASE + WriteAddr,secRemain);//写已经擦除了的,直接写入扇区剩余区间.
status = NorFlash_Write_NoCheck(pBuffer,CHIP_FLAH_BASE + WriteAddr,secRemain);//写已经擦除了的,直接写入扇区剩余区间.
if (status != kStatus_Success)
{
return status;
}
}
if(NumByteToWrite == secRemain)
@ -1003,33 +876,138 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
}
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : Flash_Read
* : Flash内容
* : addr:
buf:
len:
* : kStatus_Success
*******************************************************************************/
status_t Flash_Read(uint32_t addr, uint8_t *buf, uint32_t len)
{
/* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */
if((addr >= 0x60000000) && (addr < 0x61000000))
{
return FLASH_ReadBuf(addr, (void *)buf, len);
}
else
{
void* result = memcpy(buf, (void*)addr, len);
if(result == NULL)
{
return (status_t)kStatus_Fail;
}
else
{
return (status_t)kStatus_Success;
}
}
}
/*******************************************************************************
* : Flash_Copy
* : flash数据在分区之间的拷贝
* : srcAddr:flash的起始地址
dstAddr:flash的起始地址;
imageSize:flash空间大小,
* : kStatus_Success
*******************************************************************************/
status_t Flash_Copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
{
uint32_t PageNum, Remain, i;
status_t status;
if((srcAddr == dstAddr) || imageSize > APP_FLASH_SIZE)
{
return (status_t)kStatus_Fail;
}
status = Flash_Erase(dstAddr,imageSize);
if(status != kStatus_Success)
{
KPrintf("Erase flash 0x%08x failure !\r\n",dstAddr);
return status;
}
PageNum = imageSize/FLASH_PAGE_SIZE;
Remain = imageSize%FLASH_PAGE_SIZE;
for(i=0;i<PageNum;i++)
{
memset(buffer, 0, sizeof(buffer));
status = Flash_Read(srcAddr + i*FLASH_PAGE_SIZE, buffer, sizeof(buffer));
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = Flash_Write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, FLASH_PAGE_SIZE);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
if(Remain)
{
memset(buffer, 0, sizeof(buffer));
status = Flash_Read(srcAddr + i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = Flash_Write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : NOR_FLASH_Write
* : W25QXX在指定地址开始写入指定长度的数据
* : FlashAddress:Flash地址的指针
Data:
DataLength:
* : 0
* : , kStatus_Success,
*******************************************************************************/
#ifndef USE_HIGHT_SPEED_TRANS
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength)
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength)
{
status_t status;
uint32_t WriteAddr;
WriteAddr = *FlashAddress;
NorFlash_Write(Data,WriteAddr,DataLength);
status = Flash_Write(WriteAddr,Data,DataLength);
if (status != kStatus_Success)
{
return status;
}
*FlashAddress += DataLength;
return 0;
return (status_t)kStatus_Success;
}
#else
uint8_t packetNum = 0;
uint32_t dataLen = 0;
uint32_t WriteAddr;
uint8_t dataBuff[5*1024];
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag)
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag)
{
status_t status;
if(!doneFlag)
{
memcpy(&dataBuff[dataLen],Data,DataLength);
@ -1042,7 +1020,11 @@ uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
if(dataLen>=SECTOR_SIZE)
{
NorFlash_Write(dataBuff,WriteAddr,dataLen);
status = Flash_Write(WriteAddr,dataBuff,dataLen);
if (status != kStatus_Success)
{
return status;
}
packetNum = 0;
dataLen = 0;
}
@ -1050,10 +1032,14 @@ uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
}
else
{
NorFlash_Write(dataBuff,WriteAddr,dataLen);
status = Flash_Write(WriteAddr,dataBuff,dataLen);
if (status != kStatus_Success)
{
return status;
}
packetNum = 0;
dataLen = 0;
}
return (0);
return (status_t)kStatus_Success;;
}
#endif

View File

@ -280,7 +280,7 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
}
/* erase user application area */
NOR_FLASH_Erase(addr,size);
Flash_Erase(addr,size);
Send_Byte(ACK);
Send_Byte(CRC16);
}
@ -300,9 +300,9 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
/* Write received data in Flash */
#ifndef USE_HIGHT_SPEED_TRANS
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length) == 0)
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length) == kStatus_Success)
#else
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length, 0) == 0)
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length, 0) == kStatus_Success)
#endif
{
Send_Byte(ACK);
@ -349,7 +349,10 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
}
}
#ifdef USE_HIGHT_SPEED_TRANS
NOR_FLASH_Write(&flashdestination, buf, (uint16_t) packet_length,1);
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t) packet_length,1) != kStatus_Success)
{
return -4;
}
#endif
return (int32_t)size;
}

View File

@ -63,21 +63,19 @@ void FLASH_Init(void);
void FLASH_DeInit(void);
uint8_t FLASH_EraseSector(uint32_t addr);
uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len);
status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len);
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt);
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt);
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len);
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);
status_t FLASH_ReadBuf(uint32_t addr, uint32_t *buf, uint32_t len);
status_t NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
status_t NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize);
void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
status_t Flash_Erase(uint32_t start_addr, uint32_t imageSize);
status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWrite);
status_t Flash_Read(uint32_t addr, uint8_t *buf, uint32_t len);
status_t Flash_Copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);
#ifndef USE_HIGHT_SPEED_TRANS
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength);
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength);
#else
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag);
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag);
#endif
#endif

View File

@ -30,8 +30,8 @@ typedef struct
void (*flash_deinit)(void);
/* flash operation */
status_t (*op_flash_erase)(uint32_t start_addr, uint32_t byte_cnt);
status_t (*op_flash_write)(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt);
status_t (*op_flash_erase)(uint32_t start_addr, uint32_t imageSize);
status_t (*op_flash_write)(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWrite);
status_t (*op_flash_read)(uint32_t addr, uint8_t *buf, uint32_t len);
status_t (*op_flash_copy)(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);

View File

@ -39,11 +39,6 @@ static void BackupVersion(void);
static void BootLoaderJumpApp(void);
static status_t UpdateOTAFlag(ota_info_t *ptr);
#ifdef MCUBOOT_APPLICATION
static void app_ota(void);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
@ -54,10 +49,10 @@ static const mcuboot_t mcuboot =
Serial_PutString,
FLASH_Init,
FLASH_DeInit,
flash_erase,
flash_write,
flash_read,
flash_copy,
Flash_Erase,
Flash_Write,
Flash_Read,
Flash_Copy,
SerialDownload,
mcuboot_reset,
mcuboot_jump,