QuakeGod
2022-12-10 0fe6b074f3f3994d87af195f37e349a83e27882c
FP0 fix utf8 char
1个文件已删除
19个文件已修改
1 文件已重命名
4个文件已添加
7240 ■■■■ 已修改文件
Inc/GlobalDef.h 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Inc/KBus.h 62 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Inc/KLink.h 35 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Inc/KMachine.h 310 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Inc/ModbusRTU.h 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Inc/functions.h 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/.vscode/c_cpp_properties.json 101 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/.vscode/keil-assistant.log 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/.vscode/uv4.log.lock 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/F030C8T6_FP0.uvprojx 90 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/F030C8T6_Test2.uvoptx 881 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/JLinkLog.txt 4960 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/RTE/_F030C8T6_Test2/RTE_Components.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/pemicro_connection_settings.ini 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/BSP.c 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/FP0.c 34 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/KBus.c 242 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/KLink.c 197 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/KMachine.c 50 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/ModbusRTU.c 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/PLCfunctions.c 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/debug.c 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/functions.c 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/main.c 73 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Src/stm32f0xx_it.c 16 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Inc/GlobalDef.h
@@ -36,17 +36,23 @@
    BOARD_V42_SLAVE,        //10    New V4.2 Slave 8 in 8 o    
    
    BOARD_V30_MINI    =11,        //11  Mini Board
    BOARD_V45_NET = 13,
    BOARD_EXT_FP0 = 14,
    BOARD_V50_RADIO_16 = 15,
    BOARD_V50_RADIO_8 = 16,
    
    BOARD_V50_RADIO = 15,
};
#define BOARD_TYPE 14
#define XLAT_FREQ 12
#define BOARD_VER 1
#if (BOARD_TYPE == 11)
#define XLAT_FREQ 12
#elif (BOARD_TYPE == 14)
#define XLAT_FREQ 12
#else
#define XLAT_FREQ 8
#endif
#define GetBoardType() (BOARD_TYPE)
@@ -54,13 +60,13 @@
typedef struct tagUartStat
{
    volatile int Inited;
    volatile int RecvBytes;
    volatile int SendBytes;
    volatile int Sending;
    volatile int Recving;
    volatile int UseAltRecvBuf;
    volatile int IntCount;
    volatile int RecvBytes;
    volatile char bInited;
    volatile char bSending;
    volatile char bRecving;
    volatile char bUseAltRecvBuf;
    volatile int IRQCount;
    volatile int RXNECount;
    volatile int TXECount;    
    volatile int PECount;
@@ -91,21 +97,21 @@
extern unsigned char Uart1RecvBuf1[128];
extern int Uart1RecvBuf1DataLen;
extern unsigned char Uart1RecvBuf2[128];
extern int Uart1RecvBuf2DataLen;
//extern unsigned char Uart1RecvBuf2[128];
//extern int Uart1RecvBuf2DataLen;
extern unsigned char Uart2RecvBuf1[128];
extern int Uart2RecvBuf1DataLen;
extern unsigned char Uart2RecvBuf2[128];
extern int Uart2RecvBuf2DataLen;
//extern unsigned char Uart2RecvBuf2[128];
//extern int Uart2RecvBuf2DataLen;
extern volatile int Uart1BaudGot;
extern volatile int Uart1BaudFirstGot;
extern volatile int Uart2BaudGot;
extern volatile int Uart2BaudFirstGot;
//extern volatile int Uart2BaudGot;
//extern volatile int Uart2BaudFirstGot;
extern volatile int Uart1DmaInts;
extern volatile int Uart2DmaInts;
//extern volatile int Uart1DmaInts;
//extern volatile int Uart2DmaInts;
struct stSysConfig{
    int bInited:1;
Inc/KBus.h
@@ -41,25 +41,38 @@
extern unsigned char PacketBuf1[128];
extern unsigned char PacketBuf2[128];
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;
extern unsigned char BufferIn[16];
extern unsigned char BufferOut[16];
extern unsigned char nAddr;
extern unsigned char nStationID;
extern unsigned char nChilds;
extern int ChildStat[16];
extern unsigned char nCurPollId;
extern unsigned char nSeq;
extern volatile unsigned char MasterRecved;
extern volatile unsigned char MasterRecvOK;
extern volatile unsigned char SlaveRecved;
extern unsigned int SendTimeuS;
extern volatile int RecvTimeuS;
extern int DelayuS;
extern int MaxDelayuS;
extern volatile unsigned char KBusMasterRecved;
extern volatile unsigned char KBusMasterRecvOK;
extern volatile unsigned char KBusSlaveRecved;
extern unsigned int KBusSendTimeuS;
extern volatile int KBusRecvTimeuS;
extern int KBusDelayuS;
extern int KBusMaxDelayuS;
extern volatile unsigned int nSlaveTick;
typedef struct tagVerInfo
{
@@ -107,8 +120,7 @@
    unsigned char OutPutCount;
    unsigned char InDWCount;
    unsigned char OutDWCount;
    unsigned char Online;
    unsigned char bOnline;
};
#pragma anon_unions
@@ -144,7 +156,7 @@
    };
} stChnStat;
extern stChnStat ChnStats[8];
extern stChnStat KBusChnStats[8];
typedef struct tagSlaveStat
{
@@ -188,16 +200,16 @@
    evClosed = 4,
};
typedef struct tagPacket
typedef struct tagKBPacket
    {
        unsigned char Sign;        //起始标记
        unsigned char DstHost;    //目标地址
        unsigned char SrcAddr;    //源地址
        unsigned char nCMD;        //命令
        unsigned char nSEQ;        //序列号
        unsigned char PacketLen;    //数据载荷长度    不包括头部5个字节,不包括尾部BCC。
        unsigned char data[1];        //数据载荷,最末尾是BCC,数据长度为0时,实际也有一个数据。
    }stPacket,* pPacket;
        unsigned char Sign;        //起始标记
        unsigned char DstHost;    //目标地址
        unsigned char SrcAddr;    //源地址
        unsigned char nCMD;        //命令
        unsigned char nSEQ;        //序列号
        unsigned char PacketLen;    //数据载荷长度    不包括头部5个字节,不包括尾部BCC。
        unsigned char data[1];        //数据载荷,最末尾是BCC,数据长度为0时,实际也有一个数据。
    }stKBPacket,* pKBPacket;
