#ifndef _SPI_FLASH_H_
#define _SPI_FLASH_H_

//#include "stm32f10x_type.h"
//#include "stm32f10x_lib.h"
//#include "stm32.h"
#include "stm32f10x.h"
//#include "Fat_Disk.h"

#define FLASHOVERTIMECNT        4//超时计数，用于避免FLASH死掉

/*
#define FLASH_CHREAD 0x0B
#define FLASH_CLREAD 0x03
#define FLASH_PREAD	0xD2

#define FLASH_BUFWRITE1 0x84
#define FLASH_IDREAD 0x9F
#define FLASH_STATUS 0xD7
#define PAGE_ERASE 0x81
#define PAGE_READ 0xD2
#define MM_PAGE_TO_B1_XFER 0x53				// 将主存储器的指定页数据加载到第一缓冲区
#define BUFFER_2_WRITE 0x87					// 写入第二缓冲区
#define B2_TO_MM_PAGE_PROG_WITH_ERASE 0x86	// 将第二缓冲区的数据写入主存储器（擦除模式）
*/
#define FAT_SECTOR_SIZE 				4096
#define FAT_SECTOR_NUM					1536//256*6M=1536
#define FLASH_SECTOR_SIZE				4096
#define FLASH_PAGE_SIZE					256//一页大小
#define FLASH_PAGE_SNUM					16//一扇区里包含的页数
#define FLASH_SECTOR_SIZE				FAT_SECTOR_SIZE//4096//一个扇区大小
#define FLASH_SECTOR_COUNT			FAT_SECTOR_NUM//2048//扇区个数,8M=4096*2048
#define FLASH_BLOCK_SIZE				16//一块的扇区个数
#define FLASH_PAGES_PER_SECTOR	FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE//一个扇区里包含的页数
#define FLASH_ERASE_PAGE_SIZE		4096//擦除一页大小
#define FLASH_BUF_SIZE					256//Flash芯片接收一次数据大小
typedef enum
{//带*号为必须片选NSS必须在1Byte发送命令发生前拉低,发送完毕之后拉高
	SF_CMD_WREN=0X06,//写使能*
	SF_CMD_WRDIS=0X04,//写禁止*
	SF_CMD_RDID1=0X9F,//读ID
	SF_CMD_RDID2=0X9E,
	SF_CMD_RDSR=0X05,//读状态寄存器
	SF_CMD_WRSR=0X01,//写状态寄存器*
	SF_CMD_WRLR=0XE5,//写锁存器*
	SF_CMD_RDLR=0XE8,//读锁存器
	SF_CMD_RDDB=0X03,//读数据字节
	SF_CMD_RDDBHS=0X0B,//高速读数据字节
	SF_CMD_DOFRD=0X3B,//双输出快速读
	SF_CMD_RDOTP=0X4B,//读操作字节
	SF_CMD_PROOTP=0X42,//操作字节编程*
	SF_CMD_PP=0X02,//页编程*
	SF_CMD_DIFP=0XA2,//双输入快速编程*
	SF_CMD_SUBSERASE=0X20,//擦除扇区4K*
	SF_CMD_SERASE=0XD8,//块擦除4K*16=64K
	SF_CMD_BULKERASE=0XC7,//完全擦除*
	SF_CMD_DPD=0XB9,//睡眠*
	SF_CMD_RDPD=0XAB//激活
}SPI_FLASH_CMD_DEFINED;
#define SF_SR_BUSY		0X01//0x03
#define SF_SR_WIP			0X01//Write in progress bit
#define SF_SR_WEL			0X02//Write enable latch bit
#define SF_SR_BP012		0X1C//Block protect bits
#define SF_SR_TB			0X20//Top/bottom bit
#define SF_SR_SRWD  	0X80//Status register write protect
//Dataflash指令表
#define Cmd_DF_PageRead         0xD2
#define Cmd_DF_Buffer1Read      0xD1
#define Cmd_DF_Buffer2Read      0xD3
#define Cmd_DF_WriteBuffer1     0x84
#define Cmd_DF_WriteBuffer2     0x87
#define Cmd_DF_Buffer1ToPage  	0x83
#define Cmd_DF_Buffer2ToPage  	0x86
#define Cmd_DF_PageErase        0x81
#define Cmd_DF_BlockErase       0x50
#define Cmd_DF_SectorErase      0x7C
#define Cmd_DF_AllErase         0xC7
#define Cmd_DF_ReadState        0xD7
#define Cmd_DF_ReadID           0x9F

#define Dummy_Byte 0xA5

extern u8 Select_FlashFlag;
// Select SPI FLASH: ChipSelect pin low
#define Select_Flash()      {/*while(Select_FlashFlag);Select_FlashFlag=1;*/GPIO_ResetBits(GPIOB,GPIO_Pin_12);}
// Deselect SPI FLASH: ChipSelect pin high
#define NotSelect_Flash()   {/*if(Select_FlashFlag)Select_FlashFlag=0;*/GPIO_SetBits(GPIOB,GPIO_Pin_12);}

typedef enum
{
	SPI_NONE=0,
	SPI_SUCCESS,
	SPI_ERROR,
	SPI_OVERTIME
}SPI_FLASH_DEFINED;

void sFlash_Init(void);	//SPI初始化
u8 sFlash_ReadByte(void);		//flash操作基本函数，读一个字节
u8 sFlash_SendByte(u8 byte);		//	FLASH操作基本函数，发送一个字节
void sFlashPageEarse(u16 page);	//擦除指定的页,页范围0-4095

void sFlashPageRead(u16 page,u8 *Data);		//读取整页，页范围0-4095
//void sFlashPageWrite1(u16 page,u8 *Data);		//写一整页，页范围0-4095
u8 sFlashPageWrite1(u16 page,u8 *Data);		//写一整页，页范围0-4095
void sFlashPageWrite2(u16 page, u8 * Data);

//void sFlashWaitBusy(void);			//Flash忙检测
u8 sFlashWaitBusy(void);			//Flash忙检测20140329
void sFlashReadID(u8 *ProdustID);		//读取flashID四个字节
u8 sFlashReadState(void);	
void sFlashModity512bytes(void);
u8 sFlashMultiRead(u8 *buf,u32 addr,u16 page_len);

u8 sFlash_WriteSector(u32 nSector, u8* pBuffer);
u8 SPI_Flash_Erase_Sector(u32 Dst_Addr);

u8 sFlash_ReadMultiBytes(u32 Addr,u16 lenght,u8* pBuffer);
u8 sFlash_WriteMultiBytes(u32 Addr,u16 lenght,u8* pBuffer);
u8 sFlash_Read_Sector(u32 nSector, u8* pBuffer);
u8 sFlash_SetReadStartAddr(u32 mAddr);
u8 sFlash_WaitReadSectorFinsh(void);
u8 sFlash_WaitReadFinsh(void);
#endif
