Init_EEPROM()
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Initial Data Flash as EEPROM.
* @param[in] data_size: The amount of user's data, unit in byte. The maximun amount is
128.
* @param[in] use_pages: The amount of page which user want to use.
* @retval Err_OverPageSize: The amount of user's data is over than the maximun amount.
* @retval Err_OverPageAmount: The amount of page which user want to use is over than
the maximun amount.
* @retval 0: Success
*/
uint32_t Init_EEPROM(uint32_t data_amount, uint32_t use_pages)
{
uint32_t i;
/* The amount of data includes 1 byte address and 1 byte data */
Amount_of_Data = data_amount;
/* The amount of page which user want to use */
Amount_Pages = use_pages;
/* Check setting is valid or not */
/* The amount of user's data is more than the maximun amount or not */
if(Amount_of_Data > Max_Amount_of_Data)
return Err_OverAmountData;
/* For M051 Series, the max. amount of Data Flash pages is 8 */
if(Amount_Pages > 8)
return Err_OverPageAmount;
/* Init SRAM for data */
Written_Data = (uint8_t *)malloc(sizeof(uint8_t) * Amount_of_Data);
/* Fill initial data 0xFF*/
for(i = 0; i < Amount_of_Data; i++)
{
Written_Data[i] = 0xFF;
}
return 0;
}
/**
* @brief Search which page has valid data and where is current cursor for the next
data to write.
*/
void Search_Valid_Page(void)
{
uint32_t i, temp;
uint8_t addr, data;
uint16_t *Page_Status_ptr;
/* Enable FMC ISP function */
FMC_Enable();
/* Set information of each pages to Page_Status */
Page_Status_ptr = (uint16_t *)malloc(sizeof(uint16_t) * Amount_Pages);
for(i = 0; i < Amount_Pages; i++)
{
Page_Status_ptr[i] = (uint16_t)FMC_Read(DataFlash_BaseAddr +
(FMC_FLASH_PAGE_SIZE * i));
}
/* Search which page has valid data */
for(i = 0; i < Amount_Pages; i++)
{
if(Page_Status_ptr[i] != Status_Unwritten)
Current_Valid_Page = i;
}
/* If Data Flash is used for first time, set counter = 0 */
if(Page_Status_ptr[Current_Valid_Page] == Status_Unwritten)
{
/* Set counter = 0 */
FMC_Write(DataFlash_BaseAddr + (FMC_FLASH_PAGE_SIZE *
Current_Valid_Page), 0xFFFF0000);
/* Set cursor to current Data Flash address */
Current_Cursor = 2;
}
else
{
/* Search where is current cursor for the next data to write and get the
data has been written */
/* Check even value */
temp = FMC_Read(DataFlash_BaseAddr + (FMC_FLASH_PAGE_SIZE *
Current_Valid_Page));
addr = (temp & Even_Addr_Mask) >> Even_Addr_Pos;
data = (temp & Even_Data_Mask) >> Even_Data_Pos;
/* Check Address is 0xFF (un-written) of not */
if(addr == 0xFF)
{
/* If Address is 0xFF, then set cursor to current Data Flash
address */
Current_Cursor = 2;
}
else
{
/* Copy the address and data to SRAM */
Written_Data[addr] = data;
/* Check the whole Data Flash */
for(i = 4; i < FMC_FLASH_PAGE_SIZE; i += 4)
{
/* Check odd value */
temp = FMC_Read(DataFlash_BaseAddr +
(FMC_FLASH_PAGE_SIZE * Current_Valid_Page) + i);
addr = (temp & Odd_Addr_Mask) >> Odd_Addr_Pos;
data = (temp & Odd_Data_Mask) >> Odd_Data_Pos;
/* Check Address is 0xFF (un-written) of not */
if(addr == 0xFF)
{
/* If Address is 0xFF, then set cursor to
current Data Flash address */
Current_Cursor = i;
break;
}
else
{
/* Copy the address and data to SRAM */
Written_Data[addr] = data;
}
/* Check even value */
addr = (temp & Even_Addr_Mask) >> Even_Addr_Pos;
data = (temp & Even_Data_Mask) >> Even_Data_Pos;
/* Check Address is 0xFF (un-written) of not */
if(addr == 0xFF)
{
/* If Address is 0xFF, then set cursor to
current Data Flash address */
Current_Cursor = i + 2;
break;
}
else
{
/* Copy the address and data to SRAM */
Written_Data[addr] = data;
}
}
}
}
}
|