📄 drv_eeprom.c
字号:
////2
databuff[1]=UpRouteTable[1].Dest_ADDR[2]>>8;
databuff[0]=UpRouteTable[1].Dest_ADDR[2];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE1_ADDR2,databuff,2);
////3
databuff[1]=UpRouteTable[1].Dest_ADDR[3]>>8;
databuff[0]=UpRouteTable[1].Dest_ADDR[3];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE1_ADDR3,databuff,2);
////4
databuff[1]=UpRouteTable[1].Dest_ADDR[4]>>8;
databuff[0]=UpRouteTable[1].Dest_ADDR[4];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE1_ADDR4,databuff,2);
/////////////////////////////////////////////////////////////////////
databuff[0]=UpRouteTable[2].vaildAddrItemNum;
databuff[1]=0x5a;
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE2_ITEM_,databuff,2);
////0
databuff[1]=UpRouteTable[2].Dest_ADDR[0]>>8;
databuff[0]=UpRouteTable[2].Dest_ADDR[0];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE2_ADDR0,databuff,2);
////1
databuff[1]=UpRouteTable[2].Dest_ADDR[1]>>8;
databuff[0]=UpRouteTable[2].Dest_ADDR[1];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE2_ADDR1,databuff,2);
////2
databuff[1]=UpRouteTable[2].Dest_ADDR[2]>>8;
databuff[0]=UpRouteTable[2].Dest_ADDR[2];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE2_ADDR2,databuff,2);
////3
databuff[1]=UpRouteTable[2].Dest_ADDR[3]>>8;
databuff[0]=UpRouteTable[2].Dest_ADDR[3];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE2_ADDR3,databuff,2);
////4
databuff[1]=UpRouteTable[2].Dest_ADDR[4]>>8;
databuff[0]=UpRouteTable[2].Dest_ADDR[4];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,UP_R_TABLE2_ADDR4,databuff,2);
}
/////////////////////////////////////////////////////////////////////
void SaveRouteTableDN(void)
{
uint8 databuff[2];
Uart0SendString("Save RouteTableDN");
databuff[0]=DnRouteTable[0].vaildAddrItemNum;
databuff[1]=0x5a;
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE0_ITEM_,databuff,2);
////0
databuff[1]=DnRouteTable[0].Dest_ADDR[0]>>8;
databuff[0]=DnRouteTable[0].Dest_ADDR[0];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE0_ADDR0,databuff,2);
////1
databuff[1]=DnRouteTable[0].Dest_ADDR[1]>>8;
databuff[0]=DnRouteTable[0].Dest_ADDR[1];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE0_ADDR1,databuff,2);
////2
databuff[1]=DnRouteTable[0].Dest_ADDR[2]>>8;
databuff[0]=DnRouteTable[0].Dest_ADDR[2];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE0_ADDR2,databuff,2);
////3
databuff[1]=DnRouteTable[0].Dest_ADDR[3]>>8;
databuff[0]=DnRouteTable[0].Dest_ADDR[3];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE0_ADDR3,databuff,2);
////4
databuff[1]=DnRouteTable[0].Dest_ADDR[4]>>8;
databuff[0]=DnRouteTable[0].Dest_ADDR[4];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE0_ADDR4,databuff,2);
/////////////////////////////////////////////////////////////////////
databuff[0]=DnRouteTable[1].vaildAddrItemNum;
databuff[1]=0x5a;
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE1_ITEM_,databuff,2);
////0
databuff[1]=DnRouteTable[1].Dest_ADDR[0]>>8;
databuff[0]=DnRouteTable[1].Dest_ADDR[0];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE1_ADDR0,databuff,2);
////1
databuff[1]=DnRouteTable[1].Dest_ADDR[1]>>8;
databuff[0]=DnRouteTable[1].Dest_ADDR[1];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE1_ADDR1,databuff,2);
////2
databuff[1]=DnRouteTable[1].Dest_ADDR[2]>>8;
databuff[0]=DnRouteTable[1].Dest_ADDR[2];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE1_ADDR2,databuff,2);
////3
databuff[1]=DnRouteTable[1].Dest_ADDR[3]>>8;
databuff[0]=DnRouteTable[1].Dest_ADDR[3];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE1_ADDR3,databuff,2);
////4
databuff[1]=DnRouteTable[1].Dest_ADDR[4]>>8;
databuff[0]=DnRouteTable[1].Dest_ADDR[4];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE1_ADDR4,databuff,2);
/////////////////////////////////////////////////////////////////////
databuff[0]=DnRouteTable[2].vaildAddrItemNum;
databuff[1]=0x5a;
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE2_ITEM_,databuff,2);
////0
databuff[1]=DnRouteTable[2].Dest_ADDR[0]>>8;
databuff[0]=DnRouteTable[2].Dest_ADDR[0];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE2_ADDR0,databuff,2);
////1
databuff[1]=DnRouteTable[2].Dest_ADDR[1]>>8;
databuff[0]=DnRouteTable[2].Dest_ADDR[1];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE2_ADDR1,databuff,2);
////2
databuff[1]=DnRouteTable[2].Dest_ADDR[2]>>8;
databuff[0]=DnRouteTable[2].Dest_ADDR[2];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE2_ADDR2,databuff,2);
////3
databuff[1]=DnRouteTable[2].Dest_ADDR[3]>>8;
databuff[0]=DnRouteTable[2].Dest_ADDR[3];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE2_ADDR3,databuff,2);
////4
databuff[1]=DnRouteTable[2].Dest_ADDR[4]>>8;
databuff[0]=DnRouteTable[2].Dest_ADDR[4];
I2C_WriteNByte(CHIP_CAT1025,ONE_BYTE_SUBA,DN_R_TABLE2_ADDR4,databuff,2);
}
/////////////////////////////////////////////////////////////////////
/*
**********************************************************************
** 函数名称:I2C_SendByte()
** 函数功能:向无子地址器件发送1字节数据。
** 入口参数:sla 器件地址
** dat 要发送的数据
** 出口参数:返回值为0时表示出错,为1时表示操作正确。
** 说明: 使用前要初始化好I2C引脚功能和I2C中断,并已使能I2C主模式
**********************************************************************
*/
uint8 I2C_SendByte(uint8 sla, uint8 dat)
{
I2C_StopAct();
DelayMS_(10);
/* 参数设置 */
I2C_sla = sla; // 写操作的器件地址
I2C_buf = &dat; // 待发送的数据
I2C_num = 1; // 发送1字节数据
I2C_suba_en = 0; // 无子地址
I2C_end = 0;
I2CONCLR = 0x2C;
I2CONSET = 0x60; // 设置为主机,并启动总线
// while(0==I2C_end); //???
WatchDogClear(); //clr watchdog
mSecondTick=WHILE_DELAY_TIME;
while((0==I2C_end)&&(mSecondTick>1));
if(mSecondTick<=1) Uart0SendString("Time overflow in I2C_SendByte");
WatchDogClear(); //clr watchdog
if(1==I2C_end) return(1);
else return(0);
}
/*
*************************************************************************************
** 函数名称:I2C_RcvByte()
** 函数功能:向无子地址器件读取1字节数据。
** 入口参数:sla 器件地址
** dat 接收数据的变量指针
** 出口参数:返回值为0时表示操作出错,为1时表示操作正确。
** 说明:使用前要初始化好I2C引脚功能和I2C中断,并已使能I2C主模式
*************************************************************************************
*/
uint8 I2C_RcvByte(uint8 sla, uint8 *dat)
{
I2C_StopAct();
DelayMS_(10);
/* 参数设置 */
I2C_sla = sla+1; // 读操作的器件地址
I2C_buf = dat;
I2C_num = 1;
I2C_suba_en = 0; // 无子地址
I2C_end = 0;
I2CONCLR = 0x2C;
I2CONSET = 0x60; // 设置为主机,并启动总线
// while(0==I2C_end); //???
WatchDogClear(); //clr watchdog
mSecondTick=WHILE_DELAY_TIME;
while((0==I2C_end)&&(mSecondTick>1));
if(mSecondTick<=1) Uart0SendString("Time overflow in I2C_RcvByte");
WatchDogClear(); //clr watchdog
if(1==I2C_end) return(1);
else return(0);
}
/*
******************************************************************************************
** 函数名称 :I2C_ReadNByte()
** 函数功能 :从有子地址器件任意地址开始读取N字节数据
** 入口参数 : sla 器件从地址
** suba_type 子地址结构 1-单字节地址 2-8+X结构 2-双字节地址
** suba 器件子地址
** s 数据接收缓冲区指针
** num 读取的个数
** 出口参数 : TRUE 操作成功
** FALSE 操作失败
******************************************************************************************
*/
uint8 I2C_ReadNByte (uint8 sla, uint32 suba_type, uint32 suba, uint8 *s, uint32 num)
{
I2C_StopAct();
DelayMS_(10);
if (num > 0) /* 判断num个数的合法性 */
{ /* 参数设置 */
if (suba_type == 1)
{ /* 子地址为单字节 */
I2C_sla = sla + 1; /* 读器件的从地址,R=1 */
I2C_suba = suba; /* 器件子地址 */
I2C_suba_num = 1; /* 器件子地址为1字节 */
}
if (suba_type == 2)
{ /* 子地址为2字节 */
I2C_sla = sla + 1; /* 读器件的从地址,R=1 */
I2C_suba = suba; /* 器件子地址 */
I2C_suba_num = 2; /* 器件子地址为2字节 */
}
if (suba_type == 3)
{ /* 子地址结构为8+X*/
I2C_sla = sla + ((suba >> 7 )& 0x0e) + 1; /* 读器件的从地址,R=1 */
I2C_suba = suba & 0x0ff; /* 器件子地址 */
I2C_suba_num = 1; /* 器件子地址为8+x */
}
I2C_buf = s; /* 数据接收缓冲区指针 */
I2C_num = num; /* 要读取的个数 */
I2C_suba_en = 1; /* 有子地址读 */
I2C_end = 0;
/* 清除STA,SI,AA标志位 */
I2CONCLR = (1 << 2)| /* AA */
(1 << 3)| /* SI */
(1 << 5); /* STA */
/* 置位STA,启动I2C总线 */
I2CONSET = (1 << 5)| /* STA */
(1 << 6); /* I2CEN */
/* 等待I2C操作完成 */
// while(I2C_end == 0);
WatchDogClear(); //clr watchdog
mSecondTick=WHILE_DELAY_TIME;
while((I2C_end == 0)&&(mSecondTick>1));
if(mSecondTick<=1) Uart0SendString("Time overflow in I2C_ReadNByte");
WatchDogClear(); //clr watchdog
if (I2C_end == 1) return (TRUE);
else return (FALSE);
}
return (FALSE);
}
/*
****************************************************************************************
** 函数名称 :I2C_WriteNByte()
** 函数功能 :向有子地址器件写入N字节数据
** 入口参数 : sla 器件从地址
** suba_type 子地址结构 1-单字节地址 3-8+X结构 2-双字节地址
** suba 器件内部物理地址
** *s 将要写入的数据的指针
** num 将要写入的数据的个数
** 出口参数 : TRUE 操作成功
** FALSE 操作失败
***************************************************************************************
*/
uint8 I2C_WriteNByte(uint8 sla, uint8 suba_type, uint32 suba, uint8 *s, uint32 num)
{
I2C_StopAct();
DelayMS_(10);
IO0CLR=EEP_P0_9; //eeprom write protection pin
if (num > 0)/* 如果读取的个数为0,则返回错误 */
{ /* 设置参数 */
if (suba_type == 1)
{ /* 子地址为单字节 */
I2C_sla = sla; /* 读器件的从地址 */
I2C_suba = suba; /* 器件子地址 */
I2C_suba_num = 1; /* 器件子地址为1字节 */
}
if (suba_type == 2)
{ /* 子地址为2字节 */
I2C_sla = sla; /* 读器件的从地址 */
I2C_suba = suba; /* 器件子地址 */
I2C_suba_num = 2; /* 器件子地址为2字节 */
}
if (suba_type == 3)
{ /* 子地址结构为8+X */
I2C_sla = sla + ((suba >> 7 )& 0x0e); /* 读器件的从地址 */
I2C_suba = suba & 0x0ff; /* 器件子地址 */
I2C_suba_num = 1; /* 器件子地址为8+X */
}
I2C_buf = s; /* 数据 */
I2C_num = num; /* 数据个数 */
I2C_suba_en = 2; /* 有子地址,写操作 */
I2C_end = 0;
/* 清除STA,SI,AA标志位 */
I2CONCLR = (1 << 2)| /* AA */
(1 << 3)| /* SI */
(1 << 5); /* STA */
/* 置位STA,启动I2C总线 */
I2CONSET = (1 << 5)| /* STA */
(1 << 6); /* I2CEN*/
/* 等待I2C操作完成 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -