From 842bb64195f958b050867c50db66fc0aa413dafb Mon Sep 17 00:00:00 2001
From: QuakeGod <quakegod@sina.com>
Date: 星期六, 27 七月 2024 10:42:56 +0800
Subject: [PATCH] KBus upgrade

---
 Radio_LLCC68/Src/main.c |  197 ++++++++++++++++++++++++++++++++++---------------
 1 files changed, 137 insertions(+), 60 deletions(-)

diff --git a/Radio_LLCC68/Src/main.c b/Radio_LLCC68/Src/main.c
index 025920a..0c2f3de 100644
--- a/Radio_LLCC68/Src/main.c
+++ b/Radio_LLCC68/Src/main.c
@@ -83,9 +83,14 @@
 unsigned char FastFlicker=0;
 
 unsigned int Uart1IdelTimer = 0;
+#if (ENABLE_PLC)
 stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE;
-
+#endif
 uint32_t us1,us2,us3,us4,us5,us6;
+
+stKBusDef KBus1;							// 
+
+extern 	stDeviceInfo MyDeviceInfo;
 
 /* USER CODE END PV */
 
@@ -118,7 +123,7 @@
 static int Count=0;
 	CurTickuS += 100;	
 	nCurTick++;
-	nSlaveTick++;
+	KBus1.nSlaveTick++;
 	Count++;
 	if (Count>=10000) 
 	{
@@ -131,6 +136,73 @@
 
 	return;
 }
+
+
+void PendSvCallBack()
+{
+#if (BOARD_TYPE == 14)
+///*	
+		if (bSPI1RecvDone)
+		{
+			bSPI1RecvDone=0;
+			FPxParsePkt(SPI1RecvBuf,nSPI1RecvLenInBuf);
+		}
+//*/	
+#endif		
+	if (Uart2Stat.bPacketRecved)
+	{
+		KBusParsePacket(&KBus1, (pKBPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen);		
+		Uart2RecvBuf1DataLen=0;
+		Uart2Stat.bPacketRecved=0;
+		Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));		
+		KMem.WDT[2]++;
+	}
+}
+
+/*
+KBus閫氳鍥炶皟鍑芥暟锛屽綋閫氳鐘舵�佹敼鍙樻垨鏁版嵁鏇存柊鏃惰璋冪敤銆�
+鎴栬�呯郴缁熻姹傛椂銆�
+*/
+void * KBusEvCallBackFunc(void*  pParam, int nEvent, void *pBuf, int nLen1)
+{
+	switch (nEvent){
+		
+		case KBusEvNone:
+			break;
+		case KBusEvCreate:
+			break;
+		case KBusEvConnected:
+			break;
+		case KBusEvDisConnected:
+			break;
+		case KBusEvClosed:
+			break;
+		case KBusEvStateChange:
+			break;
+		case KBusEvTimeSync:
+			break;
+		case KBusEvDataUpdate:
+			KMem.WLX[0] = KBusMem.WLX[0];
+			KMem.WLY[0] = KBusMem.WLY[0];
+		
+			if (KBus1.bMaster) {
+				KMem.WY[0]=KBusMem.WLX[0];			//KPLC with KBus Master
+				KBusMem.WLY[0]=KMem.WX[0];
+			} else if (KBus1.bSlave) {
+				KMem.WYB[0]=KBusMem.WLYB[0];			//WireLess with KBus Slave
+				KBusMem.WLXB[0]=KMem.WXB[0];
+				
+			}
+			break;
+		case KBusEvCmdResponse:
+			break;
+		
+		default:
+			break;
+	}
+	return 0;
+}
+
 
 /* USER CODE END 0 */
 
@@ -155,36 +227,6 @@
 
   /* USER CODE BEGIN Init */
 
