#define Dummy_Byte
0xff
#define Read_Data0x03//读取存储器数据
#define HighSpeedReadData0x0B//快速读取存储器数据
#define SectorErace_4KB0x20//扇区擦除
#define BlockErace_32KB0x52//32KB块擦除
#define BlockErace_64KB0xD8//64KB块擦除
#define ChipErace0x60//片擦除
#define Byte_Program0x02//页面编程--写数据
#define AAI_WordProgram0xAD
#define ReadStatusRegister0x05//读状态寄存器
#define EnableWriteStatusRegister0x50
#define WriteStatusRegister0x01//写状态寄存器
#define WriteEnable0x06//写使能,设置状态寄存器
#define WriteDisable0x04//写禁止
#define ReadDeviceID0xAB//获取设备ID信息
#define ReadJedec_ID0x9F//JEDEC的ID信息
#define EBSY0X70
#define DBSY0X80
//初始化SPI口
void SPI2init(void)
{
SPI_InitTypeDefSPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_SCK | GPIO_MISO | GPIO_MOSI;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_PORT_CS, &GPIO_InitStructure);
SST25V_CS_HIGH();
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
//flash初始化
void SST25V_Init(void)
{
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// GPIO_ResetBits(GPIOD, GPIO_Pin_15);//关闭Flash电源
// Delay(10);//延时10mS
GPIO_SetBits(GPIOD, GPIO_Pin_15);//打开Flash电源
//GPIO_ResetBits(GPIOC, GPIO_Pin_8);
}
SPI2init();
SST25V_CS_HIGH();
SST25V_EnableWriteStatusRegister();//使能写状态寄存器
SST25V_WriteStatusRegister(0);//写状态寄存器设置BPL BP3 BP2 BP1 BP00 0 0 0 0
SST25V_DBSY();
}
//读字节,参数:地址
u8 SST25V_ByteRead(u32 ReadAddr)
{
u8 Temp = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(Read_Data);
SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_Flash_SendByte(ReadAddr & 0xFF);
Temp = SPI_Flash_ReceiveByte();
SST25V_CS_HIGH();
return Temp;
}
//读字节串,参数:起始地址、读入缓冲、读取长度
void SST25V_BufferRead(u32 ReadAddr, u8* pBuffer, u16 NumByteToRead)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(Read_Data);
SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_Flash_SendByte(ReadAddr & 0xFF);
while(NumByteToRead--)
{
*pBuffer = SPI_Flash_ReceiveByte();
pBuffer++;
}
SST25V_CS_HIGH();
}
//高速读字节串,参数:起始地址、读入缓冲、读取长度
void SST25V_HighSpeedBufferRead( u32 ReadAddr,u8* pBuffer, u16 NumByteToRead)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(HighSpeedReadData);
SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_Flash_SendByte(ReadAddr & 0xFF);
SPI_Flash_SendByte(Dummy_Byte);
while(NumByteToRead--)
{
*pBuffer = SPI_Flash_ReceiveByte();
pBuffer++;
}
SST25V_CS_HIGH();
}
//读字节,参数:地址
u8 SST25V_HighSpeedRead(u32 ReadAddr)
{
u32 Temp = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(HighSpeedReadData);
SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_Flash_SendByte(ReadAddr & 0xFF);
SPI_Flash_SendByte(Dummy_Byte);
Temp = SPI_Flash_ReceiveByte();
SST25V_CS_HIGH();
return Temp;
}