/************************ (C) COPYRIGHT Suzhou Winrobs Co., Ltd.******
* File Name : xxx.c
* Version : V1.0.0
* Date :10/20/23 09:27:02
* Description :
* History :
* Author : LGH
*Modification :
**********************************************************************
*/
#include "XL9555.h"
#include <stdio.h>
//记录IO状态
uint8_t IO0_status=0x00;
uint8_t IO1_status=0x00;
//输出配置初始值
uint8_t CPORT0_Value=0x00;
uint8_t CPORT1_Value=0x00;
uint8_t OPORT0_Value=0xff;
uint8_t OPORT1_Value=0xff;
//xl9555地址编号按地址顺序控制
//xl9555初始化配置及命名
XL9555_CONFIG XL9555_Init[][5]=
{
{
{XL9555_A1,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX1_XL9555_1"},
{XL9555_A2,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX1_XL9555_2"},
{XL9555_A3,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX1_XL9555_3"},
{XL9555_A4,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX1_XL9555_4"},
{XL9555_A5,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX1_XL9555_5"},
},
{
{XL9555_A1,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX2_XL9555_1"},
{XL9555_A2,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX2_XL9555_2"},
{XL9555_A3,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX2_XL9555_3"},
{XL9555_A4,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX2_XL9555_4"},
{XL9555_A5,{CPORT0,CPORT1},{0x00,0x00},{OPORT0,OPORT1},{0x00,0x00},"EX2_XL9555_5"},
},
};
//xl9555数量确定
uint8_t xl9555_i2c1_number=sizeof(XL9555_Init[0])/sizeof(XL9555_Init[0][0]);
uint8_t xl9555_i2c2_number=sizeof(XL9555_Init[1])/sizeof(XL9555_Init[1][0]);
/*
* Function Name:XL9555_Send_Nbyte
* Author:LGH
* Date:10/20/23 09:30:25
* Function:xl9555发送1个字节
* Input Parameter:
* Return:
* Others:
*/
uint8_t XL9555_Send_Nbyte(SOFT_I2C_TypeDef* IIC,uint8_t addr,uint8_t cmd,uint8_t wrbuf)
{
return IIC_Send_Nbyte(IIC,addr,cmd,&wrbuf,1);
}
/*
* Function Name:XL9555_Read_Nbyte
* Author:LGH
* Date:10/20/23 09:31:07
* Function:xl9555读取数据
* Input Parameter:
* @ACK:控制读取的个数
* Return:
* Others:
*/
uint8_t XL9555_Read_Nbyte(SOFT_I2C_TypeDef* IIC,uint8_t addr,uint8_t cmd,uint8_t *rbuf,int ACK)
{
return IIC_Read_Nbyte(IIC,addr,cmd,rbuf,ACK);
}
/*
* Function Name:XL9555_Addr_Label
* Author:LGH
* Date:10/20/23 09:35:58
* Function:根据输入的地址获取此地址对应于XL9555_Init中的位置
* Input Parameter:
* @IIC:总线名称
* @addr:器件地址
* Return:
* Others:
*/
uint8_t XL9555_Addr_Label(uint8_t Addr)
{
uint8_t i;
i =(Addr&0x0f)>>1;
return i;
}
/*
* Function Name:XL9555_INIT
* Author:LGH
* Date:10/20/23 09:36:56
* Function:根据输入的总线地址初始化xl9555
* Input Parameter:
* @IIC:总线名称
* @addr:器件地址
* Return:
* Others:
*/
uint8_t XL9555_Config_Init(SOFT_I2C_TypeDef* IIC)
{
uint8_t i,
Addr,
i2cx,
xl9555_num;
if(IIC ==&MCU_IO_IIC[IIC1])
{
xl9555_num =xl9555_i2c1_number;
i2cx =IIC1;
}
else if(IIC ==&MCU_IO_IIC[IIC2])
{
xl9555_num =xl9555_i2c2_number;
i2cx =IIC2;
}
else
{
printf("IIC Bus Error\r\n");
return 0;
}
for(i=0;i<xl9555_num;i++)
{
Addr =XL9555_Init[i2cx][i].Addr;
i=XL9555_Addr_Label(Addr);
//IO CFG
if(!XL9555_Send_Nbyte(IIC,Addr,XL9555_Init[i2cx][i].CPORT[0],XL9555_Init[i2cx][i].CPORT_Value[0])) //检测IIC是否正常
{
printf("IIC%d:%s Fail\r\n",i2cx,XL9555_Init[i2cx][i].name);
return 0;
}
XL9555_Send_Nbyte(IIC,Addr,XL9555_Init[i2cx][i].CPORT[1],XL9555_Init[i2cx][i].CPORT_Value[1]);
//IO INIT
XL9555_Send_Nbyte(IIC,Addr,XL9555_Init[i2cx][i].OPORT[0],XL9555_Init[i2cx][i].OPORT_Value[0]);
XL9555_Send_Nbyte(IIC,Addr,XL9555_Init[i2cx][i].OPORT[1],XL9555_Init[i2cx][i].OPORT_Value[1]);
}
return 1;
}
/*
* Function Name:XL9555_Read_Channel
* Author:LGH
* Date:10/20/23 09:37:34
* Function:读取xl9555的状态
* Input Parameter:
* @IIC:总线名称
* @addr:器件地址
* @channel:读取通道
* Return:
* Others:
*/
uint8_t XL9555_Read_Channel(SOFT_I2C_TypeDef* IIC,uint8_t addr,uint8_t channel)
{
uint8_t XL9555_ReadStatus[2]={0};
uint8_t read_status=0;
XL9555_Read_Nbyte(IIC,addr,IPORT1,&XL9555_ReadStatus[1],1);
XL9555_Read_Nbyte(IIC,addr,IPORT0,&XL9555_ReadStatus[0],1);
if(channel>15)
{
printf("Channel Error\r\n");
return 0;
}
else if(channel>7)
{
read_status=1<<(channel-8);
read_status&=XL9555_ReadStatus[1];
return read_status>>(channel-8);
}
else
{
read_status=1<<channel;
read_status&=XL9555_ReadStatus[0];
return read_status>>channel;
}
}
/*
* Function Name:XL9555_Set_Channel
* Author:LGH
* Date:10/20/23 09:37:34
* Function:设置xl9555的状态
* Input Parameter:
* @IIC:总线名称
* @addr:器件地址
* @channel:通道
* Return:
* Others:
*/
uint8_t XL9555_Set_Channel(SOFT_I2C_TypeDef* IIC,uint8_t Addr,uint8_t channel)
{
uint8_t i,
temp_ch,
i2cx;
temp_ch= channel;
if(IIC ==&MCU_IO_IIC[IIC1])
{
i2cx =IIC1;
}
else if(IIC ==&MCU_IO_IIC[IIC2])
{
i2cx =IIC2;
}
else
{
printf("IIC Bus Error\r\n");
return 0;
}
i=XL9555_Addr_Label(Addr);
if(temp_ch>15)
{
printf("Channel Error\r\n");
return 0;
}
else if(temp_ch>7)
{
channel=1<<(channel-8);
XL9555_Init[i2cx][i].OPORT_Value[1]&=(~channel);
XL9555_Init[i2cx][i].OPORT_Value[1]|=channel;
IO1_status=XL9555_Init[i2cx][i].OPORT_Value[1];
if(!(XL9555_Send_Nbyte(IIC,Addr,OPORT1,IO1_status)))
return 0;
}
else
{
channel=1<<channel;
XL9555_Init[i2cx][i].OPORT_Value[0]&=(~channel);
XL9555_Init[i2cx][i].OPORT_Value[0]|=channel;
IO0_status=XL9555_Init[i2cx][i].OPORT_Value[0];
if(!(XL9555_Send_Nbyte(IIC,Addr,OPORT0,IO0_status)))
return 0;
}
return 1;
}
/*
* Function Name:XL9555_Reset_channel
* Author:LGH
* Date:10/20/23 09:37:34
* Function:复位xl9555的状态
* Input Parameter:
* @IIC:总线名称
* @addr:器件地址
* @channel:通道
* Return:
* Others:
*/
uint8_t XL9555_Reset_Channel(SOFT_I2C_TypeDef* IIC,uint8_t Addr,uint8_t channel)
{
uint8_t i,
temp_ch,
i2cx;
temp_ch= channel;
if(IIC ==&MCU_IO_IIC[IIC1])
{
i2cx =IIC1;
}
else if(IIC ==&MCU_IO_IIC[IIC2])
{
i2cx =IIC2;
}
else
{
printf("IIC Bus Error\r\n");
return 0;
}
i=XL9555_Addr_Label(Addr);
if(temp_ch>15)
{
printf("Channel Error\r\n");
return 0;
}
else if(temp_ch>7)
{
channel=1<<(channel-8);
XL9555_Init[i2cx][i].OPORT_Value[1]&=(~channel);
IO1_status=XL9555_Init[i2cx][i].OPORT_Value[1];
if(!(XL9555_Send_Nbyte(IIC,Addr,OPORT1,IO1_status)))
return 0;
}
else
{
channel=1<<channel;
XL9555_Init[i2cx][i].OPORT_Value[0]&=(~channel);
IO0_status=XL9555_Init[i2cx][i].OPORT_Value[0];
if(!(XL9555_Send_Nbyte(IIC,Addr,OPORT0,IO0_status)))
return 0;
}
return 1;
}
/*
* Function Name:XL9555_Write_channel
* Author:LGH
* Date:10/20/23 09:37:34
* Function:根据参数写xl9555的状态
* Input Parameter:
* @IIC:总线名称
* @addr:器件地址
* @channel:通道
* @new_status:io状态
* Return:
* Others:
*/
uint8_t XL9555_Write_Channel(SOFT_I2C_TypeDef* IIC,uint8_t Addr,uint8_t channel,uint8_t new_status)
{
uint8_t I2C_Status;
if(new_status==1)
I2C_Status=XL9555_Set_Channel(IIC,Addr,channel);
else if(new_status==0)
I2C_Status=XL9555_Reset_Channel(IIC,Addr,channel);
else
return 0xff;
return I2C_Status;
}
/*
以下为单通道控制xl9555 io状态
*/
//IO1
int XL9555_BIT1(SOFT_I2C_TypeDef* IIC,uint8_t Addr,uint8_t status)
{
status=(status<<0); // xxxx xxx0
IO0_status&=0xfe;
IO0_status|=status;
if(!(XL9555_Send_Nbyte(IIC,Addr,OPORT0,IO0_status))) return 0;
else return 1;
}
//IO2
int XL9555_BIT2(SOFT_I2C_TypeDef* IIC,uint8_t Addr,uint8_t status)
{
status=(status<<1); // xxxx xx0x
IO0_status&=