QuakeGod
2024-07-27 842bb64195f958b050867c50db66fc0aa413dafb
ComLib/Src/KLink.c
@@ -16,7 +16,7 @@
#endif
//extern stWLRunStat KwRunStat;
unsigned char KLPacketBuf1[256];
//unsigned char KLPacketBuf1[256];
unsigned char KLPacketBuf2[256];
unsigned char KLBufferIn[16]={0};
@@ -30,6 +30,14 @@
int KLThisuS=0;
int KLRecvTimeuS=0;
stPortDef KLinkPortReg1 = {.nPortHardType = 3,.nPortUseType = 2, .bEnable = 1, .bRunning =1, .StationId = 1, .bMaster = 0};
int KLinkInit(int n)
{
      KMRegisterPort(0,&KLinkPortReg1);
   return 0;
}
unsigned char KLBCC(void const * pData, int nSize)
{
   unsigned char k;
@@ -149,6 +157,7 @@
   unsigned short nByteAddr=0;
   unsigned short nBitAddr=0;
   unsigned short DataLen=0;   //p1->LoadLen;
   unsigned short nValue=0;
//int nSrcAddr=p1->SrcAddr;
   nKLStatus.nSEQ = ((pKLStat)(&(p1->Stat)))->nSEQ;;
@@ -221,8 +230,10 @@
            else if (nDataType == KLDataTypeWR)      {      pData=KMem.WR+nWordAddr;      }
            else if (nDataType == KLDataTypeWLX)      {      pData=KMem.WLX+nWordAddr;      }
            else if (nDataType == KLDataTypeWLY)      {      pData=KMem.WLY+nWordAddr;      }
            else if (nDataType == KLDataTypeSV)      {      pData=KMem.SV+nWordAddr;   }
            else if (nDataType == KLDataTypeEV)      {      pData=KMem.EV+nWordAddr;   }
#if (ENABLE_PLC)
            else if (nDataType == KLDataTypeSV)      {      pData=PLCMem.SV+nWordAddr;   }
            else if (nDataType == KLDataTypeEV)      {      pData=PLCMem.EV+nWordAddr;   }
#endif
            else if (nDataType == KLDataTypeTest)   {      pData=KMem.SDT+nWordAddr;   }
            else if (nDataType == KLDataTypeWDT)   {      pData=KMem.WDT+nWordAddr;   }
            else if (nDataType == KLDataTypeKBD)   {      pData=(unsigned short *)&KBus1.KBusChnStats  +nWordAddr;   }
@@ -254,8 +265,10 @@
            else if (nDataType == KLDataTypeWR)      {      pData=KMem.WR+nWordAddr;      }
            else if (nDataType == KLDataTypeWLX)      {      pData=KMem.WLX+nWordAddr;      }
            else if (nDataType == KLDataTypeWLY)      {      pData=KMem.WLY+nWordAddr;      }
            else if (nDataType == KLDataTypeSV)      {      pData=KMem.SV+nWordAddr;   DataLen=0;}
            else if (nDataType == KLDataTypeEV)      {      pData=KMem.EV+nWordAddr;   DataLen=0;}
#if (ENABLE_PLC)
            else if (nDataType == KLDataTypeSV)      {      pData=PLCMem.SV+nWordAddr;   DataLen=0;}
            else if (nDataType == KLDataTypeEV)      {      pData=PLCMem.EV+nWordAddr;   DataLen=0;}
#endif
            else if (nDataType == KLDataTypeTest)   {      pData=KMem.SDT+nWordAddr;   DataLen=0;}
            else if (nDataType == KLDataTypeWDT)   {      pData=KMem.WDT+nWordAddr;   DataLen=0;}
            else if (nDataType == KLDataTypeKBD)   {      pData=(unsigned short *)KBus1.KBusChnStats +nWordAddr;   DataLen=0;}
@@ -281,8 +294,10 @@
            else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
            else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
            else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
            else if (nDataType == KLCoilTypeT) { rData[0] = KMem.Timers[nBitAddr].bTon;}
            else if (nDataType == KLCoilTypeC) { rData[0] = KMem.Timers[nBitAddr].bTon;}
#if (ENABLE_PLC)
            else if (nDataType == KLCoilTypeT) { rData[0] = PLCMem.Timers[nBitAddr].bTon;}
            else if (nDataType == KLCoilTypeC) { rData[0] = PLCMem.Timers[nBitAddr].bTon;}
#endif
            else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
            
            else if (nDataType == KLCoilTypeLR) { rData[0] = 0;}
@@ -297,9 +312,10 @@
            else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
            else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
            else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
            else if (nDataType == KLCoilTypeT) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
            else if (nDataType == KLCoilTypeC) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
            else if (nDataType == KLCoilTypeC) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
#if (ENABLE_PLC)
            else if (nDataType == KLCoilTypeT) { PLCMem.Timers[nBitAddr].bTon = p1->Params[2];}
            else if (nDataType == KLCoilTypeC) { PLCMem.Timers[nBitAddr].bTon = p1->Params[2];}
#endif
            else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);;}
            else {rData[0]=0;}
            DataLen=0;