enum eResult
{    
@@ -210,13 +222,13 @@
unsigned char KBusBCC(void * pData, int nSize);
//
    /* Make a Packet and return Packet Length */
int KBusMakePacket(pPacket p1,uchar Src, uchar Dst, uchar nType, uchar nSEQ, uchar DataLen, void *pData );
int KBusMakePacket(pKBPacket p1,uchar Src, uchar Dst, uchar nType, uchar nSEQ, uchar DataLen, void *pData );
/*   */
int KBusCheckPacket(int nChn, pPacket p1, int len1);
int KBusCheckPacket(int nChn, pKBPacket p1, int len1);
/* */
int KBusParsePacket(int nChn, pPacket p1, int Len1);
int KBusParsePacket(int nChn, pKBPacket p1, int Len1);
typedef struct tagMachineConfig
{
    int bKBusMaster;
Inc/KLink.h
@@ -85,6 +85,8 @@
        KLCmdReadData = 0x35,
        KLCmdWriteData = 0x36 ,
        
        KLCmdStopBlinkLED = 0x4F,
        KLCmdBlinkLED = 0x50,
        
        KLCmdReadProg = 0x51,
        KLCmdWriteProg,
@@ -93,6 +95,7 @@
        KLCmdSaveSysCfg,
        KLCmdSaveRunStat,
        KLCmdReadRunStat,
        KLCmdClearRunStat,
        
        KLCmdEraseFlashPage = 0x61,
        KLCmdWriteToFlash = 0x62,
@@ -131,7 +134,7 @@
extern unsigned char KLBufferIn[16];
extern unsigned char KLBufferOut[16];
extern unsigned char nKLAddr;
extern unsigned char nKLStationId;
extern unsigned char nKLSeq;
extern int KLThisuS;
@@ -189,20 +192,20 @@
/*
typedef struct tagKLPktHdr
{
        unsigned char StSign;        //起始标记
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
        unsigned char StSign;        //起始标记
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
    
}stKLPtHdr, * pKLPktHdr;
*/
/*
typedef struct tagKLRdPkt
{
        unsigned char StSign;        //起始标记
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
        unsigned char StSign;        //起始标记
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
        unsigned char nType;
        unsigned char nAddr;    //
}stKLRdPkt,* pKLRdKpt;
@@ -210,13 +213,13 @@
/*
typedef struct tagKLPacket
    {
        unsigned char StSign;        //起始标记
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
        unsigned char nSEQ;        //序列号
        unsigned char LoadLen;    //数据载荷长度    不包括头部5个字节,不包括尾部BCC。
        unsigned char data[1];        //数据载荷,最末尾是BCC,数据长度为0时,实际也有一个数据。
        unsigned char StSign;        //起始标记
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
        unsigned char nSEQ;        //序列号
        unsigned char LoadLen;    //数据载荷长度    不包括头部5个字节,不包括尾部BCC。
        unsigned char data[1];        //数据载荷,最末尾是BCC,数据长度为0时,实际也有一个数据。
    }stKLPacket,* pKLPacket;    
*/
Inc/KMachine.h
@@ -49,18 +49,24 @@
#define LoHofB(x) ((x)&0xf)
#define HiHofB(x) (((x)>>4)&0xf)
//  信息块
//  工厂参数配置块
//  用户/系统参数配置块
//  信息块
//  工厂参数配置块
//  用户/系统参数配置块
//  
//
typedef struct tagInfoBlock
typedef struct tagInfoBlock // 20 bytes
{
    USHORT nDeviceType;
    USHORT ProgVer;
    USHORT KLinkVer;
    USHORT nCapacity;
//    USHORT nBlockLenth;
    USHORT nDeviceTypeVer;            //device type        x.y
//    UCHAR nDevierVer;
    USHORT ProgVer;                    //prog version    x.y
    USHORT KLinkVer;                //x.y
    USHORT KBusVer;                    //x.y
    UCHAR nCapacity1;                //    ? K
    UCHAR nCapacity2;                //    ? k
    UCHAR nDInput;
    UCHAR nDOutput;
    UCHAR nAInput;
@@ -69,8 +75,47 @@
    UCHAR nHOutput;
    UCHAR nExt1;
    UCHAR nExt2;
    UCHAR nLogSize;
    UCHAR nPorts;
    UCHAR nManSize;
    UCHAR nAbility;
    UCHAR nSwitchBits;
    
}stKMInfoBlock;
/*
typedef struct tagFactData
{
    USHORT Sign;                    //
    USHORT nLength;                //
    UCHAR LOT_NO[16];            //
    UINT MANDate;                    //
    UINT SN;                            //
    UINT REV1[24];                //
    USHORT rev9;
    USHORT CRC;
}stFactData;
*/
typedef struct tagFactoryData        //工厂量产参数,数据
{
    USHORT Sign1;
    USHORT Seq1;
    USHORT nModelNo;
    USHORT nModelVer;
    UINT nLotNo;
     UINT nProductDateTime;
    UINT SN1;
    UINT nProtocalVer;
    UINT nDefaultFunc;
    UCHAR ModelStr[16];
    UCHAR LOT_NO[16];            //
    UCHAR SNStr[16];
    unsigned short CRC1;
    unsigned short EndSign1;
}stFactoryData,* pFactoryData;
enum enStoreCfg
{
@@ -78,7 +123,6 @@
    START_SIGN = 0x55aa,
    END_SIGN    =    0x5aa5,
};
enum enInputFilter
{
@@ -101,74 +145,66 @@
    Output_Set_1 = 2,
};
typedef struct tagPortStatus
{
    USHORT porttype;        //232,485,net , wireless// KLink, KBus, KNet, KWireless
    UCHAR StationID;
    UCHAR MaxStations;
    UCHAR CurStations;
    UCHAR Running;
    UCHAR Health;
};
enum enPortType
{
    PortType_Com = 0,    //计算机通讯
    PortType_Gen = 1,    //通用通讯,自由口
    PortType_KLink = 2, //KlinkͨѶ
    PortType_KBus = 3,     //KBusͨѶ
    PortType_KNet = 4,     // KNetͨѶ
    PortType_ModbusRTU = 5, //Modbus RTU ͨѶ
    PortType_Default = 0, //默认
    PortType_KLink = 1, //KLink通讯
    PortType_KBus = 2,     //KBus通讯
    PortType_KNet = 3,     // KNet通讯
    PortType_ModbusRTU = 4, //Modbus RTU 通讯
    PortType_Com = 5,    //    计算机通讯
    PortType_Gen = 6,    //通用通讯,自由口
};
//每个模块有 0/1/2/3/4/5/6/7/8个 通讯port
//不限于 UART, 网口,无线,单总线等,都是port
//甚至可以有虚拟的port
typedef struct tagPortStat
{
    UCHAR nWorking;            //工作中
    UCHAR nStation;            //自己站号
    UCHAR bBus;                    //总线?,,全双工?
    UCHAR bMaster;            //主机
    UCHAR PortType;            //端口工作模式
    UCHAR nDevices;            //连接的设备数量//不包括自己 //device list;
}stPortStat,*pPortStat;
enum enKeventType
{
    EventTypeNone = 0,
    EventTypePowerUp = 1,
    EventTypePowerDown = 2,
    EventTypePowerRecover = 3,
    EventTypeConnected = 4,
    EventTypeLostCon = 5,
    EventTypeSetTime = 6,
    EventTypeSysCfg = 7,
    EventTypeProg = 8,
    EventTypeForce = 9,
    EventTypeClearEvent = 10,
    EventType
};
typedef struct tagKMFuncParam
{
    USHORT EnablePLC:1;        //使能内部PLC功能
    USHORT RunMode;                //工作模式
}stKMFuncParam;
// 输入输出地址映射
typedef struct tagComPortParam            //4 Bytes
{
    USHORT WorkMode;                /* 0-5=Default,KLink,KBus,KNet,RTU,Com,Gen */
    USHORT Station;                    /* 0=From jumper */
    USHORT WorkMode;                /* 0-5=Com,Gen,KLink,KBus,KNet,RTU */
    USHORT BaudRate;       /* =*100 Baudrate at which running       */
//    USHORT PortType:4;                /* 0-5=Com,Gen,KLink,KBus,KNet,RTU */
  USHORT ByteSize:2;        /* 0-1=Number of bits/byte, 7-8    */
    USHORT Parity:4;                    /* 0-4=None,Odd,Even,Mark,Space    */
  USHORT StopBits:2;        /* 0,1,2 = 1, 1.5, 2               */
    USHORT EndType:2;                    /* 0=ByChar, 1= ByTime */
  USHORT EofChar:4;         /* 0,1,2 = None, CR, CR+LF, ETX;  End of character  */
    USHORT SofChar:2;                    /* 0,1,2 = None, STX */
    USHORT EndTime;
    USHORT EndTime;                        /*(*0.01mS) */
    USHORT RecvAddr;
    USHORT RecvSize;
    
}stComPortParam;
}stComPortParam, *pstComPortParam;
//每个模块有 0/1/2/3/4/5/6/7/8个 通讯port
//不限于 UART, 网口,无线,单总线等,都是port
//甚至可以有虚拟的port
typedef struct tagPortStat
{
    UCHAR PortType;            //端口工作模式
    UCHAR nStation;            //自己站号
    UCHAR bWorking;            //工作中
    UCHAR bBus;                    //总线?,,全双工?
    UCHAR bMaster;            //主机
    UCHAR nDevices;            //连接的设备数量//不包括自己 //device list;
}stPortStat,*pPortStat;
typedef struct tagKMFuncParam
{
    USHORT EnablePLC:1;        //使能内部PLC功能
    USHORT RunMode;                //工作模式
}stKMFuncParam;
// 输入输出地址映射
typedef struct tagInputFilterParam        // 1 Bytes
{
@@ -189,12 +225,12 @@
    USHORT Version;                                        // SC0    // 2 Bytes
    USHORT workmode;                                    // SC1  // 2 Bytes 0=From jumper  
    USHORT SwitchFunc;                                // SC2  // 2 Bytes 
    USHORT OutMappings[6];                                        //12 Bytes //输出映射
    USHORT pad1;                                            // 2 Bytes
    
    stComPortParam PortParams[2];                            // 8 Bytes
    stOutputHoldParam OutputParams[16];                //16 Bytes
    stInputFilterParam InputParams[16];                //16 Bytes
    stOutputHoldParam OutputParams[16];                //16 Bytes
    USHORT OutMappings[6];                //12 Bytes //输出映射
    
    UINT cfgvar3;                                                            // 4 Bytes
    UINT cfgvar4;                                                            // 4 Bytes
@@ -206,11 +242,11 @@
    UINT cfgvar10;                                                        // 4 Bytes
    UINT cfgvar11;                                                        // 4 Bytes
    UINT cfgvar12;                                                        // 4 Bytes
    UINT cfgvar13;                                                        // 4 Bytes
    UINT cfgvar14;                                                        // 4 Bytes
    UINT cfgvar15;                                                        // 4 Bytes
    UINT cfgvar16;                                                        // 4 Bytes
    UINT Space1[5];                                                        //20 Bytes
//    UINT cfgvar13;                                                        // 4 Bytes
//    UINT cfgvar14;                                                        // 4 Bytes
//    UINT cfgvar15;                                                        // 4 Bytes
//    UINT cfgvar16;                                                        // 4 Bytes
//    UINT Space1[4];                                                        //16 Bytes
}stKMSysCfg,* pKMSysCfg;
@@ -223,38 +259,23 @@
    unsigned short EndSign1;
}stStoredKMSysCfg,*pStoredKMSysCfg;
/*
typedef struct tagFactData
enum enKeventType
{
    USHORT Sign;                    //
    USHORT nLength;                //
    UCHAR LOT_NO[16];            //
    UINT MANDate;                    //
    UINT SN;                            //
    UINT REV1[24];                //
    USHORT rev9;
    USHORT CRC;
    EventTypeNone = 0,
    EventTypePowerUp = 1,
    EventTypePowerDown = 2,
    EventTypePowerRecover = 3,
    EventTypeConnected = 4,
    EventTypeLostCon = 5,
    EventTypeSetTime = 6,
    EventTypeSysCfg = 7,
    EventTypeProg = 8,
    EventTypeForce = 9,
    EventTypeClearEvent = 10,
    
}stFactData;
*/
typedef struct tagFactoryData        //工厂量产参数,数据
{
    USHORT Sign1;
    USHORT Seq1;
    USHORT nModelNo;
    USHORT nModelVer;
    UINT nLotNo;
     UINT nProductDateTime;
    UINT SN1;
    UINT nProtocalVer;
    UINT nDefaultFunc;
    UCHAR ModelStr[16];
    UCHAR LOT_NO[16];            //
    UCHAR SNStr[16];
    unsigned short CRC1;
    unsigned short EndSign1;
    
}stFactoryData,* pFactoryData;
    EventType
};
typedef struct tagEventLog
{
@@ -278,7 +299,8 @@
    volatile unsigned short WorkMode2;
    volatile unsigned short nBinProgBank;
    volatile unsigned short nBinProgSize;
    unsigned int Reserved2[1];
    unsigned short bLEDFlick;
    unsigned short Reserved2[1];
    unsigned short CRC1;
    unsigned short EndSign1;
}stRunStat, *pRunStat;
@@ -324,39 +346,10 @@
        KLDataTypeLD = 13 | TYPEDATA,
        KLDataSysCfg = 25 | TYPEDATA,
        KLDataTypeFlash = 33 | TYPEDATA,
        KLDataTypeWDT = 41 | TYPEDATA,
        KLDataTypeKBD = 51 | TYPEDATA,
        KLDataTypeTest = 254 | TYPEDATA,
    };
enum enKLDataCounts
    {
        KLDataDTCount = 256,
        KLDataSDTCount = 256,
        KLDataWXCount = 16,
        KLDataWYCount = 16,
        KLDataWRCount = 16,
        KLDataLDCount = 64,
        KLDataWLCount = 8,
        KLCoilXCount = KLDataWXCount * 16,
        KLCoilYCount = KLDataWYCount * 16,
        KLCoilRCount = KLDataWRCount * 16,
        KLCoilTCount = 64,
        KLCoilCCount = KLCoilTCount,
        KLDataSVCount = KLCoilTCount,
        KLDataEVCount = KLCoilTCount,
        KLCoilLXCount = 128,
        KLCoilLYCount = 128,
        KLCoilLRCount = 128,
        KLCoilSRCount = 128,
    };
#define TOTAL_WDFS (16)        //Total DF Count
#define TOTAL_CurVAL (16)        //
#define TOTALTIMERS (64)
typedef struct tagTimerStat
{
@@ -388,6 +381,40 @@
    };
}stTimer;
#define TOTAL_WDFS (32)        //Total DF Count
#define TOTAL_CurVAL (16)        //
#define TOTALTIMERS (64)
enum enKLDataCounts
    {
        KLDataWXCount = 16,
        KLDataWYCount = 16,
        KLDataWRCount = 16,
        KLDataLDCount = 64,
        KLDataWLCount = 8,
        KLCoilXCount = KLDataWXCount * 16,
        KLCoilYCount = KLDataWYCount * 16,
        KLCoilRCount = KLDataWRCount * 16,
        KLCoilTCount = 64,
        KLCoilCCount = KLCoilTCount,
        KLDataSVCount = KLCoilTCount,
        KLDataEVCount = KLCoilTCount,
        KLCoilLXCount = 128,
        KLCoilLYCount = 128,
        KLCoilLRCount = 128,
        KLCoilSRCount = 128,
        KLDataDTCount = 128,
        KLDataSDTCount = 128,
        KLDataWDTCount = 128,
    };
