/**************************************************************************//**
 * @file     fmc.h
 * @version  V1.00
 * $Revision: 1 $
 * $Date: 14/07/15 2:52p $
 * @brief    N572 Flash Memory Controller Driver Header File
 *
 * @note
 * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
 *
 ******************************************************************************/
#ifndef __FMC_H__
#define __FMC_H__

#ifdef __cplusplus
extern "C"
{
#endif

/** @addtogroup N572_Device_Driver N572 Device Driver
  @{
*/

/** @addtogroup N572_FMC_Driver FMC Driver
  @{
*/


/** @addtogroup N572_FMC_EXPORTED_CONSTANTS FMC Exported Constants
  @{
*/


/*---------------------------------------------------------------------------------------------------------*/
/* Define Base Address                                                                                     */
/*---------------------------------------------------------------------------------------------------------*/
#define FMC_APROM_BASE          0x00000000UL    /*!< APROM Base Address           \hideinitializer */
#define FMC_APROM_END           0x00011FFFUL    /*!< APROM End Address            \hideinitializer */
#define FMC_CONFIG_BASE         0x00300000UL    /*!< User Configuration Address   \hideinitializer */

#define FMC_FLASH_PAGE_SIZE     0x200           /*!< Flash Page Size (512 bytes)   \hideinitializer */

/*---------------------------------------------------------------------------------------------------------*/
/*  ISPCMD constant definitions                                                                            */
/*---------------------------------------------------------------------------------------------------------*/
#define FMC_ISPCMD_READ         0x00            /*!< ISP Command: Read flash word          \hideinitializer */
#define FMC_ISPCMD_WRITE        0x21            /*!< ISP Command: Write flash word         \hideinitializer */
#define FMC_ISPCMD_PAGE_ERASE   0x22            /*!< ISP Command: Page Erase Flash         \hideinitializer */
#define FMC_ISPCMD_READ_CID     0x0B            /*!< ISP Command: Read Company ID          \hideinitializer */
#define FMC_ISPCMD_READ_DID     0x0C            /*!< ISP Command: Read Device ID           \hideinitializer */

/*---------------------------------------------------------------------------------------------------------*/
/*  Flash Page Erase Time constant definitions                                                                            */
/*---------------------------------------------------------------------------------------------------------*/
#define FMC_PAGE_ERASE_00_75MS	(0x05 << FMC_ISPCTL_ET_Pos)		/*!< Flash Page Erase Time = 0.75 ms          \hideinitializer */
#define FMC_PAGE_ERASE_01_00MS	(0x04 << FMC_ISPCTL_ET_Pos)		/*!< Flash Page Erase Time = 1.00 ms          \hideinitializer */
#define FMC_PAGE_ERASE_01_50MS	(0x03 << FMC_ISPCTL_ET_Pos)		/*!< Flash Page Erase Time = 1.50 ms          \hideinitializer */
#define FMC_PAGE_ERASE_02_00MS	(0x00 << FMC_ISPCTL_ET_Pos)		/*!< Flash Page Erase Time = 2.00 ms          \hideinitializer */
#define FMC_PAGE_ERASE_02_50MS	(0x01 << FMC_ISPCTL_ET_Pos)		/*!< Flash Page Erase Time = 2.50 ms          \hideinitializer */
#define FMC_PAGE_ERASE_03_00MS	(0x02 << FMC_ISPCTL_ET_Pos)		/*!< Flash Page Erase Time = 3.00 ms          \hideinitializer */
#define FMC_PAGE_ERASE_04_00MS	(0x06 << FMC_ISPCTL_ET_Pos)		/*!< Flash Page Erase Time = 4.00 ms          \hideinitializer */
#define FMC_PAGE_ERASE_05_00MS	(0x07 << FMC_ISPCTL_ET_Pos)		/*!< Flash Page Erase Time = 5.00 ms          \hideinitializer */

/*---------------------------------------------------------------------------------------------------------*/
/*  Flash Page Program Time constant definitions                                                                            */
/*---------------------------------------------------------------------------------------------------------*/
#define FMC_PROGRAM_TIME_10_US	(0x06 << FMC_ISPCTL_PT_Pos)		/*!< Flash Program Time = 10 us          \hideinitializer */
#define FMC_PROGRAM_TIME_15_US	(0x07 << FMC_ISPCTL_PT_Pos)		/*!< Flash Program Time = 15 us          \hideinitializer */
#define FMC_PROGRAM_TIME_20_US	(0x00 << FMC_ISPCTL_PT_Pos)		/*!< Flash Program Time = 20 us          \hideinitializer */
#define FMC_PROGRAM_TIME_25_US	(0x01 << FMC_ISPCTL_PT_Pos)		/*!< Flash Program Time = 25 us          \hideinitializer */
#define FMC_PROGRAM_TIME_30_US	(0x02 << FMC_ISPCTL_PT_Pos)		/*!< Flash Program Time = 30 us          \hideinitializer */
#define FMC_PROGRAM_TIME_35_US	(0x03 << FMC_ISPCTL_PT_Pos)		/*!< Flash Program Time = 35 us          \hideinitializer */
#define FMC_PROGRAM_TIME_40_US	(0x04 << FMC_ISPCTL_PT_Pos)		/*!< Flash Program Time = 40 us          \hideinitializer */
#define FMC_PROGRAM_TIME_45_US	(0x05 << FMC_ISPCTL_PT_Pos)		/*!< Flash Program Time = 45 us          \hideinitializer */

/*@}*/ /* end of group N572_FMC_EXPORTED_CONSTANTS */


/** @addtogroup N572_FMC_EXPORTED_FUNCTIONS FMC Exported Functions
  @{
*/

/*---------------------------------------------------------------------------------------------------------*/
/*  Macros                                                                                                 */
/*---------------------------------------------------------------------------------------------------------*/
#define FMC_ENABLE_CFG_UPDATE()     FMC_EnableCfgUpdate()     /*!< Enable User Config update   \hideinitializer */
#define FMC_DISABLE_CFG_UPDATE()    FMC_DisableCfgUpdate()     /*!< Disable User Config update  \hideinitializer */

/*---------------------------------------------------------------------------------------------------------*/
/*  Functions                                                                                              */
/*---------------------------------------------------------------------------------------------------------*/

void FMC_EnableWrite (void);
void FMC_DisableWrite (void);
void FMC_Open(void);
void FMC_Close(void);
void FMC_SetPageEraseTime(uint32_t u32PageEraseTime);
void FMC_SetProgramTime(uint32_t u32ProgramTime);
int32_t FMC_Erase(uint32_t u32PageAddr);
void FMC_Write(uint32_t u32Addr, uint32_t u32Data);
int32_t FMC_WriteConfig(uint32_t *u32Config);
uint32_t FMC_Read(uint32_t u32Addr);
uint32_t FMC_ReadCID(void);
uint32_t FMC_ReadDID(void);
void FMC_ReadConfig(uint32_t *u32Config);
void FMC_EnsableCfgUpdate(void);
void FMC_DisableCfgUpdate(void);


/*@}*/ /* end of group N572_FMC_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group N572_FMC_Driver */

/*@}*/ /* end of group N572_Device_Driver */

#ifdef __cplusplus
}
#endif

#endif

/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