@@ -309,6 +325,12 @@
         case KLCmdReadBits:
            break;
         case KLCmdWriteBits:
            break;
#if (ENABLE_PLC)
         case KLCmdGetMode:
            rData[0]=   PLCMem.bPLCRunning;
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,1,rData);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdChgMode:
            if (nDataType ==0) {StopPLC();}
@@ -320,56 +342,75 @@
            nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
            DataLen= p1->Params[2];
             if (nDataType==0){
                  pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
                  pData = (unsigned short *)(&((stStoredBinProgs *)STORE_PRG_BASE)->BinInstrcns) + nWordAddr;
             } else if (nDataType==1){
                  pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
                  pData = (unsigned short *)(&((stStoredBinProgs *)ALT_PRG_BASE)->BinInstrcns) + nWordAddr;
             } else if (KMRunStat.nBinProgBank == 0) {
                  pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
                  pData = (unsigned short *)(&((stStoredBinProgs *)STORE_PRG_BASE)->BinInstrcns) + nWordAddr;
             }else {
                  pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
                  pData = (unsigned short *)(&((stStoredBinProgs *)ALT_PRG_BASE)->BinInstrcns) + nWordAddr;
             }
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
            SendPacket(nChn, p2, PacketLen);
            
            break;
         case KLCmdStartPLCProgram:
            DataLen=nDataType;
//            KLBufferIn[0]=p1->Params[0];
//            p1->Params[0]=KLBufferOut[0];
            if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
            DataLen=p1->Params[0] + (p1->Params[1] <<8);      //Program Size
            nValue = p1->Params[2] + (p1->Params[3] <<8);   // CRC
            //if (PLCMem.bPLCRunning) StopPLC();
            StartPLCProgram(nDataType, DataLen, nValue);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartPLCProgram,0,0);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdWritePLCProgram:
            if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
            nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
            DataLen= p1->Params[2];
            //DataLen=16;
            for (int i=0;i<DataLen;i++)
            {KLPacketBuf2[i]=p1->Params[4+i];}
            WriteProgram(nWordAddr, KLPacketBuf2, DataLen,nDataType);
            WritePLCProgram(nDataType, nWordAddr, &p1->Params[4], DataLen);
            DataLen=4;
            *((int *)(&rData[0]))=(long)(p1->Params+4);
//            *((int *)(&rData[0]))=(long)(p1->Params+4);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWritePLCProgram,0,0);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdFinishPLCProgram:
            nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);      //Program Size;
            DataLen=nDataType;
            KMRunStat.nBinProgSize=nWordAddr;
            if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;}
            else {KMRunStat.nBinProgBank=0;}
            SaveRunStat(&KMRunStat);
            //PLCMem.bPLCRunning=1;