typedef struct tagKMem
{
    unsigned short WDFs[TOTAL_WDFS];
@@ -396,12 +423,12 @@
    stTimer Timers[TOTALTIMERS];
    union {
    unsigned short WX[KLDataWXCount];        //本机的X和Y
    unsigned short WX[KLDataWXCount];        //本机的X和Y
    unsigned char WXB[KLDataWXCount*2];    
    };
    union {
    unsigned short WY[KLDataWYCount];        //本机的X和Y
    unsigned char WYB[KLDataWYCount*2];        //本机的X和Y
    unsigned short WY[KLDataWYCount];        //本机的X和Y
    unsigned char WYB[KLDataWYCount*2];        //本机的X和Y
    }; 
    unsigned short WR[KLDataWRCount];
    
@@ -412,21 +439,21 @@
    unsigned short SV[KLDataSVCount];
    
    
    unsigned short WLX[16];        //虚拟的X和Y,远程通讯时映射用。
    unsigned short WLX[16];        //虚拟的X和Y,远程通讯时映射用。
    unsigned short WLY[16];
    unsigned short WLR[16];    
    unsigned short WSR[16];
union {
    unsigned int DTD[KLDataDTCount];
    unsigned int DTD[KLDataDTCount/2];
    unsigned short DT[KLDataDTCount];
    unsigned char DTB[KLDataDTCount*2];
};
    // 配置寄存器
    // 系统状态寄存器
    // 特殊寄存器
    // 调试,监控寄存器
    // 配置寄存器
    // 系统状态寄存器
    // 特殊寄存器
    union {
        unsigned int SDD[KLDataSDTCount/2];
        unsigned short SDT[KLDataSDTCount];
@@ -455,6 +482,13 @@
            unsigned short ADCValues[20];
        };
    };
    // 调试,监控寄存器
    union {
        unsigned int WDD[KLDataWDTCount/2];
        unsigned short WDT[KLDataWDTCount];
        unsigned char WDB[KLDataWDTCount*2];
    };
}stKMem;
Inc/ModbusRTU.h
@@ -24,18 +24,18 @@
enum enModbusRTUCmd
{
    None =0,
    ReadCoils =1, //读线圈
    ReadInputs =2,  //读离散量输入
    ReadKeepRegs =3,    //读保持寄存器
    ReadInputRegs =4,    //读输入寄存器
    WriteCoil = 5,        //写单个线圈
    WriteReg =6,        //写单个寄存器
    ReadExptStat = 7,  //读取异常状态
    ReadCoils =1, //读线圈
    ReadInputs =2,  //读离散量输入
    ReadKeepRegs =3,    //读保持寄存器
    ReadInputRegs =4,    //读输入寄存器
    WriteCoil = 5,        //写单个线圈
    WriteReg =6,        //写单个寄存器
    ReadExptStat = 7,  //读取异常状态
    FetchCommEventCtr =11,   //Fetch Comm Event Ctr
                                                     // 12 Fetch Comm Event Log
                                                    
    WriteCoils =15,                //写多个线圈
    WriteRegs = 16,        //写多个寄存器
    WriteCoils =15,                //写多个线圈
    WriteRegs = 16,        //写多个寄存器
                                        //17 Report Slave ID
                                        //20 Read General Reference
                                        //21 Write General Reference
Inc/functions.h
@@ -21,7 +21,7 @@
extern volatile unsigned int CurTickuS;    
//extern volatile unsigned int ThisRunTime;    
//extern volatile unsigned int TotalRunTime;
//extern volatile unsigned int PwrCount;                //开机次数
//extern volatile unsigned int PwrCount;                //开机次数
typedef unsigned char uint8_t;
typedef unsigned char uchar;
@@ -31,7 +31,7 @@
 
int InituS(int TickFreq1);
unsigned int GetuS(void);
void logData(unsigned char d);
int InitUartstat(stUartStat * pUartstat,void * pBufRx, int nSizeRx, void * pBufTx, int nSizeTx);
int Uart1SendDMA(void * pData, int nSize);
int Uart1TriggerSendDMA(void );
@@ -54,13 +54,15 @@
int PutStr(char * str1, int len1);
int SendPacket(int nChn, void * pBuf,int len1);
//int SendPacket1(void * pBuf,int len1);
//int SendPacket2(pPacket p1,int len1);
//int SendPacket2(pKBPacket p1,int len1);
void ToggleRunLed(void );
void ToggleErrLed(void );
void ToggleErr2Led(void );
void ToggleOutStat(void );
int ReadJumperSW(void );
void SetRunLed(uchar bOn);
void SetErrLed(uchar bOn);
void SetErr2Led(uchar bOn);
void SetOutStat(uchar bOn);
void Enable595(uchar bEnable);
unsigned int GetInput(void );
MDK-ARM/.vscode/c_cpp_properties.json
New file
@@ -0,0 +1,101 @@
{
    "configurations": [
        {
            "name": "F030C8T6_Test2",
            "includePath": [
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Inc",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Src",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Drivers\\STM32F0xx_HAL_Driver\\Inc",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Drivers\\CMSIS\\Device\\ST\\STM32F0xx\\Include",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Drivers\\CMSIS\\Include",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Drivers\\STM32F0xx_HAL_Driver\\Inc\\Legacy",
                "D:\\Keil_v5\\ARM\\ARMCC\\include",
                "D:\\Keil_v5\\ARM\\ARMCC\\include\\rw",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\MDK-ARM",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Drivers\\STM32F0xx_HAL_Driver\\Src",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Src\\Ethernet",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Src\\Internet\\DNS",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Src\\Internet\\DHCP",
                "d:\\WORK\\MCU&PCB\\DIST_IO\\F030C8T6_FP0\\Src\\Ethernet\\W5500"
            ],
            "defines": [
                "USE_FULL_LL_DRIVER",
                "USE_HAL_DRIVER",
                "__CC_ARM",
                "__arm__",
                "__align(x)=",
                "__ALIGNOF__(x)=",
                "__alignof__(x)=",
                "__asm(x)=",
                "__forceinline=",
                "__restrict=",
                "__global_reg(n)=",
                "__inline=",
                "__int64=long long",
                "__INTADDR__(expr)=0",
                "__irq=",
                "__packed=",
                "__pure=",
                "__smc(n)=",
                "__svc(n)=",
                "__svc_indirect(n)=",
                "__svc_indirect_r7(n)=",
                "__value_in_regs=",
                "__weak=",
                "__writeonly=",
                "__declspec(x)=",
                "__attribute__(x)=",
                "__nonnull__(x)=",
                "__register=",
                "__breakpoint(x)=",
                "__cdp(x,y,z)=",
                "__clrex()=",
                "__clz(x)=0U",
                "__current_pc()=0U",
                "__current_sp()=0U",
                "__disable_fiq()=",
                "__disable_irq()=",
                "__dmb(x)=",
                "__dsb(x)=",
                "__enable_fiq()=",
                "__enable_irq()=",
                "__fabs(x)=0.0",
                "__fabsf(x)=0.0f",
                "__force_loads()=",
                "__force_stores()=",
                "__isb(x)=",
                "__ldrex(x)=0U",
                "__ldrexd(x)=0U",
                "__ldrt(x)=0U",
                "__memory_changed()=",
                "__nop()=",
                "__pld(...)=",
                "__pli(...)=",
                "__qadd(x,y)=0",
                "__qdbl(x)=0",
                "__qsub(x,y)=0",
                "__rbit(x)=0U",
                "__rev(x)=0U",
                "__return_address()=0U",
                "__ror(x,y)=0U",
                "__schedule_barrier()=",
                "__semihost(x,y)=0",
                "__sev()=",
                "__sqrt(x)=0.0",
                "__sqrtf(x)=0.0f",
                "__ssat(x,y)=0",
                "__strex(x,y)=0U",
                "__strexd(x,y)=0",
                "__strt(x,y)=",
                "__swp(x,y)=0U",
                "__usat(x,y)=0U",
                "__wfe()=",
                "__wfi()=",
                "__yield()=",
                "__vfp_status(x,y)=0"
            ],
            "intelliSenseMode": "${default}"
        }
    ],
    "version": 4
}
MDK-ARM/.vscode/keil-assistant.log
New file
@@ -0,0 +1,2 @@
[info] Log at : 2022/10/21|09:22:30|GMT+0800
MDK-ARM/.vscode/uv4.log.lock
MDK-ARM/F030C8T6_FP0.uvprojx
File was renamed from MDK-ARM/F030C8T6_Test2.uvprojx
@@ -637,6 +637,96 @@
          </Files>
        </Group>
        <Group>
          <GroupName>Inc</GroupName>
          <Files>
            <File>
              <FileName>BSP.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\BSP.h</FilePath>
            </File>
            <File>
              <FileName>debug.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\debug.h</FilePath>
            </File>
            <File>
              <FileName>FP0.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\FP0.h</FilePath>
            </File>
            <File>
              <FileName>functions.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\functions.h</FilePath>
            </File>
            <File>
              <FileName>GlobalDef.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\GlobalDef.h</FilePath>
            </File>
            <File>
              <FileName>KBus.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\KBus.h</FilePath>
            </File>
            <File>
              <FileName>KLink.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\KLink.h</FilePath>
            </File>
            <File>
              <FileName>KMachine.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\KMachine.h</FilePath>
            </File>
            <File>
              <FileName>main.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\main.h</FilePath>
            </File>
            <File>
              <FileName>ModbusRTU.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\ModbusRTU.h</FilePath>
            </File>
            <File>
              <FileName>MyQueue.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\MyQueue.h</FilePath>
            </File>
            <File>
              <FileName>PLCfunctions.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\PLCfunctions.h</FilePath>
            </File>
            <File>
              <FileName>shell.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\shell.h</FilePath>
            </File>
            <File>
              <FileName>stm32_assert.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\stm32_assert.h</FilePath>
            </File>
            <File>
              <FileName>stm32f0xx_hal_conf.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\stm32f0xx_hal_conf.h</FilePath>
            </File>
            <File>
              <FileName>stm32f0xx_it.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\stm32f0xx_it.h</FilePath>
            </File>
            <File>
              <FileName>w5500_port.h</FileName>
              <FileType>5</FileType>
              <FilePath>..\Inc\w5500_port.h</FilePath>
            </File>
          </Files>
        </Group>
        <Group>
          <GroupName>::CMSIS</GroupName>
        </Group>
      </Groups>
MDK-ARM/F030C8T6_Test2.uvoptx
File was deleted
MDK-ARM/JLinkLog.txt
Diff too large
MDK-ARM/RTE/_F030C8T6_Test2/RTE_Components.h
@@ -3,7 +3,7 @@
 * Auto generated Run-Time-Environment Component Configuration File
 *      *** Do not modify ! ***
 *
 * Project: 'F030C8T6_Test2'
 * Project: 'F030C8T6_FP0'
 * Target:  'F030C8T6_Test2' 
 */
MDK-ARM/pemicro_connection_settings.ini
New file
@@ -0,0 +1,3 @@
[STARTUP]
CPUTARGETTYPENAME=
Src/BSP.c
@@ -596,10 +596,11 @@
  PA2   ------> USART2_TX
  PA3   ------> USART2_RX 
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_1|LL_GPIO_PIN_2|LL_GPIO_PIN_3;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
Src/FP0.c
@@ -22,7 +22,7 @@
uint8_t bSentLen;
uint8_t bConfiged=0;
uint8_t nConfigAddr=0;
uint8_t nConfigStationId=0;
uint8_t nInputBytes=8;
uint8_t nOutputBytes=8;
uint8_t nIndex=0;
@@ -53,11 +53,11 @@
    bSPI1Sending=0;
    bSPI1Recving=0;
    if (nChilds==0) {
    nInputBytes=1;        //根据子机数量,报告扩展容量
    nInputBytes=1;        //根据子机数量,报告扩展容量
    nOutputBytes=1;
    } else
    {
        nInputBytes=nChilds;        //根据子机数量,报告扩展容量
        nInputBytes=nChilds;        //根据子机数量,报告扩展容量
        nOutputBytes=nChilds;
    }
    SetACKPin_0();
@@ -99,8 +99,8 @@
            }
        }
    if (CurSYN ==0 && oldSYN != 0) {        
        KMem.SDT[121] =    KMem.SDT[122];
        KMem.SDT[122]=0;
        KMem.WDT[121] =    KMem.WDT[122];
        KMem.WDT[122]=0;
        }
    if (CurSEL && CurSYN !=0 && oldSYN == 0){
                bSPI1Recving=1;
@@ -156,15 +156,15 @@
    uint8_t res=0;
    uint8_t nST=pBuf[0];
    uint8_t nCMD=nST&0x7;
    uint8_t nAddr=nST&0xf8;
    KMem.SDB[128+KMem.SDT[123]] = 0xFF;
    KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}
    uint8_t nStationID=nST&0xf8;
    
    if (nAddr<0x80) return 0;
    logData(0xff);
    if (nStationID<0x80) return 0;
    switch (nCMD)
    {
        case CMD_0_QUERY:
            if (!bConfiged || (bConfiged && nAddr == nConfigAddr))
            if (!bConfiged || (bConfiged && nStationID == nConfigStationId))
        {
             KMem.DT[8]++;
            pFP0QuRplyPkg p1 = (pFP0QuRplyPkg)PkgBuf2;
@@ -175,11 +175,10 @@
            p1->nBCC= 0x30|CalFP0BCC(PkgBuf2,4);
            p1->End1=0x0d;
            
            KMem.SDB[128+KMem.SDT[123]] = 0x11;
            KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}
            logData(0x11);
            
            SendFP0Pkg(PkgBuf2,sizeof(stFP0QuRplyPkg));
            nConfigAddr=nAddr;
            nConfigStationId=nStationID;
            bConfiged=1;
        }
        bSPI1Recving=1;
