NULL
This commit is contained in:
238
user/Device/CAT24C512.c
Executable file
238
user/Device/CAT24C512.c
Executable file
@@ -0,0 +1,238 @@
|
||||
#include "../main/SystemInclude.h"
|
||||
//==============================================================================
|
||||
|
||||
u32 isReadParameterOK;
|
||||
/******************************************************************************/
|
||||
void EEPROMInit(void)
|
||||
{
|
||||
EEPROM_WP_OUTPUT();
|
||||
DisableWriteEEPROM();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void WaitWriteOver(u8 eepromAdr)
|
||||
{
|
||||
u32 I;
|
||||
u32 staus;
|
||||
|
||||
for(I=0; I<8; I++)
|
||||
{
|
||||
delay_ms(1);
|
||||
SI2C_Start();
|
||||
staus = SI2C_Write(0xa0 | eepromAdr);
|
||||
SI2C_Stop();
|
||||
if(staus == 0) return;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void WriteByteToEEPROM(u16 address, u8 wrtData, u8 eepromAdr)
|
||||
{
|
||||
EnableWriteEEPROM();
|
||||
|
||||
SI2C_Start();
|
||||
SI2C_Write(0xa0 | eepromAdr);
|
||||
SI2C_Write(address>>8);
|
||||
SI2C_Write(address);
|
||||
|
||||
SI2C_Write(wrtData);
|
||||
SI2C_Stop();
|
||||
|
||||
WaitWriteOver(eepromAdr);
|
||||
|
||||
DisableWriteEEPROM();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u8 ReadByteFromEEPROM(u16 address, u8 eepromAdr)
|
||||
{
|
||||
u8 data;
|
||||
|
||||
//SI2C_Mem_Read(0xa0 | eepromAdr, address, SI2C_MEMADD_SIZE_16BIT, &data, 1);
|
||||
|
||||
SI2C_Start();
|
||||
SI2C_Write(0xa0 | eepromAdr);
|
||||
SI2C_Write(address>>8);
|
||||
SI2C_Write(address);
|
||||
|
||||
SI2C_Start();
|
||||
SI2C_Write(0xa1 | eepromAdr);
|
||||
data = SI2C_Read();
|
||||
SI2C_Stop();
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void ResetWriteAddr(u16 address, u8 eepromAdr)
|
||||
{
|
||||
SI2C_Stop();
|
||||
WaitWriteOver(eepromAdr);
|
||||
|
||||
SI2C_Start();
|
||||
SI2C_Write(0xa0 | eepromAdr);
|
||||
SI2C_Write(address>>8);
|
||||
SI2C_Write((u8)address);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void WriteMultiByteToEEPROM(u16 address, u8 *wrtData, u16 dataLenth, u8 eepromAdr)
|
||||
{
|
||||
u16 temp, I;
|
||||
|
||||
EnableWriteEEPROM();
|
||||
|
||||
SI2C_Start();
|
||||
SI2C_Write(0xa0 | eepromAdr);
|
||||
SI2C_Write(address>>8);
|
||||
SI2C_Write((u8)address);
|
||||
for(I=0; I<dataLenth; I++)
|
||||
{
|
||||
SI2C_Write(*wrtData);
|
||||
wrtData++;
|
||||
address++;
|
||||
temp = address;
|
||||
temp &= 0x00ff;
|
||||
if(temp == 0x0000) ResetWriteAddr(address, eepromAdr);
|
||||
else if(temp == 0x0040) ResetWriteAddr(address, eepromAdr);
|
||||
else if(temp == 0x0080) ResetWriteAddr(address, eepromAdr);
|
||||
else if(temp == 0x00C0) ResetWriteAddr(address, eepromAdr);
|
||||
}
|
||||
|
||||
SI2C_Stop();
|
||||
WaitWriteOver(eepromAdr);
|
||||
|
||||
DisableWriteEEPROM();
|
||||
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void ReadMultiByteFromEEPROM(u16 address, u8 *wrtData, u16 dataLenth, u8 eepromAdr)
|
||||
{
|
||||
//SI2C_Mem_Read(0xa0 | eepromAdr, address, SI2C_MEMADD_SIZE_16BIT, wrtData, dataLenth);
|
||||
|
||||
u16 I;
|
||||
|
||||
//EnableWriteEEPROM();
|
||||
|
||||
SI2C_Start();
|
||||
SI2C_Write(0xa0 | eepromAdr);
|
||||
SI2C_Write(address>>8);
|
||||
SI2C_Write((u8)address);
|
||||
|
||||
SI2C_Start();
|
||||
SI2C_Write(0xa1 | eepromAdr);
|
||||
*wrtData = SI2C_Read();
|
||||
for(I=1; I<dataLenth; I++)
|
||||
{
|
||||
SI2C_Ack();
|
||||
wrtData++;
|
||||
*wrtData = SI2C_Read();
|
||||
}
|
||||
SI2C_Stop();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void ClearMultiByteToEEPROM(u16 address, u8 dataLenth, u8 eepromAdr)
|
||||
{
|
||||
// u8 I;
|
||||
// u16 temp;
|
||||
//
|
||||
EnableWriteEEPROM();
|
||||
//
|
||||
// SI2C_Start();
|
||||
// SI2C_Write(0xa0 | eepromAdr);
|
||||
// SI2C_Write(address>>8);
|
||||
// SI2C_Write((u8)address);
|
||||
// for(I=0; I<dataLenth; I++)
|
||||
// {
|
||||
// SI2C_Write(0xff);
|
||||
// address++;
|
||||
// temp = address;
|
||||
// temp &= 0x00ff;
|
||||
// if(temp == 0x0000) ResetWriteAddr(address, eepromAdr);
|
||||
// else if(temp == 0x0040) ResetWriteAddr(address, eepromAdr);
|
||||
// else if(temp == 0x0080) ResetWriteAddr(address, eepromAdr);
|
||||
// else if(temp == 0x00C0) ResetWriteAddr(address, eepromAdr);
|
||||
// }
|
||||
//
|
||||
// SI2C_Stop();
|
||||
// WaitWriteOver(eepromAdr);
|
||||
DisableWriteEEPROM();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u16 ReadShortParameterFromEEPROM(u16 parameterAdr, u16 parameterDefault)
|
||||
{
|
||||
// for meter factor
|
||||
tempL.Word[0] = 0;
|
||||
ReadMultiByteFromEEPROM(parameterAdr, tempL.Byte, 3, PARA_EEPROM);
|
||||
|
||||
if(tempL.Byte[2] != CRC8(tempL.Byte, 2))
|
||||
{
|
||||
tempL.Word[0] = (u32)parameterDefault;
|
||||
isReadParameterOK = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tempL.Byte[2] = 0;
|
||||
isReadParameterOK = 1;
|
||||
}
|
||||
|
||||
return tempL.Word[0];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u32 ReadLongParameterFromEEPROM(u16 parameterAdr, u32 parameterDefault)
|
||||
{
|
||||
// for meter factor
|
||||
tempDev.DWord[0] = 0;
|
||||
ReadMultiByteFromEEPROM(parameterAdr, tempDev.Byte, 5, PARA_EEPROM);
|
||||
|
||||
if(tempDev.Byte[4] != CRC8(tempDev.Byte, 4))
|
||||
{
|
||||
tempDev.DWord[0] = (u32)parameterDefault;
|
||||
isReadParameterOK = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
isReadParameterOK = 1;
|
||||
}
|
||||
|
||||
return tempDev.DWord[0];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u16 WriteShortParameterToEEPROM(u16 WRAddr)
|
||||
{
|
||||
tempL.Byte[2] = CRC8(tempL.Byte, 2);
|
||||
WriteMultiByteToEEPROM(WRAddr, tempL.Byte, 3, PARA_EEPROM);
|
||||
ReadMultiByteFromEEPROM(WRAddr, tempL.Byte, 3, PARA_EEPROM);
|
||||
|
||||
tempL.Byte[2] = 0;
|
||||
return tempL.Word[0];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
//u32 WriteLongParameterToEEPROM(u16 WRAddr, u16 WRLen)
|
||||
//{
|
||||
// u16 I;
|
||||
|
||||
// I = WRLen - 1;
|
||||
// tempDev.Byte[I] = CRC8(tempDev.Byte, I);
|
||||
// WriteMultiByteToEEPROM(WRAddr, tempDev.Byte, WRLen, PARA_EEPROM);
|
||||
// tempDev.DWord[0] = 0;
|
||||
// ReadMultiByteFromEEPROM(WRAddr, tempDev.Byte, WRLen, PARA_EEPROM);
|
||||
// tempDev.Byte[I] = 0;
|
||||
// return tempDev.DWord[0];
|
||||
//}
|
||||
|
||||
u32 WriteLongParameterToEEPROM(u16 WRAddr)
|
||||
{
|
||||
tempDev.Byte[4] = CRC8(tempDev.Byte, 4);
|
||||
WriteMultiByteToEEPROM(WRAddr, tempDev.Byte, 5, PARA_EEPROM);
|
||||
tempDev.DWord[0] = 0;
|
||||
ReadMultiByteFromEEPROM(WRAddr, tempDev.Byte, 5, PARA_EEPROM);
|
||||
tempDev.Byte[4] = 0;
|
||||
return tempDev.DWord[0];
|
||||
}
|
||||
Reference in New Issue
Block a user