From a9312568cde771d4b970445e1f66a04fabb64d56 Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期三, 01 二月 2023 14:07:14 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/Radio' into dev1

---
 Src/functions.c |  214 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 166 insertions(+), 48 deletions(-)

diff --git a/Src/functions.c b/Src/functions.c
index 8ba8fe2..d564f9b 100644
--- a/Src/functions.c
+++ b/Src/functions.c
@@ -8,6 +8,9 @@
 #include "functions.h"
 #include "string.h"
 #include "stm32f0xx_hal.h"
+#if (BOARD_TYPE == 14)
+#include "fp0.h"
+#endif
 extern __IO uint32_t uwTick;
 //#include "Myprotocol.h"
 int TickFreq=10000;
@@ -16,16 +19,16 @@
 unsigned int TickPrioduS;	//
 volatile unsigned int nCurTick=0;
 volatile unsigned int CurTickuS=0;
-//volatile unsigned int ThisRunTime=0;		//开机时间
-//volatile unsigned int TotalRunTime=0; 	//总开机时间
-//volatile unsigned int PwrCount=0;				//开机次数
-unsigned short ClkuS;				//每个Clk的nS数,
+//volatile unsigned int ThisRunTime=0;		//寮�鏈烘椂闂�
+//volatile unsigned int TotalRunTime=0; 	//鎬诲紑鏈烘椂闂�
+//volatile unsigned int PwrCount=0;				//寮�鏈烘鏁�
+unsigned short ClkuS;				//姣忎釜Clk鐨刵S鏁帮紝
 
 int InituS(int TickFreq1)
 {
-		TickPrioduS=1000000/TickFreq1;	//每个SysTick的微秒数
-		CoreClkMHz=HAL_RCC_GetHCLKFreq()/1000000;		//=SystemCoreClock/1000000;每uS的时钟数
-		TickPriodClk=SystemCoreClock/TickFreq1;			//每个SysTick的时钟数
+		TickPrioduS=1000000/TickFreq1;	//姣忎釜SysTick鐨勫井绉掓暟
+		CoreClkMHz=HAL_RCC_GetHCLKFreq()/1000000;		//=SystemCoreClock/1000000;姣弖S鐨勬椂閽熸暟
+		TickPriodClk=SystemCoreClock/TickFreq1;			//姣忎釜SysTick鐨勬椂閽熸暟
 		ClkuS=(1000000LL*65536)/SystemCoreClock;
 		CurTickuS=TickPrioduS+100u;
 	return 0;
@@ -51,6 +54,13 @@
 //	unsigned short Clk1=SysTick->VAL;
 		return nCurTick;
 }
+
+void logData(unsigned char d)
+{
+			KMem.WDB[128+KMem.WDT[123]] = d;
+			KMem.WDT[123]++; 	 if (KMem.WDT[123]>=100) {KMem.WDT[123]=81;}			
+}
+
 const unsigned short crc16_table[256] = {
 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
@@ -89,11 +99,11 @@
 unsigned short crc_check(const unsigned char * data, unsigned int  length)
 {
 unsigned short crc_reg = 0xFFFF;
-while (length--)
-{
-crc_reg = (crc_reg >> 8) ^ crc16_table[(crc_reg ^ *data++) & 0xff];
-}
-return (~crc_reg) & 0xFFFF;
+	while (length--)
+	{
+	crc_reg = (crc_reg >> 8) ^ crc16_table[(crc_reg ^ *data++) & 0xff];
+	}
+	return (~crc_reg) & 0xFFFF;
 }
 
 const uint16_t polynom = 0xA001;
@@ -207,7 +217,7 @@
 	LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_2,nSize);
 	LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_2);
 	Uart1Stat.DMASendLen=nSize;
-	Uart1Stat.Sending=1;	
+	Uart1Stat.bSending=1;	
 	LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_2);
 	LL_USART_EnableDMAReq_TX(USART1);
 	return nSize;	
