QuakeGod
2024-07-27 842bb64195f958b050867c50db66fc0aa413dafb
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
#include "SLP.h"
    
//void SLPSendPacket(char * buf, uchar len1);
 
uchar SLPBCC(unsigned char * pBuf, uchar len1)
{
    uchar i;
    uchar BCC=0;
    for (i=0;i<len1;i++)
    {
        BCC += pBuf[i];
    }
    return BCC;
}
 
void SLPInit(stSLPDef * pSLP, SLPSendPktDef pFunc1)
{
    pSLP->SLPLostPkt = 0;
    pSLP->nCount =  0;
    pSLP->SLPSendPktFunc = pFunc1;
    
}
 
void SLPSetCallBack(stSLPDef * pSLP, SLPSendPktDef pFunc1)
{
        pSLP->SLPSendPktFunc = pFunc1;
}
 
void SLPparsePacket(stSLPDef * pSLP, unsigned 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 (pSLP->bSLPMaster) //master
        {
                if (pPacket->ST ==ST_S) 
                {
                    //check
                    if (pPacket->Dst == pSLP->nCurStation) {
                        pSLP->SLPMasterRecved=1;
                        pSLP->SLPLostPkt=0;
                         pSLP->inputBuf[pSLP->nCurStation] = pPacket->Data;
                    }
                }
//                SLPoutputB = (inputBuf[1] &0x0f) | ((inputBuf[2] &0x0f) << 4);
                pSLP->SLPoutputB = pSLP->inputBuf[1];
        }else
        {    //slave 
            if (pPacket->ST==ST_M) 
            {
                //check
                stSLPPacket * pRplyPkt = (stSLPPacket *)pSLP->SendBuf;        
                if (pPacket->Dst == pSLP->nStation) {
                    pSLP->SLPoutputB = pPacket->Data;
                    pSLP->SLPSlaveCountOut=0;
                    
                    pRplyPkt->ST = ST_S;
                    pRplyPkt->Dst = pSLP->nStation;
                    pRplyPkt->Data = pSLP->SLPinputB;
                    pRplyPkt->BCC = SLPBCC(pSLP->SendBuf, sizeof(stSLPPacket)-1);
    //                pRplyPkt->ED = EDsign;
                    
                    pSLP->SLPSendPktFunc(pSLP->SendBuf,sizeof(stSLPPacket));
                }
          }
    }
}
 
void SLPMasterSendPacket(stSLPDef * pSLP)
{
 
        stSLPPacket * pReqPkt = (stSLPPacket *)pSLP->SendBuf;
        pSLP->outputBuf[1]=pSLP->SLPinputB ;//&0xf;
//        outputBuf[2] = (SLPinputB & 0xf0) >> 4;
        pReqPkt->ST = ST_M;
        pReqPkt->Dst = pSLP->nCurStation;
        pReqPkt->Data = pSLP->outputBuf[pSLP->nCurStation]; ;
        pReqPkt->BCC = SLPBCC(pSLP->SendBuf, sizeof(stSLPPacket)-1);
//        pReqPkt->ED = EDsign;
        
        pSLP->SLPSendPktFunc(pSLP->SendBuf,sizeof(stSLPPacket));    
}
 
void SLPProcess(stSLPDef * pSLP)
{
            if (pSLP->bSLPMaster) //master
        {
            if ( (pSLP->nCount & 0xf) == 0 ) 
            {            //time up
                if (pSLP->SLPMasterRecved) {
//                SLPMasterRecved=0;
                        pSLP->SLPOKSign = 1;
                        if (pSLP->SLPErrSign) pSLP->SLPErrSign--;
                
                }else {
                    pSLP->SLPLostPkt++;
                    if (pSLP->SLPLostPkt > 10) {
                        pSLP->SLPErrSign=20;
                        pSLP->SLPOKSign = 0;
                    }
                }
                if (pSLP->nStation >0) {
                    pSLP->nCurStation++;
                    if (pSLP->nCurStation > pSLP->nStation) {
                        pSLP->nCurStation =1;
                    }
                    pSLP->SLPMasterRecved=0;
                    SLPMasterSendPacket(pSLP);
                }
            }                
        }else
        {
            pSLP->SLPSlaveCountOut ++;
            if (pSLP->SLPSlaveCountOut >200)          // 20mS
            {
                    pSLP->SLPErrSign=100;
            }else {
                if (pSLP->SLPErrSign) pSLP->SLPErrSign--;
            }
        }
    pSLP->nCount++;            
}