|
#include "SLP.h"
|
|
void SLPSendPacket(void * 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(uchar * pBuf, uchar len1)
|
{
|
uchar i;
|
uchar BCC=0;
|
for (i=0;i<len1;i++)
|
{
|
BCC += pBuf[i];
|
}
|
return BCC;
|
}
|
|
void SLPparsePacket(void * 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++;
|
}
|