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

---
 CCT6_TestApp1/Src/main.c |  364 +++++++++++++++++++++++----------------------------
 1 files changed, 167 insertions(+), 197 deletions(-)

diff --git a/CCT6_TestApp1/Src/main.c b/CCT6_TestApp1/Src/main.c
index 4c3cfa6..d056abb 100644
--- a/CCT6_TestApp1/Src/main.c
+++ b/CCT6_TestApp1/Src/main.c
@@ -56,14 +56,15 @@
 #include "../src/Ethernet/socket.h"
 #include "../src/Ethernet/loopback.h"
 #elif (BOARD_TYPE == 14)
-#include "FP0.h"
+#include "FPx.h"
 #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
 #include "KWireless.h"
 //#include "user.h"
 //#include "../src/radio/inc/sx126x-board.h"
 #endif
 #include "SLP.h"
-#include "YDLidar.h"
+//#include "YDLidar.h"
+#include "OrdLidar.h"
 
 /* USER CODE END Includes */
 
@@ -76,8 +77,8 @@
 #define TX2BUFSIZE 64
 
 
-unsigned char Uart1RxBuf[128];
-unsigned char Uart1TxBuf[260];
+unsigned char Uart1RxBuf[RX1BUFSIZE];
+unsigned char Uart1TxBuf[TX1BUFSIZE];
 
 unsigned char Uart2RxBuf[RX2BUFSIZE];
 unsigned char Uart2TxBuf[TX2BUFSIZE];
@@ -86,10 +87,20 @@
 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;
+
+unsigned char bSLPMaster =1;
+unsigned char nSLPStation = 1;
+
+stSLPDef SLP1;
 /* USER CODE END PV */
 
 /* Private function prototypes -----------------------------------------------*/
@@ -121,7 +132,7 @@
 static int Count=0;
 	CurTickuS += 100;	
 	nCurTick++;
-	nSlaveTick++;
+	KBus1.nSlaveTick++;
 	Count++;
 	if (Count>=10000) 
 	{
@@ -135,17 +146,71 @@
 	return;
 }
 
-#define RAM_START_ADDR 0x20000000
+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));		
+	}
+}
+
+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.WY[0]=KBusMem.WLY[0];			//KBus Slave
+			KBusMem.WLX[0]=KMem.WX[0];
+			KBusMem.WLX[1]=KMem.WX[1];
+			KBusMem.WLX[2]=KMem.WX[2];
+			KBusMem.WLX[3]=KMem.WX[3];	
+			break;
+		case KBusEvCmdResponse:
+			break;
+		
+		default:
+			break;
+	}
+	return 0;
+}
+
+//#define RAM_START_ADDR 0x20000000    // SRAM_BASE
 #define VECTOR_SIZE 46 
 #define  ApplicationAddress  0x08001000  //搴旂敤绋嬪簭棣栧湴鍧�瀹氫箟
