From 0fe6b074f3f3994d87af195f37e349a83e27882c Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期六, 10 十二月 2022 16:55:40 +0800
Subject: [PATCH] FP0 fix utf8 char

---
 Src/main.c |  739 +++++++++++++++++++++-----------------------------------
 1 files changed, 281 insertions(+), 458 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 90bd4e5..c1a7ec3 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -42,6 +42,7 @@
 
 /* USER CODE BEGIN Includes */
 #include "Globaldef.h"
+#include "debug.h"
 #include "Functions.h"
 #include "KMachine.h"
 #include "PLCfunctions.h"
@@ -49,6 +50,18 @@
 #include "KLink.h"
 #include "string.h"
 #include "BSP.h"
+#include "ModbusRTU.h"
+#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)
+#include "KWireless.h"
+//#include "user.h"
+//#include "../src/radio/inc/sx126x-board.h"
+#endif
 
 /* USER CODE END Includes */
 
@@ -57,50 +70,21 @@
 /* USER CODE BEGIN PV */
 /* Private variables ---------------------------------------------------------*/
 
-#define RXBUFSIZE 128
-#define TXBUFSIZE 128
+#define RX2BUFSIZE 64
+#define TX2BUFSIZE 64
 
-unsigned char Uart1RxBuf[256];
-unsigned char Uart1TxBuf[512];
+unsigned char Uart1RxBuf[128];
+unsigned char Uart1TxBuf[260];
 
-unsigned char Uart2RxBuf[RXBUFSIZE];
-unsigned char Uart2TxBuf[TXBUFSIZE];
+unsigned char Uart2RxBuf[RX2BUFSIZE];
+unsigned char Uart2TxBuf[TX2BUFSIZE];
 
-//unsigned char buf1[128];	
+unsigned char SlowFlicker=0;
+unsigned char FastFlicker=0;
 
-unsigned char Datas[128];		
-
-unsigned int SlowFlicker=0;
-unsigned int FastFlicker=0;
-
-volatile int PacketLength = 0;
-
-char str1[256];
-
-int TimeOutCount=0;
-int Clk3=0;
-int ContinueSend=0;
-int Uart1baudval=0;	
-int Uart2baudval=0;	
-
-int repeater=0;
-
-int SendTime,Latancy,LatancyClk,SendClk;	
-
-int LineCount=0;
-
-int LastCircleStartTime=0;
-int CircleTime=0;
-
-//volatile unsigned int nRunCount=0;
-volatile int nCount2=0;
+stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE;
 
 uint32_t us1,us2,us3,us4,us5,us6;
-const unsigned char buf1[16]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00};
-
-volatile int PowerDownEvent=0;
-volatile int OldPowerDownEvent=0;
-volatile int OldPowerDownEventTime=0;
 
 /* USER CODE END PV */
 
@@ -110,14 +94,15 @@
 /* USER CODE BEGIN PFP */
 /* Private function prototypes -----------------------------------------------*/
 
+const unsigned char LEDSEGTAB[]={
+0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,	//0-F
+0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1,  //0.-F.
+0x00,0x40,			//  ,-,_,~,o,n,N,<,>,J,r,
+};
+
 /* USER CODE END PFP */
 
 /* USER CODE BEGIN 0 */
-__asm int add1(int a,int b)
-{
-	add r0,r1,r0
-	BLX    lr
-}
 
 int HexToInt(char ch)
 {
@@ -139,348 +124,11 @@
 		Count=0; 
 		KMem.CurTimeSec++;
 		KMem.ThisRunTime++; KMem.TotalRunTime++;
+		if (KMRunStat.bLEDFlick) KMRunStat.bLEDFlick--;
+		if (KMRunStat.bLEDFlick >120) KMRunStat.bLEDFlick=120;
 	}
 
 	return;
