QuakeGod
2023-01-28 0917738f20328de743be272ce4b042945d701830
提交 | 用户 | 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;
25 uint8_t nConfigAddr=0;
26 uint8_t nInputBytes=8;
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) {
56     nInputBytes=1;        //根据子机数量,报告扩展容量
57     nOutputBytes=1;
58     } else
59     {
60         nInputBytes=nChilds;        //根据子机数量,报告扩展容量
61         nOutputBytes=nChilds;
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) {        
102         KMem.SDT[121] =    KMem.SDT[122];
103         KMem.SDT[122]=0;
104         }
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;
159     uint8_t nAddr=nST&0xf8;
160     KMem.SDB[128+KMem.SDT[123]] = 0xFF;
161     KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}
162     
163     if (nAddr<0x80) return 0;
164     switch (nCMD)
165     {
166         case CMD_0_QUERY:
167             if (!bConfiged || (bConfiged && nAddr == nConfigAddr))
168         {
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             
178             KMem.SDB[128+KMem.SDT[123]] = 0x11;
179             KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}            
180             
181             SendFP0Pkg(PkgBuf2,sizeof(stFP0QuRplyPkg));
182             nConfigAddr=nAddr;
183             bConfiged=1;
184         }
185         bSPI1Recving=1;
186             break;
187         case CMD_1:
188             KMem.DT[9]++;
189 //            bSPI1Recving=1;
190             break;
191         case CMD_2:
192             KMem.DT[10]++;
193 //            bSPI1Recving=1;
194             break;
195         case CMD_3_EXCHG:
196             if (!bConfiged || nConfigAddr != nAddr)
197             {
198                 bSPI1Recving=1;        
199                 break;
200             }
201              KMem.DT[11]++;
202         {
203             for (int j=0;j<nOutputBytes;j++)
204             {
205                 KMem.WYB[j]=((pBuf[j*2 + 1]&0xf)<<4) + ((pBuf[j*2 + 2]&0xf)<<0);
206             }
207             // KMem.DT[2]=((pBuf[1]&0xf)<<4) + ((pBuf[2]&0xf)<<0) + ((pBuf[3]&0xf)<<12) + ((pBuf[4]&0xf)<<8);
208             pFP0EXGRplyPkg p1 = (pFP0EXGRplyPkg)PkgBuf2;
209             p1->Hdr1=nST;
210             for (int j=0;j<nInputBytes;j++)
211             {
212                 p1->nInputBytes[j*2 + 0]=0x30|HiHofB(KMem.WXB[j]); //((KMem.DT[0]>>4)&0x0f);
213                 p1->nInputBytes[j*2 + 1]=0x30|LoHofB(KMem.WXB[j]); //((KMem.DT[0]>>0)&0x0f); 
214             }
215             //p1->nInputBytes[0]=0x30|HiHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>4)&0x0f);
216             //p1->nInputBytes[1]=0x30|LoHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>0)&0x0f); 
217             //p1->nInputBytes[2]=0x30|HiHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>12)&0x0f);
218             //p1->nInputBytes[3]=0x30|LoHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>8)&0x0f);        
219             //p1->nInputBytes[4]=0x30|((KMem.DT[1]>>4)&0x0f);
220             //p1->nInputBytes[5]=0x30|((KMem.DT[1]>>0)&0x0f);
221             PkgBuf2[nInputBytes*2 + 1 ]=0x30|CalFP0BCC(PkgBuf2,nInputBytes*2+1);         //    p1->nBCC= 0x30|CalBCC(PkgBuf2,7);
222             PkgBuf2[nInputBytes*2 + 2 ]=0x0d;        //        p1->End1=0x0d;
223             SendFP0Pkg(PkgBuf2,nInputBytes*2 + 3);
224         }    
225             bSPI1Recving=1;        
226         break;
227         case CMD_4:
228         KMem.DT[12]++;
229 //            bSPI1Recving=1;
230             break;
231         case CMD_5:
232          KMem.DT[13]++;
233 //            bSPI1Recving=1;
234             break;
235         case CMD_6:
236          KMem.DT[14]++;
237 //            bSPI1Recving=1;
238             break;
239         case CMD_7_END:
240          KMem.DT[15]++;
241             if (bConfiged && nConfigAddr == nAddr)
242             {        
243                 //SetFP0DEPin_0();
244                 SetACKPin_1();                
245             }
246             bSPI1Recving=1;
247             break;
248         default:
249          KMem.DT[18]++;
250             bSPI1Recving=1;
251             break;
252     }
253      KMem.DT[24+(len1&0x0f)]++;
254
255     return res;
256 }
257
258 uint8_t SendFP0Pkg(uint8_t * pBuf, uint8_t len1)
259 {
260     uint8_t res=0;
261     KMem.WR[len1&0x0f]++;
262     if (!bSPI1Sending)
263     {
264         uint8_t value;
265         memcpy(SPI1SendBuf,pBuf,len1);
266         nSPI1ToSendLen=len1;
267         nSPI1SentLen=0;
268         
269 //        SetFP0DEPin_1();
270 //        SetACKPin_0();
271         
272         value = SPI1SendBuf[nSPI1SentLen];
273         LL_SPI_TransmitData8(SPI1,value);
274         bSPI1Sending=1;
275      KMem.SDB[128+KMem.SDT[123]] = value;
276      KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}        
277         // passive mode
278         SetFP0DEPin_1();
279         SetACKPin_0();
280     }
281     return res;
282 }