📄 eeprom_low.c
字号:
/**
* This code and information is part of Trident DPTV API (TDAPI)
*
* Copyright (C) Trident Multimedia Technologies (Shanghai) Co., Ltd.
* 2003 All rights reserved.
*
* This file contains definitions and declarations related to EEPROM operations.
*
* Revision:
* 08/05/2003 Created by Archie
*
*/
#include "std.h"
#include "I2C.h"
//#include "TEEPROM.h"
#define _TRY_TIMES_ 5
static Byte s_ucSlaveAddr = 0xA0;
static Void tdEnableEEPROMI2C(Void)
{
// disable WP here
tdI2COpen1();
}
static Void tdDisableEEPROMI2C(Void)
{
// enable WP here
tdI2CClose1();
}
static Byte tdSendEEPROMStart(Word wSubAddress)
{
Byte Loop = 0;
tdStart();
while (!tdI2CSendByte((Byte)(s_ucSlaveAddr & 0xfe)))
{
Byte Loop2;
Loop++;
if (Loop == 0xFF) return _FALSE_;
for (Loop2 = 0; Loop2 < 0xFF; Loop2++)
{
Loop2--; Loop2++;
}
tdStart();
}
if (tdI2CSendByte((Byte)(wSubAddress >> 8)) && tdI2CSendByte((Byte)wSubAddress))
return _TRUE_;
return _FALSE_;
}
Void tdEEPROMWrite(Word wSubAddress, Byte * rpBuf, Word wLength)
{
Word wLength2 = wLength;
Byte * rpBuf2 = rpBuf;
Byte ucTryTimes = _TRY_TIMES_;
Byte ucPageOffset;
tdEnableEEPROMI2C();
while (ucTryTimes--)
{
wLength = wLength2;
rpBuf = rpBuf2;
ucPageOffset = wSubAddress % 32;
if(ucPageOffset != 0 && (ucPageOffset + wLength > 32)) /* Across bonduary. Should write page twice */
{
if (tdSendEEPROMStart(wSubAddress))
{
while (ucPageOffset++ < 32)
if(!tdI2CSendByte(*rpBuf++)) goto quit;
ucPageOffset = 32 - wSubAddress % 32;
wSubAddress += ucPageOffset;
wLength -= ucPageOffset;
tdStop();
} else
goto quit;
}
if (wLength > 32)
{
while (wLength > 32)
{
Byte ucLen = 32;
if (tdSendEEPROMStart(wSubAddress))
{
while (ucLen--)
if(!tdI2CSendByte(*rpBuf++)) goto quit;
wLength -= 32;
wSubAddress += 32;
tdStop();
} else
goto quit;
}
}
if (tdSendEEPROMStart(wSubAddress))
while (wLength--)
if(!tdI2CSendByte(*rpBuf++)) goto quit;
quit:
tdStop();
if (wLength == 0xFFFF)
break;
}
tdDisableEEPROMI2C();
}
Void tdEEPROMRead(Word wSubAddress, Byte * gpBuf, Word wLength)
{
Word wLength2 = wLength;
Byte * gpBuf2 = gpBuf;
Byte ucTryTimes = _TRY_TIMES_;
tdEnableEEPROMI2C();
while (ucTryTimes--)
{
wLength = wLength2;
gpBuf = gpBuf2;
if (tdSendEEPROMStart(wSubAddress))
{
tdStart();
if(tdI2CSendByte((Byte)(s_ucSlaveAddr | 0x01)))
{
while (wLength)
{
*gpBuf++ = tdI2CReceiveByte();
/*send ACK*/
if(--wLength)
tdACK();
}
/*send No ACK*/
tdNOACK();
tdStop();
break;
}
}
tdStop();
}
tdDisableEEPROMI2C();
}
Void tdEEPROMWriteByte(Word wSubAddress, Byte ucValue)
{
Byte ucTryTimes = _TRY_TIMES_;
tdEnableEEPROMI2C();
while (ucTryTimes--)
{
if (tdSendEEPROMStart(wSubAddress))
if (tdI2CSendByte(ucValue))
{
tdStop();
break;
}
tdStop();
}
tdDisableEEPROMI2C();
}
Byte tdEEPROMReadByte(Word wSubAddress)
{
Byte ucTryTimes = _TRY_TIMES_;
Byte ucValue = 0xFF;
tdEnableEEPROMI2C();
while (ucTryTimes--)
{
if (tdSendEEPROMStart(wSubAddress))
{
tdStart();
if(tdI2CSendByte((Byte)(s_ucSlaveAddr | 0x01)))
{
ucValue = tdI2CReceiveByte();
/*send No ACK*/
tdNOACK();
tdStop();
break;
}
}
tdStop();
}
tdDisableEEPROMI2C();
return ucValue;
}
/* set device address and check if the device is onling */
Bool tdEEPROMSelectDevice(Byte ucDeviceAddress)
{
Byte ucTryTimes = _TRY_TIMES_;
s_ucSlaveAddr = ucDeviceAddress;
tdEnableEEPROMI2C();
while (ucTryTimes--)
{
if (tdSendEEPROMStart(0x0000))
{
tdStop();
break; // OK, return;
}
tdStop();
// wrong, try again
}
tdDisableEEPROMI2C();
return ucTryTimes != 0xFF;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -