/****************************************************************************
*
* Copyright (C) Infineon Technologies (2011)
*
* All rights reserved.
*
* This document contains proprietary information belonging to Infineon
* Technologies. Passing on and copying of this document, and communication
* of its contents is not permitted without prior written authorization.
*
* No Warranty
* Because the program is licensed free of charge, there is no warranty for
* the program, to the extent permitted by applicable law. Except when
* otherwise stated in writing the copyright holders and/or other parties
* provide the program "as is" without warranty of any kind, either expressed
* or implied, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The entire risk as
* to the quality and performance of the program is with you. Should the
* program prove defective, you assume the cost of all necessary servicing,
* repair or correction.
*----------------------------------------------------------------------------
* Module : Emulated EEPROM Driver
* Filename : eeprom.c
* Controller : Infineon XC2000/XE166
*
* Version : 1.1
* Release Date : 8 September 2011
* Version : 1.2
* Release Date : 17 April 2011
*******************************************************************************/
/********************************************************************************************
Project Includes
********************************************************************************************/
#include <stddef.h>
#include "eeprom.h"
/********************************************************************************************
Variable Declaration
********************************************************************************************/
static ubyte Address_Count;
static ubyte Copy_Type;
static ulong Erase_Address;
ubyte EEPROM_JobType;
ubyte Erase_Status;
ubyte EEPROM_Status;
char __shuge *Write_ptr;
char __shuge *Erase_ptr;
static char __shuge *CopyData_ptr;
static char __shuge *NextActiveBlock;
static char __shuge *PreviousBlock_ptr;
static char __shuge *ActiveBlock_ptr;
static int __shuge *Read_page;
extern char DataToBeStored[];
/********************************************************************************************
Local Function Prototypes
********************************************************************************************/
static int EEPROM_intGetActiveBlock (void);
static void EEPROM_vCheckPointer (void);
static void EEPROM_vCopyData (void);
static void EEPROM_vEraseInactiveBlock (void);
static void EEPROM_vEraseReturn (void);
static int EEPROM_intCheckValidity (int __shuge *Checkptr);
static int EEPROM_intCheckAndFixed (void);
static void EEPROM_vIdentifyPage (void);
static void FLASH_Reset (void);
static void EEPROM_vUserEraseReturn (void);
static void EEPROM_vEraseBlock (unsigned long sector);
static void FLASH_vErase (unsigned long sector);
static void FLASH_vErasePage (unsigned long PA);
static void FLASH_vWrite (uword *dataPtr);
static void FLASH_Write (uword *dataPtr , unsigned long address);
static int FLASH_intGetValidData (int __shuge *Start_Read, uword address);
static void EEPROM_WriteRoutine (uword *DataPtr, uword address);
/******************************************************************************
Function int EEPROM_Init (void)
-------------------------------------------------------------------------------
Description This function initializes the Emulated EEPROM driver.
It will check for the EEPROM Emulation parameters and
and recover previously saved data in the FLASH. All block
will be erase except Active Block or no data found in the
FLASH.
-------------------------------------------------------------------------------
Returnvalue CONFIG_ERROR
INIT_SUCCESS
DATA_NOT_COMPLETE
-------------------------------------------------------------------------------
Parameters None
*******************************************************************************/
int EEPROM_Init (void)
{
int RetVal;
int RetVal2;
ubyte GLOBAL_INT;
if (EEPROM_Status == ALREADY_INIT)
{
return INIT_SUCCESS;
}
/* Check if start address align with the 4k boundary */
#if((EEPROM_START_ADDRESS & 0x0FFF) != 0)
#error CONFIG_ERROR: EEPROM_START_ADDRESS not align with 4k boundary
#endif
/* Check if emulation block size is align with the 4k boundary */
#if((EEPROM_BLOCK_SIZE & 0x0FFF) != 0)
#error CONFIG_ERROR: EEPROM_BLOCK_SIZE not align with 4k boundary
#endif
/* Check if emulation block size is less than 1 sector*/
#if(EEPROM_BLOCK_SIZE < 0x1000)
#error CONFIG_ERROR: EEPROM_BLOCK_SIZE is less than 1 sector
#endif
/* Check if emulation block size is less than emulation data set size*/
#if (EEPROM_BLOCK_SIZE <= (NO_OF_PAGE*0x080))
#error CONFIG_ERROR: EEPROM_BLOCK_SIZE is less than emulation data set size
#endif
/*Check if the total emulation size is the multiple of emulation block size*/
#if ((TOTAL_EMULATION_SIZE % EEPROM_BLOCK_SIZE) != 0)
#error CONFIG_ERROR: TOTAL_EMULATION_SIZE is not the multiple of emulation block size
#endif
/* Store the current interrupt status and disable the interrupt*/
GLOBAL_INT = PSW_IEN;
/* Disable global interrupt */
PSW_IEN = 0;
RetVal = EEPROM_intGetActiveBlock ();
if (RetVal == INVALID)
{
return DATA_FULL;
}
else
{
RetVal2 = EEPROM_intCheckAndFixed();
}
NextActiveBlock = (char __shuge *)(((ulong)ActiveBlock_ptr) + EEPROM_BLOCK_SIZE);
EEPROM_Status = ALREADY_INIT;
EEPROM_vCheckPointer();
while (EEPROM_JobType != IDLE)
{
while(FLASH_BUSY_STATUS != 0);
EEPROM_vIntProcess();
}
EEPROM_vEraseInactiveBlock();
EEPROM_vIdentifyPage();
/* Restore previous global interrupt status */
PSW_IEN = GLOBAL_INT;
if (RetVal == VALID)
{
if (RetVal2 == VALID)
{
return INIT_SUCCESS;
}
else
{
return DATA_INCOMPLETE;
}
}
else
{
return DATA_EMPTY;
}
/* End of function */
}
/******************************************************************************
Function FLASH_vErase (unsigned long sector)
-------------------------------------------------------------------------------
Description This function shall erase the sector of the provided
address in the flash memory.
-------------------------------------------------------------------------------
Returnvalue None
-------------------------------------------------------------------------------
Parameters sector:
- Address of the sector to be erase
*******************************************************************************/
static void FLASH_vErase (unsigned long sector)
{
unsigned int __shuge *Flash_Command_1 = (unsigned int __shuge *) 0x0c000AA;
unsigned int __shuge *Flash_Command_2 = (unsigned int __shuge *) 0x0c00054;
unsigned int __shuge *Flash_Command_3 = (unsigned int __shuge *) sector;
while(FLASH_BUSY_STATUS != 0);
/* erase sector command sequence */
*Flash_Command_1 = 0x0080;
*Flash_Command_2 = 0x00AA;
*Flash_Command_3 = 0x0033;
/* End of function */
}
static void FLASH_vErasePage (unsigned long PA)
{
unsigned int __shuge *Flash_Command_1 = (unsigned int __shuge *) 0x0c000AA;
unsigned int __shuge *Flash_Command_2 = (unsigned int __shuge *) 0x0c00054;
unsigned int __shuge *Flash_Command_3 = (unsigned int __shuge *) PA;
while(FLASH_BUSY_STATUS != 0);
/* erase sector command sequence */
*Flash_Command_1 = 0x0080;
*Flash_Command_2 = 0x00AA;
*Flash_Command_3 = 0x0003;
/* End of function */
}
/******************************************************************************
Function int EEPROM_intWrite (uword *Dat