#ifndef __WINBOND_H
#define __WINBOND_H

#include "sys.h"
#include "string.h"
#include "ssi_drv.h"
#include "dma_hal.h"
#include "eport_hal.h"

#define	cSetb0		0x01
#define	cSetb1		0x02
#define	cSetb2		0x04
#define	cSetb3		0x08
#define	cSetb4		0x10
#define	cSetb5		0x20
#define	cSetb6		0x40
#define	cSetb7		0x80

#define	cClrb0		0xfe
#define	cClrb1		0xfd
#define	cClrb2		0xfb
#define	cClrb3		0xf7
#define	cClrb4		0xef
#define	cClrb5		0xdf
#define	cClrb6		0xbf
#define	cClrb7		0x7f

//#define SSI2_SS_RESET (*(volatile unsigned char *)(0x4002e000+0x05)) &= ~(1<<0)
//#define SSI2_SS_SET	  (*(volatile unsigned char *)(0x4002e000+0x05)) |= (1<<0)
	
//#define SSI2_SS_RESET	EPORT2->EPDR &=~(1<<0)
//#define SSI2_SS_SET		EPORT2->EPDR |= (1<<0)

//extern void (*CS_FLash_RESET)(void);
//extern void (*CS_FLash_SET)(void);

//void CS0_NorFLash_RESET(void);
//void CS0_NorFLash_SET(void);
//void CS1_NandFLash_RESET(void);
//void CS1_NandFLash_SET(void);


//四线SPI在DMA下 可以用4分频，非DMA用8分频 
#define SSI2_STD_BaudRatePrescaler   2
#define SSI2_Dual_BaudRatePrescaler  4
#define SSI2_Quad_BaudRatePrescaler  2
#define SSI2_Quad_BaudRatePrescaler_for_NandFlash  4

#define SSI2_DMA 1

extern u8 W25Q256;

extern SSI_InitTypeDef SPI5;

void Check_SSI2_Busy(void);

void SPI5_STD_Init(void);
void SPI5_Quad_Read_Init(void);
void SSI2_DMA_RX(unsigned char * p,unsigned char cmd,unsigned int addr,unsigned short num,unsigned Byte_depth,unsigned char mode,unsigned char addrtype);
void SSI2_DMA_RX_Continue(unsigned char * p,unsigned char cmd,unsigned int addr,unsigned short num,unsigned Byte_depth,unsigned char mode,unsigned char addrtype);

u8 W25QXX_ReadSR(void);
u16 W25QXX_ReadID_STD(void);

void W25QXX_Enter_4Byte_AddressMode_STD(void);

void W25QXX_Open_QUAD(void);
UINT32 W25QXX_ReadID_STD_9F(void);
void W25QXX_Read_STD(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead);
void W25QXX_Write_NoCheck_STD(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);

void W25QXX_Erase_Chip_STD(void);
void W25QXX_SectorErase4KB_STD(u32 addr);
void W25QXX_BlockErase64KB_STD(u32 addr);




void W25QXX_Read_DUAL(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead);

void W25QXX_Read_QUAD(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead);
void W25QXX_Write_NoCheck_QUAD(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);  

void W25QXX_Read_QUAD_DMA(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead);  //读取数目为4byte 倍数

void Flash_Enter_QPI(void); //Flash进入QPI模式
void W25QXX_Enter_QPI_STD(void);
void SPI5_QPI_Write_Init(void);
void SPI5_QPI_Set_DataNum(u16 num);
u16 W25QXX_ReadID_QPI(void);



//
void W25NXX_ContinuousRead_Mode(void);
void W25N01GV_PageRead_Mode(void);
void Write_Page(u32 page);

void W25NXX_Wait_Busy(void);
void W25NXX_Write_SR(uint8_t reg, uint8_t val);
uint32_t W25NXX_ReadID(void);
void W25NXX_Erase_Block(uint16_t block);
void W25NXX_EraseFlash(unsigned short block_num);
void W25NXX_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite,unsigned char mode);
void W25NXX_WritePageAddr_Data_STD(uint8_t* pBuffer,uint16_t ColumnAddr,uint32_t PageAddr,uint16_t NumByteToWrite);
void W25NXX_ReadPageAddr_Data_STD(uint8_t* pBuffer,uint16_t ColumnAddr,uint32_t PageAddr,uint16_t NumByteToRead);
void W25NXX_Read_NoCheck(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead,unsigned char mode);

#endif
