From 9aed5d7e7b3c7bf09da712e9c272ece401a7acc9 Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期一, 25 十一月 2024 14:51:23 +0800
Subject: [PATCH] add UltraSonic and MultiWireLess

---
 ComLib/Src/functions.c |  461 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 433 insertions(+), 28 deletions(-)

diff --git a/ComLib/Src/functions.c b/ComLib/Src/functions.c
index e1fb73a..436f204 100644
--- a/ComLib/Src/functions.c
+++ b/ComLib/Src/functions.c
@@ -192,7 +192,7 @@
 	0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
 	0x40
 };
- 
+ /*
 uint16_t crc16table(const uint8_t *ptr, uint16_t len)
 {
 	uint8_t crchi = 0xff;
@@ -206,6 +206,27 @@
 	}
 	return (crchi << 8 | crclo);
 }
+*/
+const uint16_t crctalbeabs[] = { 
+	0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 
+	0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 
+};
+
+uint16_t crc16tablefast(const uint8_t *ptr, uint16_t len) 
+{
+	uint16_t crc = 0xffff; 
+	uint16_t i;
+	uint8_t ch;
+ 
+	for (i = 0; i < len; i++) {
+		ch = *ptr++;
+		crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4);
+		crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
+	} 
+	
+	return crc;
+}
+
  /*
 void modbuscrc16test()
 {
@@ -262,6 +283,14 @@
 {
 	for (int i=0;i<nUs;i++)
 	 for (volatile int j=0;j<24;j++)
+		{
+			__nop();
+		}
+}
+void Delay100nS(int n100nS)
+{
+	for (int i=0;i<n100nS;i++)
+	 for (volatile int j=0;j<1;j++)
 		{
 			__nop();
 		}
@@ -383,12 +412,13 @@
 //		NVIC_SetPendingIRQ(PendSV_IRQn);
 //	  SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos;
 	
-	if (Uart1RecvBuf1DataLen >0)
+	if (Uart1RxBuf1DataLen >0)
 	{
 		Uart1Stat.bPacketRecved=1;
 //	  SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos;
 //		KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen);
 //		Uart1RecvBuf1DataLen=0;
+		TriggerPendSV();		
 	}
 }
 
@@ -404,10 +434,10 @@
 }
 void Uart2RecvDone()
 {
-	Uart2RecvBuf1DataLen=sizeof(Uart2RecvBuf1) - LL_DMA_GetDataLength(DMA1,LL_DMA_CHANNEL_5);
+	Uart2RxBuf1DataLen=sizeof(Uart2RxBuf1) - LL_DMA_GetDataLength(DMA1,LL_DMA_CHANNEL_5);
 	Uart2Stat.bPacketRecved=1;
 	Uart2Stat.IdelCount++;
-	if (Uart2RecvBuf1DataLen>0)
+	if (Uart2RxBuf1DataLen>0)
 		TriggerPendSV();
 	//	ParsePacket((pKBPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen);
 }
@@ -445,7 +475,8 @@
 int SendPacket(int nChn, void * pBuf,int len1)
 {
 	if (nChn==1) 	{
-		PutStr1((char *)pBuf,len1);
+		Uart1SendDMA(pBuf, len1);
+//	PutStr1((char *)pBuf,len1);
 //	PushIn(&Uart1Stat.QTx,p1,len1);
 //	Uart1TriggerSendDMA();
 
@@ -488,12 +519,12 @@
 	else {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_15);}	
 }
 #else
-void ToggleOutStat() {	LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_11);}
+void ToggleOutStat() {	} //LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_11);}
 
 void SetOutStat(uchar bOn)
 {
-	if (bOn) {LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_11);}
-	else {LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_11);}	
+//	if (bOn) {LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_11);}
+//	else {LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_11);}	
 }
 #endif
 
@@ -868,27 +899,15 @@
 	__disable_irq();
 	STRCLK2_1();	
 	LL_SPI_TransmitData8(SPI2,Y>>8);
-	int i=0;
-	while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET)
-	{
-	}
-	KMem.SDD[28]=i;
-	i=0;
-	while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET)
-	{
-		i++;
-	}	
+
+	while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET)	{	}
+
+	while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET)	{	}	
 	LL_SPI_TransmitData8(SPI2,Y);
-	while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET)
-	{
-	}
-	KMem.SDD[28]=i;
-	i=0;
-	while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET)
-	{
-		i++;
-	}		
-	KMem.SDD[30]=i;
+	while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET)	{	}
+
+	while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET)	{	}		
+
 		STRCLK2_0();
 		STRCLK2_1();
 	__enable_irq();
@@ -958,3 +977,389 @@
 	STRCLK1_1();
 	__enable_irq();	
 }
+
+
+#define W25X_WriteEnable 0x06 
+#define W25X_WriteDisable 0x04 
+#define W25X_ReadStatusReg 0x05 
+#define W25X_ReadStatusReg2 0x35 
+#define W25X_WriteStatusReg 0x01 
+#define W25X_ReadData 0x03 
+#define W25X_FastReadData 0x0B 
+#define W25X_FastReadDual 0x3B 
+#define W25X_PageProgram 0x02 
+#define W25X_BlockErase 0xD8 
+#define W25X_SectorErase 0x20 
+#define W25X_ChipErase 0xC7 
+#define W25X_PowerDown 0xB9 
+#define W25X_ReleasePowerDown 0xAB 
+#define W25X_DeviceID 0xAB 
+#define W25X_ManufactDeviceID 0x90 
+#define W25X_JedecDeviceID 0x9F 
+#define W25X_ReadUniqueID 0x4B 
+
+
+//W25X??/Q??????   
+//W25Q80 ID  0XEF13
+//W25Q16 ID  0XEF14
+//W25Q32 ID  0XEF15
+//W25Q32 ID  0XEF16
+
+#define W25Q80 	0XEF13
+#define W25Q16 	0XEF14
+#define W25Q32 	0XEF15
+#define W25Q64 	0XEF16
+#define W25Q128 0xEF17
+//?????
+#define SPI1_FLASH_CS    PAout[15]  //??FLASH,???????PB12?
+#define SPI1_CS_EN 		LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_15)
+#define SPI1_CS_NA		LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_15)
+
+
+
+uint8_t SPI_Tranceive8(SPI_TypeDef * SPIx, uint8_t Y)
+{
+	int i=0;
+	while (LL_SPI_IsActiveFlag_TXE(SPIx) == RESET){ DelayUs(1);i++;	if (i>3200) break;	}	
+	LL_SPI_TransmitData8(SPIx,Y);
+	i=0;
+	while (LL_SPI_IsActiveFlag_BSY(SPIx) == SET)	{		DelayUs(1);i++;	if (i>3200) break;}	
+	i=0;
+	while (LL_SPI_IsActiveFlag_RXNE(SPIx) == RESET)	{		DelayUs(1);i++;	if (i>3200) break;}		
+	Y = LL_SPI_ReceiveData8(SPIx);
+	return Y;
+}
+
+uint8_t SPI_Transmit(SPI_TypeDef * SPIx, uint8_t * tData, uint8_t nLen, uint8_t timeout)
+{
+	int nToSend=nLen;
+	while(nLen > 0)
+	{
+		if (LL_SPI_IsActiveFlag_TXE(SPI1) && nToSend>0) {
+			LL_SPI_TransmitData8(SPI1,*tData++); nToSend--;
+		}
+		if (LL_SPI_IsActiveFlag_RXNE(SPI1)) {
+			LL_SPI_ReceiveData8(SPI1);
+			nLen--;
+		}
+	}	
+	
+	return 0;
+	for (int i=0;i<nLen;i++)	{
+		SPI_Tranceive8(SPIx, tData[i]);
+	}
+	return 0;
+}
+
+uint8_t SPI_TransmitReceive(SPI_TypeDef * SPIx, uint8_t * tData, uint8_t * rData, uint8_t nLen, uint8_t timeout)
+{
+	for (int i=0;i<nLen;i++)	{
+	//		rData[i] = SPI_Tranceive8(SPIx, tData[i]);
+	__IO uint32_t  * pSR = &(SPI1->SR);
+		int j=0;
+		while ((* pSR & SPI_SR_TXE) == RESET){ 	}	
+		LL_SPI_TransmitData8(SPIx,tData[i]);
+		j=0;
+	//	while (LL_SPI_IsActiveFlag_BSY(SPIx) == SET)	{		DelayUs(1);j++;	if (j>3200) break;}	
+	//	i=0;
+		while (LL_SPI_IsActiveFlag_RXNE(SPIx) == RESET)	{		}		
+		rData[i] = LL_SPI_ReceiveData8(SPIx);
+	}
+	return 0;
+}
+
+uint8_t SPI1_Tranceive(uint8_t * tData, uint8_t * rData, uint8_t nLen)
+{
+	register int nToSend=nLen;
+// /*	
+	__IO uint32_t  * pSR = &(SPI1->SR);
+	__IO uint8_t  * pDR = (__IO uint8_t *)&(SPI1->DR);
+	__disable_irq();	
+	while(nLen >0)
+	{
+		if (*pSR & SPI_SR_TXE && nToSend>0) {
+			LL_SPI_TransmitData8(SPI1,*tData++); nToSend--;			
+		}
+		if (*pSR & SPI_SR_RXNE) {
+			*rData++ = * pDR;
+			nLen--;  // if (nLen == 0) break;		
+		}
+	}
+	
+// */	
+ /*	
+	while(nLen > 0)
+	{
+		if (LL_SPI_IsActiveFlag_TXE(SPI1) && nToSend>0) {
+			LL_SPI_TransmitData8(SPI1,*tData++); nToSend--;
+		}
+		if (LL_SPI_IsActiveFlag_RXNE(SPI1)) {
+			*rData++ = LL_SPI_ReceiveData8(SPI1);
+			nLen--;  // if (nLen == 0) break;
+		}
+	}
+// */	
+	__enable_irq();	
+	return nLen;
+}
+
+
+uint8_t SPI1_Flash_ReadSR(void)   
+{  
+uint8_t Data1[2]= {W25X_ReadStatusReg,0x00};
+uint8_t Rxdata[2];	
+uint8_t byte=0;   
+			SPI1_CS_EN;
+			SPI1_Tranceive(Data1,Rxdata,2);
+			SPI1_CS_NA;
+			byte=Rxdata[1];
+return byte;   
+} 
+void SPI_Flash_Wait_Busy(void)   
+{   
+ while((SPI1_Flash_ReadSR()&0x01)==0x01);   // ??BUSY???
+}  
+
+void SPI_FLASH_Write_SR(uint8_t sr)   
+{   
+uint8_t Data1[2]= {W25X_ReadStatusReg,0x00};
+			Data1[1]=sr;
+			SPI1_CS_EN;
+			SPI_Transmit(SPI1, Data1, 2,100);
+			SPI1_CS_NA;
+ }    
+
+uint16_t SPI_Flash_ReadID(void)
+{
+	uint16_t Temp = 0;  
+	uint8_t Data1[6] = {W25X_ManufactDeviceID,0x00,0x00,0x00,0x00,0x00};
+	uint8_t Data2[6]= {0x00,0x00};
+//	uint8_t Rxdata[2];	
+			SPI1_CS_EN;	
+			//SPI_Transmit(SPI1, Data1,4,100);
+			// SPI_TransmitReceive(SPI1,Data1,Data2,6,100);
+			SPI1_Tranceive(Data1,Data2,6);
+			SPI1_CS_NA;
+
+	Temp=(Data2[4]<<8)|Data2[5];
+return Temp;
+}       
+uint64_t SPI_Flash_ReadUID(uint8_t * Uid)
+{
+	union 
+	{
+			uint64_t Temp; 
+			uint32_t temp2[2];
+			uint8_t Rxdata[8];		
+	}uids;
+ 
+	uint8_t Data1[5] = {W25X_ReadUniqueID,0x00,0x00,0x00,0x00};
+	uint8_t Data2[8]= {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+	SPI1_CS_EN;
+	SPI_Transmit(SPI1, Data1,5,100);	
+	SPI_TransmitReceive(SPI1, Data2,uids.Rxdata,8,100);	
+	SPI1_CS_NA;	
+	memcpy(Uid,uids.Rxdata,8);
+	uint32_t t1 = __rev(uids.temp2[0]);
+	uids.temp2[0]= __rev(uids.temp2[1]);
+	uids.temp2[1]=t1;
+return uids.Temp;
+}       
+
+void SPI_FLASH_Write_Enable(void)   
+{
+	uint8_t Txdata[2]={W25X_WriteEnable};	
+	SPI1_CS_EN;
+	SPI1_Tranceive(Txdata,Txdata,1);	
+	SPI1_CS_NA;	    
+} 
+
+void SPI_FLASH_Write_Disable(void)   
+{  
+	uint8_t Txdata[2]={W25X_WriteDisable};	
+	SPI1_CS_EN;
+	SPI_Transmit(SPI1, Txdata,1,100);	
+	SPI1_CS_NA;	      
+}       
+void W25QXX_Erase_Sector(uint32_t Dst_Addr)   
+{  
+//??falsh????,???   
+
+  Dst_Addr*=4096;
+	uint8_t Data1[4] = {W25X_SectorErase,0x00,0x00,0x00};
+	Data1[1]=Dst_Addr>>16;
+	Data1[2]=Dst_Addr>>8;
+	Data1[3]=Dst_Addr;	
+  SPI_FLASH_Write_Enable();                   //SET WEL  ,???
+		SPI_Flash_Wait_Busy();  	
+	
+	SPI1_CS_EN;
+	SPI1_Tranceive(Data1,Data1,4);	
+	SPI1_CS_NA;	  
+		SPI_Flash_Wait_Busy();     //??????	
+ }  
+void W25QXX_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead)   
+{ 
+  uint16_t i; 
+	uint8_t Data1[4] = {W25X_ReadData,0x00,0x00,0x00};
+//	uint8_t Data2[16]= {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
+	
+	Data1[1]=ReadAddr>>16;
+	Data1[2]=ReadAddr>>8;
+	Data1[3]=ReadAddr;
+	
+	SPI1_CS_EN;
+	SPI_Transmit(SPI1, Data1,4,100);	
+//	HAL_SPI_TransmitReceive(&hspi1,pBuffer,pBuffer,NumByteToRead,10);
+//		HAL_SPI_TransmitReceive_DMA(&hspi1,pBuffer,pBuffer,NumByteToRead);
+	SPI1_Tranceive(pBuffer,pBuffer,NumByteToRead);	
+/*	
+   for(i=0;i<NumByteToRead/16;i++)
+		{ 
+			HAL_SPI_TransmitReceive(&hspi1,Data2,pBuffer+i*16,16,100);       
+    }
+		if (NumByteToRead%16)
+		{
+				HAL_SPI_TransmitReceive(&hspi1,Data2,pBuffer+i*16,NumByteToRead%16,100);   		
+		}
+	*/
+	SPI1_CS_NA;   
+}  
+void W25QXX_Write_Page(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
+{
+	uint8_t Data1[4] = {W25X_PageProgram,0x00,0x00,0x00};
+	
+	Data1[1]=WriteAddr>>16;
+	Data1[2]=WriteAddr>>8;
+	Data1[3]=WriteAddr;
+	
+	SPI_FLASH_Write_Enable();                   //SET WEL  ,???
+	SPI_Flash_Wait_Busy();  	
+	SPI1_CS_EN;
+	SPI_Transmit(SPI1, Data1,4,100);	
+	SPI_Transmit(SPI1, pBuffer,NumByteToWrite,100);	
+	SPI1_CS_NA; 
+	SPI_Flash_Wait_Busy();     //??????		
+} 
+/*
+int flash_func(int argc, char * argv[])
+{
+	char str1[128];
+	int len1;
+	uint8_t databuf[4096];
+	if (argc<=1)
+	{
+		len1=sprintf(str1," id uid sr en dis read speed er write write2\r\n");fputstr(str1,len1,0);
+	}else if (argc>=2)
+	{
+		if (strcmp(argv[1],"id")==0)
+		{
+			uint16_t flashid;
+			flashid=SPI_Flash_ReadID();
+			len1=sprintf(str1,"Readid %4X \r\n",flashid);
+			fputstr(str1,len1,0);
+			
+		}		else if (strcmp(argv[1],"sr")==0)
+		{
+			uint8_t flashsr;
+			flashsr=SPI_Flash_ReadSR();
+			len1=sprintf(str1,"Readsr %2X \r\n",flashsr);
+			fputstr(str1,len1,0);	
+		}		else if (strcmp(argv[1],"uid")==0)
+		{
+			uint8_t Uid[8];
+			uint64_t uid;
+			uid=SPI_Flash_ReadUID(Uid);
+			len1=sprintf(str1,"Uid %016llX %02X%02X%02X%02X%02X%02X%02X%02X \r\n",uid,Uid[0],Uid[1],Uid[2],Uid[3],Uid[4],Uid[5],Uid[6],Uid[7]);
+			fputstr(str1,len1,0);	
+		}		else if (strcmp(argv[1],"en")==0)
+		{
+			SPI_FLASH_Write_Enable();
+			len1=sprintf(str1,"Write Enable \r\n");
+			fputstr(str1,len1,0);	
+		}		else if (strcmp(argv[1],"dis")==0)
+		{
+			SPI_FLASH_Write_Disable();
+			len1=sprintf(str1,"Write Disable \r\n");
+			fputstr(str1,len1,0);	
+		}else if (strcmp(argv[1],"read")==0)
+		{
+			len1=0;
+			uint32_t addr=0;
+			if (argc>=3) addr=atoi(argv[2]);
+			uint16_t datalens=256;
+			len1=sprintf(str1,"Read Flash %08X for %d bytes \r\n",addr,datalens);
+			fputstr(str1,len1,0);				
+			W25QXX_Read(databuf,addr,datalens);
+			for (int i=0;i<16;i++)
+			{
+				len1=sprintf(str1,"%2X: ",i);
+				for (int j=0;j<16;j++)
+				{
+					len1+=sprintf(str1+len1," %2X",databuf[i*16+j]);
+				}
+				len1+=sprintf(str1+len1,"\r\n");
+				fputstr(str1,len1,0);				
+			}
+		}else if (strcmp(argv[1],"speed")==0)
+		{
+			len1=0;
+			uint32_t addr=0;
+			if (argc>=3) addr=atoi(argv[2]);
+			uint16_t datalens=4096;
+			int times=1000;
+			len1=sprintf(str1,"Read Flash speed test for %d bytes %d times\r\n",datalens,times);
+			fputstr(str1,len1,0);	
+			int time1=HAL_GetTick();
+			for (int i=0;i<times;i++)
+			{
+				addr=i*4096;
+				W25QXX_Read(databuf,addr,datalens);			
+			}
+			int time2=HAL_GetTick();
+			int deltime=time2-time1;
+			if (deltime < 1) {deltime =1;}
+			len1=sprintf(str1,"%d mS %dK/s \r\n",deltime,datalens*times/deltime);
+			fputstr(str1,len1,0);	
+			
+		}else if (strcmp(argv[1],"er")==0)
+		{
+			uint32_t addr=0;
+			if (argc>=3) addr=atoi(argv[2]);
+			W25QXX_Erase_Sector(addr);
+			len1=sprintf(str1," erase %d\r\n",addr);
+			fputstr(str1,len1,0);	
+		}	else if (strcmp(argv[1],"write")==0)
+		{
+			uint32_t addr=0;
+			if (argc>=3) addr=atoi(argv[2]);
+			uint16_t datalens=256;
+			uint8_t value;
+			if (argc>=4) 
+			{
+				value=atoi(argv[3]);
+				for(int i=0;i<256;i++)	{databuf[i]=value;}
+			}else 	for(int i=0;i<256;i++)	{databuf[i]=i;}
+			W25QXX_Write_Page(databuf,addr,datalens);
+			len1=sprintf(str1," write %06X for %d bytes\r\n",addr,datalens);
+			fputstr(str1,len1,0);	
+		}	else if (strcmp(argv[1],"write2")==0)
+		{
+			uint32_t addr=0;
+			if (argc>=3) addr=atoi(argv[2]);
+			uint16_t datalens=256;
+			for(int i=0;i<256;i++)	{databuf[i]=0;}
+			W25QXX_Write_Page(databuf,addr,datalens);
+			len1=sprintf(str1," write %06X for %d bytes\r\n",addr,datalens);
+			fputstr(str1,len1,0);			}	
+		else
+		{
+				len1=sprintf(str1,"unknown %s \r\n",argv[1]);
+				fputstr(str1,len1,0);					
+		}
+		
+	}
+	return 0;
+}
+*/
+

--
Gitblit v1.9.1