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 | 205 +++++++++++++++++++++++++++++++------------------- 1 files changed, 127 insertions(+), 78 deletions(-) diff --git a/ComLib/Src/functions.c b/ComLib/Src/functions.c index 534de72..436f204 100644 --- a/ComLib/Src/functions.c +++ b/ComLib/Src/functions.c @@ -519,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 @@ -899,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(); @@ -1016,83 +1004,144 @@ //W25Q16 ID 0XEF14 //W25Q32 ID 0XEF15 //W25Q32 ID 0XEF16 -#define W25Q80 0XEF13 -#define W25Q16 0XEF14 -#define W25Q32 0XEF15 -#define W25Q64 0XEF16 + +#define W25Q80 0XEF13 +#define W25Q16 0XEF14 +#define W25Q32 0XEF15 +#define W25Q64 0XEF16 +#define W25Q128 0xEF17 //????? -#define SPI_FLASH_CS PAout[15] //??FLASH,???????PB12? -#define SPI_CS_EN LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_15) -#define SPI_CS_NA LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_15) +#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(uint16_t Y) +uint8_t SPI_Tranceive8(SPI_TypeDef * SPIx, uint8_t Y) { int i=0; - while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET){ DelayUs(1);i++; if (i>320) break; } - LL_SPI_TransmitData8(SPI2,Y>>8); + 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(SPI2) == SET) { DelayUs(1);i++; if (i>320) break;} + while (LL_SPI_IsActiveFlag_BSY(SPIx) == SET) { DelayUs(1);i++; if (i>3200) break;} i=0; - while (LL_SPI_IsActiveFlag_RXNE(SPI2) == RESET) { DelayUs(1);i++; if (i>320) break;} - Y = LL_SPI_ReceiveData8(SPI2); + 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(uint8_t * tData, uint8_t nLen, uint8_t timeout) +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(tData[i]); + SPI_Tranceive8(SPIx, tData[i]); } return 0; } -uint8_t SPI_TransmitReceive(uint8_t * tData, uint8_t * rData, uint8_t nLen, uint8_t timeout) +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(tData[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 SPI_Flash_ReadSR(void) + +uint8_t SPI1_Flash_ReadSR(void) { uint8_t Data1[2]= {W25X_ReadStatusReg,0x00}; uint8_t Rxdata[2]; uint8_t byte=0; - SPI_CS_EN; - SPI_TransmitReceive(Data1,Rxdata,2,100); - SPI_CS_NA; + SPI1_CS_EN; + SPI1_Tranceive(Data1,Rxdata,2); + SPI1_CS_NA; byte=Rxdata[1]; return byte; } void SPI_Flash_Wait_Busy(void) { - while((SPI_Flash_ReadSR()&0x01)==0x01); // ??BUSY??? + 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; - SPI_CS_EN; - SPI_Transmit(Data1,2,100); - SPI_CS_NA; + 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[4] = {W25X_ManufactDeviceID,0x00,0x00,0x00}; - uint8_t Data2[2]= {0x00,0x00}; - uint8_t Rxdata[2]; - SPI_CS_EN; - SPI_Transmit(Data1,4,100); - SPI_TransmitReceive(Data2,Rxdata,2,100); - SPI_CS_NA; - Temp=(Rxdata[0]<<8)|Rxdata[1]; + 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) @@ -1106,10 +1155,10 @@ uint8_t Data1[5] = {W25X_ReadUniqueID,0x00,0x00,0x00,0x00}; uint8_t Data2[8]= {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - SPI_CS_EN; - SPI_Transmit(Data1,5,100); - SPI_TransmitReceive(Data2,uids.Rxdata,8,100); - SPI_CS_NA; + 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]); @@ -1120,17 +1169,17 @@ void SPI_FLASH_Write_Enable(void) { uint8_t Txdata[2]={W25X_WriteEnable}; - SPI_CS_EN; - SPI_Transmit(Txdata,1,100); - SPI_CS_NA; + SPI1_CS_EN; + SPI1_Tranceive(Txdata,Txdata,1); + SPI1_CS_NA; } void SPI_FLASH_Write_Disable(void) { uint8_t Txdata[2]={W25X_WriteDisable}; - SPI_CS_EN; - SPI_Transmit(Txdata,1,100); - SPI_CS_NA; + SPI1_CS_EN; + SPI_Transmit(SPI1, Txdata,1,100); + SPI1_CS_NA; } void W25QXX_Erase_Sector(uint32_t Dst_Addr) { @@ -1144,26 +1193,26 @@ SPI_FLASH_Write_Enable(); //SET WEL ,??? SPI_Flash_Wait_Busy(); - SPI_CS_EN; - SPI_Transmit(Data1,4,100); - SPI_CS_NA; + 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}; +// 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; - SPI_CS_EN; - SPI_Transmit(Data1,4,100); + 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); - SPI_TransmitReceive(pBuffer,pBuffer,NumByteToRead,100); + SPI1_Tranceive(pBuffer,pBuffer,NumByteToRead); /* for(i=0;i<NumByteToRead/16;i++) { @@ -1174,7 +1223,7 @@ HAL_SPI_TransmitReceive(&hspi1,Data2,pBuffer+i*16,NumByteToRead%16,100); } */ - SPI_CS_NA; + SPI1_CS_NA; } void W25QXX_Write_Page(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite) { @@ -1186,10 +1235,10 @@ SPI_FLASH_Write_Enable(); //SET WEL ,??? SPI_Flash_Wait_Busy(); - SPI_CS_EN; - SPI_Transmit(Data1,4,100); - SPI_Transmit(pBuffer,NumByteToWrite,100); - SPI_CS_NA; + SPI1_CS_EN; + SPI_Transmit(SPI1, Data1,4,100); + SPI_Transmit(SPI1, pBuffer,NumByteToWrite,100); + SPI1_CS_NA; SPI_Flash_Wait_Busy(); //?????? } /* -- Gitblit v1.9.1