21ic问答首页 - 大佬们,急问
大佬们,急问
guiguihhh2022-03-08
本帖最后由 tyw 于 2022-3-9 12:39 编辑
我用了新唐M4系列的主控,然后用SPI1想和SPI Flash通信,结果写入和读出的数据不准确,是什么原因。以下是我使用的代码
// *****************************************************************************
// 函数名称: void SPI_Flash_Send_Add(uint32_t Address)
// 函数功能: 向SPI Flash发送地址
// 入口参数:
// 出口参数:
// 说 明:
// *****************************************************************************
void SPI_Flash_Send_Add(uint32_t Address)
{
SPI_WRITE_TX(SPI1,(Address>>16)&0xff);
SPI_WRITE_TX(SPI1,(Address>>8)&0xff);
SPI_WRITE_TX(SPI1,Address);
/* Wait ready */
while(SPI_IS_BUSY(SPI1));
}
// *****************************************************************************
// 函数名称: uint8_t SPI_Flash_Read_Data(uint8_t *pRamAddress, uint16_t cnt)
// 函数功能: 从SPI Flash中读取数据到指定地址的Ram区域
// 入口参数: cnt不能超过SPI传输的最大值!!
// 出口参数:
// 说 明:
// *****************************************************************************
void SPI_Flash_Read_Buff(uint32_t Address, uint8_t *pRamAddress, uint16_t cnt)
{
// Send the read slow command
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_READSLOW);
/* Wait ready */
while(SPI_IS_BUSY(SPI1));
// Send the address
SPI_Flash_Send_Add(Address);
// Read number of bytes
while(cnt)
{
//8位,读的好像是32位???
SPI_WRITE_TX(SPI1,Dummy_Byte);
*pRamAddress++ = SPI_READ_RX(SPI1); // 读SPI DATA IN 数据
/* Wait ready */
//while(SPI_IS_BUSY(SPI1));
cnt--;
}
SPI_SET_SS_HIGH(SPI1);
}
// *****************************************************************************
// 函数名称: uint8_t SPI_Flash_Write_Buff
// 函数功能: 将BUFF中的数据写入到SPI Flash的指定地址
// 入口参数:
// 出口参数:
// 说 明:
// *****************************************************************************
void SPI_Flash_Write_Buff(uint32_t Address, uint8_t *pRamAddress, uint16_t cnt)
{
while(cnt)
{
// Send a write enable command
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_WREN);
while(SPI_IS_BUSY(SPI1));
SPI_SET_SS_HIGH(SPI1);
// Send a page program command
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_PROGRAM);
while(SPI_IS_BUSY(SPI1));
// Send the address
SPI_Flash_Send_Add(Address);
// Send at least one byte
SPI_WRITE_TX(SPI1,*pRamAddress++);
while(SPI_IS_BUSY(SPI1));
// Increment
Address++;
cnt--;
// Send bytes until we get to the end of a page
while ((cnt > 0) && ((Address&0x0000ff) != 0))
{
SPI_WRITE_TX(SPI1,*pRamAddress++);
while(SPI_IS_BUSY(SPI1));
Address++;
cnt--;
}
// Deselect the chip
SPI_SET_SS_HIGH(SPI1);
// Poll status until the write completes
SPI_Flash_Read_Status();
}
}
uint8_t SPI_Flash_Erase_Sector(uint32_t Address)
{
uint8_t Status;
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_WREN);
while(SPI_IS_BUSY(SPI1));
SPI_SET_SS_HIGH(SPI1);
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_SECTORERASE);
while(SPI_IS_BUSY(SPI1));
SPI_Flash_Send_Add(Address);
SPI_SET_SS_HIGH(SPI1);
Status = SPI_Flash_Read_Status();
return Status;
}
uint8_t SPI_Flash_Read_Status(void)
{
uint8_t MEMSTAT;
do
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_RDSR);
//while(SPI_IS_BUSY(SPI1));
MEMSTAT = SPI_READ_RX(SPI1);
//while(SPI_IS_BUSY(SPI1));
SPI_SET_SS_HIGH(SPI1);
} while (MEMSTAT&0x01);
return MEMSTAT;
}
int main()
{ SPI1_Init();
SPI_Flash_Read_Buff(0x00,puint8_tRxBuf,sizeof(puint8_tRxBuf));
SPI_Flash_Erase_Sector(0x00);
SPI_Flash_Read_Buff(0x00,puint8_tRxBuf,sizeof(puint8_tRxBuf));
SPI_Flash_Write_Buff(0x00,puint8_tTxBuf,sizeof(puint8_tTxBuf));
SPI_Flash_Read_Buff(0x00,puint8_tRxBuf,sizeof(puint8_tRxBuf));
while(1);
}
我用了新唐M4系列的主控,然后用SPI1想和SPI Flash通信,结果写入和读出的数据不准确,是什么原因。以下是我使用的代码
// *****************************************************************************
// 函数名称: void SPI_Flash_Send_Add(uint32_t Address)
// 函数功能: 向SPI Flash发送地址
// 入口参数:
// 出口参数:
// 说 明:
// *****************************************************************************
void SPI_Flash_Send_Add(uint32_t Address)
{
SPI_WRITE_TX(SPI1,(Address>>16)&0xff);
SPI_WRITE_TX(SPI1,(Address>>8)&0xff);
SPI_WRITE_TX(SPI1,Address);
/* Wait ready */
while(SPI_IS_BUSY(SPI1));
}
// *****************************************************************************
// 函数名称: uint8_t SPI_Flash_Read_Data(uint8_t *pRamAddress, uint16_t cnt)
// 函数功能: 从SPI Flash中读取数据到指定地址的Ram区域
// 入口参数: cnt不能超过SPI传输的最大值!!
// 出口参数:
// 说 明:
// *****************************************************************************
void SPI_Flash_Read_Buff(uint32_t Address, uint8_t *pRamAddress, uint16_t cnt)
{
// Send the read slow command
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_READSLOW);
/* Wait ready */
while(SPI_IS_BUSY(SPI1));
// Send the address
SPI_Flash_Send_Add(Address);
// Read number of bytes
while(cnt)
{
//8位,读的好像是32位???
SPI_WRITE_TX(SPI1,Dummy_Byte);
*pRamAddress++ = SPI_READ_RX(SPI1); // 读SPI DATA IN 数据
/* Wait ready */
//while(SPI_IS_BUSY(SPI1));
cnt--;
}
SPI_SET_SS_HIGH(SPI1);
}
// *****************************************************************************
// 函数名称: uint8_t SPI_Flash_Write_Buff
// 函数功能: 将BUFF中的数据写入到SPI Flash的指定地址
// 入口参数:
// 出口参数:
// 说 明:
// *****************************************************************************
void SPI_Flash_Write_Buff(uint32_t Address, uint8_t *pRamAddress, uint16_t cnt)
{
while(cnt)
{
// Send a write enable command
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_WREN);
while(SPI_IS_BUSY(SPI1));
SPI_SET_SS_HIGH(SPI1);
// Send a page program command
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_PROGRAM);
while(SPI_IS_BUSY(SPI1));
// Send the address
SPI_Flash_Send_Add(Address);
// Send at least one byte
SPI_WRITE_TX(SPI1,*pRamAddress++);
while(SPI_IS_BUSY(SPI1));
// Increment
Address++;
cnt--;
// Send bytes until we get to the end of a page
while ((cnt > 0) && ((Address&0x0000ff) != 0))
{
SPI_WRITE_TX(SPI1,*pRamAddress++);
while(SPI_IS_BUSY(SPI1));
Address++;
cnt--;
}
// Deselect the chip
SPI_SET_SS_HIGH(SPI1);
// Poll status until the write completes
SPI_Flash_Read_Status();
}
}
uint8_t SPI_Flash_Erase_Sector(uint32_t Address)
{
uint8_t Status;
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_WREN);
while(SPI_IS_BUSY(SPI1));
SPI_SET_SS_HIGH(SPI1);
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_SECTORERASE);
while(SPI_IS_BUSY(SPI1));
SPI_Flash_Send_Add(Address);
SPI_SET_SS_HIGH(SPI1);
Status = SPI_Flash_Read_Status();
return Status;
}
uint8_t SPI_Flash_Read_Status(void)
{
uint8_t MEMSTAT;
do
{
SPI_SET_SS_LOW(SPI1);
SPI_WRITE_TX(SPI1,SPIFLH_CMD_RDSR);
//while(SPI_IS_BUSY(SPI1));
MEMSTAT = SPI_READ_RX(SPI1);
//while(SPI_IS_BUSY(SPI1));
SPI_SET_SS_HIGH(SPI1);
} while (MEMSTAT&0x01);
return MEMSTAT;
}
int main()
{ SPI1_Init();
SPI_Flash_Read_Buff(0x00,puint8_tRxBuf,sizeof(puint8_tRxBuf));
SPI_Flash_Erase_Sector(0x00);
SPI_Flash_Read_Buff(0x00,puint8_tRxBuf,sizeof(puint8_tRxBuf));
SPI_Flash_Write_Buff(0x00,puint8_tTxBuf,sizeof(puint8_tTxBuf));
SPI_Flash_Read_Buff(0x00,puint8_tRxBuf,sizeof(puint8_tRxBuf));
while(1);
}
赞0
评论
2022-03-10
赞0
评论
2022-03-10
赞0
评论
2022-03-09
赞0
评论
2022-03-09
赞0
评论
2022-03-09
不好意思哈,第一次发帖没有太注意
赞0
评论
2022-03-08
您需要登录后才可以回复 登录 | 注册