From 7a2ff321965f1f24f3047fedd44a83f451f1f793 Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期三, 01 二月 2023 10:58:10 +0800
Subject: [PATCH] radio add new device type

---
 Src/main.c |  158 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 92 insertions(+), 66 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 81f5051..fcbdaf3 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -51,12 +51,16 @@
 #include "string.h"
 #include "BSP.h"
 #include "ModbusRTU.h"
-#if (BOARD_TYPE == 12)
-#include "FP0.h"
-#elif (BOARD_TYPE == 13)
+#if (BOARD_TYPE == 13)
 #include "w5500_port.h"
 #include "../src/Ethernet/socket.h"
 #include "../src/Ethernet/loopback.h"
+#elif (BOARD_TYPE == 14)
+#include "FP0.h"
+#elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+#include "KWireless.h"
+//#include "user.h"
+//#include "../src/radio/inc/sx126x-board.h"
 #endif
 
 /* USER CODE END Includes */
@@ -70,15 +74,13 @@
 #define TX2BUFSIZE 128
 
 unsigned char Uart1RxBuf[256];
-unsigned char Uart1TxBuf[512];
+unsigned char Uart1TxBuf[280];
 
 unsigned char Uart2RxBuf[RX2BUFSIZE];
 unsigned char Uart2TxBuf[TX2BUFSIZE];
 
-unsigned int SlowFlicker=0;
-unsigned int FastFlicker=0;
-
-int ContinueSend=0;
+unsigned char SlowFlicker=0;
+unsigned char FastFlicker=0;
 
 stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE;
 
@@ -186,9 +188,9 @@
   SystemClock_Config();
 
   /* USER CODE BEGIN SysInit */
-	TickFreq=10000;		//Tick频率
+	TickFreq=10000;		//Tick棰戠巼
 	InituS(TickFreq);	
- // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq);	//重新定义SysTick的频率�
+ // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq);	//閲嶆柊瀹氫箟SysTick鐨勯鐜囷拷
 
   /* USER CODE END SysInit */
 
@@ -197,21 +199,26 @@
   MX_DMA_Init();
 	
 	KMachineInit();
-	ReadSysCfgFromFlash(&KMSysCfg);
+	ReadSysCfgFromFlash(&storedKMSysCfg);
 	
 	KMem.EffJumperSW=ReadJumperSW();
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 	KMem.EffJumperSW|=0x10;
 	nAddr=KMem.EffJumperSW&0x0f;
-  if ((KMem.EffJumperSW&0x10)!=0) {bMaster=1;bSlave=0;}
-	else{bMaster=0;bSlave=1;}
+  if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	else{bKBusMaster=0;bKBusSlave=1;}
 	nChilds=nAddr;
 	FP0_Init();
+#elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+	nAddr=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
 	nAddr=KMem.EffJumperSW&0x7;
-	if (KMem.EffJumperSW == 0x0f) {bRepeater=1;bMaster=1;bSlave=0;}
-  else if ((KMem.EffJumperSW&0x08)!=0) {bMaster=1;bSlave=0;}
-	else{bMaster=0;bSlave=1;}
+	if (KMem.EffJumperSW == 0x0f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
+  else if ((KMem.EffJumperSW&0x08)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	else{bKBusMaster=0;bKBusSlave=1;}
 #endif
 	nChilds=nAddr;
 	nCurPollId=1;
@@ -223,7 +230,7 @@
 	MX_SPI1_Init();
 	LL_SPI_EnableIT_RXNE(SPI1);
 
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 //	MX_SPI2_Init();
 //  MX_ADC_Init();
 #else
@@ -255,7 +262,7 @@
 	
 //	res = listen(0);
 #endif	
-//	if (bSlave)
+//	if (bKBusSlave)
 	{
 	//	LL_USART_EnableAutoBaudRate(USART1);
 	//	LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);		
@@ -273,7 +280,7 @@
 	SetRunLed(1);				//Turn On Run Led
 	SetErrLed(0);				//Turn Off Err Led
 
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 //	PutOutput (0);					//Clear all Output
 //	Enable595(1);						//Enable 595 Output 
 #else
@@ -282,7 +289,7 @@
 #endif
 
 		if (GetBoardType() == 7 || GetBoardType() ==8 
-			|| GetBoardType() == 9 || GetBoardType() ==10 ) 
+			|| GetBoardType() == 9 || GetBoardType() ==10 ||GetBoardType() ==13 ||GetBoardType() ==15 || BOARD_TYPE == 16) 
 		{
 			displayInput(0xffff);		//
 			EnableDisIn(1);				//Input Diaplay Enable 595 
@@ -297,14 +304,16 @@
 		StartPLC();
 	}
 	KMem.WX[7]=0x5a;
+#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+	KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x1f);
+	KWireLessStart();
+#endif
 
   while (1)
   {
 		//int MyKeyStat1,MyKeyStat2;
 		//MyKeyStat1=GetInput();
-#if (BOARD_TYPE == 12)
-		FP0_Proc();
-#endif
+
 		//*((unsigned int *)&(PLCMem.SDT[10]))=nRunCount;
 	//	KMem.nRunCount=nRunCount;
 		SlowFlicker=0;
@@ -322,42 +331,20 @@
 //		KMem.SDD[13]=PendSvCount;
 //		KMem.SDD[14]=RCC->CSR;		
 
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 //		KMem.WX[0]= GetInput();		
+		FP0_Proc();
 #else
 		KMem.WX[0]= GetInput();		
 #endif
 		
 		if (GetBoardType() == 7 || GetBoardType() ==8 
-			|| GetBoardType() == 9 || GetBoardType() ==10 ) 
+			|| GetBoardType() == 9 || GetBoardType() ==10 || GetBoardType() ==15 || GetBoardType() ==16) 
 		{
 			displayInput(KMem.WX[0]);
 		}
 		us2=GetuS();
-		
-		
-//		pProgs = (stBinProg1 *) STORE_PRG_BASE;
-
-		if (	KMRunStat.WorkMode==1)
-		{
-			if (KMRunStat.nBinProgBank == 0){
-				pProgs=(stBinProg1 *)STORE_PRG_BASE;
-			}else {
-				pProgs=(stBinProg1 *)ALT_PRG_BASE;
-			}
-			nSizeProg1=KMRunStat.nBinProgSize;
-			
-			ProcessPLCBinProg(pProgs, nSizeProg1);
-		}
-
-		KMem.ScanTimeuS=us2-KMem.LastScanTime;
-		KMem.LastScanTime = us2;
-		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
-		if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
-//		if (repeater)		{	RepeaterFunc();	}
-
-		us3=GetuS();
-
+///*
 		if ((KMem.nRunCount &0x1f) == 0x02)
 		{
 			ADCProcess();
@@ -379,19 +366,47 @@
 				}
 			}
 		}