-	for (int i=0;i<9;i++)
-	{
-//		memset(KBusChnStats[i],0,0);		
-		KBusChnStats[i].SendPackets=0;
-		KBusChnStats[i].RecvPackets=0;
-		KBusChnStats[i].LostPackets=0;
-		KBusChnStats[i].CtnLstPkts=0;
-		KBusChnStats[i].MaxCtnLstPkts=0;
-		KBusChnStats[i].NotPkgErr=0;
-		KBusChnStats[i].PkgLenErr=0;
-		KBusChnStats[i].TimeOutErr=0;
-		KBusChnStats[i].BCCErr=0;
-		KBusChnStats[i].Delay=0;
-		KBusChnStats[i].MaxDelay=0;
-	}
-	
-		KMem.LastScanTime=0;
-		KMem.ScanTimeuS=0;
-		KMem.MinScanTimeuS=99999;
-		KMem.MaxScanTimeuS=0;
-
-//		KMem.SDD[14]=(unsigned int)&KMStoreSysCfg;
-//		KMem.SDD[15]=(unsigned int)&KMStoreSysCfg1;
-		KMem.SDD[12]=((uint32_t *)UID_BASE)[0];
-//		KMem.SDD[13]=((uint32_t *)UID_BASE)[1];
-//		KMem.SDD[14]=((uint32_t *)UID_BASE)[2];
-		KMem.SDD[13]=PendSvCount;
-		KMem.SDD[14]=RCC->CSR;
-//		KMem.SDD[15]=*(uint32_t *)FLASHSIZE_BASE;
-//		KMem.SDD[16]=(unsigned int)&KMSysCfg;
 	
   /* USER CODE END Init */
 
@@ -207,6 +249,16 @@
 	
 	KMRunStat.bLEDFlick = 1;
 	
+	KLinkInit(1);  	//娉ㄥ唽KLink绔彛
+
+//	stPortDef PortReg1 = {.nPortHardType = 3,.nPortUseType = 2};
+//	KMRegisterPort(0,&PortReg1);
+
+
+	unsigned char bKBusMaster, bKBusSlave, bKBusRepeater;
+	int nKBusStationID;
+	int nKBusChilds;
+	
 	KMem.CurJumperSW=ReadJumperSW();
 	KMem.EffJumperSW=KMem.CurJumperSW;
 	
@@ -221,7 +273,7 @@
 	FP0_Init();
 
 #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
-	nStationID=1 ;//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
@@ -232,10 +284,23 @@
   else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}
 #endif
-	nChilds=nStationID;
-	nCurPollId=1;
+	nKBusChilds=nKBusStationID;
+
+		if (bKBusMaster) {
+		KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusChilds);		
+	}	else if (bKBusSlave) {
+		KBusInitSlave(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusStationID,&MyDeviceInfo);	
+	}		
+	KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc);
+	
+	UNUSED(bKBusRepeater);
+	// 娉ㄥ唽KBus绔彛
+/*	
+	stPortDef PortReg2 = {.nPortHardType = 4,.nPortUseType = 3};
+	KMRegisterPort(1,&PortReg2);
+*/	
 	//if (KMem.EffJumperSW == 0x00)
-		Uart1Baud = DefaultUart1Baud;
+	Uart1Baud = DefaultUart1Baud;
   MX_USART1_UART_Init();
   MX_USART2_UART_Init();
 
@@ -264,6 +329,7 @@
 	Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));	
 	LL_USART_EnableIT_IDLE(USART2);
 	LL_USART_EnableIT_TC(USART2);
+	
 #if (BOARD_TYPE == 13)
 	int res;
 	res = w5500_init();
@@ -314,18 +380,27 @@
 	KMRunStat.WorkMode2=0;
 		
 	KMRunStat.WorkMode = storedKMSysCfg.theKMSysCfg.workmode;
-		
+
+#if (ENABLE_PLC)		
 	if (KMRunStat.WorkMode == 1){
 		InitPLC();
 		KMRunStat.WorkMode2 = KMem.CurJumperSW&0x20 ;
 		if (KMRunStat.WorkMode2) {
 			StartPLC(); }
 	}
-	KMem.WX[7]=0x5a;
+#endif
+	
 #if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+	LoadKwConfig();
+//	KWireLessInit(&KWInitStruct);
+	
 	KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x0f);
 	KWireLessStart();
 #endif
