QuakeGod
2024-02-24 8b51c78f1b88d94a89bb8c37ae38a54f523cb597
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
 
#include "SLP.h"
    
void SLPSendPacket(char * buf, uchar len1);
uchar bSLPMaster;
uchar nStation;
uchar SLPinputB;
uchar SLPoutputB;
uchar nCurStation;
uchar inputBuf[5];
uchar outputBuf[5];
 
uchar SendBuf[8];
 
uchar SLPMasterRecved;        //Ö÷»úÊÕµ½×Ó»ú»Ø¸´
unsigned int SLPSlaveCountOut;
 
unsigned int SLPLostPkt=0;             // ¶ª°üÊýÁ¿
 
uchar SLPOKSign;
uchar SLPErrSign;
 
uchar SLPBCC(char * pBuf, uchar len1)
{
    uchar i;
    uchar BCC=0;
    for (i=0;i<len1;i++)
    {
        BCC += pBuf[i];
    }
    return BCC;
}
 
void SLPparsePacket(char * pRecvBuf, uchar len1)
{
    
    stSLPPacket * pPacket = (stSLPPacket *)pRecvBuf;
    if (len1 != sizeof(stSLPPacket)) return;
//    if (pPacket->ED != EDsign) return;
    if (pPacket->BCC != SLPBCC(pRecvBuf,len1-1)) return;
        if (bSLPMaster) //master
        {
                if (pPacket->ST ==ST_S) 
                {
                    //check
                    if (pPacket->Dst == nCurStation) {
                        SLPMasterRecved=1;
                        SLPLostPkt=0;
                         inputBuf[nCurStation] = pPacket->Data;
                    }
                }
//                SLPoutputB = (inputBuf[1] &0x0f) | ((inputBuf[2] &0x0f) << 4);
                SLPoutputB = inputBuf[1];
        }else
        {    //slave 
            if (pPacket->ST==ST_M) 
            {
                //check
                stSLPPacket * pRplyPkt = (stSLPPacket *)SendBuf;        
                if (pPacket->Dst == nStation) {
                    SLPoutputB = pPacket->Data;
                    SLPSlaveCountOut=0;
                    
                    pRplyPkt->ST = ST_S;
                    pRplyPkt->Dst = nStation;
                    pRplyPkt->Data = SLPinputB;
                    pRplyPkt->BCC = SLPBCC(SendBuf, sizeof(stSLPPacket)-1);
    //                pRplyPkt->ED = EDsign;
                    
                    SLPSendPacket(SendBuf,sizeof(stSLPPacket));
                }
          }
    }
}
 
void SLPMasterSendPacket(void)
{
 
        stSLPPacket * pReqPkt = (stSLPPacket *)SendBuf;
        outputBuf[1]=SLPinputB ;//&0xf;
//        outputBuf[2] = (SLPinputB & 0xf0) >> 4;
        pReqPkt->ST = ST_M;
        pReqPkt->Dst = nCurStation;
        pReqPkt->Data = outputBuf[nCurStation]; ;
        pReqPkt->BCC = SLPBCC(SendBuf, sizeof(stSLPPacket)-1);
//        pReqPkt->ED = EDsign;
        
        SLPSendPacket(SendBuf,sizeof(stSLPPacket));    
}
void SLPProcess(void)
{
    static int nCount =0;
            if (bSLPMaster) //master
        {
            if ( (nCount & 0x3f) == 0 ) 
            {            //time up
                if (SLPMasterRecved) {
//                SLPMasterRecved=0;
                        SLPOKSign = 1;
                        if (SLPErrSign) SLPErrSign--;
                
                }else {
                    SLPLostPkt++;
                    if (SLPLostPkt > 10) {
                        SLPErrSign=20;
                        SLPOKSign = 0;
                    }
                }
                if (nStation >0) {
                    nCurStation++;
                    if (nCurStation > nStation) {
                        nCurStation =1;
                    }
                    SLPMasterRecved=0;
                    SLPMasterSendPacket();
                }
            }                
        }else
        {
            SLPSlaveCountOut ++;
            if (SLPSlaveCountOut >200)          // 20mS
            {
                    SLPErrSign=100;
            }else {
                if (SLPErrSign) SLPErrSign--;
            }
        }
    nCount++;            
}