#ifndef _FLASH_H_
#define _FLASH_H_

#include "stdint.h"

extern uint8_t Temp_Flash_Buff[4200];

#if (USE_CORE_TYPE == 1)    //使用C1F核心板

	#define Flash_WP_H	GPIOA_SetBits(GPIO_Pin_3)
	#define Flash_WP_L	GPIOA_ResetBits(GPIO_Pin_3)
	#define Flash_CS_H	GPIOA_SetBits(GPIO_Pin_12)
	#define Flash_CS_L	GPIOA_ResetBits(GPIO_Pin_12)
#elif (USE_CORE_TYPE == 2)   //使用C1核心板

	#define Flash_WP_H	GPIOA_SetBits(GPIO_Pin_3)
	#define Flash_WP_L	GPIOA_ResetBits(GPIO_Pin_3)
	#define Flash_CS_H	GPIOB_SetBits(GPIO_Pin_22)
	#define Flash_CS_L	GPIOB_ResetBits(GPIO_Pin_22)
	
#endif  //USE_CORE_TYPE == CORE_TYPE_C1F

#define Flash_ADDRESS_MAX				0x00200000	

/***********指令表**********/
//Read
#define P24Q40H_ReadData				0x03
#define P24Q40H_FastReadData			0x0B
#define P24Q40H_FastReadDual			0x3B 
//Program and Erase
#define P24Q40H_PageErase				0x81
#define P24Q40H_SectorErase				0x20
#define P24Q40H_BlockErase				0xD8
#define P24Q40H_ChipErase				0xC7
#define P24Q40H_PageProgram				0x02
//Protection
#define P24Q40H_WriteEnable				0x06
#define P24Q40H_WriteDisable			0x04
//Status Register
#define P24Q40H_ReadStatusReg			0x05
#define P24Q40H_WriteStatusReg			0x01
//Other Commands
#define P24Q40H_PowerDown				0xB9
#define P24Q40H_ReleasePowerDown		0xAB
#define P24Q40H_ReadManufactureID		0x90
#define P24Q40H_ReadDeviceID			0x9F
#define P24Q40H_ResetEnable				0x66
#define P24Q40H_Reset					0x99

void Flash_Init(void);						//Flash初始化
uint8_t Flash_ReadSR(void);				//读取Flash状态寄存器
void Flash_WriteSR(uint8_t sr_val);		//写Flash状态寄存器
void Flash_Write_Enable(void);			//Flash写使能
void Flash_Write_Disable(void);			//Flash写禁用
uint16_t Flash_ReadID(void);				//读取Flash芯片信号
uint8_t Flash_Wait_Busy(void);				//等待Flash空闲
void Flash_PowerDown(void);				//进入掉电模式
void Flash_Wakeup(void);					//唤醒
void Flash_Erase_Chip(void);				//擦除整个芯片
void Flash_Erase_Block(uint32_t BLK_ID);	//块擦除
void Flash_Erase_Sector(uint32_t DST_ID);	//擦除扇区
void Flash_Erase_Page(uint32_t Page_ID);
void Flash_Erase_Pageaddr(uint32_t Page_addr);
void Flash_Read(uint8_t* pBuffer,uint16_t NumByteToRead,uint32_t ReadAddr);				//Flash读取数据
void Flash_Write_Page(uint8_t* pBuffer,uint16_t NumByteToWrite,uint32_t writeAddr);		//Flash写数据
void Flash_Write_NoCheck(uint8_t* pBuffer,uint16_t NumByteToWrite,uint32_t writeAddr);	//Flash写数据，自动换页
void Flash_Write(uint8_t* pBuffer,uint16_t NumByteToWrite,uint32_t WriteAddr);			//Flash写数据，自动擦除，自动换页
void Flash_APPDATA_Erase(void);
void Flash_LOGICDATA_Erase(void);

#endif