+
+
+	stPortDef PortReg3 = {.nPortHardType = 7,.nPortUseType = 6};
+	KMRegisterPort(2,&PortReg3);
 	
   while (1)
   {
@@ -341,13 +416,14 @@
 		
 		int thisJumperSW=ReadJumperSW();
 		
+#if (ENABLE_PLC)		
 		if (KMRunStat.WorkMode&1){
 			if (thisJumperSW&0x20 && !(KMem.CurJumperSW&0x20))  // Run 寮�鍏� 姝� 璺冲彉銆�
 			{StartPLC();}
 			if (!(thisJumperSW&0x20) && (KMem.CurJumperSW&0x20))  // Run 寮�鍏� 璐� 璺冲彉銆�
 			{StopPLC();}
 		}
-		
+#endif		
 		KMem.CurJumperSW=thisJumperSW;
 		KMem.haltick=haltick;
 //		KMem.TotalRunTime=TotalRunTime;
@@ -356,6 +432,7 @@
 //		*((unsigned int *)&(PLCMem.SDT[2]))=nChilds;
 //		KMem.SDD[13]=PendSvCount;
 //		KMem.SDD[14]=RCC->CSR;		
+		
 		
 		int a;
 		a		= LL_GPIO_ReadInputPort(GPIOA);
@@ -413,7 +490,7 @@
 #endif
 
 //		pProgs = (stBinProg1 *) STORE_PRG_BASE;
-
+#if (ENABLE_PLC)
 		if (	KMRunStat.WorkMode==1 ) //&& bKBusMaster)
 		{
 			if (KMRunStat.nBinProgBank == 0){
@@ -426,7 +503,7 @@
 			
 			ProcessPLCBinProg(pProgs, nSizeProg1);
 		}
-
+#endif // ENABLE_PLC
 		KMem.ScanTimeuS=us2-KMem.LastScanTime;
 		KMem.LastScanTime = us2;
 		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
@@ -445,15 +522,9 @@
 //			BufferOut[1]=KMem.WX[0]&0xff;
 //			BufferOut[2]=(KMem.WX[0]>>8)&0xff;
 #endif
-			if (nChilds>0) {		KBusMasterFunc(2); }
-
-#if (BOARD_TYPE == 14)			
-//			KMem.WX[0]=BufferIn[1]+(BufferIn[2]<<8);
-#else
-//			KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
-#endif
-
 		}
+			KBusLoopProcess(&KBus1);
+			
 			if (haltick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
 			if (haltick&0x00000800) FastFlicker=1;
@@ -467,20 +538,18 @@
 		//	if (! KMem.RunStat) {BufferIn[0]=0;}
 		//	KMem.WY[0]=BufferIn[0];
 #else
-			KBusSlaveFunc(2);	
+			KBusSlaveFunc(&KBus1);	
 			if (! KMem.RunStat) {BufferIn[0]=0;}
 			KMem.WY[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 (KMRunStat.bLEDFlick)
 		{
@@ -492,14 +561,20 @@
 		}
 		else
 		{
+			KMem.ErrStat = KwRunStat.ErrStat; 
+			if ((KMem.EffJumperSW&0x10)==0x10) 		KMem.ErrStat += KBus1.ErrStat;
+#if (ENABLE_PLC)		
 			if (KMRunStat.WorkMode==1 ) {
 				if (PLCMem.bPLCRunning){SetRunLed(SlowFlicker);}
 					else {SetRunLed(0);}
 			}
-			else {
+			else
+#endif // ENABLE_PLC
+			{
 					if (!KMem.RunStat) SetRunLed(SlowFlicker);
 					else SetRunLed(FastFlicker);
 			}
+
 			if (!KMem.ErrStat) 
 			{
 				SetErrLed(0);
@@ -521,11 +596,12 @@
 		us4=GetuS();
 //		EffJumperSW = GetInput(20)&0xff;
 
+		
 #if (BOARD_TYPE == 15 || BOARD_TYPE == 16)		
 		
 		if ((KMem.EffJumperSW&0x10)==0x10) {
-			KMem.WFY[1]=KMem.WLY[0];
-			KMem.WLX[0]=KMem.WFX[1];			
+			KMem.WFY[1]=KBusMem.WLYB[1];
+			KBusMem.WLXB[1]=KMem.WFX[1];
 		}else
 		{
 			KMem.WFY[1]=KMem.WX[0];
@@ -573,14 +649,15 @@
 			Uart1Stat.bPacketRecved=0;
 			Uart1IdelTimer = 0;
 		}else {
-			if (Uart1IdelTimer>600000) {
+			
+			if (Uart1IdelTimer>300000) { // 瓒呰繃60绉掓病鏈夋暟鎹紶杈擄紝閲嶆柊杩涘叆鑷�傚簲娉㈢壒鐜囩姸鎬�
 				LL_USART_EnableAutoBaudRate(USART1);
 				LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
 			}else {
 					Uart1IdelTimer++;
 			}
 		}
- if (bKBusSlave)	HAL_Delay(0);
+// if (bKBusSlave)	HAL_Delay(0);
 /*
 		if (!IsEmpty(&Uart1Stat.QRx))
 		{

--
Gitblit v1.9.1