+//*/
 
-		if (bMaster)		
+#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+		Radio.IrqProcess( ); // Process Radio IRQ
+#endif
+
+//		pProgs = (stBinProg1 *) STORE_PRG_BASE;
+
+		if (	KMRunStat.WorkMode==1 && bKBusMaster)
 		{
-#if (BOARD_TYPE == 12)
+			if (KMRunStat.nBinProgBank == 0){
+				pProgs=(stBinProg1 *)STORE_PRG_BASE;
+			}else {
+				pProgs=(stBinProg1 *)ALT_PRG_BASE;
+			}
+			nSizeProg1=KMRunStat.nBinProgSize;
+			
+			ProcessPLCBinProg(pProgs, nSizeProg1);
+		}
+
+		KMem.ScanTimeuS=us2-KMem.LastScanTime;
+		KMem.LastScanTime = us2;
+		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
+		if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
+
+		//		if (bKBusRepeater)		{	KBusRepeaterFunc();	}
+
+		us3=GetuS();
+
+		if (bKBusMaster)		
+		{
+#if (BOARD_TYPE == 14)
 			for (int i=0;i<nOutputBytes;i++)
 			{BufferOut[i+1]=KMem.WYB[i];}
 #else
 //			BufferOut[1]=KMem.WX[0]&0xff;
 //			BufferOut[2]=(KMem.WX[0]>>8)&0xff;
 #endif
-			if (nChilds>0) {		MasterFunc(2); }
+			if (nChilds>0) {		KBusMasterFunc(2); }
 
-#if (BOARD_TYPE == 12)			
+#if (BOARD_TYPE == 14)			
 //			KMem.WX[0]=BufferIn[1]+(BufferIn[2]<<8);
 #else
 //			KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
@@ -403,13 +418,18 @@
 			if (haltick&0x00000800) FastFlicker=1;
 			else FastFlicker=0;	
 
-		if (bSlave)		
+		if (bKBusSlave)		
 		{
 //			BufferOut[0]=KMem.WX[0];
-			SlaveFunc(2);	
+#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+//			KBusSlaveFunc(2);	
+		//	if (! KMem.RunStat) {BufferIn[0]=0;}
+		//	KMem.WY[0]=BufferIn[0];
+#else
+			KBusSlaveFunc(2);	
 			if (! KMem.RunStat) {BufferIn[0]=0;}
 			KMem.WY[0]=BufferIn[0];
-			
+#endif
 			if (nSlaveTick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
 			if (nSlaveTick&0x00000800) FastFlicker=1;
@@ -442,17 +462,21 @@
 		us4=GetuS();
 //		EffJumperSW = GetInput(20)&0xff;
 
-#if (BOARD_TYPE == 12)
-		PutOutput (KMem.WY[0]);
-#else
+#if (BOARD_TYPE == 14)
 //		PutOutput (KMem.WY[0]);
+#else
+		PutOutput (KMem.WY[0]);
 #endif
 		//PutOutput (KMem.nRunCount>>8);
 		//PutOutput(0x0f70);
-		
+#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)		
+	//	KMem.WY[1]=KMem.nRunCount>>6;
+		KMem.WY[1]=KMem.WX[0];
+		KMem.WY[0]=KMem.WX[1];
+#endif
 		us5=GetuS();
-//		if (bMaster) ShowInfo();
-//		if (bSlave) ShowInfo();
+//		if (bKBusMaster) ShowInfo();
+//		if (bKBusSlave) ShowInfo();
 		us6=GetuS();
 		add1(10,10);
 		for (int i=0;i<64;i++)
@@ -476,7 +500,7 @@
 			Uart1RecvBuf1DataLen=0;
 			Uart1Stat.bPacketRecved=0;
 		}
- if (bSlave)	HAL_Delay(0);
+ if (bKBusSlave)	HAL_Delay(0);
 /*
 		if (!IsEmpty(&Uart1Stat.QRx))
 		{
@@ -488,7 +512,7 @@
 		}
 */
 
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 		//process 6 output
 		{
 				// mapping bits.
@@ -509,6 +533,7 @@
 		}
 #endif
 
+/*		
 		{
 		unsigned char pos,seg;
 		unsigned short val;
@@ -527,9 +552,10 @@
 		seg=~seg;
 		//	PutOutputSPI1(pos|(seg<<8));
 	}
+*/
+
 #if (BOARD_TYPE == 13)	
 	 w5500_network_info_show();
-	
 //	 loopback_tcps(0,str1,5000);
 #endif
 

--
Gitblit v1.9.1