@@ -193,7 +192,7 @@
//            bSPI1Recving=1;
            break;
        case CMD_3_EXCHG:
            if (!bConfiged || nConfigAddr != nAddr)
            if (!bConfiged || nConfigStationId != nStationID)
            {
                bSPI1Recving=1;        
                break;
@@ -238,7 +237,7 @@
            break;
        case CMD_7_END:
         KMem.DT[15]++;
            if (bConfiged && nConfigAddr == nAddr)
            if (bConfiged && nConfigStationId == nStationID)
            {        
                //SetFP0DEPin_0();
                SetACKPin_1();                
@@ -272,8 +271,9 @@
        value = SPI1SendBuf[nSPI1SentLen];
        LL_SPI_TransmitData8(SPI1,value);
        bSPI1Sending=1;
     KMem.SDB[128+KMem.SDT[123]] = value;
     KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}
    logData(value);
        // passive mode
        SetFP0DEPin_1();
        SetACKPin_0();
Src/KBus.c
@@ -17,22 +17,22 @@
unsigned char BufferIn[16]={0};
unsigned char BufferOut[16]={0};
stChnStat ChnStats[8];
unsigned char nAddr=0;
stChnStat KBusChnStats[8];
unsigned char nStationID=0;
unsigned char nChilds;
int ChildStat[16];
unsigned char nCurPollId=0;
unsigned char nSeq=0;
volatile unsigned char MasterRecved=1;
volatile unsigned char MasterRecvOK=1;
volatile unsigned char KBusMasterRecved=1;
volatile unsigned char KBusMasterRecvOK=1;
volatile unsigned char SlaveRecved=1;
unsigned int SendTimeuS=0;
volatile int RecvTimeuS=0;
volatile unsigned char KBusSlaveRecved=1;
unsigned int KBusSendTimeuS=0;
volatile int KBusRecvTimeuS=0;
int DelayuS=0;
int MaxDelayuS=0;
int KBusDelayuS=0;
int KBusMaxDelayuS=0;
int ThisuS;
volatile unsigned int nSlaveTick=0;
@@ -58,7 +58,7 @@
    return k;    
}
int KBusMakePacket(pPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nSEQ, unsigned char DataLen,void * pData )
int KBusMakePacket(pKBPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nSEQ, unsigned char DataLen,void * pData )
{
    p1->Sign=StartSign;
    p1->DstHost=dst;
@@ -73,17 +73,17 @@
        case cmdPing:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;
        
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case cmdPingReply:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;        
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case cmdRead:
            break;
@@ -94,67 +94,67 @@
        case cmdWriteReply:
            p1->PacketLen=DataLen;
         if (DataLen !=0 )    memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case cmdGetVersion:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case cmdVerInfo:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case cmdExChgData:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case cmdExChgDataReply:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;        
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
                
        case cmdSyncRead:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;        
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case cmdSyncWrite:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;        
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case cmdSequenRead:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;        
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;        
        
        case cmdSyncTime:
            p1->PacketLen=DataLen;
            memcpy(p1->data,pData,DataLen);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
            p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
            p1->data[DataLen+1]=EndSign;        
            PacketLenth=sizeof(stPacket)+DataLen+1;
            PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        
        default:
@@ -164,90 +164,90 @@
    return PacketLenth;
}
int KBusCheckPacket(int nChn, pPacket p1, int nLen1)
int KBusCheckPacket(int nChn, pKBPacket p1, int nLen1)
{
    if (p1->Sign != StartSign)
    {
        Uart2Stat.NotPacketErr++;
        ChnStats[nCurPollId].NotPkgErr++;
        KBusChnStats[nCurPollId].NotPkgErr++;
        return -1;
    }
    int DataLen=p1->PacketLen;
    if (DataLen>MaxPacketLength) 
    {
        Uart2Stat.LengthErr++;
        ChnStats[nCurPollId].PkgLenErr++;
        KBusChnStats[nCurPollId].PkgLenErr++;
        return -1;
    }
    if (nLen1<DataLen+sizeof(stPacket)+1)
    if (nLen1<DataLen+sizeof(stKBPacket)+1)
    {
        //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
        //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
        //PutStr(str3,len4);
        ChnStats[nCurPollId].PkgLenErr++;
        KBusChnStats[nCurPollId].PkgLenErr++;
        Uart2Stat.LengthErr++;
        return -3;    //not long enough                    
    }
//    if (p1->data[DataLen+1] != EndSign)
//    {
//        ChnStats[nCurPollId].NoEndErr++;
//        KBusChnStats[nCurPollId].NoEndErr++;
//        Uart2Stat.LengthErr++;        
//        return -2;
//    }
    unsigned char thisBCC=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
    unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
    if (thisBCC != p1->data[DataLen]) 
    {//BCC Error;
        Uart2Stat.BCCerr++;
        ChnStats[nCurPollId].BCCErr++;
        KBusChnStats[nCurPollId].BCCErr++;
        return -4;
    }         
    return 0;
}
int KBusSlaveCheckPacket(int nChn, pPacket p1, int nLen1)
int KBusSlaveCheckPacket(int nChn, pKBPacket p1, int nLen1)
{
    if (p1->Sign != StartSign)
    {
        Uart2Stat.NotPacketErr++;
        ChnStats[0].ClientNotPktErr++;
        KBusChnStats[0].ClientNotPktErr++;
        return -1;
    }
    int DataLen=p1->PacketLen;
    if (DataLen>MaxPacketLength) 
    {
        Uart2Stat.LengthErr++;
        ChnStats[0].ClientPkgLenErr++;
        KBusChnStats[0].ClientPkgLenErr++;
        return -1;
    }
    if (nLen1<DataLen+sizeof(stPacket)+1)
    if (nLen1<DataLen+sizeof(stKBPacket)+1)
    {
        //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
        //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
        //PutStr(str3,len4);
        ChnStats[0].ClientPkgLenErr++;
        KBusChnStats[0].ClientPkgLenErr++;
        Uart2Stat.LengthErr++;
        return -3;    //not long enough                    
    }
//    if (p1->data[DataLen+1] != EndSign)
//    {
//        ChnStats[nCurPollId].NoEndErr++;
//        KBusChnStats[nCurPollId].NoEndErr++;
//        Uart2Stat.LengthErr++;        
//        return -2;
//    }
    unsigned char thisBCC=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
    unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
    if (thisBCC != p1->data[DataLen]) 
    {//BCC Error;
        Uart2Stat.BCCerr++;
        ChnStats[0].ClientBccErr++;
        KBusChnStats[0].ClientBccErr++;
        return -4;
    }         
    return 0;
}
int KBusMasterParsePacket(int nChn, pPacket p1, int Len1)
int KBusMasterParsePacket(int nChn, pKBPacket p1, int Len1)
{
        int DataLen=p1->PacketLen;
        ChnStats[nCurPollId].RecvPackets++;
        pPacket p2=(pPacket)PacketBuf2;
        KBusChnStats[nCurPollId].RecvPackets++;
        pKBPacket p2=(pKBPacket)PacketBuf2;
        int PacketLen=0;
        //LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_6);
        int ChildId=p1->SrcAddr;
@@ -263,18 +263,18 @@
                SendPacket(nChn, p2, PacketLen);
                break;
            case cmdPingReply:
                DelayuS=ThisuS-SendTimeuS;
                if (DelayuS > MaxDelayuS) MaxDelayuS = DelayuS;
                KBusDelayuS=ThisuS-KBusSendTimeuS;
                if (KBusDelayuS > KBusMaxDelayuS) KBusMaxDelayuS = KBusDelayuS;
                
                BufferIn[ChildId]=p1->data[0];
                
                //RunStat=100;
                ChnStats[nCurPollId].CtnLstPkts=0;
                ChnStats[nCurPollId].Delay=DelayuS;
            if (DelayuS > ChnStats[nCurPollId].MaxDelay)
                ChnStats[nCurPollId].MaxDelay=DelayuS;
                KBusChnStats[nCurPollId].CtnLstPkts=0;
                KBusChnStats[nCurPollId].Delay=KBusDelayuS;
            if (KBusDelayuS > KBusChnStats[nCurPollId].MaxDelay)
                KBusChnStats[nCurPollId].MaxDelay=KBusDelayuS;
                //PutOutput(outputvalue);
                MasterRecvOK=1;
                KBusMasterRecvOK=1;
                break;
            case cmdRead:
                break;
@@ -283,7 +283,7 @@
            case cmdWrite:
                break;
            case cmdWriteReply:
                MasterRecved=1;
                KBusMasterRecved=1;
                break;
            case cmdGetVersion:
                break;
@@ -294,12 +294,12 @@
                //PutOutput(outputvalue);
                //memcpy(DispBuf,p1->data+2,8);
                p1->data[0]=BufferOut[0];
                PacketLen=KBusMakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
                PacketLen=KBusMakePacket(p2,nStationID,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
                SendPacket(nChn, p2, PacketLen);
                break;
            case cmdExChgDataReply:
                DelayuS=ThisuS-SendTimeuS;
                if (DelayuS > MaxDelayuS) MaxDelayuS = DelayuS;
                KBusDelayuS=ThisuS-KBusSendTimeuS;
                if (KBusDelayuS > KBusMaxDelayuS) KBusMaxDelayuS = KBusDelayuS;
#if (BOARD_TYPE == 14)                
                BufferIn[ChildId]=p1->data[0];
@@ -318,17 +318,17 @@
#endif
                //RunStat=100;
                ChnStats[nCurPollId].CtnLstPkts=0;
                ChnStats[nCurPollId].Delay=DelayuS;
            if (DelayuS > ChnStats[nCurPollId].MaxDelay)
                ChnStats[nCurPollId].MaxDelay=DelayuS;
                KBusChnStats[nCurPollId].CtnLstPkts=0;
                KBusChnStats[nCurPollId].Delay=KBusDelayuS;
            if (KBusDelayuS > KBusChnStats[nCurPollId].MaxDelay)
                KBusChnStats[nCurPollId].MaxDelay=KBusDelayuS;
                //PutOutput(outputvalue);
                
            
                nIndex=p1->data[3];
                ChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[4]|(p1->data[5]<<8)|(p1->data[6]<<16)|(p1->data[7]<<24);
                KBusChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[4]|(p1->data[5]<<8)|(p1->data[6]<<16)|(p1->data[7]<<24);
            
                MasterRecvOK=1;
                KBusMasterRecvOK=1;
                
                break;
                    
@@ -343,28 +343,28 @@
    return 0;
}
unsigned char nClientDataIndex=0;
int KBusSlaveParsePacket(int nChn, pPacket p1, int Len1)
int KBusSlaveParsePacket(int nChn, pKBPacket p1, int Len1)
{
    Uart2Stat.OKPacket++;                
    int DataLen=p1->PacketLen;    
//int nSrcAddr=p1->SrcAddr;
    int nDstHost=p1->DstHost;
        
//    RecvTimeuS=ThisuS;
//    SlaveRecved=1;
//    KBusRecvTimeuS=ThisuS;
//    KBusSlaveRecved=1;
    
    pPacket p2=(pPacket)PacketBuf2;
    pKBPacket p2=(pKBPacket)PacketBuf2;
    int PacketLen=0;
    unsigned char nIndex = p1->nSEQ & 0x07;
    if (nDstHost!=nAddr && nDstHost != 0xff)
    if (nDstHost!=nStationID && nDstHost != 0xff)
    {
        ChnStats[0].ClientMisIdPkts++;
        KBusChnStats[0].ClientMisIdPkts++;
        return -1;
    }
    if (nDstHost==nAddr || nDstHost==0xff)
    if (nDstHost==nStationID || nDstHost==0xff)
    {
        RecvTimeuS=ThisuS;
        SlaveRecved=1;
        KBusRecvTimeuS=ThisuS;
        KBusSlaveRecved=1;
        switch (p1->nCMD)
        {
            case cmdNone:
@@ -374,9 +374,9 @@
                //PutOutput(outputvalue);
                //memcpy(DispBuf,p1->data+2,8);
                p1->data[0]=BufferOut[0];
                RecvTimeuS=ThisuS;
                PacketLen=KBusMakePacket(p2,nAddr,0,cmdPingReply,p1->nSEQ,DataLen,p1->data);
                ChnStats[0].ClientSendPkts++;
                KBusRecvTimeuS=ThisuS;
                PacketLen=KBusMakePacket(p2,nStationID,0,cmdPingReply,p1->nSEQ,DataLen,p1->data);
                KBusChnStats[0].ClientSendPkts++;
                SendPacket(nChn, p2, PacketLen);
                break;
            case cmdPingReply:
@@ -388,7 +388,7 @@
            case cmdWrite:
                //memcpy(DispBuf,p1->data,DataLen);
                PacketLen=KBusMakePacket(p2,1,0,cmdWriteReply,p1->nSEQ,0,0);
                ChnStats[0].ClientSendPkts++;
                KBusChnStats[0].ClientSendPkts++;
                SendPacket(nChn, p2, PacketLen);                    
                break;
            case cmdWriteReply:
@@ -405,7 +405,7 @@
                //PutOutput(outputvalue);
                //memcpy(DispBuf,p1->data+2,8);
                nIndex=nClientDataIndex;
        //        ChnStats[0].ClientDatas[7]++;
        //        KBusChnStats[0].ClientDatas[7]++;
//                BufferOut[0]=GetInput();
        //        BufferOut[0]=GetInput();
#else
@@ -413,20 +413,20 @@
                //PutOutput(outputvalue);
                //memcpy(DispBuf,p1->data+2,8);
                nIndex=nClientDataIndex;
        //        ChnStats[0].ClientDatas[7]++;
        //        KBusChnStats[0].ClientDatas[7]++;
//                BufferOut[0]=GetInput();
                BufferOut[0]=GetInput();
#endif
                p1->data[0]=BufferOut[0];
                p1->data[3]=nIndex;
                p1->data[4]=ChnStats[0].ClientDatas[nIndex];
                p1->data[5]=ChnStats[0].ClientDatas[nIndex]>>8;
                p1->data[6]=ChnStats[0].ClientDatas[nIndex]>>16;
                p1->data[7]=ChnStats[0].ClientDatas[nIndex]>>24;
                p1->data[4]=KBusChnStats[0].ClientDatas[nIndex];
                p1->data[5]=KBusChnStats[0].ClientDatas[nIndex]>>8;
                p1->data[6]=KBusChnStats[0].ClientDatas[nIndex]>>16;
                p1->data[7]=KBusChnStats[0].ClientDatas[nIndex]>>24;
                nClientDataIndex++;
                if (nClientDataIndex >= 10) { nClientDataIndex=0;}
                PacketLen=KBusMakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
                ChnStats[0].ClientSendPkts++;
                PacketLen=KBusMakePacket(p2,nStationID,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
                KBusChnStats[0].ClientSendPkts++;
                SendPacket(nChn, p2, PacketLen);
                break;
            case cmdExChgDataReply:
@@ -448,37 +448,37 @@
    }    
    return 0;
}
int KBusParsePacket(int nChn, pPacket p1, int Len1)
int KBusParsePacket(int nChn, pKBPacket p1, int Len1)
{
    ThisuS=GetuS();
    int Result=0;
    if (bKBusMaster)
    {
            MasterRecved=1;
            KBusMasterRecved=1;
            Result=KBusCheckPacket(nChn, p1, Len1);
            if (Result != S_OK)
            {
                return Result;
            }
            MasterRecvOK=1;
            KBusMasterRecvOK=1;
            Result=KBusMasterParsePacket(nChn, p1, Len1);            
            return Result;
    }
    if (bKBusSlave)
    {
            ChnStats[0].ClientRecvPkts++;
            KBusChnStats[0].ClientRecvPkts++;
            Result=KBusSlaveCheckPacket(nChn, p1, Len1);
            if (Result != S_OK)
            {
                return Result;
            }
            ChnStats[0].ClientTimeOutErr=KMem.RunStat;
            KBusChnStats[0].ClientTimeOutErr=KMem.RunStat;
            Result=KBusSlaveParsePacket(nChn, p1, Len1);
            return Result;
    }
    //int len1=p1->PacketLen;
//    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
//    pPacket p2=(pPacket)PacketBuf2;
//    pKBPacket p2=(pKBPacket)PacketBuf2;
//            Uart2Stat.OKPacket++;
    return Result;
@@ -544,20 +544,20 @@
        int len1=0;
        if ((MasterRecved && MasterRecvOK && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1000u)
        if ((KBusMasterRecved && KBusMasterRecvOK && thisuS-KBusSendTimeuS>50) || thisuS-KBusSendTimeuS>1000u)
        {
            if (!MasterRecvOK)
            if (!KBusMasterRecvOK)
            {
                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)
                KBusChnStats[nCurPollId].LostPackets++;
                KBusChnStats[nCurPollId].CtnLstPkts++;
                if (!KBusMasterRecved) {KBusChnStats[nCurPollId].TimeOutErr++;}
                if (KBusChnStats[nCurPollId].CtnLstPkts>KBusChnStats[nCurPollId].MaxCtnLstPkts)
                {KBusChnStats[nCurPollId].MaxCtnLstPkts=KBusChnStats[nCurPollId].CtnLstPkts;}
                if (KBusChnStats[nCurPollId].CtnLstPkts>3)
                {
                    ChnStats[nCurPollId].Stat=0;
                    KBusChnStats[nCurPollId].Stat=0;
                    KMem.ErrStat=200;
                     
                    {BufferIn[nCurPollId]=0;}
@@ -565,7 +565,7 @@
            //    LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
            }else
            {
                ChnStats[nCurPollId].Stat=1;
                KBusChnStats[nCurPollId].Stat=1;
                
                KMem.RunStat=100;
            }
@@ -599,24 +599,24 @@
            Datas[0]=BufferOut[nCurPollId];
            Datas[1]=BufferOut[nCurPollId+1];;
            Datas[2]=ChnStats[nCurPollId].Stat;
            Datas[2]=KBusChnStats[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=KBusMakePacket((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;
            KBusSendTimeuS=thisuS;
            len1=KBusMakePacket((pKBPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
            SendPacket(nChn, (pKBPacket)PacketBuf1, len1);
            KBusChnStats[nCurPollId].SendPackets++;
            KBusChnStats[nCurPollId].SendTimeInterval=KBusSendTimeuS-KBusChnStats[nCurPollId].LastSentTimeuS;
            KBusChnStats[nCurPollId].LastSentTimeuS=KBusSendTimeuS;
//            PacketLength = len1;
            SendTime=tick1;
            MasterRecved=0;
            MasterRecvOK=0;
            KBusMasterRecved=0;
            KBusMasterRecvOK=0;
        //    LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);        
            //ToggleErrLed();
//                ToggleOut8();
@@ -632,23 +632,23 @@
int KBusSlaveFunc(int nChn)
{
        int ThisuS=GetuS();
        int thisRecvTime=RecvTimeuS;
        if (SlaveRecved)
        int thisRecvTime=KBusRecvTimeuS;
        if (KBusSlaveRecved)
        {
            KMem.RunStat=8000;
            SlaveRecved=0;
            KBusSlaveRecved=0;
        }else if ((ThisuS - thisRecvTime) >12000u)
        {
            KMem.ErrStat=8000;
            KMem.SDD[17]=1;
            KMem.SDD[18]=ThisuS;
            KMem.SDD[19]=RecvTimeuS;
            KMem.SDD[19]=KBusRecvTimeuS;
        }else if ( ThisuS > (thisRecvTime + 12000u))
        {
            KMem.ErrStat=8000;
            KMem.SDD[17]=2;
            KMem.SDD[18]=ThisuS;
            KMem.SDD[19]=RecvTimeuS;
            KMem.SDD[19]=KBusRecvTimeuS;
        }
        
    return 0;
Src/KLink.c
@@ -16,7 +16,7 @@
unsigned char KLBufferIn[16]={0};
unsigned char KLBufferOut[16]={0};
unsigned  char nKLAddr=1;
unsigned  char nKLStationId=1;
unsigned  char nKLSeq=0;
unKLStat nKLStatus ={0};
@@ -61,7 +61,7 @@
        case KLCmdRead:
//        case KLCmdReadReply:
            //p1->data[DataLen+1]=KLEndSign;
            //PacketLenth=sizeof(stPacket)+DataLen+1;
            //PacketLenth=sizeof(stKBPacket)+DataLen+1;
            break;
        case KLCmdWrite:
//        case KLCmdWriteReply:
@@ -91,13 +91,13 @@
    }
    if (nLen1<DataLen+sizeof(stKLReqPktHdr))
    {
        //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
        //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
        //PutStr(str3,len4);
        return 3;    //not long enough                    
    }
//    if (p1->data[DataLen+1] != EndSign)
//    {
//        ChnStats[nCurPollId].NoEndErr++;
//        KBusChnStats[nCurPollId].NoEndErr++;
//        Uart2Stat.LengthErr++;        
//        return -2;
//    }
@@ -129,11 +129,13 @@
{
    pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
    int nDstHost=p1->DstHost;
    unsigned char nDstHost=p1->DstHost;
    KLRecvTimeuS=KLThisuS;
    int nDataType=p1->nType1;
    int nAddr=0;
    int DataLen=0;    //p1->LoadLen;
    unsigned char nDataType=p1->nType1;
    unsigned short nWordAddr=0;
    unsigned short nByteAddr=0;
    unsigned short nBitAddr=0;
    unsigned short DataLen=0;    //p1->LoadLen;
//int nSrcAddr=p1->SrcAddr;
    nKLStatus.nSEQ = ((pKLStat)(&(p1->Stat)))->nSEQ;;
@@ -144,7 +146,7 @@
    int PacketLen=0;
    KLRecvTimeuS=KLThisuS;
    if (nDstHost==nKLAddr || nDstHost==0xff)
    if (nDstHost==nKLStationId || nDstHost==0xff)
    {
        switch (p1->nCMD)
        {
@@ -183,7 +185,7 @@
                SendPacket(nChn, p2, PacketLen);                
                break;
            case KLCmdWriteFactoryData:
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);
                nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
                DataLen= p1->Params[2];
                //DataLen=16;
                for (int i=0;i<DataLen;i++)
@@ -196,22 +198,24 @@
            case KLCmdRead:
            case KLCmdReadDataWord:
            case KLCmdReadDataByte:                
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);
                nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
                DataLen= p1->Params[2];
                if (nDataType ==KLDataTypeDT)                {            pData=KMem.DT+nAddr;        }
                else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nAddr;    }
                else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nAddr;    }
                else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nAddr;        }
                else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nAddr;        }
                else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nAddr;        }
                else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nAddr;        }
                else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nAddr;        }
                else if (nDataType == KLDataTypeSV)        {        pData=KMem.SV+nAddr;    }
                else if (nDataType == KLDataTypeEV)        {        pData=KMem.EV+nAddr;    }
                else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nAddr;    }
                else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg + nAddr;}
                else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;}
                else                {                    pData=KLBufferOut+nAddr;                }
                if (nDataType ==KLDataTypeDT)                {            pData=KMem.DT+nWordAddr;        }
                else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
                else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
                else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
                else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
                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;    }
                else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nWordAddr;    }
                else if (nDataType == KLDataTypeWDT)    {        pData=KMem.WDT+nWordAddr;    }
                else if (nDataType == KLDataTypeKBD)    {        pData=(unsigned short *)&KBusChnStats  +nWordAddr;    }
                else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg.theKMSysCfg + nWordAddr;}
                else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
                else                {                    pData=KLBufferOut+nWordAddr;                }
                
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
                SendPacket(nChn, p2, PacketLen);
