#ifndef __SPIFLASH_H
#define __SPIFLASH_H

#include "debug.h"

#define SPI_FLASH_USE_DMA   1

/*FLASH instruction, 不同FLASH 指令不同,目前为Demo Flash 型号为W25Q128FV */

#define  FLASH_GETID              0x9F
#define  FLASH_CMD_WREN           0x06  /*!< Write enable instruction */
#define  FLASH_CMD_ERASE_SECTOR	  0x20  /*!< Sector Erase instruction */
#define  FLASH_CMD_READSTS	      0x05  /*!< Read Status Register instruction */
#define  FLASH_CMD_WRITEPAGE	    0x02  /*!< Program Page Register instruction */
#define  FLASH_CMD_READPAGE	      0x03  /*!< Read Page Register instruction */

#define Dummy_Byte              0xA5

#define FLASH_SPI_CS_PIN            GPIO_Pin_2
#define FLASH_SPI_CS_GPIO           GPIOA

#define flash_spi_cs_low()       GPIO_ResetBits(FLASH_SPI_CS_GPIO, FLASH_SPI_CS_PIN)
#define flash_spi_cs_high()      GPIO_SetBits(FLASH_SPI_CS_GPIO, FLASH_SPI_CS_PIN)

#define flash_dummy_byte    0xff
#define flah_page_size      256
#define flah_sector_size      0x1000

extern volatile uint32_t Flash_ID;

extern uint32_t spi_flash_size ;

void spiflash_gpioinit(void);
void spiflash_init(void);
uint32_t flash_spi_write(uint8_t* pbuffer, uint32_t writeaddr, uint16_t numbytetowrite);
uint32_t flash_spi_read(uint8_t* pbuffer, uint32_t readaddr, uint16_t numbytetoread);
uint32_t flash_spi_getsize(void);
uint32_t flash_spi_sectorsize(void);
uint32_t flash_spi_pagesize(void);
uint32_t flash_spi_getid(void);
void flash_spi_erasesector(uint32_t sectoraddr);
void FLASH_RD_Block( uint8_t *pbuf, uint32_t len );
void FLASH_RD_Block_Start( uint32_t address );


#endif


