Files
CHJ/user/Device/CAT24C512.c

238 lines
6.2 KiB
C
Raw Normal View History

2026-03-20 21:16:58 +08:00
#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);
2026-03-20 21:19:53 +08:00
2026-03-20 21:16:58 +08:00
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();
}
/******************************************************************************/
2026-03-20 21:19:53 +08:00
u16 ReadShortParameterFromEEPROM(u16 parameterAdr, u16 parameterDefault)
2026-03-20 21:16:58 +08:00
{
// for meter factor
2026-03-20 21:19:53 +08:00
tempL.Word[0] = 0;
ReadMultiByteFromEEPROM(parameterAdr, tempL.Byte, 3, PARA_EEPROM);
2026-03-20 21:16:58 +08:00
2026-03-20 21:19:53 +08:00
if(tempL.Byte[2] != CRC8(tempL.Byte, 2))
2026-03-20 21:16:58 +08:00
{
2026-03-20 21:19:53 +08:00
tempL.Word[0] = (u32)parameterDefault;
2026-03-20 21:16:58 +08:00
isReadParameterOK = 0;
}
else
{
2026-03-20 21:19:53 +08:00
tempL.Byte[2] = 0;
2026-03-20 21:16:58 +08:00
isReadParameterOK = 1;
}
2026-03-20 21:19:53 +08:00
return tempL.Word[0];
2026-03-20 21:16:58 +08:00
}
/******************************************************************************/
2026-03-20 21:19:53 +08:00
u32 ReadLongParameterFromEEPROM(u16 parameterAdr, u32 parameterDefault)
2026-03-20 21:16:58 +08:00
{
2026-03-20 21:19:53 +08:00
// for meter factor
2026-03-20 21:16:58 +08:00
tempDev.DWord[0] = 0;
2026-03-20 21:19:53 +08:00
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;
}
2026-03-20 21:16:58 +08:00
return tempDev.DWord[0];
}
/******************************************************************************/
2026-03-20 21:19:53 +08:00
u16 WriteShortParameterToEEPROM(u16 WRAddr)
2026-03-20 21:16:58 +08:00
{
2026-03-20 21:19:53 +08:00
tempL.Byte[2] = CRC8(tempL.Byte, 2);
WriteMultiByteToEEPROM(WRAddr, tempL.Byte, 3, PARA_EEPROM);
ReadMultiByteFromEEPROM(WRAddr, tempL.Byte, 3, PARA_EEPROM);
2026-03-20 21:16:58 +08:00
2026-03-20 21:19:53 +08:00
tempL.Byte[2] = 0;
return tempL.Word[0];
2026-03-20 21:16:58 +08:00
}
2026-03-20 21:19:53 +08:00
/******************************************************************************/
//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];
}