-}
-
-int FormatHex(char * buf1,uchar * data,int n)
-{
-	int len1=0;
-		for (int i=0;i<n;i++)
-		{len1+=sprintf(buf1+len1,"%02X ",data[i]);}
-		len1+=sprintf(buf1+len1,"\r\n");	
-	return len1;
-}
-
-int RepeaterFunc()
-{
-		KMem.WY[0]=KMem.WX[0];
-		if ((KMem.nRunCount &0x7f) == 88) 
-		{ 
-			nCount2++;	
-			ToggleRunLed();
-//		int len1=sprintf(str1,"%d %d Cfg %02X  Input %02X  \r\n",nCount,nCount2,EffJumperSW,MyKeyStat1);
-//		PutStr(str1,len1);
-		}				
-	return 0;
-}
-
-
-int ShowInitInfo()
-{
-	int len1=0;
-	clearscreen();
-//	Locate(1,1);
-
-/*	
-	LoadFlashDatas();
-	
-	LoadAndUpdateStoreCfg();
-	
-	HAL_StatusTypeDef res;	
-	stStoreCfg * pFCfg = (stStoreCfg *) GetCurStoreCfgAddr();
-	stStoreCfg * pFCfg2 = GetNextStoreCfgAddr(pFCfg);	
-
-int t11=GetuS();
-	
-	for (int i=0;i<20;i++)
-	{
-		tims[i]=GetuS();
-	}		
-	clearscreen();
-	len1+=sprintf(str1+len1," Ver 001 \r\n");
-	len1+=sprintf(str1+len1," Uart1Baud %d Uart2Baud %d  UID  %08x %08x %08x \r\n",Uart1Baud,Uart2Baud,pUID[0],pUID[1],pUID[2]);
-	len1+=sprintf(str1+len1," Flash = %d %d %d %d  res = %d   ",FlashDatas[0],FlashDatas[1],FlashDatas[2],FlashDatas[3],res);
-	len1+=sprintf(str1+len1,"flash operation = %u %u %u\r\n",t11-t10,t10,t11);
-	PutStr(str1,len1);
-	len1=0;
-	len1+=sprintf(str1+len1,"%08X  %X %X , PowerOn %X  UpTime %X %X %X %X \r\n",
-	(uint32_t)pFCfg,pFCfg[0].Sign1,pFCfg[0].SN1,pFCfg[0].PowerCount,pFCfg[0].UpTime,pFCfg[0].UserData1,pFCfg[0].CRC1,pFCfg[0].EndSign1);
-	len1+=sprintf(str1+len1,"%08X  %X %X , PowerOn %X  UpTime %X %X %X %X \r\n",
-	(uint32_t)pFCfg2,Cfg2.Sign1,Cfg2.SN1,Cfg2.PowerCount,Cfg2.UpTime,Cfg2.UserData1,Cfg2.CRC1,Cfg2.EndSign1);
-	PutStr(str1,len1);
-*/	
-	len1=0;
-/*	
-	for (int i=0;i<8;i++)
-	{
-		len1=0;
-		len1+=sprintf(str1+len1,"%02X:",i*32);
-		for (int j=0;j<8;j++)
-		{
-				len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
-		}
-				len1+=sprintf(str1+len1,"  %02X",pFlash1[i*32+8]);
-		for (int j=9;j<16;j++)
-		{
-				len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
-		}
-				len1+=sprintf(str1+len1," | %02X",pFlash1[i*32+16]);
-		for (int j=17;j<24;j++)
-		{
-				len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
-		}
-				len1+=sprintf(str1+len1,"  %02X",pFlash1[i*32+24]);
-		for (int j=25;j<32;j++)
-		{
-				len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
-		}
-		len1+=sprintf(str1+len1,"\r\n");
-		PutStr(str1,len1);
-	}
-*/	
-	us1=GetuS();
-	int crc1 = crc_check(buf1,16);
-	us2=GetuS();
-	int crc2 = crc16bitbybit(buf1,16);
-	us3=GetuS();
-	int crc3 = crc16table(buf1, 16);
-	us4=GetuS();
-  int crc4 = crc16tablefast(buf1, 16);
-	us5=GetuS();
-	LL_CRC_ResetCRCCalculationUnit(CRC);
-	LL_CRC_SetInitialData(CRC,0xFFFFFFFF);
-	LL_CRC_SetInitialData(CRC,0xA001);
-	for (int i=0;i<16;i++)
-	{
-		LL_CRC_FeedData8(CRC,buf1[i]);
-	}
-	int crc5 = LL_CRC_ReadData32(CRC);
-	us6=GetuS();
-	
-		len1+=sprintf(str1+len1,"CRC  %04X  %04X  %04X  %04X  %04X\r\n",crc1,crc2,crc3,crc4,crc5);
-		len1+=sprintf(str1+len1,"time %04d  %04d  %04d  %04d  %04d\r\n",us2-us1,us3-us2,us4-us3,us5-us4,us6-us5);
-		
-		PutStr(str1,len1);
-
-	InitTimer(0,0);
-	InitTimer(1,1);
-	InitTimer(2,2);
-	InitTimer(3,3);
-	
-	StartTimer(0,1000);
-//	StartTimer(2,1000);
-	Locate(13,1);LineCount=3;
-	return 0;
-}
-int sprintftime = 0;
-int putstrtime = 0;
-
-int ADCProcess()
-{
-					uint16_t ADC_ConvertedValue=0;
-static int CurChannel=LL_ADC_CHANNEL_0;
-//static int waitcount = 0;
-				if (!LL_ADC_REG_IsConversionOngoing(ADC1))
-				{
-					//waitcount++;
-					//if (waitcount<2) return 0;
-					//waitcount=0;
-					ADC_ConvertedValue = LL_ADC_REG_ReadConversionData12(ADC1);
-					
-		//			ADC_RegularChannelConfig(LL_ADC_CHANNEL_17,);
-					int channels = CurChannel ;//LL_ADC_REG_GetSequencerChannels(ADC1);
-					int nextchannel = LL_ADC_CHANNEL_0;
-					if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
-					{
-						KMem.ADCValues[0] = ADC_ConvertedValue;
-						nextchannel = LL_ADC_CHANNEL_7;
-					}else if ((channels & LL_ADC_CHANNEL_7) == LL_ADC_CHANNEL_7)
-					{
-						KMem.ADCValues[2] = ADC_ConvertedValue;						
-						nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
-						if (KMem.ADCValues[2] < 2200) 
-						{
-							PowerDownEvent=1;
-						}else 
-						{
-							PowerDownEvent=0;
-						}
-					}else if ((channels & LL_ADC_CHANNEL_16) == LL_ADC_CHANNEL_16)
-					{
-						KMem.ADCValues[6] = ADC_ConvertedValue;		
-							nextchannel = LL_ADC_CHANNEL_VREFINT;
-					}else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
-					{
-						KMem.ADCValues[7] = ADC_ConvertedValue;						
-						nextchannel = LL_ADC_CHANNEL_0;
-					}else
-					{
-						//ADCValues[0] = ADC_ConvertedValue;						
-					}
-					//nextchannel = LL_ADC_CHANNEL_VREFINT;
-					LL_ADC_REG_SetSequencerChannels(ADC1,nextchannel);					
-					LL_ADC_REG_StartConversion(ADC1);
-					CurChannel = nextchannel;
-				}	
-	return 0;
-}
-
-int PowerDownProcess(void )
-{
-	AddEventLog(KMem.CurTimeSec,EventTypePowerDown,1,12345);
-	SaveRunStat(&KMRunStat);
-	KMem.PwrFailCount++;
-	KMem.LastPwrFailTime = KMem.CurTimeSec;
-	return 0;
-}
-
-int PowerRecoverProcess(void)
-{
-	KMem.PwrFailCount++;
-	
-	return 0;
-}
-int ShowRunningInfo()
-{
-			int Clk1=SysTick->VAL;			
-			if (Uart1BaudFirstGot)
-			{
-				Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;
-				Uart1BaudFirstGot=0;
-			}
-			if (Uart2BaudFirstGot)
-			{
-				Uart2baudval = HAL_RCC_GetPCLK1Freq() / USART2->BRR;
-				Uart2BaudFirstGot=0;
-			}		
-			int Reload=SysTick->LOAD;
-
-			int Clk2=SysTick->VAL;
-			//int us2=GetuS();
-			int haltick=HAL_GetTick();
-			int len1=0;
-			uint32_t theUs = GetuS();
-			int nRunCount2=KMem.nRunCount;
-			if (!Uart1Stat.QTx.bEmpty) return 0;
-			
-			if ( (nRunCount2 & 0xff) == 0x03)
-			{
-				Locate(13,1);LineCount=3;
-			} else	if ((nRunCount2 & 0xff) == 0x0f)
-			{
-				int timeus1;
-				int timeus2;
-		
-				len1=sprintf((char *)str1," N %8d Tk %8d %9u CFG %02X R %d M %d S %d %4d IN %04X OUT %04X  \r\n",
-					KMem.nRunCount, haltick, theUs, KMem.EffJumperSW, repeater, bMaster, bSlave, Clk2, KMem.WX[0],KMem.WY[0]);
-				//len1=sprintf((char *)str1,"U%02X%02XA",x2,x2);
-				// Locate(10,1);
-				timeus1=GetuS();
-				PutStr(str1,len1);
-				timeus2=GetuS();
-				sprintftime = timeus1 - theUs;
-				putstrtime = timeus2 - timeus1;
-				if (IsTimerOn(0)) {StartTimer(1,1000);StopTimer(3);}
-				if (IsTimerOn(1)) {StartTimer(2,100);StopTimer(0);}
-				if (IsTimerOn(2)) {StartTimer(3,10);StopTimer(1);}
-				if (IsTimerOn(3)) {StartTimer(0,10000);StopTimer(2);}
-		  }
-			if ((nRunCount2 & 0xff) == 0x2f && 0)
-			{
-
-		  }	
-			if ((nRunCount2 & 0xff) == 0x0af)
-			{
-				
-		  }				
-			return 0;
-}
-
-int MasterFunc()
-{
-		uint32_t tick1=HAL_GetTick();
-		uint32_t thisuS=GetuS();
-	
-			int len1=0;
-
-			if ((MasterRecved && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1500u)
-			{
-				if (!MasterRecved) 
-				{
-					TimeOutCount++;
-					Uart2Stat.TimeOutErr++; 
-					ChnStats[nCurPollId].LostPackets++;
-					ChnStats[nCurPollId].CtnLstPkts++;
-					if (ChnStats[nCurPollId].CtnLstPkts>ChnStats[nCurPollId].MaxCtnLstPkts)
-					{ChnStats[nCurPollId].MaxCtnLstPkts=ChnStats[nCurPollId].CtnLstPkts;}
-					if (ChnStats[nCurPollId].CtnLstPkts>3)
-					{
-						ChnStats[nCurPollId].Stat=0;
-						KMem.ErrStat=200;
-						
-						{BufferIn[nCurPollId]=0;}
-					}
-				//	LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
-				}else
-				{
-					ChnStats[nCurPollId].Stat=1;
-					
-					KMem.RunStat=100;
-				}
-				nCurPollId ++;
-				if (nCurPollId > nChilds)
-				{
-					CircleTime=thisuS-LastCircleStartTime;
-					LastCircleStartTime=thisuS;
-					nSeq++;
-					nCurPollId=1;
-				}				
-				Datas[0]=BufferOut[nCurPollId];
-				Datas[1]=BufferOut[nCurPollId+1];;
-				Datas[2]=ChnStats[nCurPollId].Stat;
-				Datas[3]=0;
-				Datas[4]=tick1&0xff;
-				Datas[5]=(tick1>>8)&0xff;
-				Datas[6]=(tick1>>16)&0xff;
-				Datas[7]=(tick1>>24)&0xff;
-				
-				SendTimeuS=thisuS;				
-				len1=MakePacket((pPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
-				SendPacket2((pPacket)PacketBuf1,len1);
-				ChnStats[nCurPollId].SendPackets++;
-				ChnStats[nCurPollId].SendTimeInterval=SendTimeuS-ChnStats[nCurPollId].LastSentTimeuS;
-				ChnStats[nCurPollId].LastSentTimeuS=SendTimeuS;
-				PacketLength = len1;
-				SendTime=tick1;
-
-				MasterRecved=0;
-			//	LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);		
-				//ToggleErrLed();
-//				ToggleOut8();
-
-			}
-		
-			Clk3=SysTick->VAL;
-		//	LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_4);
-	//	HAL_Delay(1);				
-	return 0;
-}
-
-int SlaveFunc()
-{
-		int ThisuS=GetuS();
-		int thisRecvTime=RecvTimeuS;
-		if (SlaveRecved)
-		{
-			KMem.RunStat=100;
-			SlaveRecved=0;
-		}else if ((ThisuS - thisRecvTime) >12000u)
-		{
-			KMem.ErrStat=200;
-			KMem.SDD[17]=1;
-			KMem.SDD[18]=ThisuS;
-			KMem.SDD[19]=RecvTimeuS;
-		}else if ( ThisuS > (thisRecvTime + 12000u))
-		{
-			KMem.ErrStat=200;
-			KMem.SDD[17]=2;
-			KMem.SDD[18]=ThisuS;
-			KMem.SDD[19]=RecvTimeuS;
-		}
-		
-	return 0;
 }
 
 /* USER CODE END 0 */
@@ -493,9 +141,10 @@
 int main(void)
 {
   /* USER CODE BEGIN 1 */
-
-	InitUartstat(&Uart1Stat,Uart1TxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
-	InitUartstat(&Uart2Stat,Uart2TxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
+	KMRunStat.bLEDFlick = 1;
+	
+	InitUartstat(&Uart1Stat,Uart1RxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
+	InitUartstat(&Uart2Stat,Uart2RxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
   /* USER CODE END 1 */
 
   /* MCU Configuration----------------------------------------------------------*/
@@ -505,20 +154,20 @@
 
   /* USER CODE BEGIN Init */
 
-	for (int i=0;i<16;i++)
+	for (int i=0;i<9;i++)
 	{
-//		memset(ChnStats[i],0,0);		
-		ChnStats[i].SendPackets=0;
-		ChnStats[i].RecvPackets=0;
-		ChnStats[i].LostPackets=0;
-		ChnStats[i].CtnLstPkts=0;
-		ChnStats[i].MaxCtnLstPkts=0;
-		ChnStats[i].NotPkgErr=0;
-		ChnStats[i].PkgLenErr=0;
-		ChnStats[i].TimeOutErr=0;
-		ChnStats[i].BCCErr=0;
-		ChnStats[i].Delay=0;
-		ChnStats[i].MaxDelay=0;
+//		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;
@@ -542,9 +191,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 */
 
@@ -553,33 +202,72 @@
   MX_DMA_Init();
 	
 	KMachineInit();
-	ReadSysCfgFromFlash(&KMSysCfg);
+	ReadSysCfgFromFlash(&storedKMSysCfg);
+	
+	KMRunStat.bLEDFlick = 1;
 	
 	KMem.EffJumperSW=ReadJumperSW();
-	nAddr=KMem.EffJumperSW&0x7;
-	if (KMem.EffJumperSW == 0x0f) {repeater=1;bMaster=1;bSlave=0;}
-  else if ((KMem.EffJumperSW&0x08)!=0) {bMaster=1;bSlave=0;}
-	else{bMaster=0;bSlave=1;}	
-	nChilds=nAddr;
+#if (BOARD_TYPE == 14)
+	KMem.EffJumperSW|=0x10;
+	nStationID=KMem.EffJumperSW&0x0f;
+  if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	else{bKBusMaster=0;bKBusSlave=1;}
+	nChilds=nStationID;
+	FP0_Init();
+#elif (BOARD_TYPE == 15)
+	nStationID=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
+	nStationID=KMem.EffJumperSW&0x7;
+	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=nStationID;
 	nCurPollId=1;
-	if (KMem.EffJumperSW == 0x00) Uart1Baud = DefaultUart1Baud;
+	//if (KMem.EffJumperSW == 0x00)
+		Uart1Baud = DefaultUart1Baud;
   MX_USART1_UART_Init();
   MX_USART2_UART_Init();
-//	MX_SPI1_Init();
+
+	MX_SPI1_Init();
+	LL_SPI_EnableIT_RXNE(SPI1);
+
+#if (BOARD_TYPE == 14)
+//	MX_SPI2_Init();
+//  MX_ADC_Init();
+#else
 	MX_SPI2_Init();
   MX_ADC_Init();
+#endif
+
 	MX_IWDG_Init();
+
+	MX_TIM6_Init();
+	LL_TIM_EnableCounter(TIM6);
 	
   /* USER CODE BEGIN 2 */
 	LL_USART_EnableIT_RXNE(USART1);
 	LL_USART_EnableIT_IDLE(USART1);
 	LL_USART_EnableIT_TC(USART1);
 
-	LL_USART_EnableIT_RXNE(USART2);
+//	LL_USART_EnableIT_RXNE(USART2);
+	Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));	
 	LL_USART_EnableIT_IDLE(USART2);
 	LL_USART_EnableIT_TC(USART2);
+#if (BOARD_TYPE == 13)
+	int res;
+	res = w5500_init();
+	KMem.SDD[28]=res;
 	
-//	if (bSlave)
+//	res=socket(0,Sn_MR_TCP,5000,0);
+	KMem.SDD[29]=res;	
+	
+//	res = listen(0);
+#endif	
+//	if (bKBusSlave)
 	{
 	//	LL_USART_EnableAutoBaudRate(USART1);
 	//	LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);		
@@ -596,22 +284,39 @@
 	HAL_Delay(10);				
 	SetRunLed(1);				//Turn On Run Led
 	SetErrLed(0);				//Turn Off Err Led
+
+#if (BOARD_TYPE == 14)
+//	PutOutput (0);					//Clear all Output
+//	Enable595(1);						//Enable 595 Output 
+#else
 	PutOutput (0);					//Clear all Output
 	Enable595(1);						//Enable 595 Output 
+#endif
+
 		if (GetBoardType() == 7 || GetBoardType() ==8 
-			|| GetBoardType() == 9 || GetBoardType() ==10 ) 
+			|| GetBoardType() == 9 || GetBoardType() ==10 ||GetBoardType() ==13 ||GetBoardType() ==15 ) 
 		{
 			displayInput(0xffff);		//
 			EnableDisIn(1);				//Input Diaplay Enable 595 
 		}
 	SetOutStat(0);			//OK Good, signal
-//	ShowInitInfo();
+	ShowInitInfo();
 	KMem.LastScanTime = GetuS();
+
+	KMRunStat.WorkMode=0;
+	if (KMRunStat.WorkMode == 1){
+		InitPLC();
+		StartPLC();
+	}
+	KMem.WX[7]=0x5a;
+#if (BOARD_TYPE == 15)
+	KWireLessInit(KMem.EffJumperSW&0x20);
+	KWireLessStart();
+#endif
   while (1)
   {
 		//int MyKeyStat1,MyKeyStat2;
 		//MyKeyStat1=GetInput();
-
 
 		//*((unsigned int *)&(PLCMem.SDT[10]))=nRunCount;
 	//	KMem.nRunCount=nRunCount;
@@ -620,30 +325,30 @@
 		us1=GetuS();
 		int haltick=HAL_GetTick();
 		
-		int CurJumperSW=ReadJumperSW();		
-		KMem.CurJumperSW=CurJumperSW;
+//		int CurJumperSW=ReadJumperSW();		
+//		KMem.CurJumperSW=CurJumperSW;
 		KMem.haltick=haltick;
 //		KMem.TotalRunTime=TotalRunTime;
 //		KMem.ThisRunTime=ThisRunTime;		
 		
 //		*((unsigned int *)&(PLCMem.SDT[2]))=nChilds;
-		
-		KMem.WX[0]= GetInput();
+//		KMem.SDD[13]=PendSvCount;
+//		KMem.SDD[14]=RCC->CSR;		
+
+#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) 
 		{
 			displayInput(KMem.WX[0]);
 		}
 		us2=GetuS();
-
-//		ProcessPLCPROG(prog1, nSizeProg1);
-//		ScanTimeuS=us2-LastScanTime;
-//		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();
@@ -665,50 +370,107 @@
 				}
 			}
 		}
-		if (bMaster)		
-		{
-			BufferOut[1]=KMem.WX[0]&0xff;
-			BufferOut[2]=(KMem.WX[0]>>8)&0xff;
-			MasterFunc();
-			
-			KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
+//*/
 
+#if (BOARD_TYPE == 15)
+		Radio.IrqProcess( ); // Process Radio IRQ
+#endif
+
+//		pProgs = (stBinProg1 *) STORE_PRG_BASE;
+
+		if (	KMRunStat.WorkMode==1 && bKBusMaster)
+		{
+			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) {		KBusMasterFunc(2); }
+
+#if (BOARD_TYPE == 14)			
+//			KMem.WX[0]=BufferIn[1]+(BufferIn[2]<<8);
+#else
+//			KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
+#endif
+
+		}
 			if (haltick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
 			if (haltick&0x00000800) FastFlicker=1;
 			else FastFlicker=0;	
-			
-		}
-		if (bSlave)		
+
+		if (bKBusSlave)		
 		{
-			BufferOut[0]=KMem.WX[0];
-			SlaveFunc();	
+//			BufferOut[0]=KMem.WX[0];
+#if (BOARD_TYPE == 15)
+//			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;
 			else FastFlicker=0;			
 
 		}
+
 //		KMem.WY[0]=nCount2>>5;
 		if (KMem.RunStat) {KMem.RunStat--;}
 		if (KMem.ErrStat) {KMem.ErrStat--;}
 		
-		if (!KMem.RunStat) SetRunLed(SlowFlicker);
-		else SetRunLed(FastFlicker);
-		
-		if (!KMem.ErrStat) 
+		if (KMRunStat.bLEDFlick)
 		{
-			SetErrLed(0);
-			SetOutStat(1);
-		}
-		else 
-		{
+			SetRunLed(FastFlicker);
 			SetErrLed(FastFlicker);
-			SetOutStat(0);
+			SetErr2Led(FastFlicker);
+			SetOutStat(!FastFlicker);
+			//KMRunStat.bLEDFlick-- ;
+		}
+		else
+		{
+			if (!KMem.RunStat) SetRunLed(SlowFlicker);
+			else SetRunLed(FastFlicker);
 			
+			if (!KMem.ErrStat) 
+			{
+				SetErrLed(0);
+				SetErr2Led(0);
+				SetOutStat(1);
+			}
+			else 
+			{
+				SetErrLed(FastFlicker);
+				SetErr2Led(FastFlicker);
+				SetOutStat(0);
+				
+			}
 		}
 		
 //		SetRunLed(RunStat);
@@ -717,13 +479,21 @@
 		us4=GetuS();
 //		EffJumperSW = GetInput(20)&0xff;
 
+#if (BOARD_TYPE == 14)
+//		PutOutput (KMem.WY[0]);
+#else
 		PutOutput (KMem.WY[0]);
+#endif
 		//PutOutput (KMem.nRunCount>>8);
 		//PutOutput(0x0f70);
-		
+#if (BOARD_TYPE == 15)		
+	//	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++)
@@ -731,18 +501,23 @@
 //			ProcessTimer(i);
 		}
 		KMem.nRunCount++;
-		int nSize=sizeof(stChnStat);
-		memcpy(&KMem.SDT[64],&ChnStats[1],nSize);
-		memcpy(&KMem.SDT[64+nSize/2],&ChnStats[2],nSize);
+//		int nSize=sizeof(stChnStat);
+//		memcpy(&KMem.SDT[64],&KBusChnStats[1],nSize);
+//		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)
 		{
-			KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen);
+			int res1 = -1;
+			res1 = ModBusSlaveParsePkg(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
+			if (res1 !=0)
+			{
+				KLParsePacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
+			}
 			Uart1RecvBuf1DataLen=0;
 			Uart1Stat.bPacketRecved=0;
 		}
- if (bSlave)	HAL_Delay(0);
+ if (bKBusSlave)	HAL_Delay(0);
 /*
 		if (!IsEmpty(&Uart1Stat.QRx))
 		{
@@ -752,7 +527,55 @@
 				clearscreen();
 			}
 		}
-*/		
+*/
+
+#if (BOARD_TYPE == 14)
+		//process 6 output
+		{
+				// mapping bits.
+			if (KMem.WXB[0]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_12);}
+			else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_12);}
+			if (KMem.WXB[1]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_13);}
+			else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_13);}
+			if (KMem.WXB[2]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_14);}
+			else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_14);}
+			if (KMem.WXB[3]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_15);}
+			else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_15);}
+			if (KMem.WXB[4]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_0);}
+			else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_0);}
+			if (KMem.WXB[5]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_1);}
+			else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1);}			
+			if (KMem.WXB[5]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_2);}
+			else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_2);}			
+		}
+#endif
+
+/*		
+		{
+		unsigned char pos,seg;
+		unsigned short val;
+		pos=((KMem.nRunCount)&0x3);
+		//val=(KMem.nRunCount)&0xfff;
+		val=KMem.ErrStat;
+		char buf5[20];
+		sprintf(buf5,"%4d",val);
+		val=buf5[3-pos];
+		if (val <'0' || val >'9') {seg=0;}
+		else {seg=LEDSEGTAB[val-'0'];}
+		
+		pos=1<<pos;
+		//pos=1;
+		//seg=2;
+		seg=~seg;
+		//	PutOutputSPI1(pos|(seg<<8));
+	}
+*/
+
+#if (BOARD_TYPE == 13)	
+	 w5500_network_info_show();
+//	 loopback_tcps(0,str1,5000);
+#endif
+
 	 LL_IWDG_ReloadCounter(IWDG);
 		
   }	//while (1) ;

--
Gitblit v1.9.1