|
/*结合上面的初始化程序,就是这个FRAM的驱动,希望对你有帮助*/
#define WREN_CMD 0x06
#define WRDI_CMD 0x04
#define WRSR_CMD 0x01
#define RDSR_CMD 0x05
#define WRITE_CMD 0x02
#define READ_CMD 0x03
//读或写一个直接
//返回读写是否成功标记。 1失败,0成功。
UCHAR SPI_ReadWriteByte(UCHAR TxData,UCHAR *RxData)
{
USHORT cw=0,cr=0;
//等待发送缓冲区空
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
{
if(++cw > 60000)return 1;
}
//发一个字节
SPI_I2S_SendData(SPI2, TxData);
//等待数据接收
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
{
if(++cr > 60000)return 1;
}
//取数据
*RxData = SPI_I2S_ReceiveData(SPI2);
return 0;
}
//SPI写一个字节
void SPIWriteByte(UCHAR nByte)
{
UCHAR rd;
SPI_ReadWriteByte(nByte,&rd);
}
//SPI读一个字节
UCHAR SPIReadByte(void)
{
UCHAR data;
SPI_ReadWriteByte(0xA5,&data); //0xA5是dummy data
return data;
}
/*-----------------------------------------------------------*/
/*
* 函数:
*
* 入口:
*
* 出口:
*
* 说明: 写FRAM函数
*
* 创建:
*
*/
//写一个字节到FRAM
void FMWriteOne(USHORT nFMAddr,UCHAR nFMData)
{
FM_CS_0;
SPIWriteByte(WREN_CMD);
FM_CS_1;
DelayNOP(10);
FM_CS_0;
SPIWriteByte(WRITE_CMD);
SPIWriteByte((UCHAR)(nFMAddr>>8));
SPIWriteByte((UCHAR)(nFMAddr&0x00ff));
SPIWriteByte(nFMData);
FM_CS_1;
}
//连续写N个字节到FRAM
UCHAR FMWriteSerial(USHORT nFMAddr,USHORT nFMNum,UCHAR *nFMData)
{
USHORT i;
UCHAR xor; //写的同时进行异或
xor = 0;
FM_CS_0;
SPIWriteByte(WREN_CMD);
FM_CS_1;
Delayms(3);
FM_CS_0;
SPIWriteByte(WRITE_CMD);
SPIWriteByte((UCHAR)(nFMAddr>>8));
SPIWriteByte((UCHAR)(nFMAddr&0x00ff));
for(i=0;i<nFMNum;i++)
{
SPIWriteByte(*nFMData);
xor = xor ^ (*nFMData);
nFMData++;
}
FM_CS_1;
return xor;
}
/*-----------------------------------------------------------*/
/*
* 函数:
*
* 入口:
*
* 出口:
*
* 说明: 读FRAM函数
*
* 创建:
*
*/
//从FRAM里读出一个字节
UCHAR FMReadOne(USHORT nFMAddr)
{
UCHAR nData;
FM_CS_0;
SPIWriteByte(READ_CMD);
SPIWriteByte((UCHAR)(nFMAddr>>8));
SPIWriteByte((UCHAR)(nFMAddr&0x00ff));
nData = SPIReadByte();
FM_CS_1;
return nData;
}
//从FRAM里连续读出N个字节
UCHAR FMReadSerial(USHORT nFMAddr,USHORT nFMNum,UCHAR *nFMData)
{
USHORT i;
UCHAR xor;
xor = 0;
FM_CS_0;
SPIWriteByte(READ_CMD);
SPIWriteByte((UCHAR)(nFMAddr>>8));
SPIWriteByte((UCHAR)(nFMAddr&0x00ff));
for(i=0;i<nFMNum;i++)
{
*nFMData = SPIReadByte();
xor = xor ^ *nFMData;
nFMData++;
}
FM_CS_1;
return xor;
}
//从FRAM读出数据后校验
UCHAR FMCheck(USHORT nFMAddr,USHORT nFMNum)
{
USHORT i;
UCHAR xor,data;
xor=0;
FM_CS_0;
SPIWriteByte(READ_CMD);
SPIWriteByte((UCHAR)(nFMAddr>>8));
SPIWriteByte((UCHAR)(nFMAddr&0x00ff));
for(i=0;i<nFMNum;i++)
{
data = SPIReadByte();
xor = xor ^ data;
}
FM_CS_1;
return xor;
}
/*-----------------------------------------------------------*/
/*
* 函数:
*
* 入口:
*
* 出口:
*
* 说明: 读FRAM状态
*
* 创建:
*
*/
UCHAR FMReadStatus(void)
{
UCHAR nStatus;
FM_CS_0;
SPIWriteByte(RDSR_CMD);
nStatus = SPIReadByte();
FM_CS_1;
return nStatus;
}
/*-----------------------------------------------------------*/
/*
* 函数:
*
* 入口:
*
* 出口:
*
* 说明: 写FRAM状态
*
* 创建:
*
*/
void FMWriteStatus(UCHAR nStatus)
{
FM_CS_0;
SPIWriteByte(WREN_CMD);
FM_CS_1;
DelayNOP(10);
FM_CS_0;
SPIWriteByte(WRSR_CMD);
SPIWriteByte(nStatus);
FM_CS_1;
} |
|