QuakeGod
2024-07-27 842bb64195f958b050867c50db66fc0aa413dafb
提交 | 用户 | age
5dd1b7 1 /**
Q 2   ******************************************************************************
3   * @file           : FPx.c
4   * @brief          : FPx funcstions program body
5   ******************************************************************************
6     */
7
8 #include "globaldef.h"
9 #include "FPx.h"
10 #include "string.h"
11 #include "functions.h"
12
13 // uint8_t PktBuf1[64];
14 uint8_t PktBuf2[64];
15
16 uint8_t PktLen1=0;
17 uint8_t PktLen2=0;
18
19 /*
20 uint8_t bReceiving;
21 uint8_t nReceivedLen;
22 uint8_t bSending;
23 uint8_t bSentLen;
24
25 uint8_t bConfiged=0;
26 uint8_t nConfigStationId=0;
27 uint8_t nInputBytes=8;
28 uint8_t nOutputBytes=8;
29 uint8_t nIndex=0;
30 volatile uint8_t oldSYN=0;
31
32 int RSTCount0;
33 uint8_t bFirstReq = 0;
34
35 FPxCBFuncDef FPxCBFunc;
36 uint8_t bFPxCallBackFuncSet = 0;
37 */
38
39 stFPxStat FPxStat;
40 stFPxMem FPxMem;
41
42 uint8_t FPx_Init(int nChilds)
43 {
44     bSPI1Sending=0;
45     bSPI1Recving=0;
46     if (nChilds==0) {
47     FPxStat.nInputBytes=1;        //根据子机数量,报告扩展容量
48     FPxStat.nOutputBytes=1;
49     } else if (nChilds ==1) {
50         FPxStat.nInputBytes=3;        //只有一个子机也分配一个完整DT
51         FPxStat.nOutputBytes=2;    
52     } else
53     {
842bb6 54         FPxStat.nInputBytes=nChilds;        //根据子机数量,报告扩展容量
5dd1b7 55         FPxStat.nOutputBytes=nChilds;
Q 56     }
57     SetACKPin_0();
58     SetACKPin_0();
59     SetFPxDEPin_0();
60
61         HAL_Delay(10);    
62     FPxStat.RSTCount0=GetuS();
63     return 0;
64 }
65
66 uint8_t FPx_SetIOCount(int nInputBytes, int nOutputBytes)
67 {
68         FPxStat.nInputBytes=nInputBytes;        //设置扩展容量,字节单位
69         FPxStat.nOutputBytes=nOutputBytes;    
70     return 0;
71 }
72
842bb6 73 int FPxSetCallBackFunc(FPxEvCBFuncDef func1)
5dd1b7 74 {
842bb6 75     FPxStat.FPxEvCBFunc = func1;
Q 76 //    FPxStat.bFPxCallBackFuncSet = 1;
5dd1b7 77     return 0;
Q 78 }
79
80 uint8_t FPx_Proc()
81 {
82         uchar CurRST=GetRSTPin();
83         uchar CurSEL=GetSELPin();
84         uchar CurSYN=GetSYNPin();
85         uchar CurACK=GetACKPin();
86         uchar CurOE=GetOEPin();
87 //        KMem.WY[4]=CurRST;
88 //        KMem.WY[5]=CurSEL;
89 //        KMem.WY[6]=CurSYN; 
90 //        KMem.WY[7]=CurACK; 
842bb6 91         if (CurOE) {            // disable output
5dd1b7 92             for (int j=0;j<FPxStat.nOutputBytes;j++)
Q 93                 {
94                     FPxMem.WLYB[j]=0; 
95                 }
842bb6 96                 if (FPxStat.FPxEvCBFunc) FPxStat.FPxEvCBFunc(evFPxStateChange,0,0);
Q 97                 if (FPxStat.FPxEvCBFunc) FPxStat.FPxEvCBFunc(evFPxDataUpdate,0,0);
5dd1b7 98             }
Q 99       
842bb6 100         if (!CurRST) {         // in  reset 
Q 101             FPxStat.RSTCount0=GetuS();
102             SetACKPin_0();
103             FPxStat.bFirstReq=0;
104             LL_SPI_Disable(SPI2);
105             FPxStat.bConfiged = 0;
106             nSPI1RecvPos=0;
107             
108         }    
5dd1b7 109         if (!FPxStat.bFirstReq && CurRST)
842bb6 110         {                // after Reset; 
5dd1b7 111             int RSTCount = GetuS() - FPxStat.RSTCount0 ;
842bb6 112             if (RSTCount>=1000)
5dd1b7 113             {
842bb6 114                 logData(0x11);
Q 115                 
5dd1b7 116                 SetACKPin_1();
Q 117                 FPxStat.bFirstReq=1;
118             //    RSTCount=0;
119         //    bSPI1Recving=1;
120         //        KMem.WX[7]=RSTCount/1000;
842bb6 121                 LL_SPI_Disable(SPI2);
Q 122                 LL_SPI_Enable(SPI2);
123                 nSPI1RecvPos=0;                
5dd1b7 124             }
Q 125         }
126     if (CurSYN ==0 && FPxStat.oldSYN != 0) {        
127         KMem.WDT[121] =    KMem.WDT[122];
128         KMem.WDT[122]=0;
842bb6 129         nSPI1RecvPos=0;    
Q 130             logData(0x12);
5dd1b7 131         }
Q 132     if (CurSEL && CurSYN !=0 && FPxStat.oldSYN == 0){
133                 bSPI1Recving=1;
842bb6 134             //    nSPI1RecvPos=0;
5dd1b7 135             LL_SPI_Disable(SPI2);
842bb6 136             //    nSPI1RecvPos=0;
5dd1b7 137                 LL_SPI_Enable(SPI2);
842bb6 138             logData(0x13);
5dd1b7 139         }
Q 140     if (CurSEL && CurSYN && !bSPI1Sending && !bSPI1Recving)
141     {
142                 bSPI1Recving=1;
143                 nSPI1RecvPos=0;        
842bb6 144             logData(0x14);
5dd1b7 145     }
Q 146         
147     if (CurSYN == 0){
148         bSPI1Recving=0;
149       nSPI1RecvPos=0;
150     }    
151         FPxStat.oldSYN=CurSYN;    
152     
153 /*        
154         if (bSPI1RecvDone)
155         {
156             bSPI1RecvDone=0;
157             ParsePkt(SPI1RecvBuf,nSPI1RecvLenInBuf);
158         }
159 // */    
160     
161     return 0;
162 }
163
842bb6 164 uint8_t FPxBCC(uint8_t* pBuf, uint8_t len1)
5dd1b7 165 {
Q 166     uint8_t BCC=0;
167     for (int i=0;i<len1;i++)
168     {
169         BCC+=pBuf[i]&0x0f;
170     }
171     BCC&=0x0f;
172     return BCC;
173 }
174
175 uint8_t FPxChecPkt(uint8_t * pBuf, uint8_t len1)
176 {
177     uint8_t res=0;
178     
179     return res;
180 }
181
182 uint8_t FPxParsePkt(uint8_t * pBuf, uint8_t len1)
183 {
184     uint8_t res=0;
185     uint8_t nST=pBuf[0];
186     uint8_t nCMD=nST&0x7;
187     uint8_t nStationID=nST&0xf8;
188     
189     logData(0xff);
190     
191     if (nStationID<0x80) return 0;
192     switch (nCMD)
193     {
194         case CMD_0_QUERY:
195             if (!FPxStat.bConfiged || (FPxStat.bConfiged && nStationID == FPxStat.nConfigStationId))
196         {
197              KMem.WDT[8]++;
842bb6 198             
Q 199              KMem.WDT[0]= nST;
200              KMem.WDT[1]= len1;
201              KMem.WDT[2] = FPxStat.nConfigStationId;
202             
203              KMem.WDT[4]= FPxStat.nInputBytes;
204              KMem.WDT[5]= FPxStat.nOutputBytes;            
205             
5dd1b7 206             pFPxQuRplyPkt p1 = (pFPxQuRplyPkt)PktBuf2;
Q 207             p1->Hdr1=nST;
208             p1->nInputBytes=0x30|FPxStat.nInputBytes;
209             p1->nOutputBytes=0x30|FPxStat.nOutputBytes;
210             p1->nParam1=0x30|0x05;
842bb6 211             p1->nBCC= 0x30|FPxBCC(PktBuf2,4);
5dd1b7 212             p1->End1=0x0d;
Q 213             
842bb6 214             logData(0x22);
5dd1b7 215             
Q 216             FPxSendPkt(PktBuf2,sizeof(stFPxQuRplyPkt));
217             FPxStat.nConfigStationId=nStationID;
218             FPxStat.bConfiged=1;
842bb6 219                 if (FPxStat.FPxEvCBFunc) FPxStat.FPxEvCBFunc(evFPxStateChange,0,0);            
5dd1b7 220         }
Q 221         bSPI1Recving=1;
222             break;
223         case CMD_1:
224             KMem.WDT[9]++;
225 //            bSPI1Recving=1;
226             break;
227         case CMD_2:
228             KMem.WDT[10]++;
229 //            bSPI1Recving=1;
230             break;
231         case CMD_3_EXCHG:
232             if (!FPxStat.bConfiged || FPxStat.nConfigStationId != nStationID)
233             {
234                 bSPI1Recving=1;        
235                 break;
236             }
237              KMem.WDT[11]++;
238         {
239             for (int j=0;j<FPxStat.nOutputBytes;j++)
240             {
241                 FPxMem.WLYB[j]=((pBuf[j*2 + 1]&0xf)<<4) + ((pBuf[j*2 + 2]&0xf)<<0);
242             }
243 /* test 
244             for (int j=0;j<nOutputBytes;j++)
245             {
246                 KMem.WYB[j]=(0xff);
247             }    
248 // */        
842bb6 249             if (FPxStat.FPxEvCBFunc) FPxStat.FPxEvCBFunc(evFPxDataUpdate,0,0);
5dd1b7 250             
Q 251             // KMem.WDT[2]=((pBuf[1]&0xf)<<4) + ((pBuf[2]&0xf)<<0) + ((pBuf[3]&0xf)<<12) + ((pBuf[4]&0xf)<<8);
252             pFPxEXGRplyPkt p1 = (pFPxEXGRplyPkt)PktBuf2;
253             p1->Hdr1=nST;
254             for (int j=0;j<FPxStat.nInputBytes;j++)
255             {
256                 p1->nInputBytes[j*2 + 0]=0x30|HiHofB(FPxMem.WLXB[j]); //((KMem.DT[0]>>4)&0x0f);
257                 p1->nInputBytes[j*2 + 1]=0x30|LoHofB(FPxMem.WLXB[j]); //((KMem.DT[0]>>0)&0x0f); 
258             }
259             //p1->nInputBytes[0]=0x30|HiHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>4)&0x0f);
260             //p1->nInputBytes[1]=0x30|LoHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>0)&0x0f); 
261             //p1->nInputBytes[2]=0x30|HiHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>12)&0x0f);
262             //p1->nInputBytes[3]=0x30|LoHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>8)&0x0f);        
263             //p1->nInputBytes[4]=0x30|((KMem.DT[1]>>4)&0x0f);
264             //p1->nInputBytes[5]=0x30|((KMem.DT[1]>>0)&0x0f);
842bb6 265             PktBuf2[FPxStat.nInputBytes*2 + 1 ]=0x30|FPxBCC(PktBuf2,FPxStat.nInputBytes*2+1);         //    p1->nBCC= 0x30|CalBCC(PktBuf2,7);
5dd1b7 266             PktBuf2[FPxStat.nInputBytes*2 + 2 ]=0x0d;        //        p1->End1=0x0d;
Q 267             FPxSendPkt(PktBuf2,FPxStat.nInputBytes*2 + 3);
268         }    
269             bSPI1Recving=1;        
270         break;
271         case CMD_4:
272         KMem.WDT[12]++;
273 //            bSPI1Recving=1;
274             break;
275         case CMD_5:
276          KMem.WDT[13]++;
277 //            bSPI1Recving=1;
278             break;
279         case CMD_6:
280          KMem.WDT[14]++;
281 //            bSPI1Recving=1;
282             break;
283         case CMD_7_END:
284          KMem.WDT[15]++;
285             if (FPxStat.bConfiged && FPxStat.nConfigStationId == nStationID)
286             {        
287                 //SetFPxDEPin_0();
288                 SetACKPin_1();                
289             }
290             bSPI1Recving=1;
291             break;
292         default:
293          KMem.WDT[18]++;
294             bSPI1Recving=1;
295             break;
296     }
297      KMem.WDT[24+(len1&0x0f)]++;
298
299     return res;
300 }
301
302 uint8_t FPxSendPkt(uint8_t * pBuf, uint8_t len1)
303 {
304     uint8_t res=0;
305     KMem.WR[len1&0x0f]++;
306     if (!bSPI1Sending)
307     {
308         uint8_t value;
309         memcpy(SPI1SendBuf,pBuf,len1);
310         nSPI1ToSendLen=len1;
311         nSPI1SentLen=0;
312         
313 //        SetFPxDEPin_1();
314 //        SetACKPin_0();
315         
316         value = SPI1SendBuf[nSPI1SentLen];
317         LL_SPI_TransmitData8(SPI1,value);
318         bSPI1Sending=1;
319         
320     logData(value);        
321
322         // passive mode
323         SetFPxDEPin_1();
324         SetACKPin_0();
325     }
326     return res;
327 }