提交 | 用户 | age
|
8b51c7
|
1 |
#include "SLP.h" |
Q |
2 |
|
842bb6
|
3 |
//void SLPSendPacket(char * buf, uchar len1); |
8b51c7
|
4 |
|
842bb6
|
5 |
uchar SLPBCC(unsigned char * pBuf, uchar len1) |
8b51c7
|
6 |
{ |
Q |
7 |
uchar i; |
|
8 |
uchar BCC=0; |
|
9 |
for (i=0;i<len1;i++) |
|
10 |
{ |
|
11 |
BCC += pBuf[i]; |
|
12 |
} |
|
13 |
return BCC; |
|
14 |
} |
|
15 |
|
842bb6
|
16 |
void SLPInit(stSLPDef * pSLP, SLPSendPktDef pFunc1) |
Q |
17 |
{ |
|
18 |
pSLP->SLPLostPkt = 0; |
|
19 |
pSLP->nCount = 0; |
|
20 |
pSLP->SLPSendPktFunc = pFunc1; |
|
21 |
|
|
22 |
} |
|
23 |
|
|
24 |
void SLPSetCallBack(stSLPDef * pSLP, SLPSendPktDef pFunc1) |
|
25 |
{ |
|
26 |
pSLP->SLPSendPktFunc = pFunc1; |
|
27 |
} |
|
28 |
|
|
29 |
void SLPparsePacket(stSLPDef * pSLP, unsigned char * pRecvBuf, uchar len1) |
8b51c7
|
30 |
{ |
Q |
31 |
|
|
32 |
stSLPPacket * pPacket = (stSLPPacket *)pRecvBuf; |
|
33 |
if (len1 != sizeof(stSLPPacket)) return; |
|
34 |
// if (pPacket->ED != EDsign) return; |
|
35 |
if (pPacket->BCC != SLPBCC(pRecvBuf,len1-1)) return; |
842bb6
|
36 |
if (pSLP->bSLPMaster) //master |
8b51c7
|
37 |
{ |
Q |
38 |
if (pPacket->ST ==ST_S) |
|
39 |
{ |
|
40 |
//check |
842bb6
|
41 |
if (pPacket->Dst == pSLP->nCurStation) { |
Q |
42 |
pSLP->SLPMasterRecved=1; |
|
43 |
pSLP->SLPLostPkt=0; |
|
44 |
pSLP->inputBuf[pSLP->nCurStation] = pPacket->Data; |
8b51c7
|
45 |
} |
Q |
46 |
} |
|
47 |
// SLPoutputB = (inputBuf[1] &0x0f) | ((inputBuf[2] &0x0f) << 4); |
842bb6
|
48 |
pSLP->SLPoutputB = pSLP->inputBuf[1]; |
8b51c7
|
49 |
}else |
Q |
50 |
{ //slave |
|
51 |
if (pPacket->ST==ST_M) |
|
52 |
{ |
|
53 |
//check |
842bb6
|
54 |
stSLPPacket * pRplyPkt = (stSLPPacket *)pSLP->SendBuf; |
Q |
55 |
if (pPacket->Dst == pSLP->nStation) { |
|
56 |
pSLP->SLPoutputB = pPacket->Data; |
|
57 |
pSLP->SLPSlaveCountOut=0; |
8b51c7
|
58 |
|
Q |
59 |
pRplyPkt->ST = ST_S; |
842bb6
|
60 |
pRplyPkt->Dst = pSLP->nStation; |
Q |
61 |
pRplyPkt->Data = pSLP->SLPinputB; |
|
62 |
pRplyPkt->BCC = SLPBCC(pSLP->SendBuf, sizeof(stSLPPacket)-1); |
8b51c7
|
63 |
// pRplyPkt->ED = EDsign; |
Q |
64 |
|
842bb6
|
65 |
pSLP->SLPSendPktFunc(pSLP->SendBuf,sizeof(stSLPPacket)); |
8b51c7
|
66 |
} |
Q |
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
|
842bb6
|
71 |
void SLPMasterSendPacket(stSLPDef * pSLP) |
8b51c7
|
72 |
{ |
Q |
73 |
|
842bb6
|
74 |
stSLPPacket * pReqPkt = (stSLPPacket *)pSLP->SendBuf; |
Q |
75 |
pSLP->outputBuf[1]=pSLP->SLPinputB ;//&0xf; |
8b51c7
|
76 |
// outputBuf[2] = (SLPinputB & 0xf0) >> 4; |
Q |
77 |
pReqPkt->ST = ST_M; |
842bb6
|
78 |
pReqPkt->Dst = pSLP->nCurStation; |
Q |
79 |
pReqPkt->Data = pSLP->outputBuf[pSLP->nCurStation]; ; |
|
80 |
pReqPkt->BCC = SLPBCC(pSLP->SendBuf, sizeof(stSLPPacket)-1); |
8b51c7
|
81 |
// pReqPkt->ED = EDsign; |
Q |
82 |
|
842bb6
|
83 |
pSLP->SLPSendPktFunc(pSLP->SendBuf,sizeof(stSLPPacket)); |
8b51c7
|
84 |
} |
842bb6
|
85 |
|
Q |
86 |
void SLPProcess(stSLPDef * pSLP) |
8b51c7
|
87 |
{ |
842bb6
|
88 |
if (pSLP->bSLPMaster) //master |
8b51c7
|
89 |
{ |
842bb6
|
90 |
if ( (pSLP->nCount & 0x7f) == 0 ) |
8b51c7
|
91 |
{ //time up |
842bb6
|
92 |
if (pSLP->SLPMasterRecved) { |
8b51c7
|
93 |
// SLPMasterRecved=0; |
842bb6
|
94 |
pSLP->SLPOKSign = 1; |
Q |
95 |
if (pSLP->SLPErrSign) pSLP->SLPErrSign--; |
8b51c7
|
96 |
|
Q |
97 |
}else { |
842bb6
|
98 |
pSLP->SLPLostPkt++; |
Q |
99 |
if (pSLP->SLPLostPkt > 10) { |
|
100 |
pSLP->SLPErrSign=20; |
|
101 |
pSLP->SLPOKSign = 0; |
8b51c7
|
102 |
} |
Q |
103 |
} |
842bb6
|
104 |
if (pSLP->nStation >0) { |
Q |
105 |
pSLP->nCurStation++; |
|
106 |
if (pSLP->nCurStation > pSLP->nStation) { |
|
107 |
pSLP->nCurStation =1; |
8b51c7
|
108 |
} |
842bb6
|
109 |
pSLP->SLPMasterRecved=0; |
Q |
110 |
SLPMasterSendPacket(pSLP); |
8b51c7
|
111 |
} |
Q |
112 |
} |
|
113 |
}else |
|
114 |
{ |
842bb6
|
115 |
pSLP->SLPSlaveCountOut ++; |
Q |
116 |
if (pSLP->SLPSlaveCountOut >200) // 20mS |
8b51c7
|
117 |
{ |
842bb6
|
118 |
pSLP->SLPErrSign=100; |
8b51c7
|
119 |
}else { |
842bb6
|
120 |
if (pSLP->SLPErrSign) pSLP->SLPErrSign--; |
8b51c7
|
121 |
} |
Q |
122 |
} |
842bb6
|
123 |
pSLP->nCount++; |
8b51c7
|
124 |
} |