#ifndef MEM_HWABSTRACTION_H_
#define MEM_HWABSTRACTION_H_
#include "platmacro.h"


/*****************************************************************************
 *  Macro Definitions
 *****************************************************************************/
#define MEM_MEMORY_TYPE_FLASH           (0x0001u) /**< A flash.*/
#define MEM_MEMORY_TYPE_EEPROM          (0x0002u) /**< A eeprom.*/
#define MEM_MEMORY_TYPE_RAM             (0x0004u) /**< A ram.*/
#define MEM_MEMORY_TYPE_VIRTUAL         (0x0008u) /**< A logical memory.*/
#define MEM_MEMORY_TYPE_EXT_FLASH       (0x0010u) /**< like flash.*/
#define MEM_MEMORY_TYPE_EXT_EEPROM      (0x0020u) /**< like eeprom.*/
#define MEM_MEMORY_TYPE_EXT_RAM         (0x0040u) /**< like ram.*/
#define MEM_MEMORY_TYPE_EXT_VIRTUAL     (0x0080u) /**< like virtual.*/
#define MEM_MEMORY_TYPE_EXTERNAL_DRV    (0x8000u) /**< the extrnal driver.*/

#define MEM_MEMORY_ID_EXT_FLASH       (0x0003u) /**< like flash.*/
#define MEM_MEMORY_ID_FLASH           (0x0002u) /**< A flash.*/
#define MEM_MEMORY_ID_EEPROM          (0x0001u) /**< A eeprom.*/
#define MEM_MEMORY_ID_RAM             (0x0000u) /**< A ram.*/

#define MEM_MEMORY_TYPE_SIZE        (2) /** the size of type of the memory.*/
#define MEM_MEMORY_INDEX_SIZE       (2) /** the size of index of the memory.*/

#define MEM_ERROR_DRVID     0xffff   /**< invalid driver id.*/
#define MEM_ERROR_MEMID     0xffff   /**< invalid memory id.*/

/*****************************************************************************
 *  Type Declarations
 *****************************************************************************/
/** \brief A alias of the struct _tag_MemoryOperations.*/
typedef struct _tag_MemoryOperations bl_MemoryOperations_t;
/** \brief A alias of the struct _tag_MemoryInfo.*/
typedef struct _tag_MemoryInfo bl_MemoryInfo_t;
/** \brief A alias of the struct _tag_MemoryInfoList.*/
typedef struct _tag_MemoryInfoList bl_MemoryInfoList_t;

/*****************************************************************************
 *  Structure Definitions
 *****************************************************************************/
/**
 *  \brief the memory operations.
 */
struct _tag_MemoryOperations
{
    Std_Ret_Type (*Init)(void);      /**< the initialization interface.*/
    Std_Ret_Type (*Deinit)(void);    /**< the deinitialization interface.*/
    Std_Ret_Type (*Write)(Std_Addr_Type address,
                          Std_Size_Type size,
                          const Std_Buf_Type *buffer);  /**< the write interface.*/
    Std_Ret_Type (*Read)(Std_Addr_Type address,
                         Std_Size_Type size,
                         Std_Buf_Type *buffer);         /**< the read interface.*/
    Std_Ret_Type (*Erase)(Std_Addr_Type address,
                          Std_Size_Type size);          /**< the erase interface.*/
    Std_Ret_Type (*ReadWithDft)(Std_Addr_Type address,  /*read with dft value interface*/
                                Std_Size_Type size,Std_Buf_Type *buffer, \
                                Std_Base_Type level, const Std_Buf_Type *dft );
    Std_Ret_Type (*BlockWrite)(Std_Addr_Type address,
                      Std_Size_Type size,
                      const Std_Buf_Type *buffer); /**< the block write interface.*/
};

/**
 *  \brief the memory informations.
 */
struct _tag_MemoryInfo
{
    uint16_t type;              /**< the type of a memory.*/
    uint16_t index;             /**< the index of a memory.*/
    Std_Addr_Type addr;          /**< the base address of a memory.*/
    Std_Size_Type size;             /**< the size of a memory.*/
    const struct _tag_MemoryOperations * ops;/**< the operations list.*/
};

/**
 *  \brief the list of the memory informations.
 */
struct _tag_MemoryInfoList
{
    uint16_t number;            /**< the number of memory managed by module.*/
    const struct _tag_MemoryInfo *memList;  /**< the memory information list.*/
};

/*****************************************************************************
 *  External Global Variable Declarations
 *****************************************************************************/

/*****************************************************************************
 *  External Function Prototype Declarations
 *****************************************************************************/
/** \brief This function is used to initialize the memory manager module.*/
extern Std_Ret_Type Mem_Init(void);
/** \brief This function is used to initialize a memory by id.*/
extern Std_Ret_Type Mem_InitMemory(uint16_t id);
/** \brief This function is used to de-initialize a memory by id.*/
extern Std_Ret_Type Mem_DeinitMemory(uint16_t id);
/** \brief This function is used to read data from a memory.*/
extern Std_Ret_Type Mem_Read(uint16_t id,
                             Std_Addr_Type address,
                             Std_Size_Type size,
                             Std_Buf_Type *buffer);
/** \brief This function is used to write data to a memory.*/
extern Std_Ret_Type Mem_Write(uint16_t id,
                              Std_Addr_Type address,
                              Std_Size_Type size,
                              const Std_Buf_Type *buffer);

/** \brief This function is used to write data to a memory.*/
Std_Ret_Type Mem_BlockWrite(uint16_t id,
                       Std_Addr_Type address,
                       Std_Size_Type size,
                       const Std_Buf_Type *buffer);

/** \brief This function is used to read data from a memory.*/
Std_Ret_Type Mem_ReadWithDft(uint16_t id,
                      Std_Addr_Type address,
                      Std_Size_Type size,
                      Std_Buf_Type *buffer,
                      Std_Base_Type level,
                      Std_Buf_Type *dft);


/** \brief This function is used to erase a memory.
    *当SIZE == 0 表示擦除最小单位
*/
extern Std_Ret_Type Mem_Erase(uint16_t id,Std_Addr_Type address,Std_Size_Type size);
/** \brief This function is used to look for a memory.*/
extern Std_Ret_Type Mem_LookforMemId(uint16_t type,
                                     uint16_t index,
                                     uint16_t *mid);
/** \brief This function is used to check the driver status.*/
extern Std_Ret_Type Mem_IsExternalDrv(uint16_t id);
/** \brief This function is used to check the memory status.*/
extern Std_Ret_Type Mem_IsReadable(uint16_t id);




#endif

