#ifndef __SPI_FLASH_H
#define __SPI_FLASH_H


#include "stm32f10x.h"

//如果使用霸道开发板，把该宏配置成1 ，指南者配置成0
#define USE_BD          1

/**************************SPI参数定义********************************/
#define             FLASH_SPIx                                SPI2
#define             FLASH_SPI_APBxClock_FUN                  RCC_APB1PeriphClockCmd
#define             FLASH_SPI_CLK                             RCC_APB1Periph_SPI2
#define             FLASH_SPI_GPIO_APBxClock_FUN            RCC_APB1PeriphClockCmd



#define             FLASH_SPI_SCK_PORT                        GPIOB   
#define             FLASH_SPI_SCK_PIN                         GPIO_Pin_13

#define             FLASH_SPI_MOSI_PORT                        GPIOB 
#define             FLASH_SPI_MOSI_PIN                         GPIO_Pin_15

#define             FLASH_SPI_MISO_PORT                        GPIOB 
#define             FLASH_SPI_MISO_PIN                         GPIO_Pin_14

#if (USE_BD ==1)
    #define             FLASH_SPI_GPIO_CLK                        RCC_APB2Periph_GPIOB

    #define             FLASH_SPI_CS_PORT                        GPIOD 
    #define             FLASH_SPI_CS_PIN                         GPIO_Pin_11
#else
    #define             FLASH_SPI_GPIO_CLK                        (RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC)

    #define             FLASH_SPI_CS_PORT                        GPIOC
    #define             FLASH_SPI_CS_PIN                         GPIO_Pin_0
#endif


//CS引脚配置
#define FLASH_SPI_CS_HIGH			GPIO_SetBits(FLASH_SPI_CS_PORT,FLASH_SPI_CS_PIN);
#define FLASH_SPI_CS_LOW			GPIO_ResetBits(FLASH_SPI_CS_PORT,FLASH_SPI_CS_PIN);


/*等待超时时间*/
#define SPIT_FLAG_TIMEOUT         ((uint32_t)0x1000)
#define SPIT_LONG_TIMEOUT         ((uint32_t)(10 * SPIT_FLAG_TIMEOUT))


/*信息输出*/
#define FLASH_DEBUG_ON         0

extern int uart_msg_send(u8 uartid, const char *buf, u32 buflen);
#define FLASH_INFO(fmt,arg...)           OTP_TRACE(fmt,##arg)//trace_otp_trace1(uart_msg_send, 4, fmt, ##arg);
#define FLASH_ERROR(fmt,arg...)          //printf("<<-FLASH-ERROR->> "fmt"\n",##arg)
#define FLASH_DEBUG(fmt,arg...)          //do{\
                                         // if(FLASH_DEBUG_ON)\
                                         // printf("<<-FLASH-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\
                                         // }while(0)

#define DUMMY                   0x00    
#define READ_JEDEC_ID     		0x9f                                                                                  
#define ERASE_SECTOR            0x20                                                                            
#define READ_STATUS             0x05
#define READ_DATA               0x03        
#define WRITE_ENABLE      		0x06                                                                                  
#define WRITE_DATA              0x02                                                                                    


uint32_t SPI_FLASH_Read_ID(void);
void SPI_FLASH_Erase_Sector(uint32_t addr);
void SPI_FLASH_Read_Data(uint32_t addr,uint8_t *readBuff,uint32_t numByteToRead);
void SPI_FLASH_Write_Data(uint32_t addr,uint8_t *writeBuff,uint32_t numByteToWrite);
void SPI_FLASH_WaitForWriteEnd(void);
//连续写入多字节
void SPI_FLASH_Write_Datas(uint32_t addr,uint8_t *writeBuff,uint32_t numByteToWrite);

#endif /* __SPI_FLASH_H */
