#include "SLP.h"
|
|
//void SLPSendPacket(char * buf, uchar len1);
|
|
uchar SLPBCC(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, 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++;
|
}
|