SPI Flash(W25Q16DV) 基本操作
读取厂家\设备 ID

发送 90H 指令,再发送 00h 的地址,然后接收即可。
代码如下:
void SPIFlashReadID(int *pMID, int *pDID)
{
SPIFlash_Set_CS(0); /* 选中SPI FLASH */
SPISendByte(0x90);
SPIFlashSendAddr(0);
*pMID = SPIRecvByte();
*pDID = SPIRecvByte();
SPIFlash_Set_CS(1);
}
两个状态寄存器

通过状态寄存器可以查询芯片此时是否忙碌、解除各种保护操作等。
写状态寄存器
/* 通过 BUSY 位得知芯片是否忙碌 */
static void SPIFlashWaitWhenBusy(void)
{
while (SPIFlashReadStatusReg1() & 1);
}
/* 使能操作 */
static void SPIFlashWriteEnable(int enable)
{
if (enable)
{
SPIFlash_Set_CS(0);
SPISendByte(0x06);
SPIFlash_Set_CS(1);
}
else
{
SPIFlash_Set_CS(0);
SPISendByte(0x04);
SPIFlash_Set_CS(1);
}
}
/* 写状态寄存器 */
static void SPIFlashWriteStatusReg(unsigned char reg1, unsigned char reg2)
{
SPIFlashWriteEnable(1);
SPIFlash_Set_CS(0);
SPISendByte(0x01);
SPISendByte(reg1);
SPISendByte(reg2);
SPIFlash_Set_CS(1);
SPIFlashWaitWhenBusy();
}
擦除操作
/* erase 4K */
void SPIFlashEraseSector(unsigned int addr)
{
SPIFlashWriteEnable(1);
SPIFlash_Set_CS(0);
SPISendByte(0x20);
SPIFlashSendAddr(addr);
SPIFlash_Set_CS(1);
SPIFlashWaitWhenBusy();
}
读写数据
/* program */
void SPIFlashProgram(unsigned int addr, unsigned char *buf, int len)
{
int i;
SPIFlashWriteEnable(1);
SPIFlash_Set_CS(0);
SPISendByte(0x02);
SPIFlashSendAddr(addr);
for (i = 0; i < len; i++)
SPISendByte(buf[i]);
SPIFlash_Set_CS(1);
SPIFlashWaitWhenBusy();
}
/* read */
void SPIFlashRead(unsigned int addr, unsigned char *buf, int len)
{
int i;
SPIFlash_Set_CS(0);
SPISendByte(0x03);
SPIFlashSendAddr(addr);
for (i = 0; i < len; i++)
buf[i] = SPIRecvByte();
SPIFlash_Set_CS(1);
}
芯片的操作指令表



下一篇文章写出 Flash 在 Linux 上的驱动程序。
感谢花费宝贵的时间浏览,
转载请注明出处。
本人将在[资源共享]分类下陆续加入学习过程中一些比较重要且有用处的资料、源码,大家可前往下载,一起进步。
感谢支持!

浙公网安备 33010602011771号