From 005755edcdc332315ba077598d4746ac195b069e Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期一, 14 十月 2024 11:45:56 +0800
Subject: [PATCH] increase KPLC KBus from 2 client to 16 clients

---
 KPLC/Src/main.c |  371 ++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 337 insertions(+), 34 deletions(-)

diff --git a/KPLC/Src/main.c b/KPLC/Src/main.c
index 7320932..3a16726 100644
--- a/KPLC/Src/main.c
+++ b/KPLC/Src/main.c
@@ -79,14 +79,42 @@
 unsigned char Uart2RxBuf[RX2BUFSIZE];
 unsigned char Uart2TxBuf[TX2BUFSIZE];
 
+unsigned char Uart1RxBuf1[Uart1RxBufSize];
+unsigned char Uart1TxBuf1[260];
+
+unsigned char Uart2RxBuf1[RX2BUFSIZE];
+unsigned char Uart2TxBuf1[TX2BUFSIZE];
+
+unsigned short Uart1RxBuf1DataLen = 0;
+unsigned short Uart2RxBuf1DataLen = 0;
+
+unsigned char Uart1Mode = 1;			//Uart1宸ヤ綔妯″紡锛� 0 : 鏅�氾紝 1 : 閫忎紶妯″紡
+
+unsigned int Uart1Baud = DefaultUart1Baud;
+unsigned int Uart2Baud = DefaultUart2Baud;
+
+//unsigned char Uart1RecvBuf1[Uart1RecvBufSize];
+//unsigned short Uart1RecvBuf1DataLen=0;
+
+//unsigned char Uart2RecvBuf1[128];
+//unsigned short Uart2RecvBuf1DataLen=0;
+
+volatile char Uart1BaudGot=0;
+volatile char Uart1BaudFirstGot=0;
+volatile char Uart1DmaInts=0;
+
+
 unsigned char SlowFlicker=0;
 unsigned char FastFlicker=0;
 
 unsigned int Uart1IdelTimer = 0;
-stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE;
 
 uint32_t us1,us2,us3,us4,us5,us6;
 
+
+stKBusDef KBus1;							// 
+
+extern 	stDeviceInfo MyDeviceInfo;
 /* USER CODE END PV */
 
 /* Private function prototypes -----------------------------------------------*/
@@ -104,6 +132,183 @@
 /* USER CODE END PFP */
 
 /* USER CODE BEGIN 0 */
