#ifndef _W25Q256_H
#define _W25Q256_H

#include "gpio.h"
#include "main.h"

/*******************************************************************************
*                              常量&宏定义                                     *
*******************************************************************************/
#define FLASH_CS_GPIO_Port      GPIOA
#define FLASH_CS_Pin            FLASH_CS_Pin
#define FLASH_CLK_GPIO_Port     GPIOA
#define FLASH_CLK_Pin           FLASH_CLK_Pin
#define FLASH_MISO_GPIO_Port    GPIOA
#define FLASH_MISO_Pin          FLASH_MISO_Pin
#define FLASH_MOSI_GPIO_Port    GPIOA
#define FLASH_MOSI_Pin          FLASH_MOSI_Pin

#define FLASH_CS_ENABLE  HAL_GPIO_WritePin(FLASH_CS_GPIO_Port, FLASH_CS_Pin, GPIO_PIN_RESET) /*片选使能*/
#define FLASH_CS_DISABLE HAL_GPIO_WritePin(FLASH_CS_GPIO_Port, FLASH_CS_Pin, GPIO_PIN_SET) /*片选失能*/

#define FLASH_CLK_HIGH   HAL_GPIO_WritePin(FLASH_CLK_GPIO_Port, FLASH_CLK_Pin, GPIO_PIN_SET) /*时钟信号高*/
#define FLASH_CLK_LOW    HAL_GPIO_WritePin(FLASH_CLK_GPIO_Port, FLASH_CLK_Pin, GPIO_PIN_RESET) /*时钟信号低*/

#define FLASH_MISO_READ  HAL_GPIO_ReadPin(FLASH_MISO_GPIO_Port, FLASH_MISO_Pin) /*MISO数据输入*/

#define FLASH_MOSI_HIGH  HAL_GPIO_WritePin(FLASH_MOSI_GPIO_Port, FLASH_MOSI_Pin, GPIO_PIN_SET) /*MOSI数据引脚高电平*/
#define FLASH_MOSI_LOW   HAL_GPIO_WritePin(FLASH_MOSI_GPIO_Port, FLASH_MOSI_Pin, GPIO_PIN_RESET) /*MOSI数据引脚低电平*/

#define FLASH_W25Q128
#ifdef FLASH_W25Q128
#define    Flash_ReadData_CMD            0x03
#endif
#ifdef FLASH_W25Q256
#define    Flash_ReadData_CMD            0x13
#endif

#define    FLASH_WRITE_BUSYBIT           0X01
#define    Flash_ReadID                  0x90 /*读ID***16位*/
#define    Flash_ReadID_only             0x4b /*读唯一ID***64位*/
#define    Flash_Chip_Erase_CMD          0xC7 /*片擦除*/
#define    Flash_WriteEnable_CMD         0x06
#define    Flash_WriteDisable_CMD        0x04
#define    Flash_PageProgram_CMD         0x02
#define    Flash_WriteSR_CMD             0x01
#define    Flash_ReadSR_CMD              0x05
#define    Flash_SecErase_CMD            0x20
#define    Flash_BlockErase_CMD          0xD8
#define    W25Q_4ByteAddrModeEnable      0xB7 
#define    W25Q_Exit4ByteAddrModeEnable  0xE9 
#define    Flash_PAGEBYTE_LENGTH         256
#define    EXT_FLASH_SECTOR_SIZE         (1024*4)
#define    EXT_FLASH_BLOCK_SIZE          (1024*64)

/*******************************************************************************
*                                 函数声明                                     *
*******************************************************************************/
/* w25q256相关引脚初始化 */
void Flash_GPIO_Init(void);
/* 扇区擦除 */
void Flash_Erase_Block(uint8_t BlockNum);
/* 块擦除 */
void Flash_Erase_Sector(uint8_t Block_Num,uint8_t Sector_Number);
/* 写数据 */
void Flash_Write_Page(uint8_t *pBuffer, uint32_t WriteAddr, uint32_t WriteBytesNum);
void Flash_Write_MorePage(uint8_t *pBuffer, uint32_t WriteAddr, uint32_t WriteBytesNum);
/* 读数据 */
void Flash_Read(uint8_t *pBuffer,uint32_t ReadAddr,uint32_t ReadBytesNum);

void W25Q128_Write(uint8_t *pBuffer, uint32_t WriteAddr, uint32_t WriteBytesNum);

#endif