QuakeGod
2023-05-26 67f9b2e17e1fd66927bbed3667503065c9129c41
提交 | 用户 | age
7b8b07 1 /**
Q 2   ******************************************************************************
3   * @file           : FP0.c
4   * @brief          : FP0 funcstions program body
5   ******************************************************************************
6     */
7
8 #include "globaldef.h"
9 #include "FP0.h"
10 #include "string.h"
11 #include "functions.h"
12
13 uint8_t PkgBuf1[32];
14 uint8_t PkgBuf2[32];
15
16 uint8_t PkgLen1=0;
17 uint8_t PkgLen2=0;
18
19 uint8_t bReceiving;
20 uint8_t nReceivedLen;
21 uint8_t bSending;
22 uint8_t bSentLen;
23
24 uint8_t bConfiged=0;
0fe6b0 25 uint8_t nConfigStationId=0;
7b8b07 26 uint8_t nInputBytes=8;
Q 27 uint8_t nOutputBytes=8;
28 uint8_t nIndex=0;
29
30
31 unsigned char SPI1RecvBuf[32]; 
32 unsigned char SPI1SendBuf[32];
33
34 volatile unsigned char bSPI1Recving=0;
35 volatile unsigned char bSPI1RecvDone =0;
36
37 volatile unsigned char nSPI1RecvPos =0;
38 volatile unsigned char nSPI1RecvLenInBuf=0;
39
40 volatile unsigned char nSPI1ToSendLen=0;
41 volatile unsigned char nSPI1SentLen=0;
42
43 volatile unsigned char bSPI1Sending=0;
44 volatile unsigned char bSPI1SendDone=0;
45
46 volatile int oldSYN=0;
47
48 int RSTCount0;
49 int bFirstReq = 0;
50     
51 uint8_t FP0_Init()
52 {
53     bSPI1Sending=0;
54     bSPI1Recving=0;
55     if (nChilds==0) {
0fe6b0 56     nInputBytes=1;        //根据子机数量,报告扩展容量
7b8b07 57     nOutputBytes=1;
Q 58     } else
59     {
0fe6b0 60         nInputBytes=nChilds;        //根据子机数量,报告扩展容量
7b8b07 61         nOutputBytes=nChilds;
Q 62     }
63     SetACKPin_0();
64     SetACKPin_0();
65     SetFP0DEPin_0();
66
67         HAL_Delay(10);    
68     RSTCount0=GetuS();
69     return 0;
70 }
71
72 uint8_t FP0_Proc()
73 {
74         int CurRST=GetRSTPin();
75         int CurSEL=GetSELPin();
76         int CurSYN=GetSYNPin();
77         int CurACK=GetACKPin();
78         int CurOE=GetOEPin();
79 //        KMem.WY[4]=CurRST;
80 //        KMem.WY[5]=CurSEL;
81 //        KMem.WY[6]=CurSYN; 
82 //        KMem.WY[7]=CurACK; 
83         if (CurOE) {
84             for (int j=0;j<nOutputBytes;j++)
85                 {KMem.WYB[j]=0; }
86             }
87       
88         if (!CurRST) {RSTCount0=GetuS();}
89         if (!bFirstReq && CurRST)
90         {    
91             int RSTCount = GetuS() - RSTCount0 ;
92             if (RSTCount>=10000)
93             {
94                 SetACKPin_1();
95                 bFirstReq=1;
96             //    RSTCount=0;
97         //    bSPI1Recving=1;
98                 KMem.WX[7]=RSTCount/1000;
99             }
100         }
101     if (CurSYN ==0 && oldSYN != 0) {        
0fe6b0 102         KMem.WDT[121] =    KMem.WDT[122];
Q 103         KMem.WDT[122]=0;
7b8b07 104         }
Q 105     if (CurSEL && CurSYN !=0 && oldSYN == 0){
106                 bSPI1Recving=1;
107                 nSPI1RecvPos=0;
108             LL_SPI_Disable(SPI2);
109                 nSPI1RecvPos=0;
110                 LL_SPI_Enable(SPI2);                
111         }
112     if (CurSEL && CurSYN && !bSPI1Sending && !bSPI1Recving)
113     {
114                 bSPI1Recving=1;
115                 nSPI1RecvPos=0;        
116
117     }
118         
119     if (CurSYN == 0){
120         bSPI1Recving=0;
121       nSPI1RecvPos=0;
122     }    
123         oldSYN=CurSYN;    
124     
125 /*        
126         if (bSPI1RecvDone)
127         {
128             bSPI1RecvDone=0;
129             ParsePkg(SPI1RecvBuf,nSPI1RecvLenInBuf);
130         }
131 // */    
132     
133     return 0;
134 }
135
136 uint8_t CalFP0BCC(uint8_t* pBuf, uint8_t len1)
137 {
138     uint8_t BCC=0;
139     for (int i=0;i<len1;i++)
140     {
141         BCC+=pBuf[i]&0x0f;
142     }
143     BCC&=0x0f;
144     return BCC;
145 }
146
147 uint8_t CheckFP0Pkg(uint8_t * pBuf, uint8_t len1)
148 {
149     uint8_t res=0;
150     
151     return res;
152 }
153
154 uint8_t ParseFP0Pkg(uint8_t * pBuf, uint8_t len1)
155 {
156     uint8_t res=0;
157     uint8_t nST=pBuf[0];
158     uint8_t nCMD=nST&0x7;
0fe6b0 159     uint8_t nStationID=nST&0xf8;
7b8b07 160     
0fe6b0 161     logData(0xff);
Q 162     
163     if (nStationID<0x80) return 0;
7b8b07 164     switch (nCMD)
Q 165     {
166         case CMD_0_QUERY:
0fe6b0 167             if (!bConfiged || (bConfiged && nStationID == nConfigStationId))
7b8b07 168         {
Q 169              KMem.DT[8]++;
170             pFP0QuRplyPkg p1 = (pFP0QuRplyPkg)PkgBuf2;
171             p1->Hdr1=nST;
172             p1->nInputBytes=0x30|nInputBytes;
173             p1->nOutputBytes=0x30|nOutputBytes;
174             p1->nParam1=0x30|0x05;
175             p1->nBCC= 0x30|CalFP0BCC(PkgBuf2,4);
176             p1->End1=0x0d;
177             
0fe6b0 178             logData(0x11);
7b8b07 179             
Q 180             SendFP0Pkg(PkgBuf2,sizeof(stFP0QuRplyPkg));
0fe6b0 181             nConfigStationId=nStationID;
7b8b07 182             bConfiged=1;
Q 183         }
184         bSPI1Recving=1;
185             break;
186         case CMD_1:
187             KMem.DT[9]++;
188 //            bSPI1Recving=1;
189             break;
190         case CMD_2:
191             KMem.DT[10]++;
192 //            bSPI1Recving=1;
193             break;
194         case CMD_3_EXCHG:
0fe6b0 195             if (!bConfiged || nConfigStationId != nStationID)
7b8b07 196             {
Q 197                 bSPI1Recving=1;        
198                 break;
199             }
200              KMem.DT[11]++;
201         {
202             for (int j=0;j<nOutputBytes;j++)
203             {
204                 KMem.WYB[j]=((pBuf[j*2 + 1]&0xf)<<4) + ((pBuf[j*2 + 2]&0xf)<<0);
205             }
206             // KMem.DT[2]=((pBuf[1]&0xf)<<4) + ((pBuf[2]&0xf)<<0) + ((pBuf[3]&0xf)<<12) + ((pBuf[4]&0xf)<<8);
207             pFP0EXGRplyPkg p1 = (pFP0EXGRplyPkg)PkgBuf2;
208             p1->Hdr1=nST;
209             for (int j=0;j<nInputBytes;j++)
210             {
211                 p1->nInputBytes[j*2 + 0]=0x30|HiHofB(KMem.WXB[j]); //((KMem.DT[0]>>4)&0x0f);
212                 p1->nInputBytes[j*2 + 1]=0x30|LoHofB(KMem.WXB[j]); //((KMem.DT[0]>>0)&0x0f); 
213             }
214             //p1->nInputBytes[0]=0x30|HiHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>4)&0x0f);
215             //p1->nInputBytes[1]=0x30|LoHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>0)&0x0f); 
216             //p1->nInputBytes[2]=0x30|HiHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>12)&0x0f);
217             //p1->nInputBytes[3]=0x30|LoHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>8)&0x0f);        
218             //p1->nInputBytes[4]=0x30|((KMem.DT[1]>>4)&0x0f);
219             //p1->nInputBytes[5]=0x30|((KMem.DT[1]>>0)&0x0f);
220             PkgBuf2[nInputBytes*2 + 1 ]=0x30|CalFP0BCC(PkgBuf2,nInputBytes*2+1);         //    p1->nBCC= 0x30|CalBCC(PkgBuf2,7);
221             PkgBuf2[nInputBytes*2 + 2 ]=0x0d;        //        p1->End1=0x0d;
222             SendFP0Pkg(PkgBuf2,nInputBytes*2 + 3);
223         }    
224             bSPI1Recving=1;        
225         break;
226         case CMD_4:
227         KMem.DT[12]++;
228 //            bSPI1Recving=1;
229             break;
230         case CMD_5:
231          KMem.DT[13]++;
232 //            bSPI1Recving=1;
233             break;
234         case CMD_6:
235          KMem.DT[14]++;
236 //            bSPI1Recving=1;
237             break;
238         case CMD_7_END:
239          KMem.DT[15]++;
0fe6b0 240             if (bConfiged && nConfigStationId == nStationID)
7b8b07 241             {        
Q 242                 //SetFP0DEPin_0();
243                 SetACKPin_1();                
244             }
245             bSPI1Recving=1;
246             break;
247         default:
248          KMem.DT[18]++;
249             bSPI1Recving=1;
250             break;
251     }
252      KMem.DT[24+(len1&0x0f)]++;
253
254     return res;
255 }
256
257 uint8_t SendFP0Pkg(uint8_t * pBuf, uint8_t len1)
258 {
259     uint8_t res=0;
260     KMem.WR[len1&0x0f]++;
261     if (!bSPI1Sending)
262     {
263         uint8_t value;
264         memcpy(SPI1SendBuf,pBuf,len1);
265         nSPI1ToSendLen=len1;
266         nSPI1SentLen=0;
267         
268 //        SetFP0DEPin_1();
269 //        SetACKPin_0();
270         
271         value = SPI1SendBuf[nSPI1SentLen];
272         LL_SPI_TransmitData8(SPI1,value);
273         bSPI1Sending=1;
0fe6b0 274         
Q 275     logData(value);        
276
7b8b07 277         // passive mode
Q 278         SetFP0DEPin_1();
279         SetACKPin_0();
280     }
281     return res;
282 }