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 |  125 +++++++++++++++++++++++++++--------------
 1 files changed, 81 insertions(+), 44 deletions(-)

diff --git a/CCT6_TestApp1/Src/main.c b/CCT6_TestApp1/Src/main.c
index 71593c5..d056abb 100644
--- a/CCT6_TestApp1/Src/main.c
+++ b/CCT6_TestApp1/Src/main.c
@@ -87,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 -----------------------------------------------*/
@@ -122,7 +132,7 @@
 static int Count=0;
 	CurTickuS += 100;	
 	nCurTick++;
-	nSlaveTick++;
+	KBus1.nSlaveTick++;
 	Count++;
 	if (Count>=10000) 
 	{
@@ -136,7 +146,27 @@
 	return;
 }
 
-void * KBusCallBackFunc(int nChn, int nEvent, void *pBuf, int nLen1)
+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){
 		
@@ -237,25 +267,23 @@
 
   /* 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;
 	
@@ -263,11 +291,16 @@
 	
 #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;
-	FPx_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;
@@ -276,17 +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
 	
-	KBusInit(2, bKBusMaster, nChilds);	
-	KBusSetCallBackFunc(2, &KBusCallBackFunc),
+	if (bKBusMaster) {
+		KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusChilds);
 	
-	nChilds=nStationID;
-	nCurPollId=1;
+	} else if (bKBusSlave) {
+		KBusInitSlave(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusStationId,&MyDeviceInfo);
+	}
+	
+	KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc);	
+
 	//if (KMem.EffJumperSW == 0x00)
 		Uart1Baud = DefaultUart1Baud;
   MX_USART1_UART_Init();
@@ -381,14 +418,15 @@
 	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();
@@ -409,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;
@@ -467,7 +505,7 @@
 // */
 
 //		pProgs = (stBinProg1 *) STORE_PRG_BASE;
-
+#if (ENABLE_PLC)
 		if (	KMRunStat.WorkMode==1 ) //&& bKBusMaster)
 		{
 			if (KMRunStat.nBinProgBank == 0){
@@ -480,7 +518,8 @@
 			
 			ProcessPLCBinProg(pProgs, nSizeProg1);
 		}
-
+#endif
+		
 		KMem.ScanTimeuS=us2-KMem.LastScanTime;
 		KMem.LastScanTime = us2;
 		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
@@ -492,9 +531,7 @@
 
 		if (bKBusMaster)		
 		{
-
-			if (nChilds>0) {		KBusMasterFunc(2); }
-
+			KBusLoopProcess(&KBus1);
 		}
 			if (haltick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
@@ -504,13 +541,13 @@
 		if (bKBusSlave)		
 		{
 
-			KBusSlaveFunc(2);	
-			if (! KMem.RunStat) {KBusMem.WLY[0]=0;}
+			KBusLoopProcess(&KBus1);	
+		//	if (! KBus1.RunStat) {KBusMem.WLY[0]=0;}
 			KMem.WLY[0]=KBusMem.WLY[0];
 
-			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;			
 
 		}
@@ -520,9 +557,6 @@
 		KBusMem.WLX[2]=KMem.WX[2];
 		KBusMem.WLX[3]=KMem.WX[3];
 		
-//		KMem.WY[0]=nCount2>>5;
-		if (KMem.RunStat) {KMem.RunStat--;}
-		if (KMem.ErrStat) {KMem.ErrStat--;}
 		
 		if (KMRunStat.bLEDFlick)
 		{
@@ -534,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);
@@ -606,14 +644,13 @@
 	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;
 
@@ -666,7 +703,7 @@
 //	KMem.WX[1]++ ;
 //	KMem.WX[2]++;
 	
-
+//	KMem.WYB[0]=1;
 	PutOutput (KMem.WY[0]);
 
 	 LL_IWDG_ReloadCounter(IWDG);

--
Gitblit v1.9.1