#ifndef W25QXX_H_
#define W25QXX_H_

#include "type.h"
#include "ssi_reg.h"


//      W25Q80DV 容量为 8M-bit，16 Block，256 Sector，4096 Page
//      W25Q16   容量为 16M-Bit，32 Block，512 Sector，8192 Page
//      W25Q32   容量为 32M-Bit，64 Block，1024 Sector，16384 Page
//      W25Q64   容量为 64M-Bit，128 Block，2048 Sector，32768 Page
//      W25Q128  容量为 128M-Bit，256 Block，4096 Sector，65536 Page
//      W25Q256  容量为 256M-Bit，512 Block，8192 Sector，131072 Page
#define  NorPageSize     (256)       			  //one Page=256B(Program)
#define  NorSectorSize   (256*16)       		//one Sector=4KB (Erase)
#define  NorBlockSize    (256*16*16)       	//one Block=64KB (Erase)


extern UINT8 Flash_Type;
extern UINT16 Nand_page;
extern UINT32  Flash_ID;

//----------------------------------------------------------------Instruction--------------------------------------------------------------------------- 
#define W25X_WriteEnable		0x06
#define W25X_WriteEnable_VSR	0x50
#define W25X_WriteDisable		0x04
#define W25X_ReadSR1		0x05
#define W25X_ReadSR2		0x35
#define W25X_ReadSR3		0x15
#define W25X_WriteSR1		0x01
#define W25X_WriteSR2		0x31
#define W25X_WriteSR3		0x11
#define W25X_ReadData			  0x03
#define W25X_FastReadData		0x0B
#define W25X_FastReadDual		0x3B
#define W25X_FastReadDualIO		0xBB
#define W25X_PageProgram		0x02
#define W25X_SectorErase		0x20
#define W25X_32KBlockErase		0x52
#define W25X_64KBlockErase		0xD8
#define W25X_ChipErase			0xC7
#define W25X_PowerDown			0xB9
#define W25X_Enter_4ByteAddr		0xB7
#define W25X_Exit_4ByteAddr		  0xE9
#define W25X_ReleasePowerDown	0xAB
#define W25X_DeviceID			0xAB
#define W25X_ManufactDeviceID	0x90
#define W25X_JedecDeviceID		0x9F

/*************************************************************** winbond NorFlash **********************************************************************/
//status register
UINT8 W25QXX_ReadSR(unsigned char Reg);			   		            //read SR
UINT8 W25QXX_WriteSR(unsigned char Reg, unsigned char val);   //write SR
void W25QXX_Wait_Busy(void);					    //busy status
void W25QXX_Write_Enable(void);   				//Write_Enable
void W25QXX_Write_EnableVolatile(void);		//Write_Enable(Volatile)
void W25QXX_Write_Disable(void);   				//Write_Disable
void W25QXX_4ByteAddress_Mode(void);          //4 Byte addr mode
UINT8 W25QXX_QE_Enable(void);             //QE mode
UINT32 W25QXX_ReadID(void);            		//read ID

//Init
void SSI1_STD_Init(SSI_TypeDef *SSIx);
void W25QXX_Init(void);

//erase
void W25QXX_SectorErase4K(UINT32 addr);		//4K    (erase/Program)
void W25QXX_BlockErase32KB(UINT32 addr); 		//32K (erase)
void W25QXX_BlockErase64KB(UINT32 addr);  	//64K	(erase)
void W25QXX_Erase_Chip(void);

//STD_Read_Mode 
void W25QXX_Read(uint8_t* pBuffer,UINT32 ReadAddr,UINT16 NumByteToRead);

//STD_Write_Mode 
void W25QXX_STD_DMA_Send(SSI_TypeDef *SSIx,int dma_ch, UINT8* psend, UINT32 addr, UINT32 length, BOOL binten);
void W25QXX_Write_Page(uint8_t* pBuffer,UINT32 WriteAddr,UINT16 NumByteToWrite);     
void W25QXX_Write_NoCheck(uint8_t* pBuffer,UINT32 WriteAddr,UINT16 NumByteToWrite); 

//-----------------Dual_Mode-----------
//Dual_Read_Mode 
void NorFlash_Read_Init(UINT8 CmdLen, UINT8 waitCycles,UINT32 data_len);
void NorFlash_Read(unsigned int addr, unsigned char *buf, int num);
//Dual_DMA_Read_Mode 
void NorFlash_DMA_Read_Init(UINT8 CmdLen, UINT8 waitCycles,UINT32 data_len);
void NorFlash_DMA_Read(UINT8* buf, UINT32 addr, UINT32 length);
void NorFlash_DMA_Wait(void);

#endif