+#define SET_SCL LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_6)
+#define CLR_SCL LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_6)
+#define GET_SCL LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_6)
+#define SET_SDA LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_7)
+#define CLR_SDA LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_7)
+#define GET_SDA LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_7)
+
+
+void soft_i2c_start()
+{
+	SET_SDA;
+	SET_SCL;
+	Delay100nS(1);		
+	CLR_SDA;
+	Delay100nS(1);	
+	CLR_SCL;
+	Delay100nS(1);
+}
+void soft_i2c_stop()
+{
+		CLR_SDA;
+		Delay100nS(1);				
+		SET_SCL;
+		Delay100nS(1);
+		SET_SDA;
+		Delay100nS(1);
+}
+void soft_i2c_send8(int nData)
+{
+	int mask;
+	mask = 0x80;
+	for (int j=0;j<8;j++)
+	{
+		if (nData & mask) {SET_SDA;}
+		else {CLR_SDA;}
+		Delay100nS(1);		
+		SET_SCL;
+		mask>>=1;
+		Delay100nS(1);	
+		CLR_SCL;
+	}	
+	return;
+}
+
+uint8_t soft_i2c_recv8()
+{
+	unsigned char nData=0;
+		for (int j=0;j<8;j++)
+		{
+			nData <<=1;				
+			Delay100nS(1);				
+			SET_SCL;
+			nData |= GET_SDA;
+			Delay100nS(1);				
+			CLR_SCL;
+		}	
+	return nData;
+}
+
+void soft_i2c_send_ack()
+{
+				CLR_SDA;	
+				Delay100nS(2);				
+				SET_SCL;
+				Delay100nS(2);
+				CLR_SCL;
+				SET_SDA;	
+				Delay100nS(2);	
+
+}
+
+void soft_i2c_send_nack()
+{
+				SET_SDA;	
+				Delay100nS(1);				
+				SET_SCL;
+				Delay100nS(1);
+				CLR_SCL;
+				Delay100nS(1);	
+				SET_SDA;
+}
+uint8_t soft_i2c_wait_ack(int nTime)
+{
+		SET_SDA;	// Open Drain;
+		Delay100nS(1);	
+		SET_SCL;	
+		for (int j=0;j<nTime;j++){
+				Delay100nS(1);
+			if (GET_SDA == 0) break;
+			if (j==nTime-1) return 0;
+		}	
+		CLR_SCL;	
+		return 1;
+}
+uint8_t soft_i2c_check_addr(uint8_t Addr)
+{
+	uint8_t res=0;
+	soft_i2c_start();	
+		// Send Device Addr  7bit;	
+	soft_i2c_send8(Addr);
+	if (soft_i2c_wait_ack(10)) {res=1;}
+	//Stop
+	soft_i2c_stop();
+//  */		
+	return res;	
+
+}
+uint8_t soft_i2c_read_len( uint8_t Addr , uint8_t Reg, uint8_t len,uint8_t *buf)
+{
+	int res=0;
+	//Start
+	soft_i2c_start();
+	// Send Device Addr  7bit;
+	soft_i2c_send8(Addr &0xfe);
+	// wait Ack;
+	if (!soft_i2c_wait_ack(1000)) {soft_i2c_stop();return 1;}
+	CLR_SCL;
+	// Send Reg Addr 8bit;
+	soft_i2c_send8(Reg);
+	if (!soft_i2c_wait_ack(1000)) {soft_i2c_stop();return 2;}
+	//Start	
+	soft_i2c_start();
+	// Send Device Addr  7bit;	
+	soft_i2c_send8(Addr | 1);
+	if (!soft_i2c_wait_ack(1000)) {soft_i2c_stop();return 3;}
+
+//  /*	
+	// Recv Data(s) n * 8bit;
+		SET_SDA;	// Open Drain;		
+		for (int i=0;i<len;i++)
+		{
+			// recv 1 data 8bit;
+			unsigned char nData = 0;
+			nData = soft_i2c_recv8();
+			buf[i]=nData;
+			// Send ACK / NACK;
+			if (i != len -1) { //ACK
+				soft_i2c_send_ack();
+			}	else {					// NACK
+				soft_i2c_send_nack();
+			}
+		}
+	
+	//Stop
+	soft_i2c_stop();
+//  */		
+	return res;
+}
+ 
+uint8_t soft_i2c_write_len(uint8_t Addr , uint8_t Reg, uint8_t len, uint8_t *buf)
+{
+	int res=0;
+	//Start
+	soft_i2c_start();
+	// Send Device Addr  7bit;
+	soft_i2c_send8(Addr &0xfe);
+	// wait Ack;
+	if (!soft_i2c_wait_ack(1000)) return 1;
+	CLR_SCL;
+	// Send Reg Addr 8bit;
+	soft_i2c_send8(Reg);
+	if (!soft_i2c_wait_ack(1000)) return 2;	
+		for (int i=0;i<len;i++)
+		{
+			// send 1 data 8bit;
+			unsigned char nData = buf[i];
+			soft_i2c_send8(nData);
+	// wait Ack;			
+			if (!soft_i2c_wait_ack(1000)) {res = 5; break;}
+		}	
+	//Stop
+	soft_i2c_stop();	
+	return res;
+	
+}
+
+
 
 int HexToInt(char ch)
 {
@@ -118,7 +323,7 @@
 static int Count=0;
 	CurTickuS += 100;	
 	nCurTick++;
-	nSlaveTick++;
+	KBus1.nSlaveTick++;
 	Count++;
 	if (Count>=10000) 
 	{
@@ -132,7 +337,32 @@
 	return;
 }
 
-void * KBusCallBackFunc(int nChn, int nEvent, void *pBuf, int nLen1)
+void PendSvCallBack()
+{
+#if (BOARD_TYPE == 14)
+///*	
+		if (bSPI1RecvDone)
+		{
+			bSPI1RecvDone=0;
+			FPxParsePkt(SPI1RecvBuf,nSPI1RecvLenInBuf);
+		}
+//*/	
+#endif		
+	if (Uart2Stat.bPacketRecved)
+	{
+		KBusParsePacket(&KBus1, (pKBPacket)Uart2RxBuf1, Uart2RxBuf1DataLen);		
+		Uart2RxBuf1DataLen=0;
+		Uart2Stat.bPacketRecved=0;
+		Uart2RecvDMA(Uart2RxBuf1,sizeof(Uart2RxBuf1));		
+		KMem.WDT[2]++;
+	}
+}
+
+/*
+KBus閫氳鍥炶皟鍑芥暟锛屽綋閫氳鐘舵�佹敼鍙樻垨鏁版嵁鏇存柊鏃惰璋冪敤銆�
+鎴栬�呯郴缁熻姹傛椂銆�
+*/
+void * KBusEvCallBackFunc(void*  pParam, int nEvent, void *pBuf, int nLen1)
 {
 	switch (nEvent){
 		
@@ -151,8 +381,23 @@
 		case KBusEvTimeSync:
 			break;
 		case KBusEvDataUpdate:
-//			KMem.WY[0]=KBusMem.WLY[0];			//KBus Slave
-//			KBusMem.WLX[0]=KMem.WX[0];
+			if (KBus1.bMaster) {
+				for (int i=0;i<16;i++)
+				{
+					KMem.WLX[i]=KBusMem.WLX[i];			//KPLC with KBus Master
+					KBusMem.WLY[i]=KMem.WLY[i];
+				}
+			} else if (KBus1.bSlave) {
+				KMem.WLX[0]=KBusMem.WLY[0];			//KPLC with KBus Slave
+				KBusMem.WLX[0]=KMem.WLY[0];
+				KMem.WLX[1]=KBusMem.WLY[1];			//KPLC with KBus Slave
+				KBusMem.WLX[1]=KMem.WLY[1];
+				KMem.WLX[2]=KBusMem.WLY[2];			//KPLC with KBus Slave
+				KBusMem.WLX[2]=KMem.WLY[2];
+				KMem.WLX[3]=KBusMem.WLY[3];			//KPLC with KBus Slave
+				KBusMem.WLX[3]=KMem.WLY[3];				
+			}
+			
 			break;
 		case KBusEvCmdResponse:
 			break;
@@ -162,6 +407,7 @@
 	}
 	return 0;
 }
+
 
 /* USER CODE END 0 */
 
@@ -223,10 +469,16 @@
 	
 	KMRunStat.bLEDFlick = 1;
 	
+	
+	KLinkInit(1);
+	unsigned char bKBusMaster, bKBusSlave, bKBusRepeater;
+	int nKBusStationID;
+	int nKBusChilds;
 	KMem.CurJumperSW=ReadJumperSW();
 	KMem.EffJumperSW=KMem.CurJumperSW;
-	
 //	Uart2Baud = AlterUart2Baud;
+	nKBusStationID = KMem.EffJumperSW&0x0f;
+	nKBusChilds = nKBusStationID;
 	
 #if (BOARD_TYPE == 14)
 	KMem.EffJumperSW|=0x10;
@@ -234,7 +486,7 @@
   if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}
 	nChilds=nStationID;
