/** ****************************************************************************** * @file : KBus.h * @brief : Header for KBus.c file. * This file contains the common defines of the application. ****************************************************************************** */ #include "KBusDefine.h" #include "KMachine.h" #ifndef __KBUS_H__ #define __KBUS_H__ #define KBUS_VER (0x104) #define MAX_CLIENT 16 //×î´óÖ§³Ö×Ó»úÊýÁ¿ #define KBUS_WAIT_TIME_OUT1 5 // 0.1mS ticks. #define KBUS_WAIT_TIME_OUT2 8 // 0.1mS ticks. #define KBUS_SLAVE_TIME_OUT 500 // 0.1ms ticks //#define SYN_PREFIX 1 typedef unsigned char uchar; typedef volatile unsigned char vuchar; typedef unsigned short ushort; enum enKBusStationType // KBus¹¤×÷ÀàÐÍ { KBNone = 0, // 0,䶨Òå KBusMaster = 1, // 1,Ö÷»ú KBusSlave = 2, // 2,×Ó»ú KBusRepeater = 3, // 3,ת·¢Æ÷,ÖмÌÆ÷ }; // diagnosis enum enKBusStats { KBusStatUnInited, // δ³õʼ»¯×´Ì¬ KBusStatInited, // Òѳõʼ»¯×´Ì¬ KBusStatConfig, // ÒÑÅäÖÃ״̬ KBusStatReady, // ¾ÍÐ÷״̬ KBusStatRunning, // ÔËÐÐ״̬ KBusStatIdel, // ÔÝͣ״̬ KBusStatSafe, // °²È«×´Ì¬ KBusStatError, // ´íÎó״̬ }; /* enum enKBusStage { KBusStageInit, KBusStageFindChild, KBusStageConfigChild, KBusStagePoll, }; */ typedef enum { KBusRunStepBroadCastCfg, KBusRunStepTimeSync, KBusRunStepTimeSyncWait, KBusRunStepMultiDataPoll, KBusRunStepMultiDataPollWait, KBusRunStepUniDataPoll, KBusRunStepUniDataPollWait, KBusRunStepUniDataTranster, KBusRunStepUniDataTransterWait, }enKBusRunStep; enum { KB_SYN = 0x55, KBStartSign='%', KBEndSign=0x0D, KBMaxPacketLength=148, }; enum enCMDs { cmdNone = 0x00, //Nothing cmdQuery = 0x01, //Query basic Info cmdQueryRply = 0x81, //Query Info Reply cmdSetCfg = 0x02, // Set Configuration cmdSetCfgRply = 0x82, // Set Configuration Response cmdToRunMode = 0x03, cmdToRunModeRply = 0x83, cmdBroadCastCfg = 0x04, // cmdMuExchgData = 0x05, // cmdMuExchgDataRply = 0x85, // cmdToSafeMode = 0x06, cmdHeartBeat = 0x07, // cmdReHeartBeat = 0x87, // cmdRemoteReq = 0x1B, cmdRemoteReqReply = cmdRemoteReq|0x80, // cmdPing = '1', //Ping cmdPingReply = '2', //PingReply cmdRead = '3', //Read cmdReadReply = '4', //ReadReply cmdWrite = '5', //Write cmdWriteReply = '6', //WriteReply cmdGetVersion = '7', //GetVersion cmdVerInfo = '8', //VersionReply cmdUniExChgData='A', //ExChangeData, = Wirte + Read cmdExChgDataReply, //ExchangeData Reply cmdSyncRead='a', //SyncRead cmdSyncWrite, //SyncWrite cmdSequenRead, //Sequence Read cmdSyncTime, //SyncTime }; typedef struct tagVerInfo // KBus °æ±¾ÐÅÏ¢ { unsigned char nMainVer; unsigned char nSubVer; }stVerInfo; typedef struct tagMasterStat // KBus Ö÷»ú״̬£¿ { unsigned int CycleTime; } stMasterStat; typedef struct tagDeviceInfo // KBus ×Ó»ú»ù±¾ÐÅÏ¢ { unsigned short DeviceType; // ×Ó»úÀàÐÍ unsigned short DeviceVer; // ×Ó»ú°æ±¾ unsigned char InBitCount; // ÊäÈ뿪¹ØÁ¿ÊýÁ¿ unsigned char OutBitCount; // Êä³ö¿ª¹ØÁ¿ÊýÁ¿ unsigned char ExInBitCount; // À©Õ¹µÄÊäÈ뿪¹ØÁ¿ÊýÁ¿ unsigned char ExOutBitCount; // À©Õ¹µÄÊä³ö¿ª¹ØÁ¿ÊýÁ¿ unsigned char DWStartAddr; // Êä³öÊý¾Ý×ÖÊý unsigned char OutDWCount; // Êä³öÊý¾Ý×ÖÊý unsigned char AIWCount; // ÊäÈëÄ£ÄâÁ¿Í¨µÀ(×Ö)Êý // 16λΪһ¸ö×Ö(ͨµÀ) unsigned char AQWCount; // Êä³öÄ£ÄâÁ¿Í¨µÀ(×Ö)Êý // 16λΪһ¸ö×Ö(ͨµÀ) // unsigned char AIBits; // ÿͨµÀλÊý // 16λÒÔÏ // unsigned char AQbits; // ÿͨµÀλÊý // 16λÒÔÏ }stDeviceInfo; typedef struct tagExDeviceInfo // ×Ó»úÀ©Õ¹ÐÅÏ¢ { unsigned short DeviceType; // ×Ó»úÀàÐÍ unsigned short DeviceVer; // ×Ó»ú°æ±¾ unsigned char UID[12]; // 12×Ö½Ú UUID unsigned char Name[16]; // 16×Ö½Ú Ãû³Æ // stVerInfo ClientVer; unsigned char InBitCount; unsigned char OutBitCount; unsigned char ExInBitCount; // À©Õ¹µÄÊäÈ뿪¹ØÁ¿ÊýÁ¿ unsigned char ExOutBitCount; // À©Õ¹µÄÊä³ö¿ª¹ØÁ¿ÊýÁ¿ unsigned char DWStartAddr; unsigned char OutDWCount; unsigned char AIWCount; unsigned char AQWCount; // unsigned char AIBits; // unsigned char AQbits; }stExDeviceInfo; enum enKBusConfigStat { KBusUnConfiged = 0, KBusDefaultConfiged =1, KBusConfiged = 2, }; typedef struct tagClientCfg // ×Ó»úÅäÖà { unsigned char Addr; // ×Ó»úµØÖ· unsigned char Configed; // ÊÇ·ñÒÑÅäÖà unsigned char bOnline; //µ±Ç°ÔÚÏß״̬ unsigned char InStartAddrBit; //ÊäÈëÆðʼλµØÖ· unsigned char OutStartAddrBit; //Êä³öÆðʼλµØÖ· unsigned char AIWStartAddrByte; //Ä£ÄâÈëÆðʼ×ÖµØÖ· unsigned char AQWStartAddrByte; //Ä£Äâ³öÆðʼ×ÖµØÖ· unsigned char DIWStartAddrByte; //Êý¾ÝÈëÆðʼ×ÖµØÖ· unsigned char DOWStartAddrByte; //Êý¾Ý³öÆðʼ×ÖµØÖ· unsigned char KeepOut; //Keep Output when comm err; }stClientCfg; /* typedef struct ServerStatus { unsigned char Addr; unsigned char Status; unsigned short Input[16]; unsigned short DT[128]; unsigned short output[16]; }stServerStatus,*pServerStatus; */ typedef struct tagMachineState // ±¾»ú״̬ { unsigned char Addr; // µØÖ· unsigned char state; //״̬ unsigned char InStartAddrBit; //ÊäÈëÆðʼλµØÖ· unsigned char OutStartAddrBit; //Êä³öÆðʼλµØÖ· unsigned char InDWStartAddrByte; //×ÖÊäÈëÆðʼ×ÖµØÖ· unsigned char OutDWStartAddrByte; //×ÖÊä³öÆðʼ×ÖµØÖ· }ClientState; #pragma anon_unions typedef struct tagChnStat { union{ unsigned char MStat; struct{ unsigned char bOnline:1; unsigned char bErr1:1; unsigned char bErr2:1; unsigned char bReq:1; }; }; unsigned char SStat; unsigned short SendPackets; unsigned short RecvPackets; unsigned short LastSentTimeTick; unsigned short LostPackets; unsigned short CtnLstPkts; unsigned short MaxCtnLstPkts; unsigned short NotPkgErr; unsigned short PkgLenErr; unsigned short BCCErr; unsigned short TimeOutErr; unsigned short Delay; unsigned short MaxDelay; unsigned short SendTimeInterval; union { unsigned short ClientDatas[10]; struct { unsigned short ClientRecvPkts; // unsigned short ClientSendPkts; // unsigned short ClientNotPktErr; // unsigned short ClientMisIdPkts; // // unsigned int ClientNoEndErr; // unsigned short ClientPkgLenErr; // unsigned short ClientBccErr; // unsigned short ClientTimeOutErr; // }; }; } stChnStat; typedef struct tagSlaveStat { unsigned int nSlaveTick; } stSlaveStat; typedef struct ServerClientListElement { unsigned char Addr; unsigned char Status; unsigned char Father; unsigned char Childs; } stServerClientListElement; typedef struct ServerClientList { unsigned char Addr; }stServerClientList,*pServerClientList; typedef struct tagMachineConfig { int bKBusMaster; int nAddr; }stMachineConfig; /* typedef struct ClientStatus { unsigned char Addr; unsigned char Status; unsigned char Father; unsigned char Childs; unsigned short Input[16]; unsigned short DT[128]; unsigned short output[16]; }stChnStatus,*pClientStatus; */ typedef struct tagStatusDef { unsigned char nSeq:2; //ÐòÁкŠunsigned char :2; unsigned char nErr1:1; unsigned char nErr2:1; unsigned char bReq:1; }stStatus; typedef union tagUnStatusDef { unsigned char nStatus; stStatus; }unStatus; typedef struct tagKBPacket { unsigned char Sign; //Æðʼ±ê¼Ç unsigned char DstHost; //Ä¿±êµØÖ· unsigned char SrcAddr; //Ô´µØÖ· unsigned char nCMD; //ÃüÁî union{ unsigned char nStatus; //״̬+ÐòÁкŠstruct{ unsigned char nSeq:2; //ÐòÁкŠunsigned char :2; unsigned char nErr1:1; //´íÎó1 unsigned char nErr2:1; //´íÎó2 unsigned char bReq:1; // }; }; unsigned char DataLen; //Êý¾ÝÔغɳ¤¶È ²»°üÀ¨Í·²¿5¸ö×Ö½Ú£¬²»°üÀ¨Î²²¿BCC¡£ unsigned char data[1]; //Êý¾ÝÔغÉ,×îĩβÊÇBCC£¬Êý¾Ý³¤¶ÈΪ0ʱ£¬Êµ¼ÊÒ²ÓÐÒ»¸öÊý¾Ý¡£ }stKBPacket,* pKBPacket; typedef struct tagKBInfoBlockV1 { unsigned short nDeviceType; unsigned short nProgVer; unsigned char InBitCount; unsigned char OutBitCount; unsigned char AICount; unsigned char AIBits; unsigned char AQCount; unsigned char AQbits; unsigned char InDWCount; unsigned char OutDWCount; }stKBInfoBlockV1, *pKBInfoBlockV1; enum eResult { S_OK =0, S_ERR=1, S_TIMEOUT=2 } ; /* typedef struct tagKBPacketV10 { unsigned char Sign; //Æðʼ±ê¼Ç unsigned char DstHost; //Ä¿±êµØÖ· unsigned char SrcAddr; //Ô´µØÖ· unsigned char nCMD; //ÃüÁî union{ unsigned char nStatus; //״̬+ÐòÁкŠstruct{ unsigned char nSeq:2; //ÐòÁкŠunsigned char :2; unsigned char nErr1:1; unsigned char nErr2:1; }; }; unsigned char DataLen; //Êý¾ÝÔغɳ¤¶È ²»°üÀ¨Í·²¿5¸ö×Ö½Ú£¬²»°üÀ¨Î²²¿BCC¡£ unsigned char data[1]; //Êý¾ÝÔغÉ,×îĩβÊÇBCC£¬Êý¾Ý³¤¶ÈΪ0ʱ£¬Êµ¼ÊÒ²ÓÐÒ»¸öÊý¾Ý¡£ }stKBPacketV10,* pKBPacketV10; typedef struct tagKBPacketV20 { unsigned char Sign; //Æðʼ±ê¼Ç unsigned char DstHost; //Ä¿±êµØÖ· unsigned char SrcAddr; //Ô´µØÖ· unsigned char nCMD; //ÃüÁî union{ unsigned char nStatus; //״̬+ÐòÁкŠstruct{ unsigned char nSeq:2; //ÐòÁкŠunsigned char :2; unsigned char nErr1:1; unsigned char nErr2:1; }; }; unsigned char DataLen; //Êý¾ÝÔغɳ¤¶È ²»°üÀ¨Í·²¿5¸ö×Ö½Ú£¬²»°üÀ¨Î²²¿BCC¡£ unsigned char data[1]; //Êý¾ÝÔغÉ,×îĩβÊÇBCC£¬Êý¾Ý³¤¶ÈΪ0ʱ£¬Êµ¼ÊÒ²ÓÐÒ»¸öÊý¾Ý¡£ }stKBPacketV20,* pKBPacketV20; */ enum enumCallBackEvent { KBusEvNone = 0, KBusEvCreate = 1, KBusEvConnected = 2, KBusEvDisConnected = 3, KBusEvClosed = 4, KBusEvStateChange =5, KBusEvTimeSync=6, KBusEvDataUpdate =7, KBusEvCmdResponse = 8, }; typedef int (*KBusSendPktFuncDef) (uchar * pBuf, int nLen); // SendPacket CallBack func for KBus typedef void *(*KBusEvCBFuncDef) (void * , int nEvent, void *, int); //callback func ,prama s is void *,void *,int; return void *; typedef int (*KBusSvCBFuncDef) (void *, int nSvType, int ,void *, int); //Service Req callback func ,param is int ,int, int, void *,int; return int; /* typedef struct tagKBusRunStat { unsigned char bMaster; // ÊÇ·ñÖ÷»ú unsigned char nStationID; // ×Ó»úµØÖ· unsigned char nChildren; // ×Ó»úÊýÁ¿ unsigned char nCurPollId; // µ±Ç°ÂÖѯµ½µÄ×Ó»ú µØÖ·¡£ unsigned char nSeq; // °üÐòÁкŠunsigned char bMasterRecved; // ״̬£¬ Ö÷»úÒѽÓÊÕµ½·µ»ØµÄÊý¾Ý°ü unsigned char bMasterRecvOK; // ״̬£¬ Ö÷»ú½ÓÊÕµ½µÄ·µ»ØµÄÊý¾Ý°üÕýÈ· unsigned char bSlaveRecved; // ״̬£¬ ×Ó»ú½ÓÊܵ½Êý¾Ý°ü¡£ unsigned int nSendTimeuS; // Êý¾Ý°ü·¢³öʱ¿Ì¡£ }stKBusRunStat , * pstKBusRunStat; */ #pragma anon_unions typedef struct tagKBusDef // KBus ¹¤×÷ÄÚ´æ { ///* union { uchar nConfig; struct { uchar bMaster:1; //KBus, ÊÇ·ñÖ÷»ú uchar bSlave:1; //KBus, ÊÇ·ñ×Ó»ú uchar bRepeater:1; //KBus, ÊÇ·ñת·¢Æ÷,ÖмÌÆ÷ }; }; // */ /* uchar bMaster; //KBus, ÊÇ·ñÖ÷»ú uchar bSlave; //KBus, ÊÇ·ñ×Ó»ú uchar bRepeater; //KBus, ÊÇ·ñת·¢Æ÷,ÖмÌÆ÷ */ uchar nStationId; //KBus Õ¾ºÅ vuchar MyStat; //KBus, ״̬»ú״̬ vuchar nRunStep; //KBus, ÔËÐв½ vuchar nSeq; //KBus, °üÐòÁкŠvuchar bMasterSent; // ״̬ Ö÷»úÒÑ·¢ËÍ vuchar bMasterRecved; // ״̬ Ö÷»úÒѽÓÊÕ »Ø°ü vuchar bMasterRecvOK; // ״̬ Ö÷»ú½ÓÊջذüÕýÈ· vuchar bSlaveRecved; // ״̬ ×Ó»úÒѽÓÊÕ°ü vuchar nChildCount; // ×Ó»úÊýÁ¿ uchar nCurPollId; // µ±Ç°ÂÖѯ×Ó»ú ºÅ ushort ErrStat; ushort RunStat; uchar bReq; //ÔËÐÐϵͳÇëÇóÖÐ uchar nReqSvrId; //ÇëÇóºÅ uchar nReqChildId; //ÇëÇóµÄ×Ó»úºÅ uchar nReqParam; //ÇëÇóµÄ²ÎÊý ushort nReqReCount; // uchar nReqDataHdr[4]; uchar pReqDatas[140]; uchar nReqDataOff; uchar nReqDataLen; int SendTimeTick; // ·¢°üʱ¿Ì int RecvTimeTick; // ÊÕ°üʱ¿Ì int DelayTick; // °üÑ­»·ÑÓ³Ù int nMaxDelayTick; // ×î´ó°üÑ­»·ÑÓ³Ù int LastCircleStartTime; //=0; int CircleTime; //=0; int nSlaveTick; // ×Ó»úʱ¼ä tick // int ThisuS; //volatile unsigned int nSlaveTick; //=0; int Clk3; //=0; int SendTime,Latancy,LatancyClk,SendClk; //volatile int nCount2; //=0; int TimeOutCount; //=0; stPortDef KBusPort; KBusSendPktFuncDef KBusSendPacket; // ·¢ËÍÊý¾Ý°üº¯ÊýÖ¸Õë KBusEvCBFuncDef KBusEvCBFunc; // ʼþ»Øµ÷º¯ÊýÖ¸Õë KMSvCBDef KBusSvCBFunc; // ·þÎñÇëÇó»Øµ÷Ö¸Õë stDeviceInfo* pMyDeviceInfo; uchar RetryCount; // ÖØÊÔ´ÎÊý uchar nTimeOutCount; // ³¬Ê±¼ÆÊý vuchar step; // µ±Ç°²½Öè uchar ChildStat[MAX_CLIENT]; // ×Ó»ú״̬(ÃÇ) uchar CurXBitAddr; // ÒÑ·ÖÅä X λ µØÖ· uchar CurYBitAddr; // ÒÑ·ÖÅä Y λ µØÖ· uchar CurAIWAddr; // ÒÑ·ÖÅä Ä£ÄâÁ¿Èë ×Ö µØÖ· uchar CurAQWAddr; // ÒÑ·ÖÅä Ä£ÄâÁ¿³ö ×Ö µØÖ· uchar CurDIWAddr; // ÒÑ·ÖÅä Êý¾ÝÈë ×Ö µØÖ· uchar CurDOWAddr; // ÒÑ·ÖÅä Êý¾Ý³ö ×Ö µØÖ· uchar MyYStartBitAddress; // ±¾»ú Æðʼ Y λµØÖ· uchar MyAQWStartWordAddress; // ±¾»ú Æðʼ Ä£ÄâÁ¿ ³ö ×Ö µØÖ· uchar MyDWStartWordAddress; // ±¾»ú Æðʼ Êý¾Ý³ö ×Ö µØÖ· stDeviceInfo DeviceInfos[MAX_CLIENT]; // ×Ó»úÐÅÏ¢(ÃÇ) stClientCfg ClientCfg[MAX_CLIENT]; // ×Ó»úÅäÖÃ(ÃÇ) stChnStat KBusChnStats[MAX_CLIENT]; // uchar bKBusEvCBFuncSet; // ʼþ»Øµ÷º¯ÊýÊÇ·ñÒѾ­ÉèÖà // uchar bKBusSvCBFuncSet; // ʼþ»Øµ÷º¯ÊýÊÇ·ñÒѾ­ÉèÖà // uchar PacketBuf1[KBMaxPacketLength]; uchar PacketBuf2[KBMaxPacketLength]; unsigned char Datas[128]; volatile int PacketLength; // = 0; unsigned char KBusDiagnosis; // =1; // Kbus Õï¶Ïģʽ unsigned char KBusSnapPos; // = 0; // ץȡ Kbus °ü // unsigned char nStationID; //=0; // unsigned char nChilds; //int ChildStat[16]; // unsigned char nCurPollId; //=0; // unsigned char nSeq; //=0; unsigned char nClientDataIndex; //=0; }stKBusDef,*pstKBusDef; typedef struct tagKBusMem { union{ ushort WLX[16]; uchar WLXB[32]; }; union{ ushort WLY[16]; uchar WLYB[32]; }; ushort AIW[16]; ushort AOW[16]; ushort DIW[16]; ushort DOW[16]; }stKBusMem, *pstKBusMem; extern stKBusMem KBusMem; /* typedef struct tagKBusDiag { int Stat; unsigned int SendPackets; unsigned int RecvPackets; unsigned int LastSentTimeuS; unsigned int LostPackets; unsigned int CtnLstPkts; unsigned int MaxCtnLstPkts; unsigned int NotPkgErr; unsigned int PkgLenErr; unsigned int BCCErr; unsigned int TimeOutErr; unsigned int Delay; unsigned int MaxDelay; unsigned int SendTimeInterval; union { unsigned int ClientDatas[10]; struct { unsigned int ClientRecvPkts; // unsigned int ClientSendPkts; // unsigned int ClientNotPktErr; // unsigned int ClientMisIdPkts; // // unsigned int ClientNoEndErr; // unsigned int ClientPkgLenErr; // unsigned int ClientBccErr; // unsigned int ClientTimeOutErr; // }; }; }stKBusDiag, *pstKBusDiag; */ int KBusInit(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int bMaster, int nChildCount); int KBusInitMaster(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int nChildCount); int KBusInitSlave(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int nStationId, stDeviceInfo *pClientInfo); int KBusLoadSavedConfig(stKBusDef * pKBus); int KBusSetEvCallBackFunc(stKBusDef * pKBus, KBusEvCBFuncDef CBFunc); // Set Event CallBack function int KBusSetSvCallBackFunc(stKBusDef * pKBus, KMSvCBDef CBFunc); // Set ServiceReq CallBack Function int KBusMakeDefaultClildInfo(stKBusDef * pKBus, int nChild); int KBusReqService (stKBusDef * pKBus, int ReqId, int nParam1, int nParam2, void **pData, unsigned short * len1); int KBusStart(stKBusDef * pKBus) ; int KBusStop(stKBusDef * pKBus) ; int KBusDataPoll(stKBusDef * pKBus); int KBusLoopProcess(stKBusDef * pKBus); int KBusMasterFunc(stKBusDef * pKBus); int KBusSlaveFunc(stKBusDef * pKBus); int KBusRepeaterFunc(stKBusDef * pKBus); /* Make a Packet and return Packet Length */ int KBusMakePacket(pKBPacket p1,uchar Src, uchar Dst, uchar nType, uchar nStatus, uchar DataLen, void *pData ); /* */ int KBusCheckPacket(stKBusDef * pKBus, pKBPacket p1, int len1); /* */ int KBusParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1); int KBusPacketSendDone(stKBusDef * pKBus); /* static inline char KBGetClientNum(int nChn) { }; static inline char KBGetClientInfo(int nChn, int nClientId) { }; static inline char KBGetClientIOCount(int nChn,int nClientId) {}; int KBGetClientStatus(int nChn); static inline char KBisSafeState(int nPortNum) { }; static inline char KBisRunState(int nPortNum) { }; static inline char KBGetState(int nPortNum) { }; static inline char KBChangeState(int nPortNum, int nNewState){ }; */ unsigned char KBusBCC(void * pData, int nSize); // int KBusSlaveRunRemoteReq(stKBusDef * pKBus, int nReqSvrId, unsigned char * pData , int Len1); /* int InitMachine(stMachineConfig * pConfig); int SetConfig(void ); int StartConfig(void); int SetMasterConfig(void); int StartPolling(void); int ReadData(void); int WriteData(void); int GetStat(void); */ #endif /* __KBUS_H__ */