//            KLBufferIn[0]=p1->Params[0];
//            p1->Params[0]=KLBufferOut[0];
            DataLen=p1->Params[0]+ (p1->Params[1]<<8);      //Program Size;
            nValue = p1->Params[2] + (p1->Params[3] <<8);            //CRC
            FinishiPLCProgram(nDataType, DataLen, nValue);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishPLCProgram,0,0);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdReadPLCAnno:
            nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
            DataLen= p1->Params[2];
            pData = ((stStoredAnno *)STORE_PLC_ANNO_BASE)->Annos + nByteAddr;
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdStartPLCAnno:
            nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
            DataLen= p1->Params[2];
            StartPLCAnno(nDataType, DataLen, nValue);
            DataLen=4;
//            *((int *)(&rData[0]))=(long)(p1->Params+4);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartPLCAnno,0,0);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdWritePLCAnno:
            nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);      ;
            DataLen = p1->Params[2];            //CRC
            WritePLCAnno(nDataType, nByteAddr, &p1->Params[4], DataLen);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWritePLCAnno,0,0);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdFinishPLCAnno:
            DataLen=p1->Params[0]+ (p1->Params[1]<<8);      //Program Size;
            nValue = p1->Params[2] + (p1->Params[3] <<8);            //CRC
            FinishiPLCAnno(nDataType, DataLen, nValue);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishPLCAnno,0,0);
            SendPacket(nChn, p2, PacketLen);
            break;
#endif // ENABLE_PLC
         case KLCmdBlinkLED:
            DataLen=nDataType;
            KMRunStat.bLEDFlick=DataLen;
@@ -453,14 +494,17 @@
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
            SendPacket(nChn, p2, PacketLen);
            __set_PRIMASK(1);    //关闭全局中断
            NVIC_SystemReset();
            KMRunService(ReqReset,0,0,0,0);
            break;               
         case KLCmdWriteFirmware:
            nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
            //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
            DataLen = p1->Params[2];
            res = WriteNewApp(nByteAddr,p1->Params+4,DataLen);
            pData = p1->Params + 4 ;
            res = KMRunService(ReqUpdateFirm,nByteAddr,0,&pData,&DataLen);
//            res = WriteNewApp(nByteAddr,p1->Params+4,DataLen);
//         res = 0;
            if (res) {
               PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,res,rData);               
            }else {               
@@ -468,14 +512,66 @@
            }
            SendPacket(nChn, p2, PacketLen);            
            break;
         case KLCmdWriteFirmInfo:
            nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
            //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
            DataLen = p1->Params[2];
            pData = p1->Params + 4 ;
            res = KMRunService(ReqUpdateFirmInfo,nByteAddr,0,&pData,&DataLen);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
            SendPacket(nChn, p2, PacketLen);      
            WriteNewAppInfo(nByteAddr,p1->Params+4,DataLen);
//            WriteNewAppInfo(nByteAddr,p1->Params+4,DataLen);
            break;
         case KLCmdGetPortInfo:
            nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
            //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
            pData = KMem.pPorts[nDataType];
            DataLen = sizeof(stPortDef);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdGetPortChnInfo:
            nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
            // nDataType --> port
            // nByteAddr --> ChildId
            //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
           KMPortReqFunc(nDataType,ReqPortChnInfo,nByteAddr,0,&pData,&DataLen);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdGetPortChildInfo:
            nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
            // nDataType --> port
            // nByteAddr --> ChildId
            //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
           KMPortReqFunc(nDataType,ReqPortChildInfo,nByteAddr,0,&pData,&DataLen);
            //DataLen = sizeof(stPortDef);
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
            SendPacket(nChn, p2, PacketLen);
            break;
         case KLCmdPortRemoteReq:      // 远程服务请求
            //p1->nType1 -> ReqId;
            //p1->Params[0] -> Port;
            //p1->Params[1] -> Child;
            //p1->params[2] -> Param;
            //p1->params[3] -> nAddr % 256;
            //p1->params[4] -> nAddr / 256;
            //p1->params[5] -> nCount;
            pData = &p1->Params[3];
            DataLen = p1->Params[5]+3;
            KMPortReqFunc(p1->Params[0],nDataType,p1->Params[1],p1->Params[2],&pData,&DataLen);
            DataLen=0;
            PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
            SendPacket(nChn, p2, PacketLen);
            break;
         default:
         //DataLen=1;