/**
|
******************************************************************************
|
* @file : FP0.c
|
* @brief : FP0 funcstions program body
|
******************************************************************************
|
*/
|
|
#include "globaldef.h"
|
#include "FP0.h"
|
#include "string.h"
|
#include "functions.h"
|
|
uint8_t PkgBuf1[32];
|
uint8_t PkgBuf2[32];
|
|
uint8_t PkgLen1=0;
|
uint8_t PkgLen2=0;
|
|
uint8_t bReceiving;
|
uint8_t nReceivedLen;
|
uint8_t bSending;
|
uint8_t bSentLen;
|
|
uint8_t bConfiged=0;
|
uint8_t nConfigStationId=0;
|
uint8_t nInputBytes=8;
|
uint8_t nOutputBytes=8;
|
uint8_t nIndex=0;
|
|
|
unsigned char SPI1RecvBuf[32];
|
unsigned char SPI1SendBuf[32];
|
|
volatile unsigned char bSPI1Recving=0;
|
volatile unsigned char bSPI1RecvDone =0;
|
|
volatile unsigned char nSPI1RecvPos =0;
|
volatile unsigned char nSPI1RecvLenInBuf=0;
|
|
volatile unsigned char nSPI1ToSendLen=0;
|
volatile unsigned char nSPI1SentLen=0;
|
|
volatile unsigned char bSPI1Sending=0;
|
volatile unsigned char bSPI1SendDone=0;
|
|
volatile int oldSYN=0;
|
|
int RSTCount0;
|
int bFirstReq = 0;
|
|
uint8_t FP0_Init()
|
{
|
bSPI1Sending=0;
|
bSPI1Recving=0;
|
if (nChilds==0) {
|
nInputBytes=1; //根据子机数量,报告扩展容量
|
nOutputBytes=1;
|
} else
|
{
|
nInputBytes=nChilds; //根据子机数量,报告扩展容量
|
nOutputBytes=nChilds;
|
}
|
SetACKPin_0();
|
SetACKPin_0();
|
SetFP0DEPin_0();
|
|
HAL_Delay(10);
|
RSTCount0=GetuS();
|
return 0;
|
}
|
|
uint8_t FP0_Proc()
|
{
|
int CurRST=GetRSTPin();
|
int CurSEL=GetSELPin();
|
int CurSYN=GetSYNPin();
|
int CurACK=GetACKPin();
|
int CurOE=GetOEPin();
|
// KMem.WY[4]=CurRST;
|
// KMem.WY[5]=CurSEL;
|
// KMem.WY[6]=CurSYN;
|
// KMem.WY[7]=CurACK;
|
if (CurOE) {
|
for (int j=0;j<nOutputBytes;j++)
|
{
|
KMem.WYB[j]=0;
|
}
|
}
|
|
if (!CurRST) {RSTCount0=GetuS();}
|
if (!bFirstReq && CurRST)
|
{
|
int RSTCount = GetuS() - RSTCount0 ;
|
if (RSTCount>=10000)
|
{
|
SetACKPin_1();
|
bFirstReq=1;
|
// RSTCount=0;
|
// bSPI1Recving=1;
|
// KMem.WX[7]=RSTCount/1000;
|
}
|
}
|
if (CurSYN ==0 && oldSYN != 0) {
|
KMem.WDT[121] = KMem.WDT[122];
|
KMem.WDT[122]=0;
|
}
|
if (CurSEL && CurSYN !=0 && oldSYN == 0){
|
bSPI1Recving=1;
|
nSPI1RecvPos=0;
|
LL_SPI_Disable(SPI2);
|
nSPI1RecvPos=0;
|
LL_SPI_Enable(SPI2);
|
}
|
if (CurSEL && CurSYN && !bSPI1Sending && !bSPI1Recving)
|
{
|
bSPI1Recving=1;
|
nSPI1RecvPos=0;
|
|
}
|
|
if (CurSYN == 0){
|
bSPI1Recving=0;
|
nSPI1RecvPos=0;
|
}
|
oldSYN=CurSYN;
|
|
/*
|
if (bSPI1RecvDone)
|
{
|
bSPI1RecvDone=0;
|
ParsePkg(SPI1RecvBuf,nSPI1RecvLenInBuf);
|
}
|
// */
|
|
return 0;
|
}
|
|
uint8_t CalFP0BCC(uint8_t* pBuf, uint8_t len1)
|
{
|
uint8_t BCC=0;
|
for (int i=0;i<len1;i++)
|
{
|
BCC+=pBuf[i]&0x0f;
|
}
|
BCC&=0x0f;
|
return BCC;
|
}
|
|
uint8_t CheckFP0Pkg(uint8_t * pBuf, uint8_t len1)
|
{
|
uint8_t res=0;
|
|
return res;
|
}
|
|
uint8_t ParseFP0Pkg(uint8_t * pBuf, uint8_t len1)
|
{
|
uint8_t res=0;
|
uint8_t nST=pBuf[0];
|
uint8_t nCMD=nST&0x7;
|
uint8_t nStationID=nST&0xf8;
|
|
logData(0xff);
|
|
if (nStationID<0x80) return 0;
|
switch (nCMD)
|
{
|
case CMD_0_QUERY:
|
if (!bConfiged || (bConfiged && nStationID == nConfigStationId))
|
{
|
KMem.DT[8]++;
|
pFP0QuRplyPkg p1 = (pFP0QuRplyPkg)PkgBuf2;
|
p1->Hdr1=nST;
|
p1->nInputBytes=0x30|nInputBytes;
|
p1->nOutputBytes=0x30|nOutputBytes;
|
p1->nParam1=0x30|0x05;
|
p1->nBCC= 0x30|CalFP0BCC(PkgBuf2,4);
|
p1->End1=0x0d;
|
|
logData(0x11);
|
|
SendFP0Pkg(PkgBuf2,sizeof(stFP0QuRplyPkg));
|
nConfigStationId=nStationID;
|
bConfiged=1;
|
}
|
bSPI1Recving=1;
|
break;
|
case CMD_1:
|
KMem.DT[9]++;
|
// bSPI1Recving=1;
|
break;
|
case CMD_2:
|
KMem.DT[10]++;
|
// bSPI1Recving=1;
|
break;
|
case CMD_3_EXCHG:
|
if (!bConfiged || nConfigStationId != nStationID)
|
{
|
bSPI1Recving=1;
|
break;
|
}
|
KMem.DT[11]++;
|
{
|
for (int j=0;j<nOutputBytes;j++)
|
{
|
KMem.WYB[j]=((pBuf[j*2 + 1]&0xf)<<4) + ((pBuf[j*2 + 2]&0xf)<<0);
|
}
|
/* test
|
for (int j=0;j<nOutputBytes;j++)
|
{
|
KMem.WYB[j]=(0xff);
|
}
|
//*/
|
// KMem.DT[2]=((pBuf[1]&0xf)<<4) + ((pBuf[2]&0xf)<<0) + ((pBuf[3]&0xf)<<12) + ((pBuf[4]&0xf)<<8);
|
pFP0EXGRplyPkg p1 = (pFP0EXGRplyPkg)PkgBuf2;
|
p1->Hdr1=nST;
|
for (int j=0;j<nInputBytes;j++)
|
{
|
p1->nInputBytes[j*2 + 0]=0x30|HiHofB(KMem.WXB[j]); //((KMem.DT[0]>>4)&0x0f);
|
p1->nInputBytes[j*2 + 1]=0x30|LoHofB(KMem.WXB[j]); //((KMem.DT[0]>>0)&0x0f);
|
}
|
//p1->nInputBytes[0]=0x30|HiHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>4)&0x0f);
|
//p1->nInputBytes[1]=0x30|LoHofB(LoBofW(KMem.DT[0])); //((KMem.DT[0]>>0)&0x0f);
|
//p1->nInputBytes[2]=0x30|HiHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>12)&0x0f);
|
//p1->nInputBytes[3]=0x30|LoHofB(HiBofW(KMem.DT[0])); //((KMem.DT[0]>>8)&0x0f);
|
//p1->nInputBytes[4]=0x30|((KMem.DT[1]>>4)&0x0f);
|
//p1->nInputBytes[5]=0x30|((KMem.DT[1]>>0)&0x0f);
|
PkgBuf2[nInputBytes*2 + 1 ]=0x30|CalFP0BCC(PkgBuf2,nInputBytes*2+1); // p1->nBCC= 0x30|CalBCC(PkgBuf2,7);
|
PkgBuf2[nInputBytes*2 + 2 ]=0x0d; // p1->End1=0x0d;
|
SendFP0Pkg(PkgBuf2,nInputBytes*2 + 3);
|
}
|
bSPI1Recving=1;
|
break;
|
case CMD_4:
|
KMem.DT[12]++;
|
// bSPI1Recving=1;
|
break;
|
case CMD_5:
|
KMem.DT[13]++;
|
// bSPI1Recving=1;
|
break;
|
case CMD_6:
|
KMem.DT[14]++;
|
// bSPI1Recving=1;
|
break;
|
case CMD_7_END:
|
KMem.DT[15]++;
|
if (bConfiged && nConfigStationId == nStationID)
|
{
|
//SetFP0DEPin_0();
|
SetACKPin_1();
|
}
|
bSPI1Recving=1;
|
break;
|
default:
|
KMem.DT[18]++;
|
bSPI1Recving=1;
|
break;
|
}
|
KMem.DT[24+(len1&0x0f)]++;
|
|
return res;
|
}
|
|
uint8_t SendFP0Pkg(uint8_t * pBuf, uint8_t len1)
|
{
|
uint8_t res=0;
|
KMem.WR[len1&0x0f]++;
|
if (!bSPI1Sending)
|
{
|
uint8_t value;
|
memcpy(SPI1SendBuf,pBuf,len1);
|
nSPI1ToSendLen=len1;
|
nSPI1SentLen=0;
|
|
// SetFP0DEPin_1();
|
// SetACKPin_0();
|
|
value = SPI1SendBuf[nSPI1SentLen];
|
LL_SPI_TransmitData8(SPI1,value);
|
bSPI1Sending=1;
|
|
logData(value);
|
|
// passive mode
|
SetFP0DEPin_1();
|
SetACKPin_0();
|
}
|
return res;
|
}
|