@@ -224,22 +228,24 @@
            case KLCmdWriteDataByte:
                
                //memcpy(DispBuf,p1->data,DataLen);
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);
                nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
                DataLen= p1->Params[2];
                    if (nDataType ==KLDataTypeDT)                {        pData=KMem.DT+nAddr;        }
                else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nAddr;    }
                else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nAddr;    }
                else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nAddr;        }
                else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nAddr;        }
                else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nAddr;        }
                else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nAddr;        }
                else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nAddr;        }
                else if (nDataType == KLDataTypeSV)        {        pData=KMem.SV+nAddr;    DataLen=0;}
                else if (nDataType == KLDataTypeEV)        {        pData=KMem.EV+nAddr;    DataLen=0;}
                else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nAddr;    DataLen=0;}
                else if (nDataType == KLDataSysCfg)        { pData = (unsigned short *)&storedKMSysCfg + nAddr;}
                else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;}
                else                {                    pData=KLBufferOut+nAddr;    DataLen=0;            }
                    if (nDataType ==KLDataTypeDT)                {        pData=KMem.DT+nWordAddr;        }
                else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
                else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
                else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
                else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
                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;}
                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 *)KBusChnStats +nWordAddr;    DataLen=0;}
                else if (nDataType == KLDataSysCfg)        { pData = (unsigned short *)&(storedKMSysCfg) + nWordAddr;}
                else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
                else                {                    pData=KLBufferOut+nWordAddr;    DataLen=0;            }
                        
                memcpy(pData,p1->Params+4,DataLen);                    
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
@@ -249,16 +255,16 @@
//                break;
            case KLCmdRead1Bit:
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);
                nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
                DataLen = 1;
                if (nDataType == KLCoilTypeX)             { rData[0] = ((KMem.WX[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
                else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
                else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
                else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
                else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
                else if (nDataType == KLCoilTypeT) { rData[0] = KMem.Timers[nAddr].bTon;}
                else if (nDataType == KLCoilTypeC) { rData[0] = KMem.Timers[nAddr].bTon;}
                else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
                if (nDataType == KLCoilTypeX)             { rData[0] = ((KMem.WX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
                else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
                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;}
                else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
                
                else if (nDataType == KLCoilTypeLR) { rData[0] = 0;}
                else {rData[0]=0;}
@@ -266,16 +272,16 @@
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdWrite1Bit:
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);
                if (nDataType == KLCoilTypeX)             { SetBitValue( &KMem.WX[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
                else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
                else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
                else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
                else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
                else if (nDataType == KLCoilTypeT) { KMem.Timers[nAddr].bTon = p1->Params[2];}
                else if (nDataType == KLCoilTypeC) { KMem.Timers[nAddr].bTon = p1->Params[2];}
                else if (nDataType == KLCoilTypeC) { KMem.Timers[nAddr].bTon = p1->Params[2];}
                else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nAddr>>4],nAddr&0x0f,p1->Params[2]);;}
                nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
                if (nDataType == KLCoilTypeX)             { SetBitValue( &KMem.WX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
                else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
                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];}
                else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);;}
                else {rData[0]=0;}
                DataLen=0;
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData);
@@ -289,16 +295,16 @@
                
                break;
            case KLCmdReadProgram:
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);
                nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
                DataLen= p1->Params[2];
                 if (nDataType==0){
                        pData = (unsigned short *)STORE_PRG_BASE + nAddr;
                        pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
                 } else if (nDataType==1){
                        pData = (unsigned short *)ALT_PRG_BASE + nAddr;
                        pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
                 } else if (KMRunStat.nBinProgBank == 0) {
                        pData = (unsigned short *)STORE_PRG_BASE + nAddr;
                        pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
                 }else {
                        pData = (unsigned short *)ALT_PRG_BASE + nAddr;
                        pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
                 }
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
                SendPacket(nChn, p2, PacketLen);
@@ -316,22 +322,22 @@
                break;
            case KLCmdWriteProgram:
                if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);
                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(nAddr, KLPacketBuf2, DataLen,nDataType);
                WriteProgram(nWordAddr, KLPacketBuf2, DataLen,nDataType);
                DataLen=4;
                *((int *)(&rData[0]))=(long)(p1->Params+4);
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWriteProgram,0,0);
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdFinishProgram:
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
                nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
                DataLen=nDataType;
                KMRunStat.nBinProgSize=nAddr;
                KMRunStat.nBinProgSize=nWordAddr;
                if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;}
                else {KMRunStat.nBinProgBank=0;}
                SaveRunStat(&KMRunStat);                
