From 9aed5d7e7b3c7bf09da712e9c272ece401a7acc9 Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期一, 25 十一月 2024 14:51:23 +0800
Subject: [PATCH] add UltraSonic and MultiWireLess

---
 Radio_LLCC68/Src/main.c |  281 +++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 199 insertions(+), 82 deletions(-)

diff --git a/Radio_LLCC68/Src/main.c b/Radio_LLCC68/Src/main.c
index 6967761..76118a0 100644
--- a/Radio_LLCC68/Src/main.c
+++ b/Radio_LLCC68/Src/main.c
@@ -70,22 +70,45 @@
 /* USER CODE BEGIN PV */
 /* Private variables ---------------------------------------------------------*/
 
-#define RX2BUFSIZE 64
-#define TX2BUFSIZE 64
 
-unsigned char Uart1RxBuf[128];
-unsigned char Uart1TxBuf[260];
 
-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 SlowFlicker=0;
 unsigned char FastFlicker=0;
 
 unsigned int Uart1IdelTimer = 0;
-stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE;
+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;
+
+
+#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 +141,7 @@
 static int Count=0;
 	CurTickuS += 100;	
 	nCurTick++;
-	nSlaveTick++;
+	KBus1.nSlaveTick++;
 	Count++;
 	if (Count>=10000) 
 	{
@@ -132,6 +155,107 @@
 	return;
 }
 
+
+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]++;
+	}
+		if (Uart1RxBuf1DataLen >0 && Uart1Stat.bPacketRecved)
+		{
+			int res1 = -1;
+			if (Uart1RxBuf1DataLen == 3 && Uart1RxBuf1[0]=='+' && Uart1RxBuf1[0]=='+' && Uart1RxBuf1[0]=='+')
+			{
+				Uart1Mode = 0;		// 鍒囨崲鍒板懡浠ゆā寮�
+			}else if (Uart1RxBuf1DataLen == 3 && Uart1RxBuf1[0]=='-' && Uart1RxBuf1[0]=='-' && Uart1RxBuf1[0]=='-')
+			{
+				Uart1Mode = 1;		// 鍒囨崲鍒伴�忎紶妯″紡
+			}else	if (Uart1Mode == 0) {
+					
+				res1 = ModBusSlaveParsePkg(1, Uart1RxBuf1, Uart1RxBuf1DataLen);
+				if (res1 !=0)
+				{
+					KLParsePacket(1, Uart1RxBuf1, Uart1RxBuf1DataLen);
+				}
+
+			}else if (Uart1Mode == 1) {
+				// 閫忎紶妯″紡
+				if (KwRunStat.sizetosend == 0) {
+					memcpy(	KwRunStat.ttTxBuf1,Uart1RxBuf1, Uart1RxBuf1DataLen);
+					KwRunStat.sentsize = 0;
+					KwRunStat.sizesending = 0;
+					KwRunStat.sizetosend = Uart1RxBuf1DataLen;
+				}else {
+					return;
+				}
+				
+				//SendPacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
+			}
+			Uart1RxBuf1DataLen=0;
+			Uart1Stat.bPacketRecved=0;
+			Uart1IdelTimer = 0;			
+		}	
+}
+
+/*
+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 */
 
 /**
@@ -144,8 +268,8 @@
   /* USER CODE BEGIN 1 */
 	KMRunStat.bLEDFlick = 1;
 	
-	InitUartstat(&Uart1Stat,Uart1RxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
-	InitUartstat(&Uart2Stat,Uart2RxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
+	InitUartstat(&Uart1Stat,Uart1RxBuf1,sizeof(Uart1RxBuf1),Uart1TxBuf1,sizeof(Uart1TxBuf1));
+//	InitUartstat(&Uart2Stat,Uart2RxBuf1,sizeof(Uart2RxBuf1),Uart2TxBuf1,sizeof(Uart2TxBuf1));
   /* USER CODE END 1 */
 
   /* MCU Configuration----------------------------------------------------------*/
@@ -155,36 +279,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 +301,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 +325,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 +336,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();
 
@@ -261,9 +378,10 @@
 	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)
 	int res;
 	res = w5500_init();
@@ -314,18 +432,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 +468,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 +484,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 +542,7 @@
 #endif
 
 //		pProgs = (stBinProg1 *) STORE_PRG_BASE;
-
+#if (ENABLE_PLC)
 		if (	KMRunStat.WorkMode==1 ) //&& bKBusMaster)
 		{
 			if (KMRunStat.nBinProgBank == 0){
@@ -426,7 +555,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 +574,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 +590,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 +613,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 +648,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];
@@ -561,26 +689,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)
-		{
-			int res1 = -1;
-			res1 = ModBusSlaveParsePkg(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
-			if (res1 !=0)
-			{
-				KLParsePacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
-			}
-			Uart1RecvBuf1DataLen=0;
-			Uart1Stat.bPacketRecved=0;
-			Uart1IdelTimer = 0;
-		}else {
-			if (Uart1IdelTimer>600000) { // 瓒呰繃60绉掓病鏈夋暟鎹紶杈擄紝閲嶆柊杩涘叆鑷�傚簲娉㈢壒鐜囩姸鎬�
+#if (USART1_AUTO_BAUDRATE == 1)
+			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);
+#endif			
+// if (bKBusSlave)	HAL_Delay(0);
 /*
 		if (!IsEmpty(&Uart1Stat.QRx))
 		{

--
Gitblit v1.9.1