

#include "IS31FL3236.h"
//#include "IIC.h"
#include "Simulated_IIC_2_Master.h"
#include "GPIO.h"


/*----------------------------------------------------*/
#define IS31_IIC_MODULE IIC_MODULE_2
/*----------------------------------------------*/
uint8_t LED_Read[72];
/*-----------------------------------------------------------*/
#define IS31_ERROR_MAX 30000U
static uint32_t u32IS31ErrorCount = 0U;
const uint8_t u8IS31ChipAddress [ IS31_CHIP_NUM ] = {


#if (IS31_CHIP_NUM >= 1U)
	IS31_CHIP1_ADDRESS,
#endif

#if (IS31_CHIP_NUM >= 2U)
	IS31_CHIP2_ADDRESS,
#endif

#if (IS31_CHIP_NUM >= 3U)
	IS31_CHIP3_ADDRESS,
#endif

#if (IS31_CHIP_NUM >= 4U)
	IS31_CHIP4_ADDRESS,
#endif

};
void LED_OFF_Init(void)
{
IS31_CHIP0_CHANNEL01  = 0u;
IS31_CHIP0_CHANNEL02  = 0u;
IS31_CHIP0_CHANNEL03  = 0u;
IS31_CHIP0_CHANNEL04  = 0u;
IS31_CHIP0_CHANNEL05  = 0u;
IS31_CHIP0_CHANNEL06  = 0u;
IS31_CHIP0_CHANNEL07  = 0u;
IS31_CHIP0_CHANNEL08  = 0u;
IS31_CHIP0_CHANNEL09  = 0u;
IS31_CHIP0_CHANNEL10  = 0u;
IS31_CHIP0_CHANNEL11  = 0u;
IS31_CHIP0_CHANNEL12  = 0u;
IS31_CHIP0_CHANNEL13  = 0u;
IS31_CHIP0_CHANNEL14  = 0u;
IS31_CHIP0_CHANNEL15  = 0u;
IS31_CHIP0_CHANNEL16  = 0u;
IS31_CHIP0_CHANNEL17  = 0u;
IS31_CHIP0_CHANNEL18  = 0u;
IS31_CHIP0_CHANNEL19  = 0u;
IS31_CHIP0_CHANNEL20  = 0u;
IS31_CHIP0_CHANNEL21  = 0u;
IS31_CHIP0_CHANNEL22  = 0u;
IS31_CHIP0_CHANNEL23  = 0u;
IS31_CHIP0_CHANNEL24  = 0u;
IS31_CHIP0_CHANNEL25  = 0u;
IS31_CHIP0_CHANNEL26  = 0u;
IS31_CHIP0_CHANNEL27  = 0u;
IS31_CHIP0_CHANNEL28  = 0u;
IS31_CHIP0_CHANNEL29  = 0u;
IS31_CHIP0_CHANNEL30  = 0u;
IS31_CHIP0_CHANNEL31  = 0u;
IS31_CHIP0_CHANNEL32  = 0u;
IS31_CHIP0_CHANNEL33  = 0u;
IS31_CHIP0_CHANNEL34  = 0u;
IS31_CHIP0_CHANNEL35  = 0u;
IS31_CHIP0_CHANNEL36  = 0u;

#if (IS31_CHIP_NUM >= 2U)                      
IS31_CHIP1_CHANNEL01  = 0u;
IS31_CHIP1_CHANNEL02  = 0u;
IS31_CHIP1_CHANNEL03  = 0u;
IS31_CHIP1_CHANNEL04  = 0u;
IS31_CHIP1_CHANNEL05  = 0u;
IS31_CHIP1_CHANNEL06  = 0u;
IS31_CHIP1_CHANNEL07  = 0u;
IS31_CHIP1_CHANNEL08  = 0u;
IS31_CHIP1_CHANNEL09  = 0u;
IS31_CHIP1_CHANNEL10  = 0u;
IS31_CHIP1_CHANNEL11  = 0u;
IS31_CHIP1_CHANNEL12  = 0u;
IS31_CHIP1_CHANNEL13  = 0u;
IS31_CHIP1_CHANNEL14  = 0u;
IS31_CHIP1_CHANNEL15  = 0u;
IS31_CHIP1_CHANNEL16  = 0u;
IS31_CHIP1_CHANNEL17  = 0u;
IS31_CHIP1_CHANNEL18  = 0u;
IS31_CHIP1_CHANNEL19  = 0u;
IS31_CHIP1_CHANNEL20  = 0u;
IS31_CHIP1_CHANNEL21  = 0u;
IS31_CHIP1_CHANNEL22  = 0u;
IS31_CHIP1_CHANNEL23  = 0u;
IS31_CHIP1_CHANNEL24  = 0u;
IS31_CHIP1_CHANNEL25  = 0u;
IS31_CHIP1_CHANNEL26  = 0u;
IS31_CHIP1_CHANNEL27  = 0u;
IS31_CHIP1_CHANNEL28  = 0u;
IS31_CHIP1_CHANNEL29  = 0u;
IS31_CHIP1_CHANNEL30  = 0u;
IS31_CHIP1_CHANNEL31  = 0u;
IS31_CHIP1_CHANNEL32  = 0u;
IS31_CHIP1_CHANNEL33  = 0u;
IS31_CHIP1_CHANNEL34  = 0u;
IS31_CHIP1_CHANNEL35  = 0u;
IS31_CHIP1_CHANNEL36  = 0u;
#endif
}
void LED_ON_Init(void)
{
IS31_CHIP0_CHANNEL01  = 255u;
IS31_CHIP0_CHANNEL02  = 255u;
IS31_CHIP0_CHANNEL03  = 255u;
IS31_CHIP0_CHANNEL04  = 255u;
IS31_CHIP0_CHANNEL05  = 255u;
IS31_CHIP0_CHANNEL06  = 255u;
IS31_CHIP0_CHANNEL07  = 255u;
IS31_CHIP0_CHANNEL08  = 255u;
IS31_CHIP0_CHANNEL09  = 255u;
IS31_CHIP0_CHANNEL10  = 255u;
IS31_CHIP0_CHANNEL11  = 255u;
IS31_CHIP0_CHANNEL12  = 255u;
IS31_CHIP0_CHANNEL13  = 255u;
IS31_CHIP0_CHANNEL14  = 255u;
IS31_CHIP0_CHANNEL15  = 255u;
IS31_CHIP0_CHANNEL16  = 255u;
IS31_CHIP0_CHANNEL17  = 255u;
IS31_CHIP0_CHANNEL18  = 255u;
IS31_CHIP0_CHANNEL19  = 255u;
IS31_CHIP0_CHANNEL20  = 255u;
IS31_CHIP0_CHANNEL21  = 255u;
IS31_CHIP0_CHANNEL22  = 255u;
IS31_CHIP0_CHANNEL23  = 255u;
IS31_CHIP0_CHANNEL24  = 255u;
IS31_CHIP0_CHANNEL25  = 255u;
IS31_CHIP0_CHANNEL26  = 255u;
IS31_CHIP0_CHANNEL27  = 255u;
IS31_CHIP0_CHANNEL28  = 255u;
IS31_CHIP0_CHANNEL29  = 255u;
IS31_CHIP0_CHANNEL30  = 255u;
IS31_CHIP0_CHANNEL31  = 255u;
IS31_CHIP0_CHANNEL32  = 255u;
IS31_CHIP0_CHANNEL33  = 255u;
IS31_CHIP0_CHANNEL34  = 255u;
IS31_CHIP0_CHANNEL35  = 255u;
IS31_CHIP0_CHANNEL36  = 255u;

#if (IS31_CHIP_NUM >= 2U)                      
IS31_CHIP1_CHANNEL01  = 255u;
IS31_CHIP1_CHANNEL02  = 255u;
IS31_CHIP1_CHANNEL03  = 255u;
IS31_CHIP1_CHANNEL04  = 255u;
IS31_CHIP1_CHANNEL05  = 255u;
IS31_CHIP1_CHANNEL06  = 255u;
IS31_CHIP1_CHANNEL07  = 255u;
IS31_CHIP1_CHANNEL08  = 255u;
IS31_CHIP1_CHANNEL09  = 255u;
IS31_CHIP1_CHANNEL10  = 255u;
IS31_CHIP1_CHANNEL11  = 255u;
IS31_CHIP1_CHANNEL12  = 255u;
IS31_CHIP1_CHANNEL13  = 255u;
IS31_CHIP1_CHANNEL14  = 255u;
IS31_CHIP1_CHANNEL15  = 255u;
IS31_CHIP1_CHANNEL16  = 255u;
IS31_CHIP1_CHANNEL17  = 255u;
IS31_CHIP1_CHANNEL18  = 255u;
IS31_CHIP1_CHANNEL19  = 255u;
IS31_CHIP1_CHANNEL20  = 255u;
IS31_CHIP1_CHANNEL21  = 255u;
IS31_CHIP1_CHANNEL22  = 255u;
IS31_CHIP1_CHANNEL23  = 255u;
IS31_CHIP1_CHANNEL24  = 255u;
IS31_CHIP1_CHANNEL25  = 255u;
IS31_CHIP1_CHANNEL26  = 255u;
IS31_CHIP1_CHANNEL27  = 255u;
IS31_CHIP1_CHANNEL28  = 255u;
IS31_CHIP1_CHANNEL29  = 255u;
IS31_CHIP1_CHANNEL30  = 255u;
IS31_CHIP1_CHANNEL31  = 255u;
IS31_CHIP1_CHANNEL32  = 255u;
IS31_CHIP1_CHANNEL33  = 255u;
IS31_CHIP1_CHANNEL34  = 255u;
IS31_CHIP1_CHANNEL35  = 255u;
IS31_CHIP1_CHANNEL36  = 255u;
#endif
}
uint8_t u8IS31Data [ IS31_CHIP_NUM ][ IS31_CHANNEL_NUM ];
uint8_t u8IS31DataBackup [ IS31_CHIP_NUM ][ IS31_CHANNEL_NUM ];