@@ -341,10 +347,50 @@
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishProgram,0,0);
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdBlinkLED:
                DataLen=nDataType;
                KMRunStat.bLEDFlick=DataLen;
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdStopBlinkLED:
                DataLen=nDataType;
                KMRunStat.bLEDFlick=0;
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdReadRunStat:
                DataLen= sizeof(stRunStat);
                pData=&KMRunStat;
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdReadSysCfg:
                nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
                pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
                DataLen = p1->Params[2];
                if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
                else {
                    if (DataLen + nByteAddr >sizeof(stKMSysCfg))
                        DataLen= sizeof(stKMSysCfg) - nByteAddr;
                }
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdWriteSysCfg:
                nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
                pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
                DataLen = p1->Params[2];
                if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
                else {
                    if (DataLen + nByteAddr >sizeof(stKMSysCfg))
                        DataLen= sizeof(stKMSysCfg) - nByteAddr;
                }
                memcpy(pData,p1->Params+4,DataLen);
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
                SendPacket(nChn, p2, PacketLen);    
                break;
            case KLCmdSaveSysCfg:
@@ -357,6 +403,11 @@
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0);
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdClearRunStat:
                memset(KBusChnStats,0,sizeof(KBusChnStats));
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdGetEventLogCount:
                DataLen= 4;
                pData=&KMem.nEventCount;
@@ -364,9 +415,9 @@
                SendPacket(nChn, p2, PacketLen);
                break;
            case KLCmdGetEventLog:
                nAddr=p1->Params[0]+ (p1->Params[1]<<8);
                nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
                DataLen= p1->Params[2] * sizeof(stEventLog);
                pData=GetEventLogAddr(nAddr);
                pData=GetEventLogAddr(nWordAddr);
                PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
                SendPacket(nChn, p2, PacketLen);
                break;
@@ -378,7 +429,7 @@
            default:
            DataLen=1;
            rData[0]=KL_UNKNOWN;
            PacketLen=KLMakeRplyPacket(p2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
            PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
            SendPacket(nChn, p2, PacketLen);            
                break;        
        }
@@ -398,7 +449,7 @@
        char rData[4];
        rData[0]=Result;
        
        PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
        PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