-	FP0_Init();
+	FP0_Init(nChilds);
 
 #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
 	nStationID=1 ;//KMem.EffJumperSW&0x0f;
@@ -243,25 +495,59 @@
 //	else
 		{bKBusMaster=0;bKBusSlave=1;}	
 #else
-	nStationID=KMem.EffJumperSW&0x0f;
+	nKBusStationID=KMem.EffJumperSW&0x0f;
 	if (KMem.EffJumperSW == 0x1f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
-  else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
-	else{bKBusMaster=0;bKBusSlave=1;}
+  else if ((KMem.EffJumperSW&0x10)!=0) {
+		bKBusMaster=1;bKBusSlave=0;
+	}
+	else{
+		bKBusMaster=0;bKBusSlave=1;
+	}
 #endif
 	
-	KBusInit(2, bKBusMaster, nChilds);	
-	KBusSetCallBackFunc(2, &KBusCallBackFunc),
+	if (bKBusMaster) {
+		KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusChilds);		
+	}	else if (bKBusSlave) {
+		KBusInitSlave(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusStationID,&MyDeviceInfo);	
+	}		
+	KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc),
+
+	UNUSED(bKBusRepeater);
 	
-	nChilds=nStationID;
-	nCurPollId=1;
 	//if (KMem.EffJumperSW == 0x00)
 		Uart1Baud = DefaultUart1Baud;
   MX_USART1_UART_Init();
   MX_USART2_UART_Init();
