/** ****************************************************************************** * @file : FP0.c * @brief : FP0 funcstions program body ****************************************************************************** */ #include "globaldef.h" #include "FP0.h" #include "string.h" #include "functions.h" uint8_t PkgBuf1[32]; uint8_t PkgBuf2[32]; uint8_t PkgLen1=0; uint8_t PkgLen2=0; uint8_t bReceiving; uint8_t nReceivedLen; uint8_t bSending; uint8_t bSentLen; uint8_t bConfiged=0; uint8_t nConfigAddr=0; uint8_t nInputBytes=8; uint8_t nOutputBytes=8; uint8_t nIndex=0; unsigned char SPI1RecvBuf[32]; unsigned char SPI1SendBuf[32]; 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; volatile int oldSYN=0; int RSTCount0; int bFirstReq = 0; uint8_t FP0_Init() { bSPI1Sending=0; bSPI1Recving=0; if (nChilds==0) { nInputBytes=1; //根据子机数量,报告扩展容量 nOutputBytes=1; } else { nInputBytes=nChilds; //根据子机数量,报告扩展容量 nOutputBytes=nChilds; } SetACKPin_0(); SetACKPin_0(); SetFP0DEPin_0(); HAL_Delay(10); RSTCount0=GetuS(); return 0; } uint8_t FP0_Proc() { int CurRST=GetRSTPin(); int CurSEL=GetSELPin(); int CurSYN=GetSYNPin(); int CurACK=GetACKPin(); int CurOE=GetOEPin(); // KMem.WY[4]=CurRST; // KMem.WY[5]=CurSEL; // KMem.WY[6]=CurSYN; // KMem.WY[7]=CurACK; if (CurOE) { for (int j=0;j=10000) { SetACKPin_1(); bFirstReq=1; // RSTCount=0; // bSPI1Recving=1; KMem.WX[7]=RSTCount/1000; } } if (CurSYN ==0 && oldSYN != 0) { KMem.SDT[121] = KMem.SDT[122]; KMem.SDT[122]=0; } if (CurSEL && CurSYN !=0 && oldSYN == 0){ bSPI1Recving=1; nSPI1RecvPos=0; LL_SPI_Disable(SPI2); nSPI1RecvPos=0; LL_SPI_Enable(SPI2); } if (CurSEL && CurSYN && !bSPI1Sending && !bSPI1Recving) { bSPI1Recving=1; nSPI1RecvPos=0; } if (CurSYN == 0){ bSPI1Recving=0; nSPI1RecvPos=0; } oldSYN=CurSYN; /* if (bSPI1RecvDone) { bSPI1RecvDone=0; ParsePkg(SPI1RecvBuf,nSPI1RecvLenInBuf); } // */ return 0; } uint8_t CalFP0BCC(uint8_t* pBuf, uint8_t len1) { uint8_t BCC=0; for (int i=0;i=100) {KMem.SDT[123]=81;} if (nAddr<0x80) return 0; switch (nCMD) { case CMD_0_QUERY: if (!bConfiged || (bConfiged && nAddr == nConfigAddr)) { KMem.DT[8]++; pFP0QuRplyPkg p1 = (pFP0QuRplyPkg)PkgBuf2; p1->Hdr1=nST; p1->nInputBytes=0x30|nInputBytes; p1->nOutputBytes=0x30|nOutputBytes; p1->nParam1=0x30|0x05; p1->nBCC= 0x30|CalFP0BCC(PkgBuf2,4); p1->End1=0x0d; KMem.SDB[128+KMem.SDT[123]] = 0x11; KMem.SDT[123]++; if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;} SendFP0Pkg(PkgBuf2,sizeof(stFP0QuRplyPkg)); nConfigAddr=nAddr; bConfiged=1; } bSPI1Recving=1; break; case CMD_1: KMem.DT[9]++; // bSPI1Recving=1; break; case CMD_2: KMem.DT[10]++; // bSPI1Recving=1; break; case CMD_3_EXCHG: if (!bConfiged || nConfigAddr != nAddr) { bSPI1Recving=1; break; } KMem.DT[11]++; { for (int j=0;jHdr1=nST; for (int j=0;jnInputBytes[j*2 + 0]=0x30|HiHofB(KMem.WXB[j]); //((KMem.DT[0]>>4)&0x0f); p1->nInputBytes[j*2 + 1]=0x30|LoHofB(KMem.WXB[j]); //((KMem.DT[0]>>0)&0x0f); } //p1->nInputBytes[0]=0x30|HiHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>4)&0x0f); //p1->nInputBytes[1]=0x30|LoHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>0)&0x0f); //p1->nInputBytes[2]=0x30|HiHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>12)&0x0f); //p1->nInputBytes[3]=0x30|LoHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>8)&0x0f); //p1->nInputBytes[4]=0x30|((KMem.DT[1]>>4)&0x0f); //p1->nInputBytes[5]=0x30|((KMem.DT[1]>>0)&0x0f); PkgBuf2[nInputBytes*2 + 1 ]=0x30|CalFP0BCC(PkgBuf2,nInputBytes*2+1); // p1->nBCC= 0x30|CalBCC(PkgBuf2,7); PkgBuf2[nInputBytes*2 + 2 ]=0x0d; // p1->End1=0x0d; SendFP0Pkg(PkgBuf2,nInputBytes*2 + 3); } bSPI1Recving=1; break; case CMD_4: KMem.DT[12]++; // bSPI1Recving=1; break; case CMD_5: KMem.DT[13]++; // bSPI1Recving=1; break; case CMD_6: KMem.DT[14]++; // bSPI1Recving=1; break; case CMD_7_END: KMem.DT[15]++; if (bConfiged && nConfigAddr == nAddr) { //SetFP0DEPin_0(); SetACKPin_1(); } bSPI1Recving=1; break; default: KMem.DT[18]++; bSPI1Recving=1; break; } KMem.DT[24+(len1&0x0f)]++; return res; } uint8_t SendFP0Pkg(uint8_t * pBuf, uint8_t len1) { uint8_t res=0; KMem.WR[len1&0x0f]++; if (!bSPI1Sending) { uint8_t value; memcpy(SPI1SendBuf,pBuf,len1); nSPI1ToSendLen=len1; nSPI1SentLen=0; // SetFP0DEPin_1(); // SetACKPin_0(); value = SPI1SendBuf[nSPI1SentLen]; LL_SPI_TransmitData8(SPI1,value); bSPI1Sending=1; KMem.SDB[128+KMem.SDT[123]] = value; KMem.SDT[123]++; if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;} // passive mode SetFP0DEPin_1(); SetACKPin_0(); } return res; }