return ReceiveData;
}
u8 User_SPI_W25X16_ReadByte(void)
{
u8 ReceiveData;
ReceiveData=User_SPI_W25X16_SendByte(NoneCode);
return ReceiveData;
}
vu16 User_SPI_W25X16_SendHalfWord(u16 HalfWord)
{
vu16 ReceiveData;
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)!=SET);
SPI_I2S_SendData(SPI1,HalfWord);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)!=SET);
ReceiveData=SPI_I2S_ReceiveData(SPI1);
return ReceiveData;
}
void User_SPI_W25X16_WriteEnable(void)
{
SPI_W25X16_CS_Select;
User_SPI_W25X16_SendByte(WriteEnable);
SPI_W25X16_CS_DisSelect;
}
void User_SPI_W25X16_WaitForWriteEnd(void)
{
u8 ReceiveStatus;//用于存放W25X16返回的状态非零就是操作结束
SPI_W25X16_CS_Select;
User_SPI_W25X16_SendByte(ReadStatusRegister);
do
{
ReceiveStatus=User_SPI_W25X16_SendByte(NoneCode);
}
while(ReceiveStatus & JudgeCode==SET);
SPI_W25X16_CS_DisSelect;
}
void User_SPI_W25X16_SectorErase(vu32 SectorAddress)
{
User_SPI_W25X16_WriteEnable();
SPI_W25X16_CS_Select;
User_SPI_W25X16_SendByte(SectorErase);
User_SPI_W25X16_SendByte((SectorAddress & 0xff0000)>>16); //发送最高8位
User_SPI_W25X16_SendByte((SectorAddress & 0xff00)>>8); // 中间8位
User_SPI_W25X16_SendByte(SectorAddress & 0xff); //发送最低8位
SPI_W25X16_CS_DisSelect;
User_SPI_W25X16_WaitForWriteEnd();
}
void User_SPI_W25X16_BulkErase(void)
{
User_SPI_W25X16_WriteEnable();
SPI_W25X16_CS_Select;
User_SPI_W25X16_SendByte(ChipErase);
SPI_W25X16_CS_DisSelect;
User_SPI_W25X16_WaitForWriteEnd();
}
void User_SPI_W25X16_PageWrite(u8 *DataTable,vu32 WriteAddress,vu16 NumberOfWrite)
{
User_SPI_W25X16_WriteEnable();
SPI_W25X16_CS_Select;
User_SPI_W25X16_SendByte(PageProgram);
User_SPI_W25X16_SendByte((WriteAddress & 0xff0000)>>16); //最高8位地址
User_SPI_W25X16_SendByte((WriteAddress & 0xff00)>>8); //中间8位地址
User_SPI_W25X16_SendByte(WriteAddress & 0xff); //最低8位地址
if(NumberOfWrite > SPI_W25X16_PerPageWriteSize) //W25X16采用的是页写入方式,最多一次性写入256个数据,然后内部地址指针归零
{
NumberOfWrite=SPI_W25X16_PerPageWriteSize;
printf("哦偶,一次性写入的数据太多,不能超过256的啦,ARM将为你写入前256个数据");
}
while(NumberOfWrite--)
{
User_SPI_W25X16_SendByte(*DataTable);
DataTable++; //数组指针 +1
}
SPI_W25X16_CS_DisSelect;
User_SPI_W25X16_WaitForWriteEnd();
}
void User_SPI_W25X16_ChipWrite(u8 *DataTable,vu32 WriteAddress,vu16 NumberOfWrite)
{
u8 AddressRemainder =0;
u8 NumberOfPage =0;
u8 Count =0; //存放地址所在页需要写入的最多数据
u8 NumberOfSingle =0; //写入数据的最后一些需要写入的数据个数
u8 Buffer =0; //保留
AddressRemainder =WriteAddress % SPI_W25X16_PageSize;
Count =SPI_W25X16_PageSize - AddressRemainder;
NumberOfPage =NumberOfWrite / SPI_W25X16_PageSize;
NumberOfSingle =NumberOfWrite % SPI_W25X16_PageSize;
if(AddressRemainder==0)
{
if(NumberOfPage==0) //NumberOfWrite < SPI_W25X16_PageSize
{
User_SPI_W25X16_PageWrite(DataTable,WriteAddress,NumberOfWrite);
}
else
{
while(NumberOfPage--)
{
User_SPI_W25X16_PageWrite(DataTable,WriteAddress,SPI_W25X16_PageSize); //一次性写入256个
DataTable+=SPI_W25X16_PageSize; //接着写下一个256数据
WriteAddress+=SPI_W25X16_PageSize; //地址就移到下一页(256为单位)
}
User_SPI_W25X16_PageWrite(DataTable,WriteAddress,NumberOfSingle);
}
}
else
{
if(NumberOfPage==0)
{
if(NumberOfWrite < Count)
{
User_SPI_W25X16_PageWrite(DataTable,WriteAddress,NumberOfWrite);
}
else
{
User_SPI_W25X16_PageWrite(DataTable,WriteAddress,Count); //起始地址所在页只需要写入Count个
Buffer=NumberOfWrite-Count; //计算出下一页要写入的数据数量
DataTable+=Count;
WriteAddress+=Count;
User_SPI_W25X16_PageWrite(DataTable,WriteAddress,Buffer);
}
}
else
{
User_SPI_W25X16_PageWrite(DataTable,WriteAddress,Count);
//之后需要重新计算以下参数 原因:数据量超过一页分两种情况:1、数据量不会覆盖完起始地址下一页;2、数据量会完全覆盖起始地址下一页
//重新计算就是看是否会覆盖掉下一页,如果会就进入while()循环,否则就不进入
DataTable+=Count;
WriteAddress+=Count;
NumberOfWrite-=Count;
NumberOfPage=NumberOfWrite / SPI_W25X16_PageSize;
NumberOfSingle=NumberOfWrite % SPI_W25X16_PageSize;