-
 	MX_SPI1_Init();
 	LL_SPI_EnableIT_RXNE(SPI1);
+/*
+	//	MX_I2C1_Init();
+	Soft_I2C1_Init();
 
+	unsigned char buf1[10] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa};
+	unsigned char buf2[10];
+//	KMem.WDB[80] = soft_i2c_read_len(0xa0,0x00,8,buf2);
+//	for (int i=0;i<8;i++){
+//		buf2[i]+=1;
+//	}
+//	soft_i2c_write_len (0xa0,0x00,8,buf2);
+//	DelayUs(500);	
+//	KMem.WDB[80] = soft_i2c_read_len(0xa0,0x00,8,&KMem.WDB[81]);
+//	DelayUs(500);
+//	KMem.WDB[90] = soft_i2c_read_len(0xa0,0x00,8,&KMem.WDB[91]);
+	
+	int nPos = 80;
+	int nCount =0;
+	for (int i=0;i<256;i++)
+	{
+		if (soft_i2c_check_addr(i)){
+			KMem.WDB[nPos + 1 + nCount ] = i;
+			nCount+=1;
+		}
+		DelayUs(10);
+	}	
+	KMem.WDB[nPos] = nCount; 
+*/		
 #if (BOARD_TYPE == 14)
 //	MX_SPI2_Init();
 //  MX_ADC_Init();
@@ -269,7 +555,7 @@
 	MX_SPI2_Init();
   MX_ADC_Init();
 #endif
-
+	
 	MX_IWDG_Init();
 
 	MX_TIM6_Init();
@@ -281,7 +567,7 @@
 	LL_USART_EnableIT_TC(USART1);
 
 //	LL_USART_EnableIT_RXNE(USART2);
-	Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));	
+	Uart2RecvDMA(Uart2RxBuf1,sizeof(Uart2RxBuf1));	
 	LL_USART_EnableIT_IDLE(USART2);
 	LL_USART_EnableIT_TC(USART2);
 #if (BOARD_TYPE == 13)
@@ -302,6 +588,9 @@
 	//	LL_USART_SetAutoBaudRateMode(USART2, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);		
 	}
 	//LL_USART_EnableIT_TXE(USART1);
+	
+//	KMem.WDT[50] = SPI_Flash_ReadID(); 
+	
   /* USER CODE END 2 */
 
 	
@@ -347,6 +636,7 @@
 	KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x0f);
 	KWireLessStart();
 #endif