//        PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData);
        SendPacket(nChn, KLPacketBuf2, PacketLen);
        return Result;
@@ -411,7 +462,7 @@
    
    //int len1=p1->PacketLen;
//    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
//    pPacket p2=(pPacket)PacketBuf2;
//    pKBPacket p2=(pKBPacket)PacketBuf2;
//            Uart2Stat.OKPacket++;
    return Result;
Src/KMachine.c
@@ -28,10 +28,14 @@
//uint32_t * pUID = (uint32_t *)(UID_BASE);
const stKMInfoBlock KMInfoBlock =
{
    BOARD_TYPE,            //nDeviceType
//    sizeof(stKMInfoBlock),
    (BOARD_TYPE<<8) + BOARD_VER,            //nDeviceType     BOARD_VER,            //nDevieVer
    0x0100,            //ProgVer
    0x0100,            //KLinkVer
    0x0100,            //nCapacity
    0x0100,            //KBusVer
    4,                    //nCapacity1    ?K
    1,                    //nCapacity2    ?k
    16,                    //nDInput;
    16,                    //nDOutput
    0,                    //nAInput
@@ -40,6 +44,11 @@
    0,                    //nHOutput
    0,                    //nExt1;
    0,                    //nExt2;
    0,                    //nLogSize;
    0,                    //nPorts;
    0,                    //nManSize;
    0,                    //nAbility;
    6,                    //nSwitchBits;
};
const char VersionStr[] __attribute__((at(FLASH_BASE + 0X1000))) //__attribute__((at(0X8001000)))
    = "3.00";
@@ -52,33 +61,42 @@
    CFG_VER,
    0x0000,
    0x0000,
    {0,0,0,0,0,0},
    0x0000,
    {
        {
            1,
            0,
            2304,                        //Buadrate * 100;
            PortType_KLink,    //PorttType
            1,                            //ByteSize
            1,                            //Station
            2304,                        //Buadrate = * 100;
            0,                            //ByteSize
            0,                            //Parity
            0,                            //StopBits
            0,                            //endType
            0,                            //EofChar
            0,                            //SofChar
            0,                            //endtime
            0,                            //recvbuf
            0,                            //bufsize
        },
        {
            1,
            0,
            2304,                        //Buadrate * 100;
            PortType_KBus,    //PorttType
            1,                            //ByteSize
            0,                            //Station
            2304,                        //Buadrate = * 100;
            0,                            //ByteSize
            0,                            //Parity
            0,                            //StopBits
            0,                            //endType
            0,                            //EofChar
            0,                            //SofChar
            0,                            //endtime
            0,                            //recvbuf
            0,                            //bufsize
        }
    },
    {{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}},
    {{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1}},
    {        //default port mapping
            0,0,0,0,0,0
    },
    0x0003,
        0x0004,
        0x0005,
@@ -89,11 +107,7 @@
        0x000a,
        0x000b,
        0x000c,
        0x000d,
        0x000e,
        0x000f,
        0x0010,
    {0},
    0x0011,
    END_SIGN,
};
@@ -190,6 +204,10 @@
}
int WriteFactoryData(void * pDataBuf, int nByteCount)
{
    stFactoryData * p1 = (stFactoryData*) pDataBuf;
    stFactoryData * p2 = (stFactoryData *)FACTORY_DATA_BASE;
    p1->Seq1= p2->Seq1+1;
    EraseAndWriteToFlashMem(pDataBuf, (stFactoryData *)FACTORY_DATA_BASE,nByteCount);
    return 0;
}
Src/ModbusRTU.c
@@ -86,12 +86,12 @@
int ModBusSlaveCheckPkg(int nChn, void *ptr, uint16_t len1)
{
    if (len1 <=4) return -1;        //包长
    if (len1 <=4) return -1;        //包长
    pModBusRTUReqPkg pPkg = (pModBusRTUReqPkg) ptr;
    if (pPkg->Dst >127) return -2;   //地址码
    if ((pPkg->Cmd&0x7f) > 0x1f) return -3;  //功能码
    uint16_t crc = crc16tablefast(ptr,len1);   //CRC 校验
    if (crc != 0 ) return 4;        //CRC 校验错误
    if (pPkg->Dst >127) return -2;   //地址码
    if ((pPkg->Cmd&0x7f) > 0x1f) return -3;  //功能码
    uint16_t crc = crc16tablefast(ptr,len1);   //CRC 校验
    if (crc != 0 ) return 4;        //CRC 校验错误
    return S_OK;
}
Src/PLCfunctions.c
@@ -372,6 +372,28 @@
    return 0;
}
inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr)
{
    (*pW)|=1<<(bitAddr&0xf);
}
inline void ResetBit(unsigned short * pW, unsigned char bitAddr)
{
    (*pW)&=~(1<<(bitAddr&0xf));
}
static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value)
{
    if (Value)    {    SetAddrBit(pW, bitAddr);}
    else {ResetBit(pW, bitAddr);}
}
static inline unsigned char GetBitValue(unsigned short W, unsigned char bitAddr)
{
    if (W&(1<<(bitAddr&0xf))) return 1;
    else return 0;
}
int ProcessPLCBinProg(const stBinProg1 * pBinprog, int nStepSize)
{
    if (!PLCMem.bPLCRunning) return 0;
@@ -744,7 +766,7 @@
            break;
        }
        lastScanInputVal = PLCMem.ProgTrace[CurPos];
        lastScanInputVal =  PLCMem.ProgTrace[CurPos]; //GetBitValue(KMem.WDFs);
        PLCMem.ProgTrace[CurPos] = KMem.CurVAL;
        CurPos += nNextPos;
    }
Src/debug.c
@@ -173,11 +173,6 @@
                Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;
                Uart1BaudFirstGot=0;
            }
            if (Uart2BaudFirstGot)
            {
                Uart2baudval = HAL_RCC_GetPCLK1Freq() / USART2->BRR;
                Uart2BaudFirstGot=0;
            }
            int Reload=SysTick->LOAD;
            int Clk2=SysTick->VAL;
Src/functions.c
@@ -19,16 +19,16 @@
unsigned int TickPrioduS;    //
volatile unsigned int nCurTick=0;
volatile unsigned int CurTickuS=0;
//volatile unsigned int ThisRunTime=0;        //开机时间
//volatile unsigned int TotalRunTime=0;     //总开机时间
//volatile unsigned int PwrCount=0;                //开机次数
unsigned short ClkuS;                //每个Clk的nS数,
//volatile unsigned int ThisRunTime=0;        //开机时间
//volatile unsigned int TotalRunTime=0;     //总开机时间
//volatile unsigned int PwrCount=0;                //开机次数
unsigned short ClkuS;                //每个Clk的nS数,
int InituS(int TickFreq1)
{
        TickPrioduS=1000000/TickFreq1;    //每个SysTick的微秒数
        CoreClkMHz=HAL_RCC_GetHCLKFreq()/1000000;        //=SystemCoreClock/1000000;每uS的时钟数
        TickPriodClk=SystemCoreClock/TickFreq1;            //每个SysTick的时钟数
        TickPrioduS=1000000/TickFreq1;    //每个SysTick的微秒数
        CoreClkMHz=HAL_RCC_GetHCLKFreq()/1000000;        //=SystemCoreClock/1000000;每uS的时钟数
        TickPriodClk=SystemCoreClock/TickFreq1;            //每个SysTick的时钟数
        ClkuS=(1000000LL*65536)/SystemCoreClock;
        CurTickuS=TickPrioduS+100u;
    return 0;
@@ -53,6 +53,12 @@
{
//    unsigned short Clk1=SysTick->VAL;
        return nCurTick;
}
void logData(unsigned char d)
{
            KMem.WDB[128+KMem.WDT[123]] = d;
            KMem.WDT[123]++;      if (KMem.WDT[123]>=100) {KMem.WDT[123]=81;}
}
const unsigned short crc16_table[256] = {
@@ -211,7 +217,7 @@
    LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_2,nSize);
    LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_2);
    Uart1Stat.DMASendLen=nSize;
    Uart1Stat.Sending=1;
    Uart1Stat.bSending=1;
    LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_2);
    LL_USART_EnableDMAReq_TX(USART1);
    return nSize;    
@@ -219,7 +225,7 @@
int Uart1TriggerSendDMA()
{
        if (!Uart1Stat.Sending&&!IsEmpty(&Uart1Stat.QTx))
        if (!Uart1Stat.bSending&&!IsEmpty(&Uart1Stat.QTx))
        {            
            int len1=GetContinueData(&Uart1Stat.QTx);
            Uart1SendDMA(GetReadBuffer(&Uart1Stat.QTx),len1);
@@ -235,7 +241,7 @@
    LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_5,nSize);
    LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_5);
    Uart2Stat.DMARecvLen=nSize;
    Uart2Stat.Recving=1;
    Uart2Stat.bRecving=1;
    LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_5);
    LL_USART_EnableDMAReq_RX(USART2);    
    return 0;
