用正点原子开发板中 SPI_Flash_Write_NoCheck函数进行写数据实验,遇到一个非常棘手的问题,分别调用自己写的页写入函数和正点原子函数但得出完全不一样的结果,问题就处在 void SPIFlashWritePage(UINT8* pBuffer,UINT32 WriteAddr,UINT16 NumByteToWrite) 函数上,但自己仔细对比源码几乎一模一样。还望高手能帮帮忙!!不胜感激!!附件是我的源码!!
这是对照正点原子开发板程序自己编写的页写入函数 ,调用此函数256字节后数据写入不成功void SPIFlashWritePage(UINT8* pBuffer,UINT32 WriteAddr,UINT16 NumByteToWrite)
{
UINT16 i;
//A Write Enable instruction must be executed before the device will accept the Page Program Instruction (Status Register bit WEL= 1).
SPIFlashWriteEnable();
SPI_FLASH_CS = ENABLE;
SPI2SendReadByte(W25X_PageProgram); //Page Program instruction
SPI2SendReadByte((UINT8)WriteAddr>>16);
SPI2SendReadByte((UINT8)WriteAddr>>8);
SPI2SendReadByte((UINT8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)
{
SPI2SendReadByte(*pBuffer++);
}
这是正点原子开发板页写入函数源码 调用此函数写入不成功
void SPI_Flash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 i;
SPIFlashWriteEnable(); //SET WEL
SPI_FLASH_CS = ENABLE; //使能器件
SPI2SendReadByte(W25X_PageProgram); //发送写页命令
SPI2SendReadByte((u8)((WriteAddr)>>16)); //发送24bit地址
SPI2SendReadByte((u8)((WriteAddr)>>8));
SPI2SendReadByte((u8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)
SPI2SendReadByte(pBuffer);//循环写数
SPI_FLASH_CS = DISABLE; //取消片选
SPIFlashCheckBusy(); //等待写入结束
}
SPI_FLASH_CS = DISABLE;
SPIFlashCheckBusy();
printf("flash program finish!");
}
这是实验函数
void SPI_Flash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite) //测试用
{
u16 pageremain;
pageremain=256-WriteAddr%256; //单页剩余的字节数
if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
while(1)
{
// SPIFlashWritePage(pBuffer, WriteAddr, pageremain); //调入此函数向flash前460个地址写入数据0x44,读出数据时前256个地址数据为0x44,后面的都为0xff
SPI_Flash_Write_Page(pBuffer,WriteAddr,pageremain);//调用此函数读写正常
if(NumByteToWrite==pageremain)break;//写入结束了
else //NumByteToWrite>pageremain
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain; //减去已经写入了的字节数
if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
else pageremain=NumByteToWrite; //不够256个字节了
}
};
printf("SPIFlashWriteDatNCheck program finish!");
}
在主函数中先擦除整片flash成功后,调用 实验函数 void SPI_Flash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite) 连续写入460个字节(串口打印结果)
SPIFlashEraseChip();
SPI_Flash_Write_Page(WTable,0,460);
串口数据如下:
read the addr 250 is 0x44
read the addr 251 is 0x44
read the addr 252 is 0x44
read the addr 253 is 0x44
read the addr 254 is 0x44
read the addr 255 is 0x44
read the addr 256 is 0xff
read the addr 257 is 0xff
read the addr 258 is 0xff
read the addr 259 is 0xff
|