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