From 005755edcdc332315ba077598d4746ac195b069e Mon Sep 17 00:00:00 2001 From: QuakeGod <QuakeGod@sina.com> Date: 星期一, 14 十月 2024 11:45:56 +0800 Subject: [PATCH] increase KPLC KBus from 2 client to 16 clients --- ComLib/Src/functions.c | 435 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 397 insertions(+), 38 deletions(-) diff --git a/ComLib/Src/functions.c b/ComLib/Src/functions.c index 64bc59e..534de72 100644 --- a/ComLib/Src/functions.c +++ b/ComLib/Src/functions.c @@ -9,7 +9,7 @@ #include "string.h" #include "stm32f0xx_hal.h" #if (BOARD_TYPE == 14) -#include "fp0.h" +#include "fpx.h" #endif extern __IO uint32_t uwTick; //#include "Myprotocol.h" @@ -23,6 +23,22 @@ //volatile unsigned int TotalRunTime=0; //鎬诲紑鏈烘椂闂� //volatile unsigned int PwrCount=0; //寮�鏈烘鏁� unsigned short ClkuS; //姣忎釜Clk鐨刵S鏁帮紝 + +unsigned char SPI1RecvBuf[64]; +unsigned char SPI1SendBuf[64]; + +volatile unsigned char bSPI1Recving=0; +volatile unsigned char bSPI1RecvDone =0; + +volatile unsigned char nSPI1RecvPos =0; +volatile unsigned char nSPI1RecvLenInBuf=0; + +volatile unsigned char nSPI1ToSendLen=0; +volatile unsigned char nSPI1SentLen=0; + +volatile unsigned char bSPI1Sending=0; +volatile unsigned char bSPI1SendDone=0; + int InituS(int TickFreq1) { @@ -57,10 +73,11 @@ void logData(unsigned char d) { - KMem.WDB[128+KMem.WDT[123]] = d; - KMem.WDT[123]++; if (KMem.WDT[123]>=100) {KMem.WDT[123]=81;} + KMem.WDB[128+KMem.WDT[7]] = d; + KMem.WDT[7]++; if (KMem.WDT[7]>=100) {KMem.WDT[7]=81;} } +/* const unsigned short crc16_table[256] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, @@ -132,7 +149,7 @@ } return(crc); } - + */ /* Table of CRC values for high-order byte */ const uint8_t crctablehi[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, @@ -175,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; @@ -189,7 +206,28 @@ } 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() { printf("\n"); @@ -200,12 +238,12 @@ // printf(" modbus crc16tablefast test, expected value : 0xd825, calculate value : 0x%x\n", crc16tablefast(crc16_data, sizeof(crc16_data))); printf(" modbus crc16bitbybit test, expected value : 0xd825, calculate value : 0x%x\n", crc16bitbybit(crc16_data, sizeof(crc16_data))); } - +*/ int InitUartstat(stUartStat * pUartstat,void * pBufRx, int nSizeRx, void * pBufTx, int nSizeTx) { memset(pUartstat,sizeof(stUartStat),0); - initQueue(&pUartstat->QRx,pBufRx,nSizeRx); - initQueue(&pUartstat->QTx,pBufTx,nSizeTx); + if (pBufRx) initQueue(&pUartstat->QRx,pBufRx,nSizeRx); + if (pBufTx) initQueue(&pUartstat->QTx,pBufTx,nSizeTx); return 0; } @@ -245,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(); } @@ -297,26 +343,6 @@ SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos; } -void PendSvCallBack() -{ -#if (BOARD_TYPE == 14) -///* - if (bSPI1RecvDone) - { - bSPI1RecvDone=0; - ParseFP0Pkg(SPI1RecvBuf,nSPI1RecvLenInBuf); - } -//*/ -#endif - if (Uart2Stat.bPacketRecved) - { - KBusParsePacket(2, (pKBPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen); - Uart2RecvBuf1DataLen=0; - Uart2Stat.bPacketRecved=0; - Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1)); - } -} - void SPI1_IRQ_CallBack() { uint8_t value; @@ -360,7 +386,7 @@ bSPI1SendDone=1; bSPI1Recving=1; nSPI1RecvPos=0; - SetFP0DEPin_0(); + SetFPxDEPin_0(); } else { value = SPI1SendBuf[nSPI1SentLen]; @@ -368,7 +394,9 @@ logData(value); } } -#endif +#else + UNUSED(value); +#endif } } @@ -384,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(); } } @@ -400,13 +429,15 @@ #else Uart2UnsetDE(); #endif + Uart2Stat.bSendDone = 1; + TriggerPendSV(); } 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); } @@ -427,6 +458,7 @@ // LL_USART_EnableIT_TXE(USART1); // LL_USART_EnableIT_TC(USART1); Uart1TriggerSendDMA(); + Uart1Stat.SentPacket++; return len1; } int PutStr2(char * str1, int len1) @@ -436,21 +468,23 @@ // LL_USART_EnableIT_TXE(USART1); // LL_USART_EnableIT_TC(USART1); // Uart2TriggerSendDMA(); + Uart2Stat.SentPacket++; return len1; } 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(); - Uart1Stat.SentPacket++; + }else if (nChn==2){ PutStr2((char *)pBuf,len1); // PushIn(&Uart2Stat.QTx,p1,len1); // Uart2TriggerSendDMA(); - Uart2Stat.SentPacket++; + } return len1; } @@ -653,7 +687,7 @@ case 13: return Input165_R(16); case 14: - return 0; //FP0 + return 0; //FPx case 15: return Input165_R(16); case 16: @@ -748,7 +782,7 @@ case 13: return ReadConfig_5(); case 14: - return (~(LL_GPIO_ReadInputPort(GPIOA)>>4))&0x0f; //FP0 + return (~(LL_GPIO_ReadInputPort(GPIOA)>>4))&0x0f; //FPx case 15: case 16: return ReadConfig_5(); //Wireless Master Slave 8 in 8 o @@ -955,3 +989,328 @@ 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 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) + + + +uint8_t SPI_Tranceive8(uint16_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); + i=0; + while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET) { DelayUs(1);i++; if (i>320) break;} + i=0; + while (LL_SPI_IsActiveFlag_RXNE(SPI2) == RESET) { DelayUs(1);i++; if (i>320) break;} + Y = LL_SPI_ReceiveData8(SPI2); + return Y; +} + +uint8_t SPI_Transmit(uint8_t * tData, uint8_t nLen, uint8_t timeout) +{ + for (int i=0;i<nLen;i++) { + SPI_Tranceive8(tData[i]); + } + return 0; +} + +uint8_t SPI_TransmitReceive(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]); + } + return 0; +} + + +uint8_t SPI_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; + byte=Rxdata[1]; +return byte; +} +void SPI_Flash_Wait_Busy(void) +{ + while((SPI_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; + } + +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]; +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}; + SPI_CS_EN; + SPI_Transmit(Data1,5,100); + SPI_TransmitReceive(Data2,uids.Rxdata,8,100); + SPI_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}; + SPI_CS_EN; + SPI_Transmit(Txdata,1,100); + SPI_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; +} +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(); + + SPI_CS_EN; + SPI_Transmit(Data1,4,100); + SPI_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; + + SPI_CS_EN; + SPI_Transmit(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); +/* + 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); + } + */ + SPI_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(); + SPI_CS_EN; + SPI_Transmit(Data1,4,100); + SPI_Transmit(pBuffer,NumByteToWrite,100); + SPI_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