IS31_Ctr_st_t stIS31Ctr;
/*--------------------------------------------------*/
void IS31_Set_SDB_Pin(uint8_t u8Level);
void IS31_Init(void);
void IS31_Shutdown(void);
void IS31_Service(void);
void IS31_Scan_Refresh(void);

/*--------------------------------------------------*/
/*
设置SDB引脚
1：高，使能
0：低，休眠
*/
void IS31_Set_SDB_Pin(uint8_t u8Level)
{
	D_3236SDB_MCU = u8Level;
}
/*------------------------------------------------------*/
/**/
void IS31_Init_Inst_Handle(void)
{
}
void IS31_Init(void)
{
	uint8_t i = 0U;
	uint8_t j = 0U;

	for (i = 0U; i < IS31_CHIP_NUM; i++)
	{
		for (j = 0U; j < IS31_CHANNEL_NUM; j++)
		{
			u8IS31Data[i][j] = 0U;
			u8IS31DataBackup[i][j] = 0U;
		}
	}

	stIS31Ctr.u8IS31Init = 1U; /*进入初始化状态*/
	stIS31Ctr.u8IS31Step = 0U;
	stIS31Ctr.u8IS31Operation = 0U;

	stIS31Ctr.u8IS31Refresh = 2U; /*初始化完成后立即刷新一次*/
	stIS31Ctr.u8IS31ChannelCount = 0U;
	stIS31Ctr.u8IS31ChipCount = 0U;

	stIS31Ctr.u8IS31ChannelBegin = IS31_CHANNEL_NUM;
	stIS31Ctr.u8IS31ChannelEnd = IS31_CHANNEL_NUM;
	stIS31Ctr.u8IS31ChannelWriteCount = 0U;
		u32IS31ErrorCount = 0U;

	IS31_Set_SDB_Pin(1U);
	Simulated_IIC_2_Init();
}

