FM25V10驱动代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include "fm25v10.h"
#include "mdelay.h"
void SPI_Init(void)
{
RCC->APB2ENR |= 1<<3; //使能PORTB口时钟
RCC->APB1ENR |= 1<<14; //使能SPI2时钟
GPIOB->CRH &= 0X0000FFFF; //配置PB13.14.15为复用推挽输出功能GPIOB->CRH |= 0XBBB30000; //PB12为IO推挽输出
GPIOB->ODR |= 0xf<<12; //上拉输出
SPI2->CR1 &= ~(1<<10); //全双工
SPI2->CR1 |= 1<<9; //启用软件从设备管理
SPI2->CR1 |= 1<<8; //nss输出1
SPI2->CR1 |= 1<<2; //主设备
SPI2->CR1 &= ~(1<<11); //8bit数据帧
SPI2->CR1 &= ~(1<<1);//SPI2->CR1 |= 1<<1; //CPOL=1
SPI2->CR1 &= ~(1<<0);//SPI2->CR1 |= 1<<0; //CPHA=1
SPI2->CR1 |= 0<<3;//4<<3; //Fsck=Fcpu/32 即72/32 = 2.25MHz SPI2->CR1 &= ~(1<<7); //MSB在前
SPI2->CR1 |= 1<<6; //开启SPI2
}
u8 SPIx_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while((SPI2->SR&(1<<1))==0) //等待发送缓冲器空
{
retry++;
if(retry>200) return 0;
}
SPI2->DR = TxData;
retry=0;
while((SPI2->SR&(1<<0))==0) //等待接收缓冲器非空
{
retry++;
if(retry>200) return 0;
}
return SPI2->DR;
}
///////////////////////////////////////////
void FramWriteByte(u32 address,u8 da)
{
u8 temH,temM,temL;
temH=(u8)((address&0xff0000)>>16);
temM=(u8)((address&0x00ff00)>>8);
temL=(u8)(address&0x0000ff);
FRAM_CS = 0;
SPIx_ReadWriteByte(WREN); //写使能FRAM_CS = 1;
//delay_us(1);
udelay(2);
FRAM_CS = 0;
SPIx_ReadWriteByte(WRITE); //写命令SPIx_ReadWriteByte(temH); //高位地址SPIx_ReadWriteByte(temM); //中位地址SPIx_ReadWriteByte(temL); //低位地址SPIx_ReadWriteByte(da); //写入数据FRAM_CS = 1;
//mdelay(5);
udelay(2);
FRAM_CS = 0;
SPIx_ReadWriteByte(WRDI); //写禁能FRAM_CS = 1;
}
u8 FramReadByte(u32 address)
{
u8 temp;
u8 temH,temM,temL;
temH=(u8)((address&0xff0000)>>16);
temM=(u8)((address&0x00ff00)>>8);
temL=(u8)(address&0x0000ff);
FRAM_CS = 0; //片选CS=0
SPIx_ReadWriteByte(READ); //读命令SPIx_ReadWriteByte(temH); //高位地址
SPIx_ReadWriteByte(temM); //中位地址SPIx_ReadWriteByte(temL); //低位地址temp = SPIx_ReadWriteByte(0xF0);
FRAM_CS = 1; //片选CS=1
return temp;
}
void FramWriteInduce(u32 address,u32 number,u8 *p) {
u8 temH,temM,temL;
u32 i;
temH=(u8)((address&0xff0000)>>16);
temM=(u8)((address&0x00ff00)>>8);
temL=(u8)(address&0x0000ff);
FRAM_CS = 0;
SPIx_ReadWriteByte(WREN); //写使能FRAM_CS = 1;
//delay_us(1);
udelay(2);
FRAM_CS = 0;
SPIx_ReadWriteByte(WRITE); //写命令SPIx_ReadWriteByte(temH); //高位地址SPIx_ReadWriteByte(temM); //中位地址SPIx_ReadWriteByte(temL); //低位地址//SPIx_ReadWriteByte(da); //写入数据for(i=0;i<number;i++)
{
SPIx_ReadWriteByte(*p++);
}
FRAM_CS = 1;
udelay(2);
//mdelay(5);
FRAM_CS = 0;
SPIx_ReadWriteByte(WRDI); //写禁能FRAM_CS = 1;
}
void FramReadInduce(u32 address,u32 number,u8 *p) {
u8 temH,temM,temL;
u32 i;
temH=(u8)((address&0xff0000)>>16);
temM=(u8)((address&0x00ff00)>>8);
temL=(u8)(address&0x0000ff);
FRAM_CS = 0; //片选CS=0 SPIx_ReadWriteByte(READ); //读命令SPIx_ReadWriteByte(temH); //高位地址SPIx_ReadWriteByte(temM); //中位地址SPIx_ReadWriteByte(temL); //低位地址//temp = SPIx_ReadWriteByte(0xF0); //读数据for(i=0;i<number;i++)
{
*p++ = SPIx_ReadWriteByte(0xF0);
}
FRAM_CS = 1; //片选CS=1
}。

相关文档
最新文档