@@ -215,7 +225,7 @@
 
 int Uart1TriggerSendDMA()
 {
-		if (!Uart1Stat.Sending&&!IsEmpty(&Uart1Stat.QTx))
+		if (!Uart1Stat.bSending&&!IsEmpty(&Uart1Stat.QTx))
 		{			
 			int len1=GetContinueData(&Uart1Stat.QTx);
 			Uart1SendDMA(GetReadBuffer(&Uart1Stat.QTx),len1);
@@ -231,7 +241,7 @@
 	LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_5,nSize);
 	LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_5);
 	Uart2Stat.DMARecvLen=nSize;
-	Uart2Stat.Recving=1;	
+	Uart2Stat.bRecving=1;	
 	LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_5);
 	LL_USART_EnableDMAReq_RX(USART2);	
 	return 0;
@@ -244,14 +254,14 @@
 	LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_4,nSize);
 	LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_4);
 	Uart2Stat.DMASendLen=nSize;
-	Uart2Stat.Sending=1;	
+	Uart2Stat.bSending=1;	
 	LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_4);
 	LL_USART_EnableDMAReq_TX(USART2);
 	return nSize;	
 }
 int Uart2TriggerSendDMA()
 {
-		if (!Uart2Stat.Sending&&!IsEmpty(&Uart2Stat.QTx))
+		if (!Uart2Stat.bSending&&!IsEmpty(&Uart2Stat.QTx))
 		{			
 			int len1=GetContinueData(&Uart2Stat.QTx);
 			Uart2SendDMA(GetReadBuffer(&Uart2Stat.QTx),len1);
@@ -266,14 +276,79 @@
 
 void PendSvCallBack()
 {
+#if (BOARD_TYPE == 14)
+///*	
+		if (bSPI1RecvDone)
+		{
+			bSPI1RecvDone=0;
+			ParseFP0Pkg(SPI1RecvBuf,nSPI1RecvLenInBuf);
+		}
+//*/	
+#endif		
 	if (Uart2Stat.bPacketRecved)
 	{
-		ParsePacket((pPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen);		
+		KBusParsePacket(2, (pKBPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen);		
 		Uart2RecvBuf1DataLen=0;
 		Uart2Stat.bPacketRecved=0;
 		Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));		
 	}
 }
+
+void SPI1_IRQ_CallBack()
+{
+		uint8_t value;
+//	LL_GPIO_TogglePin(GPIOB,LL_GPIO_PIN_7);
+	 if (LL_SPI_IsActiveFlag_RXNE(SPI1))
+	 {
+		 	value = LL_SPI_ReceiveData8( SPI1);
+#if (BOARD_TYPE == 14)
+			KMem.WDD[62]++;
+			KMem.WDT[122]++;
+		 if (!bSPI1Sending)
+		 {
+			 logData(value);
+		 }
+		 if (!bSPI1Sending && (1 || bSPI1Recving))
+		 {
+			 SPI1RecvBuf[nSPI1RecvPos]=value;
+			 nSPI1RecvPos++;
+			 
+			 if (value==0x0d)
+			 {
+				 logData(nSPI1RecvPos);
+
+				 nSPI1RecvLenInBuf=nSPI1RecvPos;
+				 bSPI1RecvDone=1;
+				 nSPI1RecvPos=0;
+				 bSPI1Recving=0;
+				 
+			 
+				 TriggerPendSV();
+			 }
+		 }
+		 if (bSPI1Sending)
+		 {
+				 nSPI1SentLen++;
+				 KMem.SDD[63]++;
+			 
+			 if (nSPI1SentLen >= nSPI1ToSendLen) {
+				 SetACKPin_1();
+				 bSPI1Sending=0;
+				 bSPI1SendDone=1;
+				 bSPI1Recving=1;
+				 nSPI1RecvPos=0;
+				 SetFP0DEPin_0();
+			 }
+			 else {
+				 value = SPI1SendBuf[nSPI1SentLen];
+				 LL_SPI_TransmitData8(SPI1,value);
+				logData(value);
+			 }
+		 }
+#endif		 
+	 }	 
+}
+
 void Uart1SendDone()
 {
 	Uart1Stat.TcCount++;
@@ -306,16 +381,16 @@
 	Uart2Stat.IdelCount++;
 	if (Uart2RecvBuf1DataLen>0)
 		TriggerPendSV();
-	//	ParsePacket((pPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen);
+	//	ParsePacket((pKBPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen);
 }
 
 int PutStr(char * str1, int len1)
 {
 //	Uart1SendDMA(str1,len1);
-	//PushIn(&Uart1Stat.QTx,str1,len1);
+	PushIn(&Uart1Stat.QTx,str1,len1);
 //	LL_USART_EnableIT_TXE(USART1);   
 //	LL_USART_EnableIT_TC(USART1); 	
-	//Uart1TriggerSendDMA();
+	Uart1TriggerSendDMA();
 	return len1;
 }
 int PutStr1(char * str1, int len1)
@@ -337,21 +412,22 @@
 	return len1;
 }
 
-void clearscreen()
+int SendPacket(int nChn, void * pBuf,int len1)
 {
-	PutStr("\33[2J\33[0;0H",10);
-	return;
+	if (nChn==1) 	{
+		PutStr1((char *)pBuf,len1);
+//	PushIn(&Uart1Stat.QTx,p1,len1);
+//	Uart1TriggerSendDMA();
+		Uart1Stat.SentPacket++;		
+	}else if (nChn==2){
+		PutStr2((char *)pBuf,len1);	
+//	PushIn(&Uart2Stat.QTx,p1,len1);
+//	Uart2TriggerSendDMA();	
+		Uart2Stat.SentPacket++;		
+	}
+		return len1;
 }
-
-void Locate(int y,int x)
-{
-		char str[16];
-		int len;
-		len=sprintf(str," \33[%d;%dH",y,x);
-		PutStr(str,len);
-		return;
-}
-
+/*
 int SendPacket1(void * pBuf,int len1)
 {
 		PutStr1((char *)pBuf,len1);
@@ -360,7 +436,7 @@
 		Uart1Stat.SentPacket++;
 	return len1;
 }
-int SendPacket2(pPacket p1,int len1)
+int SendPacket2(pKBPacket p1,int len1)
 {
 		PutStr2((char *)p1,len1);	
 //	PushIn(&Uart2Stat.QTx,p1,len1);
@@ -368,9 +444,20 @@
 		Uart2Stat.SentPacket++;
 	return len1;
 }
-
+*/
 void ToggleRunLed() {	LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);}
 void ToggleErrLed() {	LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);}
