///////////////////////////////////////////////////////////////
// 文 件 名 : Eeprom.c
// 文件功能 : EEPROM操作
// 作 者 : 仲伟
// 创建时间 : 2005年8月30日
// 项目名称 : LcmMain
// 操作系统 :
// 备 注 :
// 历史记录 :
///////////////////////////////////////////////////////////////
#include "LmcInc.h"
#include "Eeprom.h"
#include <string.h>
#include "hdDriver.h"
//等待,知道EEPROM操作空闲
//#define WAIT_EEPROM_CANWRITE() while(!hd_EepromCanWrite()){hd_ClearWatchDog();}
uint16 WAIT_EEPROM_CANWRITE(void)
{
uint32 count=0;
while(!hd_EepromCanWrite())
{
count++;
if(count>8000000L) return 0;
}
return 1;
}
//实际系统为24c64,以32个字节为一页,而且TI为最小单位字(两个字节),所以一页的单位为16
inline uint16 GetEepromNextPageBegin(uint16 Addr)
{
return (((Addr>>4)+1)<<4); //(Addr/16+1)*16
}
///////////////////////////////////////////////////////////////
// 函 数 名 : Initial_WriteEeprom
// 函数功能 : 初始化Eeprom写缓冲区
// 处理过程 :
// 备 注 :
// 作 者 : 仲伟
// 时 间 : 2005年8月31日
// 返 回 值 : void
// 参数说明 : void
///////////////////////////////////////////////////////////////
void Initial_WriteEeprom(void)
{
WriteEeprom.bWrite=FALSE;
WriteEeprom.begin=WriteEeprom.end=0;
//初始化两个指针
WriteEeprom.pEeprom=(uint8 *)&Ram.Eeprom;
WriteEeprom.pRtc=(uint8 *)&Ram.Rtc;
}
uint8 eeprom_HaveOperator(void)
{
return WriteEeprom.bWrite;
}
void WriteEepromProc_InMainCycle(void)
{
volatile TWriteBuffer * pBuff;
uint16 temp,i,temp1;
while(WriteEeprom.bWrite) //检查是否写进行中
{
if(WriteEeprom.bNeedDelay)
{
if(GetSoftTimerStatus(EEPROM_TIMER)==TIMER_FLAG_DISABLE)
{
InitSoftTimer(EEPROM_TIMER, EEPROM_DELAYTIME);
return;
}
else if(GetSoftTimerStatus(EEPROM_TIMER)==TIMER_FLAG_TRIGGER)
{
CloseSoftTimer(EEPROM_TIMER);
WriteEeprom.bNeedDelay=FALSE;
}
else
{
return; //延时还没有结束,需要继续等待
}
}
if(WriteEeprom.begin!=WriteEeprom.end)
{
if(hd_EepromCanWrite())
{
DINT;
pBuff=&WriteEeprom.wrBuf[WriteEeprom.begin];
if(pBuff->I2C_sla==EEPROM_U64)
{
temp=GetEepromNextPageBegin(pBuff->Addr)-pBuff->Addr;
if(temp>=pBuff->len)
{
for(i=0;i<pBuff->len;i++)
{
temp1=*(((uint16*)pBuff->pRamAddr)+i);
WriteEeprom.OperateBuff[i*2+0]=(temp1>>8)&0xff;
WriteEeprom.OperateBuff[i*2+1]=temp1&0xff;
}
WriteEeprom.CurrentBuf.Addr=pBuff->Addr*2;
WriteEeprom.CurrentBuf.I2C_sla=pBuff->I2C_sla;
WriteEeprom.CurrentBuf.pRamAddr=WriteEeprom.OperateBuff;
WriteEeprom.CurrentBuf.len=pBuff->len*2;
WriteEeprom.begin = (WriteEeprom.begin+1) & EEPROM_WRBUF_MASK;
}
else
{
for(i=0;i<temp;i++)
{
temp1=*(((uint16*)pBuff->pRamAddr)+i);
WriteEeprom.OperateBuff[i*2+0]=(temp1>>8)&0xff;
WriteEeprom.OperateBuff[i*2+1]=temp1&0xff;
}
WriteEeprom.CurrentBuf.Addr=pBuff->Addr*2;
WriteEeprom.CurrentBuf.I2C_sla=pBuff->I2C_sla;
WriteEeprom.CurrentBuf.pRamAddr=WriteEeprom.OperateBuff;
WriteEeprom.CurrentBuf.len=temp*2;
pBuff->len-=temp;
pBuff->pRamAddr=(uint8 *)pBuff->pRamAddr+temp;
pBuff->Addr=pBuff->Addr+temp;
}
}
else if(pBuff->I2C_sla==RTC)
{
WriteEeprom.CurrentBuf.Addr=pBuff->Addr;
WriteEeprom.CurrentBuf.I2C_sla=pBuff->I2C_sla;
WriteEeprom.CurrentBuf.pRamAddr=&Ram.Rtc.RtcTime;
WriteEeprom.CurrentBuf.len=pBuff->len;
WriteEeprom.begin = (WriteEeprom.begin+1) & EEPROM_WRBUF_MASK;
}
EINT;
//写数据到EEPROM
//DINT;
hd_EepromWriteByteArray(WriteEeprom.CurrentBuf.I2C_sla,
WriteEeprom.CurrentBuf.Addr,
WriteEeprom.CurrentBuf.len,
WriteEeprom.CurrentBuf.pRamAddr);
//下一次写,需要延时
WriteEeprom.bNeedDelay=TRUE;
}
}
else
{
WriteEeprom.bWrite=FALSE;
}
}
}
///////////////////////////////////////////////////////////////
// 函 数 名 : hd_EepromReadByteArray
// 函数功能 : 读取EEPROM一串数据的函数
// 处理过程 :
// 备 注 :
// 作 者 : 仲伟
// 时 间 : 2005年8月30日
// 返 回 值 : void
// 参数说明 : uint16 StartAddress, 读取EEPROM开始地址
// uint16 len, 数据长度
// uint8 * buff 返回数据存放地址
///////////////////////////////////////////////////////////////
uint16 hd_EepromReadByteArray(uint8 sla, uint16 StartAddress, uint16 len, void * buff)
{
//WAIT_EEPROM_CANWRITE();
if(!WAIT_EEPROM_CANWRITE()) return 0;
if(sla==EEPROM_U64)
{
hd_I2C_ReadNByte(sla, hd_I2C_GetAddrLen(sla), StartAddress*2, buff, len*2);
}
else if(sla==RTC)
{
hd_I2C_ReadNByte(sla, hd_I2C_GetAddrLen(sla), StartAddress, buff, len);
}
//WAIT_EEPROM_CANWRITE();
if(!WAIT_EEPROM_CANWRITE()) return 0;
return 1;
}
///////////////////////////////////////////////////////////////
// 函 数 名 : hd_EepromWriteByteArray
// 函数功能 : 写一串数据的接口
// 处理过程 :
// 备 注 :
// 作 者 : 仲伟
// 时 间 : 2005年8月30日
// 返 回 值 : void
// 参数说明 : uint16 StartAddress,
// uint16 len, len<=32,硬件限制,一次最长写32字节
// uint8 * buff
///////////////////////////////////////////////////////////////
void hd_EepromWriteByteArray(uint8 sla, uint16 StartAddress, uint16 len, void * buff)
{
// L_ASSERT(len>0 && buff!=NULL);
// L_ASSERT(len<=EEPROM_WRPER_MAXLEN);
if(!hd_I2C_WriteNByte(sla, hd_I2C_GetAddrLen(sla), StartAddress, buff, len))
{
L_TRACE(__FILE__, __LINE__,"write i2c fail! count=%x",Ram.I2C_errorcount);
Ram.I2C_errorcount++;
}
}
///////////////////////////////////////////////////////////////
// 函 数 名 : eeprom_SynWriteByteArray
// 函数功能 : 同步写一串数据的函数,数据长度大小没有限制
// 处理过程 :
// 备 注 : 由于EEPROM写多个数据时只能分页写,每页最大32,并且区间固定
// 0~31,32~63,...
// 作 者 : 仲伟
// 时 间 : 2005年8月31日
// 返 回 值 : void
// 参数说明 : uint8 * StartAddress,
// uint16 len,
// uint8 * buff
///////////////////////////////////////////////////////////////
void eeprom_SynWriteByteArray(uint8 sla, uint16 StartAddress, uint16 len, void * buff)
{
uint16 EeAddr=StartAddress;
uint16 l;
uint16 temp,temp1,i;
// L_ASSERT(len>0 && buff!=NULL);
//等待到EEPROM可以写数据
WAIT_EEPROM_CANWRITE();
while(len)
{
temp=GetEepromNextPageBegin(EeAddr);
l=temp-EeAddr;
if(l>=len)
{
//hd_EepromWriteByteArray(sla, EeAddr, len, ((uint8 *)buff)+EeAddr-StartAddress);
for(i=0;i<len;i++)
{
temp1=*((uint16*)buff+EeAddr-StartAddress+i);
WriteEeprom.OperateBuff[i*2+0]=(temp1>>8)&0xff;
WriteEeprom.OperateBuff[i*2+1]=temp1&0xff;
}
hd_EepromWriteByteArray(sla, EeAddr*2, len*2, WriteEeprom.OperateBuff);
len=0;
EeAddr+=len;
}
else
{
//hd_EepromWriteByteArray(sla, EeAddr, l, ((uint8 *)buff)+EeAddr-StartAddress);
for(i=0;i<l;i++)
{
temp1=*((uint16*)buff+EeAddr-StartAddress+i);
WriteEeprom.OperateBuff[i*2+0]=(temp1>>8)&0xff;
WriteEeprom.OperateBuff[i*2+1]=temp1&0xff;
}
hd_EepromWriteByteArray(sla, EeAddr*2, l*2, WriteEeprom.OperateBuff);
len-=l;
EeAddr+=l;
}
//清狗,modify by zhang
hd_ClearWatchDog();
//等待到EEPROM写数据完成
WAIT_EEPROM_CANWRITE();
//等待一段延时继续
hd_DelayMS(EEPROM_DELAYTIME);
// DELAY_US(EEPROM_DELAYTIME*1000);
// ClearMs(ONE_MSTIMER);
// while(GetMs(ONE_MSTIMER)<=15);
}
}
///////////////////////////////////////////////////////////////
// 函 数 名 : eeprom_WriteByEepromAddr
// 函数功能 : 根据EEPROM中数据地址写EEPROM
// 处理过程 :
// 备 注 : 异步写数据
// 作 者 : 仲伟
// 时 间 : 2005年8月31日
// 返 回 值 : void
// 参数说明 : uint8 * pEepromAddress,
// uint16 len
///////////////////////////////////////////////////////
Eeprom.rar_eeprom
版权申诉
152 浏览量
2022-09-24
02:13:21
上传
评论
收藏 3KB RAR 举报
alvarocfc
- 粉丝: 109
- 资源: 1万+
最新资源
- 基于SHT25温湿度传感器、FREERTOS、STM32F103C8T6、LCD1602温湿度采集显示系统proteus仿真设计
- C# 屏幕放大取色器 随时随地获取屏幕像素颜色
- 下载安装这个软件.apk
- 【数据集详细解释及案例分析】数据集详细解释及案例分析
- 基于SHT71温湿度传感器、STM32F103C8T6、LCD1602温湿度采集显示系统proteus仿真设计
- 基于TH02温湿度传感器、STM32F103C8T6、LCD1602、FREERTOS的温湿度采集系统proteus仿真设计
- 【TCP-IP协议详细解释及案例分析】TCP-IP协议详细解释及案例分析
- 一文搞懂 LSTM(长短期记忆网络).rar
- 【autosar简介及基本案例解析】autosar简介及基本案例解析
- java模拟斗地主洗牌发牌
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