Code:
uint8 param_set_entry(uint8 EntryNum, uint16 EntrySize, uint32 *EntryPtr)
{
uint8 retval;
uint8 retry;
uint16 sect;
uint16 size;
uint32 *buff = NULL;
SpiFlashOpResult result;
if((EntryNum < PARAM_ENTRY_MAX) && (EntrySize <= CONFIG_PARAM_ENTRY_SIZE))
{
sect = (CONFIG_PARAM_USRSEC_OFFSET + EntryNum * CONFIG_PARAM_ENTRY_SIZE) / CONFIG_SPI_FLASH_SEC_SIZE;
/* Allocate buff for a sector */
if( (PARAM_ENTRY_MAX * CONFIG_PARAM_ENTRY_SIZE) <= CONFIG_SPI_FLASH_SEC_SIZE )
{
/* There is only 1 sector used, the current Entry locates in the 1st Sector */
size = PARAM_ENTRY_MAX * CONFIG_PARAM_ENTRY_SIZE;
}
else /* There is only 2 sector used*/
{
if((EntryNum * CONFIG_PARAM_ENTRY_SIZE) >= CONFIG_SPI_FLASH_SEC_SIZE)
{
/*the current Entry locates in the 2st Sector*/
size = PARAM_ENTRY_MAX * CONFIG_PARAM_ENTRY_SIZE - CONFIG_SPI_FLASH_SEC_SIZE;
}
else /*the current Entry locates in the 1st Sector*/
{
size = CONFIG_SPI_FLASH_SEC_SIZE;
}
}
buff = (uint32 *)malloc(size);
if(NULL == buff)
return false;
/*Try to backup Entries to be erased */
retry = CONFIG_PARAM_RETRY;
retval = false;
while(retry)
{
result = spi_flash_read((sect * CONFIG_SPI_FLASH_SEC_SIZE), buff, (uint32)size);
switch(result)
{
case SPI_FLASH_RESULT_OK:
{
retval = true;
retry = 1;
break;
}
default:
{
break;;
}
}
retry --;
}
if(false == retval)
goto Failed;
/*Try to erased sector */
retry = CONFIG_PARAM_RETRY;
retval = false;
while(retry)
{
result = spi_flash_erase_sector(sect);
switch(result)
{
case SPI_FLASH_RESULT_OK:
{
retval = true;
retry = 1;
break;
}
default:
{
break;;
}
}
retry --;
}
if(false == retval)
goto Failed;
/* Merge Data into buffer */
memcpy((buff + ((EntryNum * CONFIG_PARAM_ENTRY_SIZE) % CONFIG_SPI_FLASH_SEC_SIZE)), EntryPtr, EntrySize);
/*Try to Write Entries to Erased Sector */
retry = CONFIG_PARAM_RETRY;
retval = false;
while(retry)
{
result = spi_flash_write((sect * CONFIG_SPI_FLASH_SEC_SIZE), buff, (uint32)size);
switch(result)
{
case SPI_FLASH_RESULT_OK:
{
retval = true;
retry = 1;
break;
}
default:
{
break;;
}
}
retry --;
}
Failed:
if(NULL != buff)
free(buff);
return retval;
}
return false;
}
Statistics: Posted by esp8266ac — Wed Mar 16, 2016 11:55 pm
SPI Flash 读写速度太慢了, 以一个Sector 为例, 从读回数据备份,擦除Sector,写回数据, 4K数据需要耗费5分钟左右
Statistics: Posted by ESP_Faye — Wed Mar 16, 2016 10:57 am
Statistics: Posted by esp8266ac — Wed Mar 16, 2016 9:45 am
Statistics: Posted by esp8266ac — Tue Mar 08, 2016 2:41 pm