提交 | 用户 | age
|
8b51c7
|
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 nConfigStationId=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 |
{ |
|
86 |
KMem.WYB[j]=0; |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
if (!CurRST) {RSTCount0=GetuS();} |
|
91 |
if (!bFirstReq && CurRST) |
|
92 |
{ |
|
93 |
int RSTCount = GetuS() - RSTCount0 ; |
|
94 |
if (RSTCount>=10000) |
|
95 |
{ |
|
96 |
SetACKPin_1(); |
|
97 |
bFirstReq=1; |
|
98 |
// RSTCount=0; |
|
99 |
// bSPI1Recving=1; |
|
100 |
// KMem.WX[7]=RSTCount/1000; |
|
101 |
} |
|
102 |
} |
|
103 |
if (CurSYN ==0 && oldSYN != 0) { |
|
104 |
KMem.WDT[121] = KMem.WDT[122]; |
|
105 |
KMem.WDT[122]=0; |
|
106 |
} |
|
107 |
if (CurSEL && CurSYN !=0 && oldSYN == 0){ |
|
108 |
bSPI1Recving=1; |
|
109 |
nSPI1RecvPos=0; |
|
110 |
LL_SPI_Disable(SPI2); |
|
111 |
nSPI1RecvPos=0; |
|
112 |
LL_SPI_Enable(SPI2); |
|
113 |
} |
|
114 |
if (CurSEL && CurSYN && !bSPI1Sending && !bSPI1Recving) |
|
115 |
{ |
|
116 |
bSPI1Recving=1; |
|
117 |
nSPI1RecvPos=0; |
|
118 |
|
|
119 |
} |
|
120 |
|
|
121 |
if (CurSYN == 0){ |
|
122 |
bSPI1Recving=0; |
|
123 |
nSPI1RecvPos=0; |
|
124 |
} |
|
125 |
oldSYN=CurSYN; |
|
126 |
|
|
127 |
/* |
|
128 |
if (bSPI1RecvDone) |
|
129 |
{ |
|
130 |
bSPI1RecvDone=0; |
|
131 |
ParsePkg(SPI1RecvBuf,nSPI1RecvLenInBuf); |
|
132 |
} |
|
133 |
// */ |
|
134 |
|
|
135 |
return 0; |
|
136 |
} |
|
137 |
|
|
138 |
uint8_t CalFP0BCC(uint8_t* pBuf, uint8_t len1) |
|
139 |
{ |
|
140 |
uint8_t BCC=0; |
|
141 |
for (int i=0;i<len1;i++) |
|
142 |
{ |
|
143 |
BCC+=pBuf[i]&0x0f; |
|
144 |
} |
|
145 |
BCC&=0x0f; |
|
146 |
return BCC; |
|
147 |
} |
|
148 |
|
|
149 |
uint8_t CheckFP0Pkg(uint8_t * pBuf, uint8_t len1) |
|
150 |
{ |
|
151 |
uint8_t res=0; |
|
152 |
|
|
153 |
return res; |
|
154 |
} |
|
155 |
|
|
156 |
uint8_t ParseFP0Pkg(uint8_t * pBuf, uint8_t len1) |
|
157 |
{ |
|
158 |
uint8_t res=0; |
|
159 |
uint8_t nST=pBuf[0]; |
|
160 |
uint8_t nCMD=nST&0x7; |
|
161 |
uint8_t nStationID=nST&0xf8; |
|
162 |
|
|
163 |
logData(0xff); |
|
164 |
|
|
165 |
if (nStationID<0x80) return 0; |
|
166 |
switch (nCMD) |
|
167 |
{ |
|
168 |
case CMD_0_QUERY: |
|
169 |
if (!bConfiged || (bConfiged && nStationID == nConfigStationId)) |
|
170 |
{ |
|
171 |
KMem.DT[8]++; |
|
172 |
pFP0QuRplyPkg p1 = (pFP0QuRplyPkg)PkgBuf2; |
|
173 |
p1->Hdr1=nST; |
|
174 |
p1->nInputBytes=0x30|nInputBytes; |
|
175 |
p1->nOutputBytes=0x30|nOutputBytes; |
|
176 |
p1->nParam1=0x30|0x05; |
|
177 |
p1->nBCC= 0x30|CalFP0BCC(PkgBuf2,4); |
|
178 |
p1->End1=0x0d; |
|
179 |
|
|
180 |
logData(0x11); |
|
181 |
|
|
182 |
SendFP0Pkg(PkgBuf2,sizeof(stFP0QuRplyPkg)); |
|
183 |
nConfigStationId=nStationID; |
|
184 |
bConfiged=1; |
|
185 |
} |
|
186 |
bSPI1Recving=1; |
|
187 |
break; |
|
188 |
case CMD_1: |
|
189 |
KMem.DT[9]++; |
|
190 |
// bSPI1Recving=1; |
|
191 |
break; |
|
192 |
case CMD_2: |
|
193 |
KMem.DT[10]++; |
|
194 |
// bSPI1Recving=1; |
|
195 |
break; |
|
196 |
case CMD_3_EXCHG: |
|
197 |
if (!bConfiged || nConfigStationId != nStationID) |
|
198 |
{ |
|
199 |
bSPI1Recving=1; |
|
200 |
break; |
|
201 |
} |
|
202 |
KMem.DT[11]++; |
|
203 |
{ |
|
204 |
for (int j=0;j<nOutputBytes;j++) |
|
205 |
{ |
|
206 |
KMem.WYB[j]=((pBuf[j*2 + 1]&0xf)<<4) + ((pBuf[j*2 + 2]&0xf)<<0); |
|
207 |
} |
|
208 |
/* test |
|
209 |
for (int j=0;j<nOutputBytes;j++) |
|
210 |
{ |
|
211 |
KMem.WYB[j]=(0xff); |
|
212 |
} |
|
213 |
//*/ |
|
214 |
// KMem.DT[2]=((pBuf[1]&0xf)<<4) + ((pBuf[2]&0xf)<<0) + ((pBuf[3]&0xf)<<12) + ((pBuf[4]&0xf)<<8); |
|
215 |
pFP0EXGRplyPkg p1 = (pFP0EXGRplyPkg)PkgBuf2; |
|
216 |
p1->Hdr1=nST; |
|
217 |
for (int j=0;j<nInputBytes;j++) |
|
218 |
{ |
|
219 |
p1->nInputBytes[j*2 + 0]=0x30|HiHofB(KMem.WXB[j]); //((KMem.DT[0]>>4)&0x0f); |
|
220 |
p1->nInputBytes[j*2 + 1]=0x30|LoHofB(KMem.WXB[j]); //((KMem.DT[0]>>0)&0x0f); |
|
221 |
} |
|
222 |
//p1->nInputBytes[0]=0x30|HiHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>4)&0x0f); |
|
223 |
//p1->nInputBytes[1]=0x30|LoHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>0)&0x0f); |
|
224 |
//p1->nInputBytes[2]=0x30|HiHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>12)&0x0f); |
|
225 |
//p1->nInputBytes[3]=0x30|LoHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>8)&0x0f); |
|
226 |
//p1->nInputBytes[4]=0x30|((KMem.DT[1]>>4)&0x0f); |
|
227 |
//p1->nInputBytes[5]=0x30|((KMem.DT[1]>>0)&0x0f); |
|
228 |
PkgBuf2[nInputBytes*2 + 1 ]=0x30|CalFP0BCC(PkgBuf2,nInputBytes*2+1); // p1->nBCC= 0x30|CalBCC(PkgBuf2,7); |
|
229 |
PkgBuf2[nInputBytes*2 + 2 ]=0x0d; // p1->End1=0x0d; |
|
230 |
SendFP0Pkg(PkgBuf2,nInputBytes*2 + 3); |
|
231 |
} |
|
232 |
bSPI1Recving=1; |
|
233 |
break; |
|
234 |
case CMD_4: |
|
235 |
KMem.DT[12]++; |
|
236 |
// bSPI1Recving=1; |
|
237 |
break; |
|
238 |
case CMD_5: |
|
239 |
KMem.DT[13]++; |
|
240 |
// bSPI1Recving=1; |
|
241 |
break; |
|
242 |
case CMD_6: |
|
243 |
KMem.DT[14]++; |
|
244 |
// bSPI1Recving=1; |
|
245 |
break; |
|
246 |
case CMD_7_END: |
|
247 |
KMem.DT[15]++; |
|
248 |
if (bConfiged && nConfigStationId == nStationID) |
|
249 |
{ |
|
250 |
//SetFP0DEPin_0(); |
|
251 |
SetACKPin_1(); |
|
252 |
} |
|
253 |
bSPI1Recving=1; |
|
254 |
break; |
|
255 |
default: |
|
256 |
KMem.DT[18]++; |
|
257 |
bSPI1Recving=1; |
|
258 |
break; |
|
259 |
} |
|
260 |
KMem.DT[24+(len1&0x0f)]++; |
|
261 |
|
|
262 |
return res; |
|
263 |
} |
|
264 |
|
|
265 |
uint8_t SendFP0Pkg(uint8_t * pBuf, uint8_t len1) |
|
266 |
{ |
|
267 |
uint8_t res=0; |
|
268 |
KMem.WR[len1&0x0f]++; |
|
269 |
if (!bSPI1Sending) |
|
270 |
{ |
|
271 |
uint8_t value; |
|
272 |
memcpy(SPI1SendBuf,pBuf,len1); |
|
273 |
nSPI1ToSendLen=len1; |
|
274 |
nSPI1SentLen=0; |
|
275 |
|
|
276 |
// SetFP0DEPin_1(); |
|
277 |
// SetACKPin_0(); |
|
278 |
|
|
279 |
value = SPI1SendBuf[nSPI1SentLen]; |
|
280 |
LL_SPI_TransmitData8(SPI1,value); |
|
281 |
bSPI1Sending=1; |
|
282 |
|
|
283 |
logData(value); |
|
284 |
|
|
285 |
// passive mode |
|
286 |
SetFP0DEPin_1(); |
|
287 |
SetACKPin_0(); |
|
288 |
} |
|
289 |
return res; |
|
290 |
} |