#ifndef __SERIAL_FLASH_H__
#define __SERIAL_FLASH_H__


#define SECTOR_ERASE_DELAY		61	 // 61ms

#define DATA_SRAM_FLASH_ADD		96   // 4     byte
#define DATA_SRAM_BASE_ADD		100  // 4096  byte 
#define POWER_DATA_BACKUP_ADD	4196 // 1024  byte


#define	DATA_BASE_ADD			0x2000
#define PAGE_SIZE				256
#define SECTOR_FILTER			0xfffff000
#define MAX_ADDRESS				0x00200000
#define LAST_FIELD_ADDRESS		MAX_ADDRESS - 1024 //0x001ffc00


/////////////////////////////////////////////////////////////////////
// SERIAL FLASH MEMORY(SPI)
#define	WREN					0x06 // Set Write Enable Latch
#define	WRDI					0x04 // Write Disable
#define	RDSR					0x05 // Read Status Register
#define	WRSR					0x01 // Write Status Register
#define	SM_READ					0x03 // Read data bytes
#define FAST_READ				0x0b // Read data bytes at higher speed
#define	PAGE_PROGRAM			0x02 // Page Program
#define SM_WRITE				0x02 
#define SECTOR_ERASE			0xd8 // Sector Erase : delay 60ms
#define BULK_ERASE				0xc7 // Bulk Erase   : delay 400ms
#define DP						0xb9 // Deep Power-down
#define RES						0xab // Release from Deep Power-down

#define WRITE_PROG_DELAY		1000

#define SCK()					pSCK = 1; SM_DELAY(); pSCK = 0
/////////////////////////////////////////////////////////////////////

typedef struct
{
	unsigned char	write_prog	: 1;
	//unsigned char   
}CFLASH_STATE;


void serial_memory_init(void);
void sflash_put_byte(unsigned char ch);
unsigned char sflash_get_byte(void);
void sflash_WREN(void);
void sflash_WRDI(void);
unsigned char sflash_RDSR(void);
void sflash_WRSR(unsigned char ch);
unsigned char sflash_read_byte(unsigned long add);
void sflash_fast_read(unsigned long add, unsigned char *buff, int len);
unsigned char sflash_page_program(unsigned long add, unsigned char *buff, int len);
int sflash_sector_erase(unsigned long add, int time);
int sflash_bulk_erase(void);


// serial ram 
unsigned char sram_RDSR(void);
void sram_WRSR(unsigned char ch);
unsigned char sram_read_byte(unsigned short add);
void sram_write_byte(unsigned short add, unsigned char ch);
void sram_read(unsigned short add, unsigned char *buff, int len);
void sram_write(unsigned short add, unsigned char *buff, int len);
void sram_write2(unsigned short add, unsigned char x, int len);


void flash_next_sector_used_check(unsigned long add);
void data_flash_to_ram(unsigned long add);
void data_ram_to_flash(unsigned long add);
void DATA_FLASH_WRITE(unsigned long add);

#endif



