QuakeGod
2023-02-01 7a2ff321965f1f24f3047fedd44a83f451f1f793
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
  ******************************************************************************
  * @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<nOutputBytes;j++)
                {KMem.WYB[j]=0; }
            }
      
        if (!CurRST) {RSTCount0=GetuS();}
        if (!bFirstReq && CurRST)
        {    
            int RSTCount = GetuS() - RSTCount0 ;
            if (RSTCount>=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<len1;i++)
    {
        BCC+=pBuf[i]&0x0f;
    }
    BCC&=0x0f;
    return BCC;
}
 
uint8_t CheckFP0Pkg(uint8_t * pBuf, uint8_t len1)
{
    uint8_t res=0;
    
    return res;
}
 
uint8_t ParseFP0Pkg(uint8_t * pBuf, uint8_t len1)
{
    uint8_t res=0;
    uint8_t nST=pBuf[0];
    uint8_t nCMD=nST&0x7;
    uint8_t nAddr=nST&0xf8;
    KMem.SDB[128+KMem.SDT[123]] = 0xFF;
    KMem.SDT[123]++;      if (KMem.SDT[123]>=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;j<nOutputBytes;j++)
            {
                KMem.WYB[j]=((pBuf[j*2 + 1]&0xf)<<4) + ((pBuf[j*2 + 2]&0xf)<<0);
            }
            // KMem.DT[2]=((pBuf[1]&0xf)<<4) + ((pBuf[2]&0xf)<<0) + ((pBuf[3]&0xf)<<12) + ((pBuf[4]&0xf)<<8);
            pFP0EXGRplyPkg p1 = (pFP0EXGRplyPkg)PkgBuf2;
            p1->Hdr1=nST;
            for (int j=0;j<nInputBytes;j++)
            {
                p1->nInputBytes[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;
}