//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535)
void SPI_Flash_Read(unsigned char* pBuffer,unsigned long ReadAddr,unsigned int NumByteToRead)
{
unsigned int i;
CS_FLASH_L(); //使能器件
nop();
outbyte(0x03); //发送读取命令
outbyte((unsigned char)((ReadAddr)>>16)); //发送24bit地址
outbyte((unsigned char)((ReadAddr)>>8));
outbyte((unsigned char)ReadAddr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer[i]=inbyte(); //循环读数
}
CS_FLASH_H(); //取消片选
}
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
void SPI_Flash_Write_Page(unsigned char *pBuffer,unsigned long WriteAddr,unsigned int NumByteToWrite)
{
unsigned int i;
CS_FLASH_L();
//nop();
SPI_FLASH_Write_Enable(); //SET WEL
flash_wait_busy();
CS_FLASH_L(); //使能器件
outbyte(0x02); //发送写页命令
outbyte((unsigned char)((WriteAddr)>>16)); //发送24bit地址
outbyte((unsigned char)((WriteAddr)>>8));
outbyte((unsigned char)WriteAddr);
for(i=0;i<NumByteToWrite;i++)
{
outbyte(pBuffer[i]); //循环写数
}
delay(50); ///CS引脚必须在整数字节(8倍数时钟)之后拉高,以确保最后一个字节被锁定。如果没有这么做,则扇区擦除指令将不会执行。
//22us > 12.8us
CS_FLASH_H(); //取消片选
flash_wait_busy(); //等待写入结束
}
页的读写指令正常
|