QuakeGod
2022-10-17 9bb88037ae08b3a1d1bcc155bfbe0665f3a899e2
Src/KBus.c
@@ -9,7 +9,7 @@
#include "string.h"
#include "stm32f0xx.h"
unsigned char bMaster=0,bSlave=0,bRepeater=0;;
unsigned char bKBusMaster=0,bKBusSlave=0,bKBusRepeater=0;;
unsigned char PacketBuf1[128];
unsigned char PacketBuf2[128];
@@ -56,136 +56,6 @@
      k^=((unsigned char *)pData)[i];
   }
   return k;   
}
int RepeaterFunc(int nChn)
{
      KMem.WY[0]=KMem.WX[0];
      if ((KMem.nRunCount &0x7f) == 88)
      {
         nCount2++;
         ToggleRunLed();
//      int len1=sprintf(str1,"%d %d Cfg %02X  Input %02X  \r\n",nCount,nCount2,EffJumperSW,MyKeyStat1);
//      PutStr(str1,len1);
      }
   return 0;
}
int MasterFunc(int nChn)
{
      uint32_t tick1=HAL_GetTick();
      uint32_t thisuS=GetuS();
         int len1=0;
         if ((MasterRecved && MasterRecvOK && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1500u)
         {
            if (!MasterRecvOK)
            {
               TimeOutCount++;
               Uart2Stat.TimeOutErr++;
               ChnStats[nCurPollId].LostPackets++;
               ChnStats[nCurPollId].CtnLstPkts++;
               if (!MasterRecved) {ChnStats[nCurPollId].TimeOutErr++;}
               if (ChnStats[nCurPollId].CtnLstPkts>ChnStats[nCurPollId].MaxCtnLstPkts)
               {ChnStats[nCurPollId].MaxCtnLstPkts=ChnStats[nCurPollId].CtnLstPkts;}
               if (ChnStats[nCurPollId].CtnLstPkts>3)
               {
                  ChnStats[nCurPollId].Stat=0;
                  KMem.ErrStat=200;
                  {BufferIn[nCurPollId]=0;}
               }
            //   LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
            }else
            {
               ChnStats[nCurPollId].Stat=1;
               KMem.RunStat=100;
            }
            nCurPollId ++;
            if (nCurPollId > nChilds)
            {
               CircleTime=thisuS-LastCircleStartTime;
               LastCircleStartTime=thisuS;
               nSeq++;
               nCurPollId=1;
            }
#if (BOARD_TYPE == 12)
            if (KMRunStat.WorkMode==0)
            {
            //   KMem.WX[0]= GetInput();
            //   KMem.WY[1]=KMem.WX[0]&0xff;
            //   KMem.WY[2]=(KMem.WX[0]>>8)&0xff;
            }
         //   BufferOut[1]=KMem.WY[1];
         //   BufferOut[2]=KMem.WY[2];
#else
            if (KMRunStat.WorkMode==0)
            {
               KMem.WX[0]= GetInput();
               KMem.WY[1]=KMem.WX[0]&0xff;
               KMem.WY[2]=(KMem.WX[0]>>8)&0xff;
            }
            BufferOut[1]=KMem.WY[1];
            BufferOut[2]=KMem.WY[2];
#endif
            Datas[0]=BufferOut[nCurPollId];
            Datas[1]=BufferOut[nCurPollId+1];;
            Datas[2]=ChnStats[nCurPollId].Stat;
            Datas[3]=0;
            Datas[4]=tick1&0xff;
            Datas[5]=(tick1>>8)&0xff;
            Datas[6]=(tick1>>16)&0xff;
            Datas[7]=(tick1>>24)&0xff;
            SendTimeuS=thisuS;
            len1=MakePacket((pPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
            SendPacket(nChn, (pPacket)PacketBuf1, len1);
            ChnStats[nCurPollId].SendPackets++;
            ChnStats[nCurPollId].SendTimeInterval=SendTimeuS-ChnStats[nCurPollId].LastSentTimeuS;
            ChnStats[nCurPollId].LastSentTimeuS=SendTimeuS;
            PacketLength = len1;
            SendTime=tick1;
            MasterRecved=0;
            MasterRecvOK=0;
         //   LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);
            //ToggleErrLed();
//            ToggleOut8();
         }
         Clk3=SysTick->VAL;
      //   LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_4);
   //   HAL_Delay(1);
   return 0;
}
int SlaveFunc(int nChn)
{
      int ThisuS=GetuS();
      int thisRecvTime=RecvTimeuS;
      if (SlaveRecved)
      {
         KMem.RunStat=8000;
         SlaveRecved=0;
      }else if ((ThisuS - thisRecvTime) >12000u)
      {
         KMem.ErrStat=8000;
         KMem.SDD[17]=1;
         KMem.SDD[18]=ThisuS;
         KMem.SDD[19]=RecvTimeuS;
      }else if ( ThisuS > (thisRecvTime + 12000u))
      {
         KMem.ErrStat=8000;
         KMem.SDD[17]=2;
         KMem.SDD[18]=ThisuS;
         KMem.SDD[19]=RecvTimeuS;
      }
   return 0;
}
int MakePacket(pPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nSEQ, unsigned char DataLen,void * pData )
@@ -431,7 +301,7 @@
            DelayuS=ThisuS-SendTimeuS;
            if (DelayuS > MaxDelayuS) MaxDelayuS = DelayuS;
#if (BOARD_TYPE==12)
#if (BOARD_TYPE == 14)
            BufferIn[ChildId]=p1->data[0];
            KMem.WXB[ChildId-1]=BufferIn[ChildId];
            if (KMRunStat.WorkMode==0) {   
@@ -530,7 +400,7 @@
         case cmdExChgData:
            BufferIn[0]=p1->data[0];
            nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24);
#if (BOARD_TYPE == 12)
#if (BOARD_TYPE == 14)
         //   PutOutput(BufferIn[0]);
            //PutOutput(outputvalue);
            //memcpy(DispBuf,p1->data+2,8);
@@ -582,7 +452,7 @@
{
   ThisuS=GetuS();
   int Result=0;
   if (bMaster)
   if (bKBusMaster)
   {
         MasterRecved=1;
         Result=CheckPacket(nChn, p1, Len1);
@@ -594,7 +464,7 @@
         Result=MasterParsePacket(nChn, p1, Len1);         
         return Result;
   }
   if (bSlave)
   if (bKBusSlave)
   {
         ChnStats[0].ClientRecvPkts++;
         Result=SlaveCheckPacket(nChn, p1, Len1);
@@ -654,3 +524,132 @@
   return S_OK;
}
int KBusRepeaterFunc(int nChn)
{
      KMem.WY[0]=KMem.WX[0];
      if ((KMem.nRunCount &0x7f) == 88)
      {
         nCount2++;
         ToggleRunLed();
//      int len1=sprintf(str1,"%d %d Cfg %02X  Input %02X  \r\n",nCount,nCount2,EffJumperSW,MyKeyStat1);
//      PutStr(str1,len1);
      }
   return 0;
}
int KBusMasterFunc(int nChn)
{
   uint32_t tick1=HAL_GetTick();
   uint32_t thisuS=GetuS();
      int len1=0;
      if ((MasterRecved && MasterRecvOK && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1000u)
      {
         if (!MasterRecvOK)
         {
            TimeOutCount++;
            Uart2Stat.TimeOutErr++;
            ChnStats[nCurPollId].LostPackets++;
            ChnStats[nCurPollId].CtnLstPkts++;
            if (!MasterRecved) {ChnStats[nCurPollId].TimeOutErr++;}
            if (ChnStats[nCurPollId].CtnLstPkts>ChnStats[nCurPollId].MaxCtnLstPkts)
            {ChnStats[nCurPollId].MaxCtnLstPkts=ChnStats[nCurPollId].CtnLstPkts;}
            if (ChnStats[nCurPollId].CtnLstPkts>3)
            {
               ChnStats[nCurPollId].Stat=0;
               KMem.ErrStat=200;
               {BufferIn[nCurPollId]=0;}
            }
         //   LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
         }else
         {
            ChnStats[nCurPollId].Stat=1;
            KMem.RunStat=100;
         }
         nCurPollId ++;
         if (nCurPollId > nChilds)
         {
            CircleTime=thisuS-LastCircleStartTime;
            LastCircleStartTime=thisuS;
            nSeq++;
            nCurPollId=1;
         }
#if (BOARD_TYPE == 14)
         if (KMRunStat.WorkMode==0)
         {
         //   KMem.WX[0]= GetInput();
         //   KMem.WY[1]=KMem.WX[0]&0xff;
         //   KMem.WY[2]=(KMem.WX[0]>>8)&0xff;
         }
      //   BufferOut[1]=KMem.WY[1];
      //   BufferOut[2]=KMem.WY[2];
#else
         if (KMRunStat.WorkMode==0)
         {
            KMem.WX[0]= GetInput();
            KMem.WY[1]=KMem.WX[0]&0xff;
            KMem.WY[2]=(KMem.WX[0]>>8)&0xff;
         }
         BufferOut[1]=KMem.WY[1];
         BufferOut[2]=KMem.WY[2];
#endif
         Datas[0]=BufferOut[nCurPollId];
         Datas[1]=BufferOut[nCurPollId+1];;
         Datas[2]=ChnStats[nCurPollId].Stat;
         Datas[3]=0;
         Datas[4]=tick1&0xff;
         Datas[5]=(tick1>>8)&0xff;
         Datas[6]=(tick1>>16)&0xff;
         Datas[7]=(tick1>>24)&0xff;
         SendTimeuS=thisuS;
         len1=MakePacket((pPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
         SendPacket(nChn, (pPacket)PacketBuf1, len1);
         ChnStats[nCurPollId].SendPackets++;
         ChnStats[nCurPollId].SendTimeInterval=SendTimeuS-ChnStats[nCurPollId].LastSentTimeuS;
         ChnStats[nCurPollId].LastSentTimeuS=SendTimeuS;
//         PacketLength = len1;
         SendTime=tick1;
         MasterRecved=0;
         MasterRecvOK=0;
      //   LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);
         //ToggleErrLed();
//            ToggleOut8();
      }
//      Clk3=SysTick->VAL;
   //   LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_4);
//   HAL_Delay(1);
   return 0;
}
int KBusSlaveFunc(int nChn)
{
      int ThisuS=GetuS();
      int thisRecvTime=RecvTimeuS;
      if (SlaveRecved)
      {
         KMem.RunStat=8000;
         SlaveRecved=0;
      }else if ((ThisuS - thisRecvTime) >12000u)
      {
         KMem.ErrStat=8000;
         KMem.SDD[17]=1;
         KMem.SDD[18]=ThisuS;
         KMem.SDD[19]=RecvTimeuS;
      }else if ( ThisuS > (thisRecvTime + 12000u))
      {
         KMem.ErrStat=8000;
         KMem.SDD[17]=2;
         KMem.SDD[18]=ThisuS;
         KMem.SDD[19]=RecvTimeuS;
      }
   return 0;
}