各位猿友们好!
小生最近在研究 W25Q32 芯片, 经 SPI 操作该芯片时, 只有 0x9F 指令返回的数据
正常, 其余的指令均返回 0xFF.
MCU 采用新唐的 NUC126.
以下是部分源代码, 希望理解的朋友给点思路或者解决方案, 谢谢!
#include "W25Qxx.h"
#if 1
#define w25q_debug(fmt, ...) printf("%s-%d: "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define w25q_debug(fmt, ...)
#endif
void W25Qxx_Reset(void)
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, 0xFF);
// SPI_WRITE_TX(SPI1, 0xFF);
SPI_SET_SS_HIGH(SPI1);
}
void W25Qxx_Write_Enable(void)
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_WR_ENABLE);
SPI_SET_SS_HIGH(SPI1);
}
void W25Qxx_Write_Disable(void)
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_WR_DISABLE);
SPI_SET_SS_HIGH(SPI1);
}
void W25Qxx_Get_JEDEC_ID(void)
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_JEDEC_ID); // 0x9F
SPI_WRITE_TX(SPI1, 0x00);
SPI_WRITE_TX(SPI1, 0x00);
SPI_WRITE_TX(SPI1, 0x00);
uint32_t i = 0;
w25q_debug();
for(i = 0; i < 4; i++)
{
printf("%x ", SPI_READ_RX(SPI1));
}
printf("\n");
SPI_SET_SS_HIGH(SPI1);
}
void W25Qxx_Get_Unique_ID(void)
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_RD_UNIQUE_ID); // 0x4B
SPI_WRITE_TX(SPI1, 0x00);
SPI_WRITE_TX(SPI1, 0x00);
SPI_WRITE_TX(SPI1, 0x00);
SPI_WRITE_TX(SPI1, 0x00);
uint32_t i = 0;
for(i = 0; i < 8; i++)
{
SPI_WRITE_TX(SPI1, 0x00);
}
w25q_debug();
for(i = 0; i < 13; i++)
{
printf("%x ", SPI_READ_RX(SPI1));
}
printf("\n");
SPI_SET_SS_HIGH(SPI1);
}
void W25Qxx_Get_Manufacturer(void)
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_MANUFACTURER_ID); // 0x90
SPI_WRITE_TX(SPI1, 0x00);
SPI_WRITE_TX(SPI1, 0x00);
SPI_WRITE_TX(SPI1, 0x00);
uint32_t i = 0;
w25q_debug();
for(i = 0; i < 2; i++)
{
printf("%x ", SPI_READ_RX(SPI1));
}
printf("\n");
SPI_SET_SS_HIGH(SPI1);
}
uint8_t W25Qxx_Get_Status_REG1(void)
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_RD_STATUS_REG1);
uint8_t rx = SPI_READ_RX(SPI1);
w25q_debug("%x\n", rx);
SPI_SET_SS_HIGH(SPI1);
return rx;
}
uint8_t W25Qxx_Get_Status_REG2(void)
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_RD_STATUS_REG2);
SPI_WRITE_TX(SPI1, 0x00);
uint8_t rx = SPI_READ_RX(SPI1);
w25q_debug("%x\n", rx);
SPI_SET_SS_HIGH(SPI1);
return rx;
}
void W25Qxx_Is_Busy(void)
{
while(0x01 & W25Qxx_Get_Status_REG1());
}
void W25Qxx_Page_Write(uint32_t addr, uint8_t* buf, uint32_t len)
{
while(SPI_IS_BUSY(SPI1));
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_PAGE_PROGRAM);
SPI_WRITE_TX(SPI1, (addr >> 16) & 0xff);
SPI_WRITE_TX(SPI1, (addr >> 8) & 0xff);
SPI_WRITE_TX(SPI1, (addr >> 0) & 0xff);
uint32_t i = 0;
w25q_debug();
for(; i < len; i++)
{
SPI_WRITE_TX(SPI1, buf[i]);
printf("%x ", buf[i]);
}
printf("\n");
SPI_SET_SS_HIGH(SPI1);
}
void W25Qxx_Read_Data(uint32_t addr, uint8_t* buf, uint32_t len)
{
while(SPI_IS_BUSY(SPI1));
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1, W25Qxx_PAGE_PROGRAM);
SPI_WRITE_TX(SPI1, (addr >> 16) & 0xff);
SPI_WRITE_TX(SPI1, (addr >> 8) & 0xff);
SPI_WRITE_TX(SPI1, (addr >> 0) & 0xff);
uint32_t i = 0;
w25q_debug();
for(; i < len; i++)
{
buf[i] = SPI_READ_RX(SPI1);
printf("%x ", buf[i]);
}
printf("\n");
SPI_SET_SS_HIGH(SPI1);
}
|