@@ -248,14 +254,14 @@
    LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_4,nSize);
    LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_4);
    Uart2Stat.DMASendLen=nSize;
    Uart2Stat.Sending=1;
    Uart2Stat.bSending=1;
    LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_4);
    LL_USART_EnableDMAReq_TX(USART2);
    return nSize;    
}
int Uart2TriggerSendDMA()
{
        if (!Uart2Stat.Sending&&!IsEmpty(&Uart2Stat.QTx))
        if (!Uart2Stat.bSending&&!IsEmpty(&Uart2Stat.QTx))
        {            
            int len1=GetContinueData(&Uart2Stat.QTx);
            Uart2SendDMA(GetReadBuffer(&Uart2Stat.QTx),len1);
@@ -281,7 +287,7 @@
#endif        
    if (Uart2Stat.bPacketRecved)
    {
        KBusParsePacket(2, (pPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen);
        KBusParsePacket(2, (pKBPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen);
        Uart2RecvBuf1DataLen=0;
        Uart2Stat.bPacketRecved=0;
        Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));        
@@ -296,12 +302,11 @@
     {
             value = LL_SPI_ReceiveData8( SPI1);
#if (BOARD_TYPE == 14)
            KMem.SDD[62]++;
            KMem.SDT[122]++;
            KMem.WDD[62]++;
            KMem.WDT[122]++;
         if (!bSPI1Sending)
         {
              KMem.SDB[128+KMem.SDT[123]] = value;
             KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}
             logData(value);
         }
         if (!bSPI1Sending && (1 || bSPI1Recving))
         {
@@ -310,8 +315,7 @@
             
             if (value==0x0d)
             {
              KMem.SDB[128+KMem.SDT[123]] = nSPI1RecvPos;
             KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}
                 logData(nSPI1RecvPos);
                 nSPI1RecvLenInBuf=nSPI1RecvPos;
                 bSPI1RecvDone=1;
@@ -338,8 +342,7 @@
             else {
                 value = SPI1SendBuf[nSPI1SentLen];
                 LL_SPI_TransmitData8(SPI1,value);
                 KMem.SDB[128+KMem.SDT[123]] = value;
                 KMem.SDT[123]++;      if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;}
                logData(value);
             }
         }
#endif         
@@ -378,7 +381,7 @@
    Uart2Stat.IdelCount++;
    if (Uart2RecvBuf1DataLen>0)
        TriggerPendSV();
    //    ParsePacket((pPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen);
    //    ParsePacket((pKBPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen);
}
int PutStr(char * str1, int len1)
@@ -433,7 +436,7 @@
        Uart1Stat.SentPacket++;
    return len1;
}
int SendPacket2(pPacket p1,int len1)
int SendPacket2(pKBPacket p1,int len1)
{
        PutStr2((char *)p1,len1);    
//    PushIn(&Uart2Stat.QTx,p1,len1);
@@ -444,6 +447,8 @@
*/
void ToggleRunLed() {    LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);}
void ToggleErrLed() {    LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);}
void ToggleErr2Led() {    LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);}
#if (BOARD_TYPE == 14)
void ToggleOutStat() {    LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);}
@@ -472,11 +477,17 @@
    if (bOn) {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_14);}
    else {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_14);}        
}
void SetErr2Led(uchar bOn)
{
    if (bOn) {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_15);}
    else {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_15);}
}
/*
void SetLeds(uchar bRun, uchar bErr)
{
    SetRunLed(bRun); SetErrLed (bErr);
}
*/
#define set165SL_0() LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_4)
#define set165SL_1() LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_4)
#define set165CLK_0() LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_5)
@@ -732,7 +743,7 @@
void Output595_8(unsigned int cc)
{
//unsigned char i;
;//        74HC595输出程序,输出8位
;//        74HC595输出程序,输出8位
//    cc=~0x3f;
    __disable_irq();
    STRCLK2_1();
@@ -758,7 +769,7 @@
void Output595_16(unsigned int cc)
{
//unsigned char i;
;//        74HC595输出程序,输出8位
;//        74HC595输出程序,输出8位
//    cc=~0x3f;
    __disable_irq();
    STRCLK2_1();
@@ -853,14 +864,15 @@
{    
#if (BOARD_TYPE == 14)
    return ;
#endif
#else
    PutOutputSPI2(Y);
    //Output595_16(Y);
#endif
}
#if (BOARD_TYPE == 9 || BOARD_TYPE == 10 || BOARD_TYPE == 15 )
//#pragma message("9,10")
    // V4.2 管脚排列向右移动了一位。
    // V4.2 管脚排列向右移动了一位。
#define SRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1)
#define SRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_1)
#define STRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_2)
@@ -869,7 +881,7 @@
#define OE1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_10)
#define SER1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_11)
#define SER1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_11)
#else        //按照原来的管脚排列
#else        //按照原来的管脚排列
#define SRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_0)
#define SRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_0)
#define STRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1)
@@ -890,7 +902,7 @@
void displayInput(unsigned int cc)
{
//unsigned char i;
;//        74HC595输出程序,输出8位
;//        74HC595输出程序,输出8位
//    cc=~0x3f;
    __disable_irq();
    STRCLK1_1();
Src/main.c
@@ -70,11 +70,11 @@
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
#define RX2BUFSIZE 128
#define TX2BUFSIZE 128
#define RX2BUFSIZE 64
#define TX2BUFSIZE 64
unsigned char Uart1RxBuf[256];
unsigned char Uart1TxBuf[280];
unsigned char Uart1RxBuf[128];
unsigned char Uart1TxBuf[260];
unsigned char Uart2RxBuf[RX2BUFSIZE];
unsigned char Uart2TxBuf[TX2BUFSIZE];
@@ -124,6 +124,8 @@
        Count=0; 
        KMem.CurTimeSec++;
        KMem.ThisRunTime++; KMem.TotalRunTime++;
        if (KMRunStat.bLEDFlick) KMRunStat.bLEDFlick--;
        if (KMRunStat.bLEDFlick >120) KMRunStat.bLEDFlick=120;
    }
    return;
@@ -139,9 +141,10 @@
int main(void)
{
  /* USER CODE BEGIN 1 */
    KMRunStat.bLEDFlick = 1;
    InitUartstat(&Uart1Stat,Uart1TxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
    InitUartstat(&Uart2Stat,Uart2TxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
    InitUartstat(&Uart1Stat,Uart1RxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
    InitUartstat(&Uart2Stat,Uart2RxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
  /* USER CODE END 1 */
  /* MCU Configuration----------------------------------------------------------*/
@@ -153,18 +156,18 @@
    for (int i=0;i<9;i++)
    {
//        memset(ChnStats[i],0,0);
        ChnStats[i].SendPackets=0;
        ChnStats[i].RecvPackets=0;
        ChnStats[i].LostPackets=0;
        ChnStats[i].CtnLstPkts=0;
        ChnStats[i].MaxCtnLstPkts=0;
        ChnStats[i].NotPkgErr=0;
        ChnStats[i].PkgLenErr=0;
        ChnStats[i].TimeOutErr=0;
        ChnStats[i].BCCErr=0;
        ChnStats[i].Delay=0;
        ChnStats[i].MaxDelay=0;
//        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;
@@ -188,9 +191,9 @@
  SystemClock_Config();
  /* USER CODE BEGIN SysInit */
    TickFreq=10000;        //Tick频率
    TickFreq=10000;        //Tick频率
    InituS(TickFreq);    
 // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq);    //ÖØж¨ÒåSysTickµÄƵÂÊÎ
 // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq);    //重新定义SysTick的频率
  /* USER CODE END SysInit */
@@ -201,26 +204,28 @@
    KMachineInit();
    ReadSysCfgFromFlash(&storedKMSysCfg);
    
    KMRunStat.bLEDFlick = 1;
    KMem.EffJumperSW=ReadJumperSW();
#if (BOARD_TYPE == 14)
    KMem.EffJumperSW|=0x10;
    nAddr=KMem.EffJumperSW&0x0f;
    nStationID=KMem.EffJumperSW&0x0f;
  if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
    else{bKBusMaster=0;bKBusSlave=1;}
    nChilds=nAddr;
    nChilds=nStationID;
    FP0_Init();
#elif (BOARD_TYPE == 15)
    nAddr=KMem.EffJumperSW&0x0f;
    nStationID=KMem.EffJumperSW&0x0f;
    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;}    
#else
    nAddr=KMem.EffJumperSW&0x7;
    nStationID=KMem.EffJumperSW&0x7;
    if (KMem.EffJumperSW == 0x0f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
  else if ((KMem.EffJumperSW&0x08)!=0) {bKBusMaster=1;bKBusSlave=0;}
    else{bKBusMaster=0;bKBusSlave=1;}
#endif
    nChilds=nAddr;
    nChilds=nStationID;
    nCurPollId=1;
    //if (KMem.EffJumperSW == 0x00)
        Uart1Baud = DefaultUart1Baud;
@@ -308,7 +313,6 @@
    KWireLessInit(KMem.EffJumperSW&0x20);
    KWireLessStart();
#endif
  while (1)
  {
        //int MyKeyStat1,MyKeyStat2;
@@ -441,19 +445,32 @@
        if (KMem.RunStat) {KMem.RunStat--;}
        if (KMem.ErrStat) {KMem.ErrStat--;}
        if (KMRunStat.bLEDFlick)
        {
            SetRunLed(FastFlicker);
            SetErrLed(FastFlicker);
            SetErr2Led(FastFlicker);
            SetOutStat(!FastFlicker);
            //KMRunStat.bLEDFlick-- ;
        }
        else
        {
        if (!KMem.RunStat) SetRunLed(SlowFlicker);
        else SetRunLed(FastFlicker);
        
        if (!KMem.ErrStat) 
        {
            SetErrLed(0);
                SetErr2Led(0);
            SetOutStat(1);
        }
        else 
        {
            SetErrLed(FastFlicker);
                SetErr2Led(FastFlicker);
            SetOutStat(0);
            
            }
        }
        
//        SetRunLed(RunStat);
@@ -485,8 +502,8 @@
        }
        KMem.nRunCount++;
//        int nSize=sizeof(stChnStat);
//        memcpy(&KMem.SDT[64],&ChnStats[1],nSize);
//        memcpy(&KMem.SDT[64+nSize/2],&ChnStats[2],nSize);
//        memcpy(&KMem.SDT[64],&KBusChnStats[1],nSize);
//        memcpy(&KMem.SDT[64+nSize/2],&KBusChnStats[2],nSize);
//        for (int i=0;i<128;i++)    {        SDT[i]=i;    }
//        SDT[48]=55;
        if (Uart1RecvBuf1DataLen >0 && Uart1Stat.bPacketRecved)
Src/stm32f0xx_it.c
@@ -149,7 +149,7 @@
void DMA1_Channel2_3_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel2_3_IRQn 0 */
    Uart1DmaInts++;
    Uart1Stat.DMACount++;
    if (LL_DMA_IsActiveFlag_TC2(DMA1))
    {
@@ -171,7 +171,7 @@
        else
        {
            Uart1Stat.DMASendLen=0;
            Uart1Stat.Sending=0;
            Uart1Stat.bSending=0;
        }                
    }
  /* USER CODE END DMA1_Channel2_3_IRQn 0 */
@@ -188,7 +188,7 @@
void DMA1_Channel4_5_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel4_5_IRQn 0 */
    Uart2DmaInts++;
    Uart2Stat.DMACount++;
    if (LL_DMA_IsActiveFlag_TC4(DMA1))
    {
        LL_DMA_ClearFlag_TC4(DMA1);
@@ -212,7 +212,7 @@
*/        
        {
            Uart2Stat.DMASendLen=0;
            Uart2Stat.Sending=0;
            Uart2Stat.bSending=0;
        }                
    }
  /* USER CODE END DMA1_Channel4_5_IRQn 0 */
@@ -262,14 +262,14 @@
        Uart1Stat.OverRunCount++;
    }
    if (LL_USART_IsEnabledIT_IDLE(USART1)&&LL_USART_IsActiveFlag_IDLE(USART1))
    {//接收完成
    {//接收完成
        LL_USART_ClearFlag_IDLE(USART1);
        Uart1RecvDone();
    }    
  /* USER CODE END USART1_IRQn 0 */
  /* USER CODE BEGIN USART1_IRQn 1 */
    if (LL_USART_IsActiveFlag_TC(USART1))
    {//发送完成
    {//发送完成
        LL_USART_ClearFlag_TC(USART1);
        Uart1SendDone();
    }    
@@ -297,14 +297,14 @@
        Uart2Stat.OverRunCount++;
    }
    if (LL_USART_IsEnabledIT_IDLE(USART2)&&LL_USART_IsActiveFlag_IDLE(USART2))
    {//接收完成
    {//接收完成
        LL_USART_ClearFlag_IDLE(USART2);
        Uart2RecvDone();
    }    
  /* USER CODE END USART2_IRQn 0 */
  /* USER CODE BEGIN USART2_IRQn 1 */
    if (LL_USART_IsActiveFlag_TC(USART2))
    {//发送完成
    {//发送完成
        LL_USART_ClearFlag_TC(USART2);
        Uart2SendDone();
    }