-
+/*
 static void RemapIrqVector(void)
 {
 	memcpy((void*)RAM_START_ADDR, (void *)ApplicationAddress, VECTOR_SIZE * 4);
 	LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
 	LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SRAM);
 }
-
+*/
 /* USER CODE END 0 */
 
 /**
@@ -172,21 +237,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;
@@ -217,35 +267,40 @@
 
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
-  LL_GPIO_InitTypeDef GPIO_InitStruct;
-  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
-  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
-  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
-  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
-  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
   MX_DMA_Init();
 	
 	KMachineInit();
 	ReadSysCfgFromFlash(&storedKMSysCfg);
 	
 	KMRunStat.bLEDFlick = 1;
-	
+	int bKBusMaster,bKBusSlave,bKBusRepeater;;
+	int nKBusStationId;
+	int nKBusChilds;
 	KMem.CurJumperSW=ReadJumperSW();
 	KMem.EffJumperSW=KMem.CurJumperSW;
+	nKBusStationId=KMem.EffJumperSW&0x0f;	
+	
+	nKBusChilds = nKBusStationId;
 	
 	bSLPMaster = 1; // KMem.EffJumperSW&0x20 ; //master?
-	nStation = 1;
+	nSLPStation = 1;
 	
 //	Uart2Baud = AlterUart2Baud;
 	
+
+	
 #if (BOARD_TYPE == 14)
 	KMem.EffJumperSW|=0x10;
-	nStationID=KMem.EffJumperSW&0x0f;
+	int nKBusChilds=KMem.EffJumperSW&0x0f;
   if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}
-	nChilds=nStationID;
-	FP0_Init();
+
+	FPxSetCallBackFunc(&FPxCallBackFunc);
+	FPx_Init(nKBusChilds);
+
+	int IOByteCount = nKBusChilds;
+	FPx_SetIOCount(IOByteCount,IOByteCount);
+	
 
 #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
 	nStationID=1 ;//KMem.EffJumperSW&0x0f;
@@ -254,13 +309,21 @@
 //	else
 		{bKBusMaster=0;bKBusSlave=1;}	
 #else
-	nStationID=KMem.EffJumperSW&0x0f;
-	if (KMem.EffJumperSW == 0x1f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
-  else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	nKBusStationId=KMem.EffJumperSW&0x0f;
+	if (KMem.EffJumperSW == 0x3f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
+  else if ((KMem.EffJumperSW&0x20)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}
 #endif
-	nChilds=nStationID;
-	nCurPollId=1;
+	
+	if (bKBusMaster) {
+		KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusChilds);
+	
+	} else if (bKBusSlave) {
+		KBusInitSlave(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusStationId,&MyDeviceInfo);
+	}
+	
+	KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc);	
+
 	//if (KMem.EffJumperSW == 0x00)
 		Uart1Baud = DefaultUart1Baud;
   MX_USART1_UART_Init();
@@ -355,20 +418,21 @@
 	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)
 	KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x0f);
 	KWireLessStart();
 #endif
 	
-	YdLidarStart(-1);
+	OrdLidarStart(-1);
 	
   while (1)
   {
@@ -383,14 +447,14 @@
 		int haltick=HAL_GetTick();
 		
 		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;
@@ -410,21 +474,12 @@
 		a		= LL_GPIO_ReadInputPort(GPIOD);
 		KMem.WDT[123]=a;
 		
-#if (BOARD_TYPE == 14)
-//		KMem.WX[0]= GetInput();		
-		FP0_Proc();
-#else
+
 		KMem.WXB[0]= GetInput();		
-#endif
 		
-		if (GetBoardType() == 7 || GetBoardType() ==8 
-			|| GetBoardType() == 9 || GetBoardType() ==10 || GetBoardType() ==15 || GetBoardType() ==16) 
-		{
-			displayInput(KMem.WX[0]);
-		}
 		us2=GetuS();
 		if (PowerDownEvent) {		KMem.WX[0]=0;}
-///*
+// /*
 		if ((KMem.nRunCount &0x1f) == 0x02)
 		{
 			ADCProcess();
@@ -447,16 +502,10 @@
 				}
 			}
 		}
-//*/
-
-#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
-		Radio.IrqProcess( ); // Process Radio IRQ
-		KWL_Process(1);
-		
-#endif
+// */
 
 //		pProgs = (stBinProg1 *) STORE_PRG_BASE;