+void ToggleErr2Led() {	LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);}
+
+#if (BOARD_TYPE == 14)
+void ToggleOutStat() {	LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);}
+
+void SetOutStat(uchar bOn)
+{
+	if (bOn) {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_15);}
+	else {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_15);}	
+}
+#else
 void ToggleOutStat() {	LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_11);}
 
 void SetOutStat(uchar bOn)
@@ -378,6 +465,7 @@
 	if (bOn) {LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_11);}
 	else {LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_11);}	
 }
+#endif
 
 void SetRunLed(uchar bOn)
 {
@@ -389,11 +477,17 @@
 	if (bOn) {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_14);}
 	else {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_14);}		
 }
+void SetErr2Led(uchar bOn)
+{
+	if (bOn) {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_15);}
+	else {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_15);}		
+}
+/*
 void SetLeds(uchar bRun, uchar bErr)
 {
 	SetRunLed(bRun); SetErrLed (bErr);
 }
-
+*/
 #define set165SL_0() LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_4)
 #define set165SL_1() LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_4)
 #define set165CLK_0() LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_5)
@@ -529,6 +623,15 @@
 						return Input165_R(8);
 		case 11:
 						return Input165_R(8);
+		case 13:
+						return Input165_R(16);
+		case 14:
+						return 0;	//FP0
+		case 15:
+						return Input165_R(16);
+		case 16:
+						return Input165_R(8);
+		
 		default:
 			break;
 	}
