u8 SPI_Flash_SendByte(u8 byte)
{
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, byte);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI2);
}
u8 SPI_Flash_ReceiveByte(void)
{
return (SPI_Flash_SendByte(Dummy_Byte));
}
//写字节,参数:待写字节、写入地址
void SST25V_ByteWrite(u8 Byte, u32 WriteAddr)
{
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(Byte_Program);
SPI_Flash_SendByte((WriteAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((WriteAddr & 0xFF00) >> 8);
SPI_Flash_SendByte(WriteAddr & 0xFF);
SPI_Flash_SendByte(Byte);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}
////写字节串,参数:起始地址、待写数据、写入长度
void SST25V_BufferWrite(u32 WriteAddr,u8* pBuffer, u16 NumByteToWrite)
{
u16 i,length;
length=NumByteToWrite;
if(length%2!=0)length++;//不足双数补1
SST25V_Init();//
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(AAI_WordProgram);
SPI_Flash_SendByte((WriteAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((WriteAddr & 0xFF00) >> 8);
SPI_Flash_SendByte(WriteAddr & 0xFF);
SPI_Flash_SendByte(pBuffer[0]);
SPI_Flash_SendByte(pBuffer[1]);
SST25V_CS_HIGH();
SST25V_Wait_Busy_AAI();
for(i=2;i
{
SST25V_CS_LOW();
SPI_Flash_SendByte(AAI_WordProgram);
SPI_Flash_SendByte(pBuffer[i]);
SPI_Flash_SendByte(pBuffer[i+1]);
SST25V_CS_HIGH();
SST25V_Wait_Busy_AAI();
}
SST25V_WriteDisable();
SST25V_Wait_Busy_AAI();
}
void SST25V_Wait_Busy_AAI(void)
{
while (SST25V_ReadStatusRegister() == 0x43)
SST25V_ReadStatusRegister();
}
// 扇区擦除4Kbit,参数:地址
void SST25V_SectorErase_4KByte(u32 Addr)
{
SST25V_Init();//
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(SectorErace_4KB);
SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
SPI_Flash_SendByte(Addr & 0xFF);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}
void SST25V_BlockErase_32KByte(u32 Addr)
{
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(BlockErace_32KB);
SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
SPI_Flash_SendByte(Addr & 0xFF);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}
void SST25V_BlockErase_64KByte(u32 Addr)
{
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(BlockErace_64KB);
SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
SPI_Flash_SendByte(Addr & 0xFF);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}
void SST25V_ChipErase(void)
{
SST25V_Init();
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(ChipErace);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}
u8 SST25V_ReadStatusRegister(void)
{
u8 StatusRegister = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(ReadStatusRegister);
StatusRegister = SPI_Flash_ReceiveByte();
SST25V_CS_HIGH();
return StatusRegister;
}
void SST25V_WriteEnable(void)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(WriteEnable);
SST25V_CS_HIGH();
}
void SST25V_WriteDisable(void)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(WriteDisable);
SST25V_CS_HIGH();
}
void SST25V_EnableWriteStatusRegister(void)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(EnableWriteStatusRegister);
SST25V_CS_HIGH();
}
void SST25V_WriteStatusRegister(u8 Byte)
{
SST25V_EnableWriteStatusRegister();
SST25V_CS_LOW();
SPI_Flash_SendByte(WriteStatusRegister);
SPI_Flash_SendByte(Byte);
SST25V_CS_HIGH();
}
void SST25V_WaitForWriteEnd(void)
{
u8 FLASH_Status = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(ReadStatusRegister);
do
{
FLASH_Status = SPI_Flash_SendByte(Dummy_Byte);
} while((FLASH_Status & WriteStatusRegister)!=0);