-
+#if (ENABLE_PLC)
 		if (	KMRunStat.WorkMode==1 ) //&& bKBusMaster)
 		{
 			if (KMRunStat.nBinProgBank == 0){
@@ -469,7 +518,8 @@
 			
 			ProcessPLCBinProg(pProgs, nSizeProg1);
 		}
-
+#endif
+		
 		KMem.ScanTimeuS=us2-KMem.LastScanTime;
 		KMem.LastScanTime = us2;
 		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
@@ -481,21 +531,7 @@
 
 		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
-
+			KBusLoopProcess(&KBus1);
 		}
 			if (haltick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
@@ -504,26 +540,23 @@
 
 		if (bKBusSlave)		
 		{
-//			BufferOut[0]=KMem.WX[0];
-#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.WLY[0]=BufferIn[0] + (BufferIn[1] <<8);
-#endif
-			if (nSlaveTick&0x00002000) SlowFlicker=1;
+
+			KBusLoopProcess(&KBus1);	
+		//	if (! KBus1.RunStat) {KBusMem.WLY[0]=0;}
+			KMem.WLY[0]=KBusMem.WLY[0];
+
+			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--;}
+		KBusMem.WLX[0]=KMem.WX[0];
+		KMem.WY[0]=KBusMem.WLY[0];
+		KBusMem.WLX[1]=KMem.WX[1];
+		KBusMem.WLX[2]=KMem.WX[2];
+		KBusMem.WLX[3]=KMem.WX[3];
+		
 		
 		if (KMRunStat.bLEDFlick)
 		{
@@ -535,14 +568,18 @@
 		}
 		else
 		{
+#if (ENABLE_PLC)			
 			if (KMRunStat.WorkMode==1 ) {
 				if (PLCMem.bPLCRunning){SetRunLed(SlowFlicker);}
 					else {SetRunLed(0);}
 			}
-			else {
+			else 
+#endif				
+			{
 					if (!KMem.RunStat) SetRunLed(SlowFlicker);
 					else SetRunLed(FastFlicker);
 			}
+			KMem.ErrStat = KBus1.ErrStat + SLP1.SLPErrSign;
 			if (!KMem.ErrStat) 
 			{
 				SetErrLed(0);
@@ -564,21 +601,6 @@
 		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];			
-		}else
-		{
-			KMem.WFY[1]=KMem.WX[0];
-			KMem.WY[0]=KMem.WFX[1];
-		}
-//	KMem.WY[0]=KMem.WLY[0];
-#else
-		KMem.WLX[0]=KMem.WX[0];
-		KMem.WY[0]=KMem.WLY[0];
-#endif
 
 		us5=GetuS();
 		
@@ -600,16 +622,16 @@
 //		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 (Uart1Stat.bPacketRecved && Uart1RecvBuf1DataLen >0)
 		{
 			int res1 = -1;
-			res1 = ModBusSlaveParsePkg(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
-			if (res1 !=0)
-			{
-				KLParsePacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
+			if (Uart1RecvBuf1[0] == KLSignStart) {
+				res1 = KLParsePacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
+			}else {
+				res1 = ModBusSlaveParsePkg(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
 			}
-			Uart1RecvBuf1DataLen=0;
 			Uart1Stat.bPacketRecved=0;
+			Uart1RecvBuf1DataLen=0;
 			Uart1IdelTimer = 0;
 		}else {
 			if (Uart1IdelTimer>600000) { // 瓒呰繃60绉掓病鏈夋暟鎹紶杈擄紝閲嶆柊杩涘叆鑷�傚簲娉㈢壒鐜囩姸鎬�
@@ -622,30 +644,32 @@
 	if (bKBusSlave)	HAL_Delay(0);
 		
 	if (Uart6Stat.bPacketRecved){
-		SLPparsePacket(Uart6RxBuf,Uart6RecvBuf1DataLen);
+		SLPparsePacket(&SLP1,Uart6RxBuf,Uart6RecvBuf1DataLen);
 		Uart6RecvBuf1DataLen =0;
 		Uart6Stat.bPacketRecved = 0;
 	}
-		
-		SLPinputB =  KMem.WYB[1];
-		SLPProcess();
-		KMem.WXB[1] = SLPoutputB;
+		SLP1.SLPinputB =  KMem.WYB[1];
+		SLPProcess(&SLP1);
+		KMem.WXB[1] = SLP1.SLPoutputB;
 
 		// YDLiDar process;
 
 	if (Uart3Stat.bPacketRecved){
 		KMem.WDT[8]++;
-		YdLidarParsePkt(0,(stLidarDotsPkt *)Uart3RxBuf,Uart3RecvBuf1DataLen);
+		OrdLidarParsePkt(0,(OradarLidarFrame *)Uart3RxBuf,Uart3RecvBuf1DataLen);
 		Uart3RecvBuf1DataLen =0;
 		Uart3Stat.bPacketRecved = 0;
 	}
 	KMem.WDT[9]=pCount1;
 	KMem.WDT[10]=dCount1;
 	KMem.WDT[11]=vCount1;
+	
+	KMem.WDT[12] = eCount1;
+	KMem.WDT[13] = eCount2;
 		
 	if (Uart5Stat.bPacketRecved){
 		KMem.WDT[16]++;
-		YdLidarParsePkt(1,(stLidarDotsPkt *)Uart5RxBuf,Uart5RecvBuf1DataLen);
+		OrdLidarParsePkt(1,(OradarLidarFrame *)Uart5RxBuf,Uart5RecvBuf1DataLen);
 		Uart5RecvBuf1DataLen =0;
 		Uart5Stat.bPacketRecved = 0;
 	}
@@ -662,79 +686,25 @@
 	KMem.WDT[27]=nPosZ1;
 	KMem.WDT[28]=nPosZ2;
 	
+	KMem.WDT[32]=results[0];
+	KMem.WDT[33]=results[1];
+	KMem.WDT[34]=results[2];
+	KMem.WDT[35]=results[3];
+	KMem.WDT[36]=results[4];
+	KMem.WDT[37]=results[5];
+	KMem.WDT[38]=results[6];
+	KMem.WDT[39]=results[7];
 	
-/*
-		if ((KMem.nRunCount&0x7f)==1) {
-			
-			SLPSendPacket("ABCDEF",6);
-			
-		};
-*/		
-		
-		/*
-		if (!IsEmpty(&Uart1Stat.QRx))
-		{
-			unsigned char k=PopOne(&Uart1Stat.QRx);
-			if (k=='L')
-			{
-				clearscreen();
-			}
-		}
-*/
-#if (BOARD_TYPE == 14)
-//		PutOutput (KMem.WY[0]);
-#else
-		PutOutput (KMem.WY[0]);
-
-#endif
-
-#if (BOARD_TYPE == 14)
-const unsigned int pins[6]= {		LL_GPIO_PIN_10,LL_GPIO_PIN_11,LL_GPIO_PIN_12,LL_GPIO_PIN_13,LL_GPIO_PIN_14,LL_GPIO_PIN_15};
-		//process 6 output
-		{
-				// mapping bits.
-			for (int i=0;i<6;i++)
-			{
-				USHORT bitaddr = storedKMSysCfg.theKMSysCfg.OutMappings[i];
-				UCHAR type =  (bitaddr&0xf000) >>12;
-				USHORT byteaddr = (bitaddr&0x0ff0) >>4;
-				UCHAR bitpos = bitaddr &0x0f;
-				UCHAR bitvalue = 0 ;
-				if (byteaddr>0) {
-					if (type == 0) bitvalue = KMem.WXB[byteaddr-1] & ( 1 << bitpos );
-					else if (type == 1 ) bitvalue = KMem.WYB[byteaddr-1] & ( 1 << bitpos );
-				}
-				if (bitvalue){ LL_GPIO_SetOutputPin(GPIOB,pins[i]);}
-				else {LL_GPIO_ResetOutputPin(GPIOB,pins[i]);}
-			}
-		}
-#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
+	
+	KMem.WX[1] = nPosX ;
+	KMem.WX[2] = nPosY ;
+	KMem.WX[3] = nPosZ;
+	
+//	KMem.WX[1]++ ;
+//	KMem.WX[2]++;
+	
+//	KMem.WYB[0]=1;
+	PutOutput (KMem.WY[0]);
 
 	 LL_IWDG_ReloadCounter(IWDG);
 		

--
Gitblit v1.9.1