void IS31_Shutdown(void)
{
	IS31_Set_SDB_Pin(0U);
		stIS31Ctr.u8IS31Init = 0U;
}
void IS31_Scan_Refresh(void)
{
	if (stIS31Ctr.u8IS31Refresh == 0U)
	{
		stIS31Ctr.u8IS31Refresh = 1U;
	}
	if (stIS31Ctr.u8IS31Init == 3U)
	{
		IS31_Set_SDB_Pin(1U);
		stIS31Ctr.u8IS31Init = 1U; /*进入初始化状态*/
		stIS31Ctr.u8IS31Step = 0U;
		stIS31Ctr.u8IS31Operation = 0U;

		stIS31Ctr.u8IS31Refresh = 2U; /*初始化完成后立即刷新一次*/
		stIS31Ctr.u8IS31ChannelCount = 0U;
		stIS31Ctr.u8IS31ChipCount = 0U;

		stIS31Ctr.u8IS31ChannelBegin = IS31_CHANNEL_NUM;
		stIS31Ctr.u8IS31ChannelEnd = IS31_CHANNEL_NUM;
		stIS31Ctr.u8IS31ChannelWriteCount = 0U;

		u32IS31ErrorCount = 0U;
	}

	if (u32IS31ErrorCount >= IS31_ERROR_MAX)
	{
		IS31_Set_SDB_Pin(0U);
		u32IS31ErrorCount = 0U;
		stIS31Ctr.u8IS31Init = 3U;
	}
}
uint8_t IS31_IIC_Receive_ACK(void)
{
	uint8_t u8IS31ACK;

	u8IS31ACK = Simulated_IIC_2_Receive_ACK();
	if (u8IS31ACK == SIMULATED_IIC_2_ACK)
	{
		u32IS31ErrorCount = 0U;
	}
	else
	{
		if (u32IS31ErrorCount < IS31_ERROR_MAX)
		{
			u32IS31ErrorCount++;
		}
	}

	return u8IS31ACK;

}
/*主循环内调用*/
void IS31_Service(void)
{
	uint8_t i = 0U;
	if (stIS31Ctr.u8IS31Init == 1U) /*初始化未完成*/
	{
		switch (stIS31Ctr.u8IS31Step)
		{
		case 0U:
		{
			switch (stIS31Ctr.u8IS31Operation)
			{
			case 0U:
			{

				Simulated_IIC_2_Start();
				stIS31Ctr.u8IS31ChannelWriteCount = 0U;
				stIS31Ctr.u8IS31Operation++;

				break;
			}
			case 1U:
			{
				Simulated_IIC_2_Transmit_Data(u8IS31ChipAddress[stIS31Ctr.u8IS31ChipCount]);
				stIS31Ctr.u8IS31Operation++;
				stIS31Ctr.u8IS31Operation++;

				break;
			}

			case 3U:
			{

				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{
					Simulated_IIC_2_Transmit_Data(0X0U);
					stIS31Ctr.u8IS31Operation++;
					stIS31Ctr.u8IS31Operation++;
				}
				else
				{

					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;
				}

				break;
			}
			case 5U:
			{

				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{
					Simulated_IIC_2_Transmit_Data(0X01U);
					stIS31Ctr.u8IS31Operation++;
					stIS31Ctr.u8IS31Operation++;
				}
				else
				{
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;
				}

				break;
			}

			case 7U:
			{

				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{
					Simulated_IIC_2_Transmit_Data(u8IS31DataBackup[stIS31Ctr.u8IS31ChipCount][stIS31Ctr.u8IS31ChannelWriteCount]);
					stIS31Ctr.u8IS31ChannelWriteCount++;
					if (stIS31Ctr.u8IS31ChannelWriteCount >= IS31_CHANNEL_NUM)
					{
						stIS31Ctr.u8IS31Operation++;
						stIS31Ctr.u8IS31Operation++;
					}
					else
					{
					}
				}
				else
				{

					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;
				}

				break;
			}
			case 9U:
			{

				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{
					Simulated_IIC_2_Transmit_Data(0X00U);
					stIS31Ctr.u8IS31Operation++;
					stIS31Ctr.u8IS31Operation++;
				}
				else
				{
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;
				}

				break;
			}
			case 11U:
			{

				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{

					stIS31Ctr.u8IS31Step = 1U;
				}
				else
				{
				}
				Simulated_IIC_2_Stop();
				stIS31Ctr.u8IS31Operation = 0U;

				break;
			}

			default:
			{
				Simulated_IIC_2_Stop();
				stIS31Ctr.u8IS31Operation = 0U;

				break;
			}
			}

			break;
		}

		case 1:
		{
			switch (stIS31Ctr.u8IS31Operation)
			{

			case 0U:
			{
				Simulated_IIC_2_Start();
				stIS31Ctr.u8IS31ChannelWriteCount = 0U;
				stIS31Ctr.u8IS31Operation++;

				break;
			}
			case 1U:
			{
				Simulated_IIC_2_Transmit_Data(u8IS31ChipAddress[stIS31Ctr.u8IS31ChipCount]);
				stIS31Ctr.u8IS31Operation++;
				stIS31Ctr.u8IS31Operation++;

				break;
			}

			case 3U:
			{
				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{
					Simulated_IIC_2_Transmit_Data(0X26U);
					stIS31Ctr.u8IS31Operation++;
					stIS31Ctr.u8IS31Operation++;
				}
				else
				{
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;
				}

				break;
			}

			case 5U:
			{
				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{
					Simulated_IIC_2_Transmit_Data(0X01U);

					stIS31Ctr.u8IS31ChannelWriteCount++;
					if (stIS31Ctr.u8IS31ChannelWriteCount >= IS31_CHANNEL_NUM)
					{
						stIS31Ctr.u8IS31Operation++;
						stIS31Ctr.u8IS31Operation++;
					}
					else
					{
					}
				}
				else
				{
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;
				}

				break;
			}
			case 7U:
			{
				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{

					stIS31Ctr.u8IS31Step = 2U;
				}
				else
				{
				}
				Simulated_IIC_2_Stop();
				stIS31Ctr.u8IS31Operation = 0U;

				break;
			}

			default:
			{
				Simulated_IIC_2_Stop();
				stIS31Ctr.u8IS31Operation = 0U;

				break;
			}

			break;
			}
			break;
		}

		case 2:
		{
			switch (stIS31Ctr.u8IS31Operation)
			{

			case 0U:
			{
				Simulated_IIC_2_Start();
				stIS31Ctr.u8IS31ChannelWriteCount = 0U;
				stIS31Ctr.u8IS31Operation++;

				break;
			}
			case 1U:
			{
				Simulated_IIC_2_Transmit_Data(u8IS31ChipAddress[stIS31Ctr.u8IS31ChipCount]);
				stIS31Ctr.u8IS31Operation++;
				stIS31Ctr.u8IS31Operation++;

				break;
			}

			case 3U:
			{
				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{
					Simulated_IIC_2_Transmit_Data(0X25U);
					stIS31Ctr.u8IS31Operation++;
					stIS31Ctr.u8IS31Operation++;
				}
				else
				{
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;
				}

				break;
			}
			case 5U:
			{
				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{
					Simulated_IIC_2_Transmit_Data(0X00U);
					stIS31Ctr.u8IS31Operation++;
					stIS31Ctr.u8IS31Operation++;
				}
				else
				{
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;
				}

				break;
			}
			case 7U:
			{
				if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
				{

					stIS31Ctr.u8IS31Step = 3U;
				}
				else
				{
				}
				Simulated_IIC_2_Stop();
				stIS31Ctr.u8IS31Operation = 0U;

				break;
			}

			default:
			{
				Simulated_IIC_2_Stop();
				stIS31Ctr.u8IS31Operation = 0U;

				break;
			}
			}

			break;
		}
		case 3:
		{

			stIS31Ctr.u8IS31Step = 0U;
			stIS31Ctr.u8IS31Operation = 0U;

			stIS31Ctr.u8IS31ChipCount++;
			if (stIS31Ctr.u8IS31ChipCount >= IS31_CHIP_NUM)
			{
				stIS31Ctr.u8IS31ChipCount = 0U;
				stIS31Ctr.u8IS31Init = 2U;
			}

			break;
		}

		default:
		{
			Simulated_IIC_2_Stop();
			stIS31Ctr.u8IS31Operation = 0U;
			stIS31Ctr.u8IS31Step = 0U;

			break;
		}
		}
	}
	else if (stIS31Ctr.u8IS31Init == 2U)
	{
		if (stIS31Ctr.u8IS31Refresh >= 1U)
		{
			switch (stIS31Ctr.u8IS31Step)
			{
			case 0U:
			{
				if (stIS31Ctr.u8IS31ChannelCount < IS31_CHANNEL_NUM)
				{
					if (stIS31Ctr.u8IS31Refresh == 1U)
					{
						for (i = 0U; i < IS31_CHANNEL_NUM; i++)
						{
							if (u8IS31DataBackup[stIS31Ctr.u8IS31ChipCount][stIS31Ctr.u8IS31ChannelCount] !=
								u8IS31Data[stIS31Ctr.u8IS31ChipCount][stIS31Ctr.u8IS31ChannelCount])
							{
								u8IS31DataBackup[stIS31Ctr.u8IS31ChipCount][stIS31Ctr.u8IS31ChannelCount] =
									u8IS31Data[stIS31Ctr.u8IS31ChipCount][stIS31Ctr.u8IS31ChannelCount];

								if (stIS31Ctr.u8IS31ChannelBegin >= IS31_CHANNEL_NUM) /*Invalid*/
								{
									stIS31Ctr.u8IS31ChannelBegin = stIS31Ctr.u8IS31ChannelCount;
								}
								stIS31Ctr.u8IS31ChannelEnd = stIS31Ctr.u8IS31ChannelCount;
							}
							else
							{
								if (stIS31Ctr.u8IS31ChannelBegin < IS31_CHANNEL_NUM) /*valid*/
								{
									stIS31Ctr.u8IS31Step = 1U;
								}
							}
							stIS31Ctr.u8IS31ChannelCount++;

							if (stIS31Ctr.u8IS31ChannelCount >= IS31_CHANNEL_NUM)
							{
								if (stIS31Ctr.u8IS31ChannelBegin < IS31_CHANNEL_NUM) /*valid*/
								{
									stIS31Ctr.u8IS31Step = 1U;
								}
							}
							if ((stIS31Ctr.u8IS31ChannelCount >= IS31_CHANNEL_NUM) || (stIS31Ctr.u8IS31Step == 1U))
							{
								break;
							}
						}
					}
					else
					{
						for (i = 0U; i < IS31_CHANNEL_NUM; i++)
						{
							u8IS31DataBackup[stIS31Ctr.u8IS31ChipCount][i] = u8IS31Data[stIS31Ctr.u8IS31ChipCount][i];
						}

						stIS31Ctr.u8IS31ChannelBegin = 0U;
						stIS31Ctr.u8IS31ChannelEnd = 35U;

						stIS31Ctr.u8IS31Step = 1U; /*准备数据写入*/
						stIS31Ctr.u8IS31ChannelCount = IS31_CHANNEL_NUM;
					}
				}
				else
				{
					stIS31Ctr.u8IS31ChannelCount = 0U;
					stIS31Ctr.u8IS31ChipCount++;
					if (stIS31Ctr.u8IS31ChipCount >= IS31_CHIP_NUM)
					{
						if (stIS31Ctr.u8IS31ChannelDataUpdate == 1U)
						{
							stIS31Ctr.u8IS31Step = 2U;
						}
						else
						{
							stIS31Ctr.u8IS31Step = 0U;
							stIS31Ctr.u8IS31Refresh = 0U;
						}
						stIS31Ctr.u8IS31ChipCount = 0U;
						stIS31Ctr.u8IS31ChannelDataUpdate = 0U;
					}
					stIS31Ctr.u8IS31Operation = 0U;
				}
				break;
			}

			case 1U:
			{
				switch (stIS31Ctr.u8IS31Operation)
				{
				case 0U:
				{
					stIS31Ctr.u8IS31ChannelDataUpdate = 1U;
					Simulated_IIC_2_Start();
					stIS31Ctr.u8IS31Operation++;

					break;
				}
				case 1U:
				{
					Simulated_IIC_2_Transmit_Data(u8IS31ChipAddress[stIS31Ctr.u8IS31ChipCount]);
					stIS31Ctr.u8IS31Operation++;
					stIS31Ctr.u8IS31Operation++;

					break;
				}

				case 3U:
				{
					if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
					{
						Simulated_IIC_2_Transmit_Data(stIS31Ctr.u8IS31ChannelBegin + 1U);
						stIS31Ctr.u8IS31ChannelWriteCount = stIS31Ctr.u8IS31ChannelBegin;
						stIS31Ctr.u8IS31Operation++;
						stIS31Ctr.u8IS31Operation++;
					}
					else
					{
						Simulated_IIC_2_Stop();
						stIS31Ctr.u8IS31Operation = 0U;
					}

					break;
				}
				case 5U:
				{
					if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
					{
						Simulated_IIC_2_Transmit_Data(u8IS31DataBackup[stIS31Ctr.u8IS31ChipCount][stIS31Ctr.u8IS31ChannelWriteCount]);

						stIS31Ctr.u8IS31ChannelWriteCount++;
						if (stIS31Ctr.u8IS31ChannelWriteCount > stIS31Ctr.u8IS31ChannelEnd)
						{
							stIS31Ctr.u8IS31Operation++;
							stIS31Ctr.u8IS31Operation++;
						}
						else
						{
						}
					}
					else
					{

						Simulated_IIC_2_Stop();
						stIS31Ctr.u8IS31ChannelWriteCount = stIS31Ctr.u8IS31ChannelBegin;
						stIS31Ctr.u8IS31Operation = 0U;
					}

					break;
				}
				case 7U:
				{
					if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
					{

						stIS31Ctr.u8IS31Step = 0U;
						stIS31Ctr.u8IS31ChannelWriteCount = IS31_CHANNEL_NUM;
						stIS31Ctr.u8IS31ChannelBegin = IS31_CHANNEL_NUM;
						stIS31Ctr.u8IS31ChannelEnd = IS31_CHANNEL_NUM;
					}
					else
					{
					}
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;

					break;
				}

				default:
				{
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;

					break;
				}
				}

				break;
			}

			case 2U:
			{
				switch (stIS31Ctr.u8IS31Operation)
				{

				case 0U:
				{
					Simulated_IIC_2_Start();
					stIS31Ctr.u8IS31Operation++;

					break;
				}
				case 1U:
				{
					Simulated_IIC_2_Transmit_Data(u8IS31ChipAddress[stIS31Ctr.u8IS31ChipCount]);
					stIS31Ctr.u8IS31Operation++;
					stIS31Ctr.u8IS31Operation++;

					break;
				}

				case 3U:
				{
					if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
					{
						Simulated_IIC_2_Transmit_Data(0X25U);
						stIS31Ctr.u8IS31Operation++;
						stIS31Ctr.u8IS31Operation++;
					}
					else
					{
						Simulated_IIC_2_Stop();
						stIS31Ctr.u8IS31Operation = 0U;
					}

					break;
				}
				case 5U:
				{
					if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
					{
						Simulated_IIC_2_Transmit_Data(0X00U);
						stIS31Ctr.u8IS31Operation++;
						stIS31Ctr.u8IS31Operation++;
					}
					else
					{
						Simulated_IIC_2_Stop();
						stIS31Ctr.u8IS31Operation = 0U;
					}

					break;
				}
				case 7U:
				{

					if (IS31_IIC_Receive_ACK() == SIMULATED_IIC_2_ACK)
					{
						stIS31Ctr.u8IS31ChipCount++;
						if (stIS31Ctr.u8IS31ChipCount >= IS31_CHIP_NUM)
						{
							stIS31Ctr.u8IS31ChipCount = 0U;
							stIS31Ctr.u8IS31Step = 0U;
							stIS31Ctr.u8IS31Operation = 0U;
							stIS31Ctr.u8IS31Refresh = 0U;
						}
					}
					else
					{
					}
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;

					break;
				}

				default:
				{
					Simulated_IIC_2_Stop();
					stIS31Ctr.u8IS31Operation = 0U;

					break;
				}
				}

				break;
			}
			default:
			{
				Simulated_IIC_2_Stop();
				stIS31Ctr.u8IS31Operation = 0U;
				stIS31Ctr.u8IS31Step = 0U;

				stIS31Ctr.u8IS31ChannelCount = 0U;
				stIS31Ctr.u8IS31ChipCount = 0U;

				break;
			}
			}
		}
	}
}
void IS31_Break(void)
{
	if (stIS31Ctr.u8IS31Refresh == 1U)
	{
		stIS31Ctr.u8IS31ChannelCount = 0U;
		stIS31Ctr.u8IS31Step = 0U;
		stIS31Ctr.u8IS31Operation = 0U;
		stIS31Ctr.u8IS31ChipCount = 0U;
		stIS31Ctr.u8IS31ChannelCount = 0U;

		stIS31Ctr.u8IS31Refresh = 2U;
	}
}
