
#ifndef FLASH_INTERFACE_H
#define FLASH_INTERFACE_H

#include "define.h"	//for CPU selection

//#define APPLICATION_START_ADDRESS 0x9D003000
//#define APPLICATION_FLASH_START_ADDRESS 0x9D002000

#define APPLICATION_START_ADDRESS 0x9D005000
#define APPLICATION_FLASH_START_ADDRESS 0x9D004000

#define FLASH_ROW_SIZE			128
#define FLASH_ROW_SIZE_IN_BYTES	(4 * FLASH_ROW_SIZE)	//512
#define FLASH_PAGE_SIZE			(8 * FLASH_ROW_SIZE)	//1024
#define FLASH_PAGE_SIZE_IN_BYTES (4 * FLASH_PAGE_SIZE)	//4096
#define MAX_SECTOR_SIZE FLASH_PAGE_SIZE_IN_BYTES

//The number of pages of application program flash
#ifdef USE_675F512_CPU
#define FLASH_NB_APPLICATION_PAGES (0x40000 / FLASH_PAGE_SIZE_IN_BYTES) - 2	//0x40000 is the size of the flash (32MX675F512L), the 2 first pages are reserved for bootloader. (-2)
#else
#define FLASH_NB_APPLICATION_PAGES (0x80000 / FLASH_PAGE_SIZE_IN_BYTES) - 4	//0x80000 is the size of the flash (32MX795F512L), the 2 first pages are reserved for bootloader. (-2)
#endif
																	

#define PROGRAM_FLASH_BEGIN_ADDRESS 0x1D000000
#define PROGRAM_FLASH_END_ADDRESS 	0x1D07FFFF


typedef struct
{
	unsigned int SectorStartAddress;
	unsigned short SectorSize;
	unsigned int SectorData[MAX_SECTOR_SIZE];

}stDataSector_t;

enum eFlashOperations
{
	FLASH_NOP_CMD = 0X0000,
	FLASH_PROGRAM_WORD_CMD = 0x4001,
	FLASH_PROGRAM_ROW_CMD = 0x4003,
	FLASH_PAGE_ERASE_CMD = 0x4004,
	FLASH_ERASE_CMD = 0x4005
};

enum eFlashReturnValues
{
	FLASH_STATUS_OK,
	FLASH_STATUS_ERROR
};

extern stDataSector_t CurrentDataSector;

void InitFlashInterface(void);
unsigned int FlashErase(void);
unsigned int FlashErasePage(void *Address);
unsigned int FlashUnlock(unsigned int Operation);
unsigned int FlashProgramWord(void *Address, unsigned int DataWord);
unsigned int FlashProgramRow(void *Address, void *DataPtr);
unsigned int FlashProgramData(void *FlashStartAddress, unsigned int *DataPtr, unsigned int Size);



#endif


//EOF