@@ -612,9 +715,16 @@
 					 return ReadConfig_5();	//New Slave 8 in 8 o
 		case 9:
 		case 10:
-					 return ReadConfig_5();	//New Slave 8 in 8 o
+					 return ReadConfig_5();	//New Master Slave 8 in 8 o
 		case 11:
 					 return ReadConfig_11(); //Mini Board
+		case 13:
+					 return ReadConfig_5();
+		case 14:
+					 return (~(LL_GPIO_ReadInputPort(GPIOA)>>4))&0x0f;	//FP0
+		case 15:
+		case 16:
+					 return ReadConfig_5();	//Wireless Master Slave 8 in 8 o
 		default:
 			
 					 return 0;
@@ -637,7 +747,7 @@
 void Output595_8(unsigned int cc)
 {
 //unsigned char i;
-;//		74HC595输出程序,输出8位
+;//		74HC595杈撳嚭绋嬪簭锛岃緭鍑�8浣�
 //	cc=~0x3f;
 	__disable_irq();
 	STRCLK2_1();
@@ -659,10 +769,11 @@
 	STRCLK2_1();	
 	__enable_irq();
 }
+
 void Output595_16(unsigned int cc)
 {
 //unsigned char i;
-;//		74HC595输出程序,输出8位
+;//		74HC595杈撳嚭绋嬪簭锛岃緭鍑�8浣�
 //	cc=~0x3f;
 	__disable_irq();
 	STRCLK2_1();
@@ -685,6 +796,7 @@
 	__enable_irq();
 }
 
+/*
 #define STRCLK12_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_7)
 #define STRCLK12_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_7)
 
@@ -715,10 +827,11 @@
 	}		
 	KMem.SDD[30]=i;
 		STRCLK12_0();
-		STRCLK12_1();			
-	__enable_irq();
+		__nop();
+		STRCLK12_1();
+		__enable_irq();
 }
-
+*/
 
 void PutOutputSPI2(unsigned int Y)
 {
@@ -747,19 +860,23 @@
 	}		
 	KMem.SDD[30]=i;
 		STRCLK2_0();
-		STRCLK2_1();			
+		STRCLK2_1();
 	__enable_irq();
 }
 
 void PutOutput(unsigned int Y)		
 {	
+#if (BOARD_TYPE == 14)
+	return ;
+#else
 	PutOutputSPI2(Y);
 	//Output595_16(Y);
+#endif
 }
 
-#if (BOARD_TYPE == 9 || BOARD_TYPE == 10 )
+#if (BOARD_TYPE == 9 || BOARD_TYPE == 10 || BOARD_TYPE == 15 ||  BOARD_TYPE == 16)
 //#pragma message("9,10")
-	// V4.2 管脚排列向右移动了一位。
+	// V4.2 绠¤剼鎺掑垪鍚戝彸绉诲姩浜嗕竴浣嶃��
 #define SRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1)
 #define SRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_1)
 #define STRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_2)
@@ -768,7 +885,7 @@
 #define OE1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_10)
 #define SER1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_11)
 #define SER1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_11)
-#else		//按照原来的管脚排列
+#else		//鎸夌収鍘熸潵鐨勭鑴氭帓鍒�
 #define SRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_0)
 #define SRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_0)
 #define STRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1)
@@ -785,10 +902,11 @@
 			if (bEnable) {OE1_0();}
 			else {OE1_1();}	
 }
+
 void displayInput(unsigned int cc)
 {
 //unsigned char i;
-;//		74HC595输出程序,输出8位
+;//		74HC595杈撳嚭绋嬪簭锛岃緭鍑�8浣�
 //	cc=~0x3f;
 	__disable_irq();
 	STRCLK1_1();

--
Gitblit v1.9.1