+
 	
   while (1)
   {
@@ -377,6 +667,16 @@
 //		*((unsigned int *)&(PLCMem.SDT[2]))=nChilds;
 //		KMem.SDD[13]=PendSvCount;
 //		KMem.SDD[14]=RCC->CSR;		
+		
+		KMem.WDB[0] = KBus1.MyStat; 
+		KMem.WDB[1] = KBus1.bMaster;
+		KMem.WDB[2] = KBus1.bMasterSent;
+		KMem.WDB[3] = KBus1.bMasterRecved;
+		KMem.WDB[4] = KBus1.bMasterRecvOK;
+		KMem.WDB[5] = KBus1.bSlaveRecved;
+		
+		KMem.WDB[8] = KBus1.RunStat;
+		KMem.WDB[9] = KBus1.ErrStat;
 		
 		int a;
 		a		= LL_GPIO_ReadInputPort(GPIOA);
@@ -433,19 +733,21 @@
 		
 #endif
 
-//		pProgs = (stBinProg1 *) STORE_PRG_BASE;
+//		pProgs = (stBinInstrcn1 *) STORE_PRG_BASE;
 
 		if (	KMRunStat.WorkMode==1 ) //&& bKBusMaster)
 		{
-			if (KMRunStat.nBinProgBank == 0){
-				pProgs=(stBinProg1 *)STORE_PRG_BASE;
+			stStoredBinProgs * pStoredBinProgs;
+
+			if (storedKMSysCfg.theKMSysCfg.nProgBank == 0){
+				pStoredBinProgs=((stStoredBinProgs *)STORE_PRG_BASE);
 			}else {
-				pProgs=(stBinProg1 *)ALT_PRG_BASE;
+				pStoredBinProgs=((stStoredBinProgs *)ALT_PRG_BASE); ;
 			}
-			nSizeProg1=KMRunStat.nBinProgSize;
-		//	pProgs=(stBinProg1 *)prog1;
+			int nSizeProg1=pStoredBinProgs->StoredHdr.nSize ;
+		//	pProgs=(stBinInstrcn1 *)prog1;
 			
-			ProcessPLCBinProg(pProgs, nSizeProg1);
+			ProcessPLCBinProg(pStoredBinProgs->BinInstrcns, nSizeProg1);
 		}
 
 		KMem.ScanTimeuS=us2-KMem.LastScanTime;
@@ -462,7 +764,7 @@
 
 			KBusMem.WLY[0]=KMem.WLY[0];
 		
-			if (nChilds>0) {		KBusMasterFunc(2); }
+			if (nKBusChilds>0) {		KBusMasterFunc(&KBus1); }
 
 			KMem.WLX[0]=KBusMem.WLX[0];
 
@@ -480,20 +782,20 @@
 		//	if (! KMem.RunStat) {BufferIn[0]=0;}
 		//	KMem.WY[0]=BufferIn[0];
 #else
-			KBusSlaveFunc(2);	
+			KBusSlaveFunc(&KBus1);	
 			if (! KMem.RunStat) {KMem.WLY[0]=0;}
 		//	KMem.WLY[0]=BufferIn[0];
 #endif
-			if (nSlaveTick&0x00002000) SlowFlicker=1;
+			if (KBus1.nSlaveTick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
-			if (nSlaveTick&0x00000800) FastFlicker=1;
+			if (KBus1.nSlaveTick&0x00000800) FastFlicker=1;
 			else FastFlicker=0;			
 
 		}
 
 //		KMem.WY[0]=nCount2>>5;
-		if (KMem.RunStat) {KMem.RunStat--;}
-		if (KMem.ErrStat) {KMem.ErrStat--;}
+		if (KBus1.RunStat) {KBus1.RunStat--;}
+		if (KBus1.ErrStat) {KBus1.ErrStat--;}
 		
 		if (KMRunStat.bLEDFlick)
 		{
@@ -513,6 +815,7 @@
 					if (!KMem.RunStat) SetRunLed(SlowFlicker);
 					else SetRunLed(FastFlicker);
 			}
+			KMem.ErrStat = 0 + KBus1.ErrStat;
 			if (!KMem.ErrStat) 
 			{
 				SetErrLed(0);
@@ -574,15 +877,15 @@
 //		memcpy(&KMem.SDT[64+nSize/2],&KBusChnStats[2],nSize);
 //		for (int i=0;i<128;i++)	{		SDT[i]=i;	}
 //		SDT[48]=55;
-		if (Uart1RecvBuf1DataLen >0 && Uart1Stat.bPacketRecved)
+		if (Uart1RxBuf1DataLen >0 && Uart1Stat.bPacketRecved)
 		{
 			int res1 = -1;
-			res1 = ModBusSlaveParsePkg(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
+			res1 = ModBusSlaveParsePkg(1, Uart1RxBuf1, Uart1RxBuf1DataLen);
 			if (res1 !=0)
 			{
-				KLParsePacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
+				KLParsePacket(1, Uart1RxBuf1, Uart1RxBuf1DataLen);
 			}
-			Uart1RecvBuf1DataLen=0;
+			Uart1RxBuf1DataLen=0;
 			Uart1Stat.bPacketRecved=0;
 			Uart1IdelTimer = 0;
 		}else {

--
Gitblit v1.9.1