/** ****************************************************************************** * @file : KWireLess.h * @brief : Header for KWireLess.c file. * This file contains the common defines of the application. ****************************************************************************** */ #include "KMachine.h" #include #include "user.h" #include "radio/inc/sx126x-board.h" #ifndef __KWIRELESS_H__ #define __KWIRELESS_H__ typedef unsigned char uchar; /************************************************************************************************************************************** Demo 程序流程 RadioEnableMaster=true 为主机端,主机端发送一个"PING"数据后切换到接收,等待从机返回的应答"PONG"数据LED闪烁 RadioEnableMaster=false 为从机端,从机端接收到主机端发过来的"PING"数据后LED闪烁并发送一个"PONG"数据作为应答 ***************************************************************************************************************************************/ typedef enum tag_KWStates // 主状态机 状态 { KW_PON, //开机 KW_UNINIT, //未初始化 KW_INITED, //已初始化 KW_UNCONFIGED, //未配置 KW_CONFIGING, //配置中 KW_CONFIGED, //已配置 KW_READY, //就绪 KW_STARTING, //启动中 KW_OPERATIONAL, //操作 KW_STOPPED, //停止 KW_ERROR1, //错误1 KW_ERROR2, //错误2 KW_ERROR3, //错误3 }KWStates; #define STORE_KWCONFIG_BASE (FLASH_BASE + 0x0000F000) //50k K //and //#define STORE_RUNSTAT_PAGESIZE (0x00000400) //Page Size = 1K #define STORE_KWCONFIG_PAGES 1 //use 1 pages enum enWLWorkMode { WLWorkModeNone, // WLWorkModeUni, // 一对一 WLWorkModeMul, // 一对多 WLWorkModeThr, // 透传 }; typedef struct tagWLConfig { uint32_t RF_T_Freq; //Hz uint32_t RF_R_Freq; //Hz uint16_t nChnSpacing; //kHz uint16_t nCycleTime; // uchar workMode; //0: FSK, 1: LoRa uchar nChannel; uchar bMaster; uchar nRadioAddr; uchar bEnableMulti; uchar Tx_Power; // dBm 5 - 22 dBm uchar LoraBandWidth; // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] uchar LoRaFactor; // [SF5 .. SF 12] uchar LoRaCodingRate; // [1 : 4/5, 2: 4/6, 3: 4/7, 4: 4/8 ] uint8_t NetWorkAddr; uint16_t DeviceAddr; uchar bEnableAddr; uchar bEnableEncrypt; uchar bEnableRelay; uchar LoRaPreambleLen; // 2 - 12 uchar bAutoPower; //自动功率 uchar bAutoReSend; //自动重发 // uchar ; // // uchar }stWLConfig, *pstWLConfig; typedef struct tagStoredWLConfig { unsigned short BlockSign; unsigned char BlockType; unsigned char nSeq; unsigned short nSize; unsigned short nCRC16; stWLConfig WLConfig; }stStoredWLConfig, *pstStoredWLConfig; typedef enum tag_runstep{ RS_IDLE, RS_MASTER_CAD, RS_SENDING, RS_SENT, RS_RECVING, RS_RECVED, }enRunStep; #pragma anon_unions typedef struct tagKwStatByte { uint8_t nSeq :2; uint8_t Err1 :1; uint8_t Err2 :1; uint8_t bHasData :1; uint8_t bHeader :1; uint8_t bTail :1; }stKwStatByte; typedef struct tagKwChnStat { uint32_t DeviceAddr; uint8_t Stat; // 状态字节 uint8_t nSeq; // 包序列号 uint8_t Tx_Power; uint8_t RSSI; uint8_t SNR; uint8_t tRSSI; uint8_t tSNR; uint32_t SendCount; uint32_t RecvCount; // uint32_t SendTime; // uint32_t RecvTime; uint16_t TxErr; uint16_t RxErr; uint16_t LostCount; uint16_t CtnLstPkts; uint16_t MaxCtnLstPkts; uchar * pTxBuffer; uint16_t sizetosend; uint16_t sentsize; uchar * pRxBuffer; // and buffer max size; // uint16_t SizeToRecv; uint16_t RecvedSize; }stKwChnStat; typedef struct tagWLStat { union { uint32_t Status; //状态 struct { uint32_t bMasterSent:1; uint32_t bMasterRecved:1; }; }; uint16_t curStat; //当前状态 uint16_t runStep; //工作步骤 uint16_t RunStat; //运行状态 uint16_t ErrStat; //错误状态 uint32_t RF_Freq; //运行频率 uint16_t nTimeOnAir; uint16_t DeviceAddr; uint8_t nSeq; uint8_t NetWorkAddr; uchar Tx_Power; // dBm 5 - 22 dBm uchar LoraBandWidth; // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] uchar LoRaFactor; // [SF5 .. SF 12] uchar LoRaCodingRate; // [1 : 4/5, 2: 4/6, 3: 4/7, 4: 4/8 ] uchar LoRaPreambleLen; // 2 - 12 uchar bEnableAddr; uchar bEnableEncrypt; uchar bEnableRelay; uchar bAutoReSend; //自动重发 uint32_t sentCount; //发送计数 uint32_t recvCount; //接收计数 uint32_t lastSendtime; //上次发送时间 uint32_t lastSenttime; //上次发完时间 uint32_t lastRecvtime; //上次启动接收时间 uint32_t lastRecvdtime; //上次收到时间 uint32_t lastActTime; //上次动作时间 uint32_t lastAckTime; //上次应答时间 uint32_t lastErrTime; //上次错误时间 uint32_t latancy; //延迟 uint32_t cycleTime; //循环时间 uint16_t LostPackets; //丢包计数 uint16_t CtnLstPkts; //连续丢包计数 uint16_t MaxCtnLstPkts; //最大连续丢包计数 uint16_t TXErr; //发送错误计数 uint16_t RXErr; //接收错误计数 uint16_t CRCErr; //CRC错误计数 uint16_t PktErr; //包错误 ; uint16_t ChnErr; //频道错误; uint16_t nErrChn; //错误的频道号; uint16_t CADDoneCount; //CAD 完成次数 uint16_t CADOkCount; //CAD 是次数 uint16_t CADNgCount; //CAD 否次数 uint16_t CADTimeOut; //CAD 超时次数 uint16_t StepErr1; //步骤错误1 uint16_t StepErr2; //步骤错误2 uint16_t Err1Count; //微闪报警次数 uint16_t Err2Count; //大闪报警次数 uint16_t Err3Count; //严重丢失信号次数 int8_t RSSI; //信号强度 int8_t SNR; //信噪比 int8_t tRSSI; //对方信号强度 int8_t tSNR; //对方信噪比 uint32_t targetSentCount; //对方发送数量 uint32_t targetRecvdCount; //对方接受数量 unsigned char ttTxBuf1[256]; uint16_t sizetosend; uint16_t sentsize; uint16_t sizesending; unsigned char ttRxBuf1[256]; uint16_t ttRxSize; unsigned char ttRxRecved; unsigned char ttAirBuf[65]; uint16_t ttAirSize; unsigned char bttAirHeader; unsigned char bttAirTail; unsigned char bttAirData; }stWLRunStat,*pstWLRunStat; enum enKwCMDs { KwcmdNone = 0x00, //Nothing KwcmdQuery = 0x01, //Query basic Info KwcmdQueryRply = 0x81, //Query Info Reply KwcmdSetCfg = 0x02, // Set Configuration KwcmdSetCfgRply = 0x82, // Set Configuration Response KwcmdToRunMode = 0x03, KwcmdToRunModeRply = 0x83, KwcmdBroadCastCfg = 0x04, // KwcmdMuExchgData = 0x05, // KwcmdMuExchgDataRply = 0x85, // KwcmdToSafeMode = 0x06, KwcmdHeartBeat = 0x07, // KwcmdReHeartBeat = 0x87, // }; enum { enReqSign = 0x55, enRplySign = 0xAA, }; typedef struct tagKwPktStatByte { uchar nSeq :2 ; uchar bErr1 :1 ; uchar bErr2 :1 ; uchar bHasData :1; uchar bHead :1; uchar bTail :1; }stKwPktStatByte; typedef struct tagKwPacket { uchar STSign; // 包开始标记 uchar DstAddr; // 子机组合地址, 包括 频道 和 子机地址 uchar Func; // 功能码 union{ uchar Stat; // 状态字 stKwPktStatByte; }; uchar Data[1]; // 数据 }stKwPacket, *pstKwPacket; int LoadKwConfig(void); int SaveKwConfig(void); int KWireLessInit(bool bRadioEnableMaster, uint32_t nChn); int KWireLessStart(void); void OnTxDone( void ); void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); void OnTxTimeout( void ); void OnRxTimeout( void ); void OnRxError( void ); void OnCadDone( bool channelActivityDetected); void LedToggle(void); int KWL_Process(int nChn); int KWLMasterProc(int nChn); int KWLSlaveProc(int nChn); int KWLMasterParsePkt(int nChn, int nSize); int KWLSlaveParsePkt(int nChn, int nSize); int KWLMasterSendReqPkt(int nChn); int KWLSlaveSendRplyPkt(int nChn); int KWMasterProc(void); int KWSlaveProc(void); int MkKwPkg(void* pPkg, int len); int KWSendPkg(void* pPkg, int len); int KWProcPkg(void); //extern uchar nRadioChannel; //extern uchar nRadioAddr; extern stWLConfig WLCfg ; extern stWLRunStat KwRunStat; #endif ///* __KBUS_H__ */