根据小苗开发板例程,修改得好用的读写FM24CL16的2K bytes代码,分享给大家。
小苗开发板的程序有点问题,I2C_Read()里最后产生stop条件时间不对;另外FM24CL16是11位地址的,在I2C_Write()里也应体现出来。
先贴代码,以后再解析。
/**************************************************************** 函数名 :drv_i2c1_init** 功能 : 初始化I2C1接口。包括GPIO配置和I2C1配置//PB6--I2C1_SCL, PB7--I2C1_SDA.** 输入 :无** 输出 :无** 返回 :无** 注意 :无
***************************************************************/
void drv_i2c1_init(void)
{GPIO_InitTypeDef GPIO_InitStructure; //GPIOI2C_InitTypeDef I2C_InitStructure; //IICRCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); //使能I2C时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIO时钟GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//IIC引脚PB6--SCL, PB7--SDA.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //复用开漏输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50MHZGPIO_Init(GPIOB, &GPIO_InitStructure);I2C_DeInit(I2C1); //将I2C1寄存器重置为缺省值I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; //设置I2C1为I2C模式I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;//I2C快速Tlow/Thigh = 2I2C_InitStructure.I2C_OwnAddress1 = 0x01; //设置第一个设备自身地址·I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //使能应答ACKI2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; //定义应答地址7bitI2C_InitStructure.I2C_ClockSpeed = 200000; //设置时钟频率200KhzI2C_Cmd(I2C1, ENABLE); //使能IIC1I2C_Init(I2C1, &I2C_InitStructure);//初始化IIC1I2C_AcknowledgeConfig(I2C1, ENABLE);//使能应答功能
}
/**************************************************************** 函数名 :I2C_Write_Byte** 功能 :字节写入操作** 输入 :addr:要写入的FRAM的地址,data:要写入的内容** 输出 :无** 返回 :无** 注意 :无
***************************************************************/
u8 I2C_Write_Byte(u16 addr,u8 data)
{u16 i = 0;u8 flag = 0;addr = addr & 0x07FF;I2C_GenerateSTART(I2C1, ENABLE);//产生IIC1传输START条件i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) //检查EV5 {i++; if(I2C_MAX_DEAD_TIME <= i){flag = 1; break;}} I2C_Send7bitAddress(I2C1, FRAM_ADDR|(((addr>>8)&0x07)<<1), I2C_Direction_Transmitter);//发送器件地址i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))//检查VE6 {i++; if(I2C_MAX_DEAD_TIME <= i){flag = 2;break;}}I2C_SendData(I2C1, (u8)addr);//发送写地址i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))//检查VE8{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 3;break;}}I2C_SendData(I2C1, (u8)data);//写入字节i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))//检查VE8 {i++; if(I2C_MAX_DEAD_TIME <= i){flag = 4;break;}} I2C_GenerateSTOP(I2C1, ENABLE); //产生IIC传输STOP条件 I2C_Standby();return flag;
}/**************************************************************** 函数名 :I2C_Write_Page** 功能 :页写入操作** 输入 :Waddr:要写入的FRAM的地址,pBuffer:存放要写入的内容,Num:写入的字节数。这里不做页检验,认为waddr和waddr+num都一页内的地址** 输出 :无** 返回 :无** 注意 :一页8个字节,每次写入操作不得大于8
***************************************************************/
static u8 I2C_Write_Page(u16 Waddr,u8* pBuffer, u8 Num)
{u16 i;u8 flag = 0;Waddr = Waddr & 0x07FF;i = 0;while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)) //总线忙{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 1;break;}}I2C_GenerateSTART(I2C1, ENABLE); //产生IIC1传输START条件i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))//检查EV5 {i++; if(I2C_MAX_DEAD_TIME <= i){flag = 2;break;}}I2C_Send7bitAddress(I2C1, FRAM_ADDR|(((Waddr>>8)&0x07)<<1), I2C_Direction_Transmitter);//发送器件地址i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))//检查VE6{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 3;break;}}I2C_SendData(I2C1, (u8)Waddr);//发送写地址i = 0;while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))//检查VE8{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 4;break;}}while(Num--) {I2C_SendData(I2C1, *pBuffer);//写入字节 pBuffer++;i = 0;while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))//检查VE8{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 5;break;}} }I2C_GenerateSTOP(I2C1, ENABLE);//产生IIC传输STOP条件 return flag;
}//For FM24CL16, 16384 bits = 2k bytes.
/**************************************************************** 函数名 :I2C_Write** 功能 :写入数据** 输入 :Waddr:要写入的FRAM的地址,pBuffer:存放要写入的内容,Num:写入的字节数。对FM24CL16, Waddr是11位地址。** 输出 :无** 返回 :无** 注意 :
***************************************************************/
void I2C_Write(u16 Waddr,u8* pBuffer, u16 Num)
{u8 ADDR;Waddr = Waddr & 0x07FF;ADDR=Waddr % I2C_PAGESIZE;ADDR=I2C_PAGESIZE-ADDR; //差ADDR个字节,满1页;if (Num <= ADDR) //写页不对齐部分,且所有 数据在本页内{I2C_Write_Page(Waddr,pBuffer,Num);//不满一页的,写完直接跳出Num=0;I2C_Standby(); }else{if(ADDR) //写页不对齐部分{I2C_Write_Page(Waddr,pBuffer,ADDR);Num-=ADDR; //减去不对齐的字节Waddr+=ADDR;//写地址后移ADDRpBuffer+=ADDR;//指针后移ADDRI2C_Standby();}while(Num)//写页对齐部分{if(Num>=I2C_PAGESIZE)//如果要写入的数据大于等于1页{I2C_Write_Page(Waddr,pBuffer,I2C_PAGESIZE); //写一页Num-=I2C_PAGESIZE; //减去一页Waddr+=I2C_PAGESIZE; //写地址向后移一页pBuffer+=I2C_PAGESIZE;//指针后移一页I2C_Standby();}else{I2C_Write_Page(Waddr,pBuffer,Num);//不满一页的,写完直接跳出Num=0;I2C_Standby();}}}
}/**************************************************************** 函数名 :I2C_Read** 功能 :从FRAM中读取1个或多个数据** 输入 :Raddr:要读取的FRAM的地址,pBuffer存放从FRAM中读取的内容,Num:读取的字节数。//If the internal address reaches 7FFh it will wrap around to 000h on the next read cycle.上面说明read不用考虑页边界和256边界.** 输出 :无** 返回 :无** 注意 :无
***************************************************************/
u8 I2C_Read(u16 Raddr ,u8* pBuffer,u16 Num)
{u16 i;u8 flag = 0;if(Num==0)return 0xFF;Raddr = Raddr & 0x07FF;i = 0;while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)) //检测总线忙标志位 {i++; if(I2C_MAX_DEAD_TIME <= i){flag = 1;break;}}I2C_AcknowledgeConfig(I2C1, ENABLE);//允许应答I2C_GenerateSTART(I2C1, ENABLE);//产生IIC1传输START条件i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))//检查EV5{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 2;break;}}I2C_Send7bitAddress(I2C1, FRAM_ADDR|(((Raddr>>8)&0x07)<<1), I2C_Direction_Transmitter);//发送器件地址i = 0;while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))//检查VE6{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 3;break;}}I2C_SendData(I2C1, (u8)Raddr);//发送伪写地址i = 0;while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))//检查VE8{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 4;break;}} I2C_GenerateSTART(I2C1, ENABLE);//产生IIC1传输START条件i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))//检查EV5{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 5;break;}}I2C_Send7bitAddress(I2C1, FRAM_ADDR|(((Raddr>>8)&0x07)<<1), I2C_Direction_Receiver); //接收地址i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))//检查VE6{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 6;break;}}while (Num){if(Num==1){I2C_AcknowledgeConfig(I2C1, DISABLE); //最后一位后要关闭应答的//I2C_GenerateSTOP(I2C1, ENABLE); //发送停止位 //It's a bug.}i = 0;while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) //检测VE7 //always dead here.{i++; if(I2C_MAX_DEAD_TIME <= i){flag = 7;break;}}*pBuffer = I2C_ReceiveData(I2C1);pBuffer++;Num--;}I2C_GenerateSTOP(I2C1, ENABLE); //发送停止位 //It's a bug. I2C_AcknowledgeConfig(I2C1, ENABLE);//再次允许应答模式return flag;
}/*
//FM24CL16不需要忙等待,EEPROM才需要
Unlike other nonvolatile memory technologies, there is no write delay with FRAM.
The entire memory cycle occurs in less time than a single bus clock.
Therefore, any operation including read or write can occur immediately following a write.
Acknowledge polling, a technique used with EEPROMs to determine if a write is complete is unnecessary and
will always return a 'ready' condition.
An actual memory array write occurs after the 8th data bit is transferred.
*/
/**************************************************************** 函数名 :I2C_Standby** 功能 :忙等待** 输入 :无** 输出 :无** 返回 :无** 注意 :无
***************************************************************/
u8 I2C_Standby(void)
{
#if 0u16 i;u8 flag = 0;i = 0;do{I2C_GenerateSTART(I2C1, ENABLE);//产生IIC1传输START条件I2C_Send7bitAddress(I2C1, FRAM_ADDR, I2C_Direction_Transmitter);//向FRAM发送地址i++; if(I2C_MAX_DEAD_TIME <= i){flag = 7;break;} }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002));//检测SR1第1位,查看地址是否发送完成I2C_ClearFlag(I2C1, I2C_FLAG_AF); //清除应答错误标志位 I2C_GenerateSTOP(I2C1, ENABLE); //产生IIC传输STOP条件return flag;
#elsereturn 0x00;
#endif
}
欢迎指教!