STR710F的SPI接口驱动EEPROM型号为AT25128,时序也没什么问题,结果却不对,请高手指点
/**************************C文件****************************/
#include "at251.h"
//#include "bspi.h"
/////////////////////////////////////////////////////////////////////////
void BSPI1_init(void)
{
GPIO_Config(GPIO0, 0x0070, GPIO_AF_PP);// GPIO_Config(GPIO0, 0x00F0, GPIO_AF_PP); //Configure GPI00 on mode Alternate function Push Pull
GPIO_Config(GPIO0, 0x0080, GPIO_OUT_PP);
BSPI_Init(BSPI1);
BSPI_ClockDividerConfig(BSPI1, 8); //Configure Baud rate Frequency :-->APB1/8
BSPI_Enable(BSPI1, ENABLE); //Enable BSPI1
BSPI_MasterEnable(BSPI1, ENABLE);
BSPI_ClkActiveHigh(BSPI1, ENABLE); //Configure the clock to be active high
BSPI_ClkFEdge(BSPI1, DISABLE); //Enable capturing the first Data sample on the first edge of SCK
BSPI_8bLEn(BSPI1, ENABLE); //Set the word length to 8 bit
BSPI_RcFifoDepth(BSPI1, 7); //Configure the depth of receive to 5 word
}
/////////////////////////////////////////////////////////////////////////////
void AT251_init(void)
{
BSPI1_init();
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
}
/////////////////////////////////////////////////////////////////////////////////////
u8 AT251_readbyte(u16 addr)
{
u8 buf;
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1,READ);
AT251_AddrWrite(addr);
buf=BSPI_WordReceive(BSPI1);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
return(buf);
}
//////////////////////////////////////////////////////////////////////////////////
void AT251_CmdWrite(u8 cmd)
{
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1, cmd);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
}
////////////////////////////////////////////////////////////////////////////////
void AT251_AddrWrite(u16 addr)
{
BSPI_WordSend(BSPI1,(u8)(addr>>8));
BSPI_WordSend(BSPI1,(u8)addr);
}
//////////////////////////////////////////////////////////////////////
void AT251_ByteWrite(u16 addr,u8 data )//指定地址写一个字节
{int i;
AT251_CmdWrite(WREN);//WRITE ENABLE
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;AT251_CmdWrite(WRITE);
BSPI_WordSend(BSPI1,WRITE);
AT251_AddrWrite(addr);
BSPI_WordSend(BSPI1,data);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
AT251_CmdWrite(WRDI );//写禁止
for(i=0;i<40;i++);
}
//////////////////////////////////////////////////////////////////////////////////
u8 CheckStatus()
{ u8 S;
//AT251_CmdWrite(WREN);//WRITE ENABLE
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1,RDSR);
S=BSPI_WordReceive(BSPI1);
GPIO_BitWrite(GPIO0,0x07,1);//ss=0;
return S;
}
/////////////////////////////////////////////////////////////////////////////////////
void AT251_DataRead(u8 *buf,u16 addr1,u16 addr2,u16 lastcounts)//指定地址读取指定个数据
{
u16 j,m;
u16 dataword;
//AT251_CmdWrite(WREN);
for(j=addr1;j<addr2;j++)
{
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1,READ);//AT251_CmdWrite(READ);
dataword=(j-1);//dataword=32*(j-1);
AT251_AddrWrite(dataword);
buf[(j-addr1)]=BSPI_WordReceive(BSPI1);
//for(m=0;m<8;m++);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
u8 AT251_DataWrite(u8 *buf,u16 addr1,u16 addr2,u16 lastcounts)//指定地址读取指定个数据
{ u8 j,m ;
u8 databyte ;
u16 dataword ;
for(j=addr1;j<addr2;j++)
{ databyte=CheckStatus(READY_BUSY);
if(databyte) return(1);
AT251_CmdWrite(WREN);
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1,WRITE);
dataword=(j-1);
AT251_AddrWrite(dataword);
BSPI_WordSend(BSPI1,buf[j-addr1]);
//for(m=0;m<8;m++);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
}
return(0);
} |
|