| | |
| | | #include "debug.h" |
| | | #include "Functions.h" |
| | | #include "KMachine.h" |
| | | #if (ENABLE_PLC) |
| | | #include "PLCfunctions.h" |
| | | #endif |
| | | //#include "KBus.h" |
| | | #include "KLink.h" |
| | | #include "string.h" |
| | | #include "BSP.h" |
| | | #include "ModbusRTU.h" |
| | | #if (BOARD_TYPE == 13) |
| | | #if (ENABLE_NET) |
| | | #include "w5500_port.h" |
| | | #include "../src/Ethernet/socket.h" |
| | | #include "../src/Ethernet/loopback.h" |
| | | #elif (BOARD_TYPE == 14) |
| | | #include "FP0.h" |
| | | #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16) |
| | | #endif |
| | | |
| | | #if (ENABLE_FPX) |
| | | #include "FPx.h" |
| | | #endif |
| | | |
| | | #if (ENABLE_RF) |
| | | #include "KWireless.h" |
| | | #endif |
| | | //#include "user.h" |
| | | //#include "../src/radio/inc/sx126x-board.h" |
| | | #endif |
| | | |
| | | |
| | | /* USER CODE END Includes */ |
| | | |
| | |
| | | unsigned char FastFlicker=0; |
| | | |
| | | unsigned int Uart1IdelTimer = 0; |
| | | #if (ENABLE_PLC) |
| | | stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE; |
| | | |
| | | #endif |
| | | uint32_t us1,us2,us3,us4,us5,us6; |
| | | |
| | | |
| | | stKBusDef KBus1; // |
| | | |
| | | /* USER CODE END PV */ |
| | | |
| | |
| | | static int Count=0; |
| | | CurTickuS += 100; |
| | | nCurTick++; |
| | | nSlaveTick++; |
| | | KBus1.nSlaveTick++; |
| | | Count++; |
| | | if (Count>=10000) |
| | | if (Count>=10000) // 0.1mS, 10000次, 秒脉冲 |
| | | { |
| | | Count=0; |
| | | KMem.CurTimeSec++; |
| | |
| | | return; |
| | | } |
| | | |
| | | void * KBusCallBackFunc(int nChn, int nEvent, void *pBuf, int nLen1) |
| | | void PendSvCallBack() |
| | | { |
| | | #if (ENABLE_FPX) |
| | | ///* |
| | | if (bSPI1RecvDone) |
| | | { |
| | | bSPI1RecvDone=0; |
| | | FPxParsePkt(SPI1RecvBuf,nSPI1RecvLenInBuf); |
| | | } |
| | | //*/ |
| | | #endif |
| | | if (Uart2Stat.bPacketRecved) |
| | | { |
| | | KBusParsePacket(&KBus1, (pKBPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen); |
| | | Uart2RecvBuf1DataLen=0; |
| | | Uart2Stat.bPacketRecved=0; |
| | | Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1)); |
| | | KMem.WDT[2]++; |
| | | } |
| | | } |
| | | |
| | | /* |
| | | KBus通讯回调函数,当通讯状态改变或数据更新时被调用。 |
| | | 或者系统请求时。 |
| | | */ |
| | | void * KBusEvCallBackFunc(void* pParam, int nEvent, void *pBuf, int nLen1) |
| | | { |
| | | switch (nEvent){ |
| | | |
| | |
| | | case KBusEvTimeSync: |
| | | break; |
| | | case KBusEvDataUpdate: |
| | | KMem.WDT[10]++; |
| | | if (KBus1.bMaster) { |
| | | KMem.WY[0]=KBusMem.WLX[0]; //KBus Master |
| | | KBusMem.WLY[0]=KMem.WX[0]; |
| | |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | extern stDeviceInfo MyDeviceInfo; |
| | | |
| | | /* USER CODE END 0 */ |
| | | |
| | |
| | | |
| | | /* USER CODE BEGIN Init */ |
| | | |
| | | for (int i=0;i<9;i++) |
| | | { |
| | | // memset(KBusChnStats[i],0,0); |
| | | KBusChnStats[i].SendPackets=0; |
| | | KBusChnStats[i].RecvPackets=0; |
| | | KBusChnStats[i].LostPackets=0; |
| | | KBusChnStats[i].CtnLstPkts=0; |
| | | KBusChnStats[i].MaxCtnLstPkts=0; |
| | | KBusChnStats[i].NotPkgErr=0; |
| | | KBusChnStats[i].PkgLenErr=0; |
| | | KBusChnStats[i].TimeOutErr=0; |
| | | KBusChnStats[i].BCCErr=0; |
| | | KBusChnStats[i].Delay=0; |
| | | KBusChnStats[i].MaxDelay=0; |
| | | } |
| | | |
| | | KMem.LastScanTime=0; |
| | | KMem.ScanTimeuS=0; |
| | | KMem.MinScanTimeuS=99999; |
| | |
| | | ReadSysCfgFromFlash(&storedKMSysCfg); |
| | | |
| | | KMRunStat.bLEDFlick = 1; |
| | | |
| | | |
| | | int bKBusMaster,bKBusSlave,bKBusRepeater;; |
| | | int nChilds; |
| | | int nKBusStationID; |
| | | KMem.CurJumperSW=ReadJumperSW(); |
| | | KMem.EffJumperSW=KMem.CurJumperSW; |
| | | |
| | | // Uart2Baud = AlterUart2Baud; nChilds=KMem.EffJumperSW&0x0f; |
| | | // Uart2Baud = AlterUart2Baud; |
| | | |
| | | nKBusStationID = KMem.EffJumperSW&0x0f; |
| | | nChilds=nKBusStationID; |
| | | |
| | | |
| | | #if (BOARD_TYPE == 14) |
| | | KMem.EffJumperSW|=0x10; |
| | | nStationID=KMem.EffJumperSW&0x0f; |
| | | if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;} |
| | | else{bKBusMaster=0;bKBusSlave=1;} |
| | | nChilds=nStationID; |
| | | FP0_Init(); |
| | | FPxSetCallBackFunc(&FPxCallBackFunc); |
| | | FPx_Init(nChilds); |
| | | |
| | | int IOByteCount = nChilds; |
| | | FPx_SetIOCount(IOByteCount,IOByteCount); |
| | | |
| | | #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16) |
| | | nStationID=1 ;//KMem.EffJumperSW&0x0f; |
| | | // if (KMem.EffJumperSW == 0x1f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;} |
| | |
| | | // else |
| | | {bKBusMaster=0;bKBusSlave=1;} |
| | | #else |
| | | nStationID=KMem.EffJumperSW&0x0f; |
| | | nKBusStationID=nChilds; |
| | | if (KMem.EffJumperSW == 0x1f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;} |
| | | else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;} |
| | | else{bKBusMaster=0;bKBusSlave=1;} |
| | | #endif |
| | | nChilds=nStationID; |
| | | nCurPollId=1; |
| | | else if ((KMem.EffJumperSW&0x10)!=0) { |
| | | bKBusMaster=1;bKBusSlave=0; |
| | | } |
| | | else{ |
| | | bKBusMaster=0;bKBusSlave=1; |
| | | } |
| | | |
| | | KBusInit(2, bKBusMaster, nChilds); |
| | | KBusSetCallBackFunc(2, &KBusCallBackFunc), |
| | | #endif |
| | | UNUSED(bKBusRepeater); |
| | | |
| | | if (bKBusMaster) { |
| | | KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nChilds); |
| | | |
| | | } else if (bKBusSlave) { |
| | | KBusInitSlave(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusStationID,&MyDeviceInfo); |
| | | } |
| | | |
| | | KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc); |
| | | |
| | | KMem.WDT[0]= nKBusStationID; |
| | | KMem.WDT[1] = KBus1.nStationId; |
| | | |
| | | //if (KMem.EffJumperSW == 0x00) |
| | | Uart1Baud = DefaultUart1Baud; |
| | |
| | | KMRunStat.WorkMode2=0; |
| | | |
| | | KMRunStat.WorkMode = storedKMSysCfg.theKMSysCfg.workmode; |
| | | |
| | | #if (ENABLE_PLC) |
| | | if (KMRunStat.WorkMode == 1){ |
| | | InitPLC(); |
| | | KMRunStat.WorkMode2 = KMem.CurJumperSW&0x20 ; |
| | | if (KMRunStat.WorkMode2) { |
| | | StartPLC(); } |
| | | } |
| | | #endif |
| | | KMem.WX[7]=0x5a; |
| | | #if (BOARD_TYPE == 15 || BOARD_TYPE == 16) |
| | | KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x0f); |
| | | KWireLessStart(); |
| | | #endif |
| | | |
| | | KMem.WY[0]=0; |
| | | while (1) |
| | | { |
| | | //int MyKeyStat1,MyKeyStat2; |
| | |
| | | int haltick=HAL_GetTick(); |
| | | |
| | | int thisJumperSW=ReadJumperSW(); |
| | | |
| | | #if (ENABLE_PLC) |
| | | if (KMRunStat.WorkMode&1){ |
| | | if (thisJumperSW&0x20 && !(KMem.CurJumperSW&0x20)) // Run 开关 正 跳变。 |
| | | {StartPLC();} |
| | | if (!(thisJumperSW&0x20) && (KMem.CurJumperSW&0x20)) // Run 开关 负 跳变。 |
| | | {StopPLC();} |
| | | } |
| | | |
| | | #endif |
| | | KMem.CurJumperSW=thisJumperSW; |
| | | KMem.haltick=haltick; |
| | | // KMem.TotalRunTime=TotalRunTime; |
| | |
| | | #else |
| | | KMem.WX[0]= GetInput(); |
| | | #endif |
| | | |
| | | if (GetBoardType() == 7 || GetBoardType() ==8 |
| | | || GetBoardType() == 9 || GetBoardType() ==10 || GetBoardType() ==15 || GetBoardType() ==16) |
| | | { |
| | |
| | | #endif |
| | | |
| | | // pProgs = (stBinProg1 *) STORE_PRG_BASE; |
| | | |
| | | #if (ENABLE_PLC) |
| | | if ( KMRunStat.WorkMode==1 ) //&& bKBusMaster) |
| | | { |
| | | if (KMRunStat.nBinProgBank == 0){ |
| | |
| | | |
| | | ProcessPLCBinProg(pProgs, nSizeProg1); |
| | | } |
| | | |
| | | #endif |
| | | |
| | | KMem.ScanTimeuS=us2-KMem.LastScanTime; |
| | | KMem.LastScanTime = us2; |
| | | if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;} |
| | |
| | | #if (BOARD_TYPE == 14) |
| | | for (int i=0;i<nOutputBytes;i++) |
| | | {BufferOut[i+1]=KMem.WYB[i];} |
| | | #else |
| | | // BufferOut[1]=KMem.WX[0]&0xff; |
| | | // BufferOut[2]=(KMem.WX[0]>>8)&0xff; |
| | | #endif |
| | | KBusMem.WLY[0]=KMem.WX[0]; |
| | | if (nChilds>0) { KBusMasterFunc(2); } |
| | | KBusLoopProcess(&KBus1); |
| | | KMem.WY[0]=KBusMem.WLX[0]; //KBus Slave |
| | | |
| | | #if (BOARD_TYPE == 14) |
| | | // KMem.WX[0]=BufferIn[1]+(BufferIn[2]<<8); |
| | | #else |
| | | // KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8); |
| | | #endif |
| | | |
| | | } |
| | | if (haltick&0x00002000) SlowFlicker=1; |
| | |
| | | // if (! KMem.RunStat) {BufferIn[0]=0;} |
| | | // KMem.WY[0]=BufferIn[0]; |
| | | #else |
| | | KBusSlaveFunc(2); |
| | | // KBusSlaveFunc(&KBus1); |
| | | KBusLoopProcess(&KBus1); |
| | | // if (! KMem.RunStat) {BufferIn[0]=0;} |
| | | // KMem.WLY[0]=BufferIn[0]; |
| | | KMem.WDT[2] = KBus1.KBusChnStats[0].ClientRecvPkts; |
| | | |
| | | KMem.WDT[3] = KBus1.KBusChnStats[0].ClientSendPkts; |
| | | |
| | | |
| | | KMem.WDT[8] = KBus1.RunStat; |
| | | KMem.WDT[9] = KBus1.ErrStat; |
| | | |
| | | KMem.WDD[20] = KBus1.RecvTimeTick; |
| | | |
| | | #endif |
| | | if (nSlaveTick&0x00002000) SlowFlicker=1; |
| | | if (KBus1.nSlaveTick&0x00002000) SlowFlicker=1; |
| | | else SlowFlicker=0; |
| | | if (nSlaveTick&0x00000800) FastFlicker=1; |
| | | if (KBus1.nSlaveTick&0x00000800) FastFlicker=1; |
| | | else FastFlicker=0; |
| | | KBusMem.WLX[0]=KMem.WX[0]; |
| | | KMem.WY[0]=KBusMem.WLY[0]; |
| | | // KBusMem.WLX[0]=KMem.WX[0]; |
| | | // KMem.WY[0]=KBusMem.WLY[0]; |
| | | } |
| | | |
| | | // KMem.WY[0]=nCount2>>5; |
| | | if (KMem.RunStat) {KMem.RunStat--;} |
| | | if (KMem.ErrStat) {KMem.ErrStat--;} |
| | | |
| | | if (KMRunStat.bLEDFlick) |
| | | { |
| | |
| | | } |
| | | else |
| | | { |
| | | KMem.ErrStat = KBus1.ErrStat; // + KwRunStat.ErrStat; |
| | | #if (ENABLE_PLC) |
| | | if (KMRunStat.WorkMode==1 ) { |
| | | if (PLCMem.bPLCRunning){SetRunLed(SlowFlicker);} |
| | | else {SetRunLed(0);} |
| | | } |
| | | else { |
| | | else |
| | | #endif |
| | | { |
| | | if (!KMem.RunStat) SetRunLed(SlowFlicker); |
| | | else SetRunLed(FastFlicker); |
| | | } |
| | |
| | | SetErrLed(FastFlicker); |
| | | SetErr2Led(FastFlicker); |
| | | SetOutStat(0); |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | #endif |
| | | |
| | | us5=GetuS(); |
| | | us5=GetTick(); |
| | | |
| | | #if (BOARD_TYPE == 14) |
| | | // PutOutput (KMem.WY[0]); |