QuakeGod
2024-08-06 7d8ba5df7d883c86c24aa38449c4a6dd126e920f
Radio_LLCC68 1.15, Stable version
16个文件已修改
609 ■■■■■ 已修改文件
ComLib/Inc/GlobalDef.h 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ComLib/Src/BSP.c 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ComLib/Src/GlobalDef.c 16 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ComLib/Src/KMachine.c 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ComLib/Src/ModbusRTU.c 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ComLib/Src/functions.c 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ComLib/Src/stm32f0xx_it.c 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
KNet.uvmpw 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MDK-ARM/startup_stm32f030x8.s 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
Radio_LLCC68/Inc/BoardType.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Radio_LLCC68/Radio/KWireLess.c 421 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Radio_LLCC68/Radio/KWireLess.h 26 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Radio_LLCC68/Radio/src/radio.c 24 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Radio_LLCC68/Radio/src/sx126x-board.c 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Radio_LLCC68/Src/BoardType.c 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
Radio_LLCC68/Src/main.c 28 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ComLib/Inc/GlobalDef.h
@@ -67,15 +67,15 @@
extern unsigned int Uart2Baud;
extern unsigned char Uart1RecvBuf1[Uart1RecvBufSize];
extern int Uart1RecvBuf1DataLen;
extern unsigned short Uart1RecvBuf1DataLen;
extern unsigned char Uart2RecvBuf1[128];
extern int Uart2RecvBuf1DataLen;
extern unsigned short Uart2RecvBuf1DataLen;
extern volatile int Uart1BaudGot;
extern volatile int Uart1BaudFirstGot;
extern volatile char Uart1BaudGot;
extern volatile char Uart1BaudFirstGot;
//extern volatile int Uart2BaudGot;
//extern volatile int Uart2BaudFirstGot;
ComLib/Src/BSP.c
@@ -478,7 +478,7 @@
  SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
  SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
  SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
  SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV4;
  SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8;
  SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
  SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  SPI_InitStruct.CRCPoly = 7;
@@ -648,9 +648,13 @@
//  LL_USART_SetDEAssertionTime(USART1, 1);
//  LL_USART_SetDEDeassertionTime(USART1, 1);
//  LL_USART_EnableOneBitSamp(USART1);
#if (USART1_AUTO_BAUDRATE == 1)
  LL_USART_EnableAutoBaudRate(USART1);
  LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
  LL_USART_DisableOverrunDetect(USART1);
#endif
    LL_USART_DisableOverrunDetect(USART1);
  LL_USART_ConfigAsyncMode(USART1);
  LL_USART_Enable(USART1);
}
ComLib/Src/GlobalDef.c
@@ -18,21 +18,21 @@
unsigned int Uart2Baud = DefaultUart2Baud;
unsigned char Uart1RecvBuf1[Uart1RecvBufSize];
int Uart1RecvBuf1DataLen=0;
unsigned short Uart1RecvBuf1DataLen=0;
//unsigned char Uart1RecvBuf2[128];
//int Uart1RecvBuf2DataLen=0;
unsigned char Uart2RecvBuf1[128];
int Uart2RecvBuf1DataLen=0;
unsigned short Uart2RecvBuf1DataLen=0;
//unsigned char Uart2RecvBuf2[128];
//int Uart2RecvBuf2DataLen=0;
volatile int Uart1BaudGot=0;
volatile int Uart1BaudFirstGot=0;
volatile int Uart1DmaInts=0;
volatile int Uart2BaudGot=0;
volatile int Uart2BaudFirstGot=0;
volatile int Uart2DmaInts=0;
volatile char Uart1BaudGot=0;
volatile char Uart1BaudFirstGot=0;
volatile char Uart1DmaInts=0;
volatile char Uart2BaudGot=0;
volatile char Uart2BaudFirstGot=0;
volatile char Uart2DmaInts=0;
volatile int SysConfigs;
ComLib/Src/KMachine.c
@@ -245,15 +245,16 @@
    
int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize)
{
//    memcpy(pBuf,pAddrFlash,nSize);
    for (int i=0;i<nByteSize/4;i++)
    {
        ((uint32_t *)pBuf)[i] = ((uint32_t *)pAddrFlash)[i];
    }
    for (int i=nByteSize/4*2;i<nByteSize/2;i++)
    {
        ((uint16_t *)pBuf)[i] = ((uint16_t *)pAddrFlash)[i];
    }
    memcpy(pBuf,pAddrFlash,nByteSize);
//    for (int i=0;i<nByteSize/4;i++)
//    {
//        ((uint32_t *)pBuf)[i] = ((uint32_t *)pAddrFlash)[i];
//    }
//    for (int i=nByteSize/4*2;i<nByteSize/2;i++)
//    {
//        ((uint16_t *)pBuf)[i] = ((uint16_t *)pAddrFlash)[i];
//    }
    return nByteSize;
}
ComLib/Src/ModbusRTU.c
@@ -61,25 +61,7 @@
    return(crc);
}
*/ 
const uint16_t crctalbeabs[] = {
    0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
    0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
};
uint16_t crc16tablefast(const uint8_t *ptr, uint16_t len)
{
    uint16_t crc = 0xffff;
    uint16_t i;
    uint8_t ch;
    for (i = 0; i < len; i++) {
        ch = *ptr++;
        crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4);
        crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
    }
    return crc;
}
//**********************************************************************
//******************  Modbus slave  ************************************
ComLib/Src/functions.c
@@ -192,7 +192,7 @@
    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
    0x40
};
 /*
uint16_t crc16table(const uint8_t *ptr, uint16_t len)
{
    uint8_t crchi = 0xff;
@@ -206,6 +206,27 @@
    }
    return (crchi << 8 | crclo);
}
*/
const uint16_t crctalbeabs[] = {
    0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
    0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
};
uint16_t crc16tablefast(const uint8_t *ptr, uint16_t len)
{
    uint16_t crc = 0xffff;
    uint16_t i;
    uint8_t ch;
    for (i = 0; i < len; i++) {
        ch = *ptr++;
        crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4);
        crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
    }
    return crc;
}
 /*
void modbuscrc16test()
{
@@ -389,6 +410,7 @@
//      SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos;
//        KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen);
//        Uart1RecvBuf1DataLen=0;
        TriggerPendSV();
    }
}
ComLib/Src/stm32f0xx_it.c
@@ -253,7 +253,9 @@
        unsigned char ch=LL_USART_ReceiveData8(USART1);
        // PushOne(&Uart1Stat.QRx,ch);
        Uart1Stat.RecvBytes++;
        Uart1RecvBuf1[Uart1RecvBuf1DataLen++]=ch;
        Uart1RecvBuf1[Uart1RecvBuf1DataLen]=ch;
        if (Uart1RecvBuf1DataLen < Uart1RecvBufSize -1 ) {Uart1RecvBuf1DataLen++;}
        //LL_USART_TransmitData8(USART1,ch);
    }
    if (LL_USART_IsActiveFlag_ORE(USART1))
KNet.uvmpw
@@ -9,6 +9,7 @@
  <project>
    <PathAndName>.\C8T6_BootLoader\MDK-ARM\C8T6_BtLdr.uvprojx</PathAndName>
    <NodeIsExpanded>1</NodeIsExpanded>
  </project>
  <project>
@@ -26,6 +27,7 @@
  <project>
    <PathAndName>.\MDK-ARM\Radio_LLCC68_C8T6_8路无线模块.uvprojx</PathAndName>
    <NodeIsActive>1</NodeIsActive>
    <NodeIsExpanded>1</NodeIsExpanded>
  </project>
  <project>
MDK-ARM/startup_stm32f030x8.s
@@ -43,7 +43,7 @@
;   <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>
Stack_Size        EQU     0x400
Stack_Size        EQU     0x600
                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
Radio_LLCC68/Inc/BoardType.h
@@ -18,6 +18,7 @@
    4  //New Master 16 in16 out V1.2
    5  //Slave 8 in 8 out V1.2
*/
#define USART1_AUTO_BAUDRATE 0
#define USART2_USE_HARDWARE_DE 1
#define USART2_DE_ASSERTION_TIME 31
Radio_LLCC68/Radio/KWireLess.c
@@ -102,24 +102,33 @@
extern bool IrqFired;
bool RadioEnableMaster=true;//主从选择
uchar nRadioChannel = 0;
uchar nRadioAddr = 1;
//bool RadioEnableMaster=true;//主从选择
//uchar nRadioChannel = 0;
//uchar nRadioAddr = 1;
uint16_t  crc_value;
#define MixAddr(x,y) ((x<<4)|(y))
/*!
 * Radio events function pointer
 */
#define MASTER_RX_TIMEOUT_VALUE                 80            //mS
#define SLAVE_RX_TIMEOUT_VALUE                  400            //mS
#define CYCLE_TIME                                                            120            //mS
const stWLConfig defaultWLConfig =
{
    .RF_T_Freq = 430620000,            // uint32_t     Hz
    .RF_R_Freq = 430620000,            //uint32_t         //Hz
    .RF_R_Freq = 430620000,            // uint32_t         //Hz
    .nChnSpacing = 530,            //          uint16_t ChannelSpacing;        //kHz    
    .nCycleTime = CYCLE_TIME,        // CyCleTime
    .workMode = 1,                //            uchar workMode;            //0: FSK,    1: LoRa    
    .Tx_Power = 22,                //      uchar Tx_Power;
    .LoraBandWidth = 0,                //             uchar LoraBandWidth;        //        [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ]
    .nChannel = 0,
    .bMaster = 0,
    .nRadioAddr =1 ,
    .bEnableMulti = 0,
    .Tx_Power = 20,                //      uchar Tx_Power;
    .LoraBandWidth = 1,                //             uchar LoraBandWidth;        //        [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ]
    .LoRaFactor = 5,                //             uchar LoRaFactor;                //        [SF5 .. SF 12]
    .LoRaCodingRate = 1,                //            uchar LoRaCodingRate;        //        [1 : 4/5,  2: 4/6,  3:  4/7,    4:  4/8
    .NetWorkAddr = 0x00,            //            uint8_t NetWorkAddr;
@@ -128,6 +137,7 @@
    .bEnableEncrypt = 0,                //            uchar bEnableEncrypt;
    .bEnableRelay = 0,                //            uchar bEnableRelay;
    .LoRaPreambleLen = 4,                //            uchar LoRaPreamble_Len;            // 2 - 12
    .bAutoPower = 1,
    .bAutoReSend = 1,                        // 自动重发
};
@@ -147,9 +157,7 @@
    TX_TIMEOUT,
}States_t;
#define MASTER_RX_TIMEOUT_VALUE                 80            //mS
#define SLAVE_RX_TIMEOUT_VALUE                  400            //mS
#define CYCLE_TIME                                                            160            //mS
#define WL_RX_BUFFER_SIZE                                                 256                         // Define the payload size here
#define WL_TX_BUFFER_SIZE                                                 128                         // Define the payload size here
@@ -213,7 +221,7 @@
int KWireLessInit(bool bRadioEnableMaster, uint32_t nChn)
{
        stWLConfig * pWLCfg = & WLCfg;
        RadioEnableMaster = bRadioEnableMaster;
        pWLCfg->bMaster = bRadioEnableMaster;
    // Radio initialization
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
@@ -223,10 +231,10 @@
    RadioEvents.CadDone = OnCadDone;
    
    Radio.Init( &RadioEvents );
        nRadioChannel = nChn;
        KwRunStat.RF_Freq = pWLCfg->RF_T_Freq + nRadioChannel * pWLCfg->nChnSpacing*1000;
        pWLCfg->nChannel = nChn;
        KwRunStat.RF_Freq = pWLCfg->RF_T_Freq + pWLCfg->nChannel * pWLCfg->nChnSpacing*1000;
    
    Radio.SetChannel( pWLCfg->RF_T_Freq + nRadioChannel * pWLCfg->nChnSpacing*1000 );
    Radio.SetChannel( KwRunStat.RF_Freq );
    
  //  Radio.WriteBuffer(0x06C0,data,2);
   // Radio.ReadBuffer(0x06C0,test,2);
@@ -272,28 +280,59 @@
#endif
        SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS    0x30 -> SD_XOSC  0x20 -> SD_RC
        
        SX126xSetCadParams(LORA_CAD_04_SYMBOL,0,1,LORA_CAD_ONLY,1);
        SX126xSetCadParams(LORA_CAD_02_SYMBOL,22,10,LORA_CAD_ONLY,1);
    nTimeOnAir = Radio.TimeOnAir(MODEM_LORA,14);
    KwRunStat.nTimeOnAir =     nTimeOnAir;                                
    KMem.WDT[38]=nTimeOnAir;
    KwRunStat.runStep=RS_IDEL;
//    KMem.WDT[38]=nTimeOnAir;
    KwRunStat.runStep=RS_IDLE;
    KMem.WDT[0]++;
        KwRunStat.Tx_Power = WLCfg.Tx_Power;
        KwRunStat.LoraBandWidth = WLCfg.LoraBandWidth;
        KwRunStat.LoRaFactor = WLCfg.LoRaFactor;
        KwRunStat.LoRaCodingRate = WLCfg.LoRaCodingRate;
        KwRunStat.LoRaPreambleLen = WLCfg.LoRaPreambleLen;
    return 0;
}
int KWireLessStart(void)
int KwResetRf()
{
    if(RadioEnableMaster)
    {
            KWLMasterSendReqPkt(1);
    }
    else
    {
    //    LoadKwConfig();
    //    KWireLessInit(RadioEnableMaster,nRadioChannel);
    Radio.Standby();
    KwRunStat.runStep=RS_IDLE;
    return 0;
}
int KwStartRecv(int nChnd)
{
        Radio.Standby();
             Radio.SetChannel( KwRunStat.RF_Freq );
            Radio.SetRxConfig( MODEM_LORA, WLCfg.LoraBandWidth, WLCfg.LoRaFactor,
                                   WLCfg.LoRaCodingRate, 0, WLCfg.LoRaPreambleLen,
                                   LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
                                   0, true, 0, 0, LORA_IQ_INVERSION_ON, false );
            Radio.SetMaxPayloadLength(MODEM_LORA,32);
       Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
            KwRunStat.runStep=RS_RECVING;
            
            KwRunStat.lastActTime = GetTick();
            KwRunStat.lastRecvtime = GetTick();            
        return 0;
}
int KWireLessStart(void)
{
    if(WLCfg.bMaster)
    {
            KWLMasterSendReqPkt(1);
    }
    else
    {
            KwStartRecv(1);
    }
    
//    while( 1 )
@@ -303,36 +342,38 @@
    return 0;
}
void OnTxDone( void )
{   
        KwRunStat.bMasterSent = 1;
        KwRunStat.bMasterRecved = 0;
        KwRunStat.runStep=RS_IDEL;
        KwRunStat.runStep=RS_IDLE;
        KMem.WDT[42]++;
        KwRunStat.lastSenttime = GetTick();
        KwRunStat.lastAckTime = GetTick();
    Radio.Standby();
        if (RadioEnableMaster) {
        if (WLCfg.bMaster) {
            Radio.Rx( MASTER_RX_TIMEOUT_VALUE ); //进入接收
        }    else     {
            Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); //进入接收
            KwStartRecv(1);
            //Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); //进入接收
        }
        KwRunStat.runStep=RS_RECVING;
        
        KwRunStat.lastActTime = GetTick();    
        KwRunStat.lastRecvtime = GetTick();
        LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);
            int us2=GetTick();
            KMem.ScanTimeuS=us2-KMem.LastScanTime;
        KMem.LastScanTime = us2;
        if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
        if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
//            int us2=GetTick();
//            KMem.ScanTimeuS=us2-KMem.LastScanTime;
//            KMem.LastScanTime = us2;
//        if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
//        if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
        
}
void OnTxTimeout( void )
{
        KwRunStat.runStep=RS_IDEL;
        KwRunStat.runStep=RS_IDLE;
        KwRunStat.lastAckTime = GetTick();
        KwRunStat.TXErr++;
    Radio.Standby();    
@@ -343,14 +384,16 @@
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
        KMem.WDT[44] = size;
    memcpy( RX_Buffer, payload, size );
        memcpy(&KMem.WDT[64],payload,size);
    Radio.Standby();
        if (bThisRxError) {
            bThisRxError=0;
            return;
        if (size <= 120) {
            memcpy( RX_Buffer, payload, size );
            memcpy(&KMem.WDT[64],payload,size);
        }else
        {
        }
    Radio.Standby();
        KwRunStat.runStep = RS_IDLE;
        
        KwRunStat.curStat = 0;
        KwRunStat.latancy = GetTick() - KwRunStat.lastSendtime;
@@ -364,24 +407,33 @@
        KwRunStat.recvCount++;
//    KwRunStat.CtnLstPkts=0;
    if(RadioEnableMaster)
    if(WLCfg.bMaster)
    {
                KWLMasterParsePkt(nRadioChannel,size);
                KWLMasterParsePkt(WLCfg.nChannel,size);
            //KWLMasterSendReqPkt(1);
    }
    else            //slave
    {
                KWLSlaveParsePkt(nRadioChannel,size);
                KWLSlaveParsePkt(WLCfg.nChannel,size);
    }
        if (bThisRxError) {
            bThisRxError=0;
    //        return;
        }
}
void OnRxTimeout( void )
{
        KwRunStat.runStep=RS_IDEL;
        KwRunStat.runStep=RS_IDLE;
    
    Radio.Standby();
        KwRunStat.lastErrTime = GetTick() - KwRunStat.lastAckTime;
        KwRunStat.lastAckTime = GetTick();
        KwRunStat.Tx_Power+=6;                //丢包, 增加发射功率
        if (KwRunStat.Tx_Power > WLCfg.Tx_Power) {KwRunStat.Tx_Power= WLCfg.Tx_Power; }
        SX126xSetRfTxPower(KwRunStat.Tx_Power);
    
        KMem.WDT[45]++;
        KwRunStat.LostPackets++;
@@ -396,45 +448,47 @@
            if (KwRunStat.CtnLstPkts == 10) {KwRunStat.Err3Count++;}        
        if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
                KMem.WDT[51]++;
                LoadKwConfig();
                KWireLessInit(RadioEnableMaster,nRadioChannel);
                KwResetRf();
                Radio.Standby();
            //    KWireLessStart();
        }
    if(RadioEnableMaster)
    if(WLCfg.bMaster)
    {
            //KWLMasterSendReqPkt(1);
    }
    else
    {
            Radio.Standby();
      Radio.Rx( SLAVE_RX_TIMEOUT_VALUE );
            KwRunStat.runStep=RS_RECVING;
            KwRunStat.lastActTime = GetTick();
            KwRunStat.lastRecvtime = GetTick();
            KwStartRecv(1);
//            Radio.Standby();
//     Radio.Rx( SLAVE_RX_TIMEOUT_VALUE );
//            KwRunStat.runStep=RS_RECVING;
//            KwRunStat.lastActTime = GetTick();
//            KwRunStat.lastRecvtime = GetTick();
    }
}
void OnRxError( void )
{
            Radio.Standby();
      Radio.Rx( SLAVE_RX_TIMEOUT_VALUE );
            KwRunStat.runStep=RS_RECVING;
            KwRunStat.lastActTime = GetTick();
            KwRunStat.lastRecvtime = GetTick();
            KwStartRecv(1);
//            Radio.Standby();
//     Radio.Rx( SLAVE_RX_TIMEOUT_VALUE );
//            KwRunStat.runStep=RS_RECVING;
//            KwRunStat.lastActTime = GetTick();
//            KwRunStat.lastRecvtime = GetTick();
    
            KwRunStat.RXErr++;
            KMem.WDT[46]++;
            bThisRxError=1;
        KwRunStat.lastErrTime = GetTick() - KwRunStat.lastAckTime;
    
    if(RadioEnableMaster)
    if(WLCfg.bMaster)
    {
//            SendPingMsg();
    }
    else
    {
/*
//      Radio.Rx( RX_TIMEOUT_VALUE ); 
                //KwRunStat.lastActTime = GetTick();            
            KwRunStat.LostPackets++;
@@ -447,14 +501,38 @@
            if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
            if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
            if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}
*/
    }
//        Radio.Standby();
}
void OnCadDone( bool channelActivityDetected)
{
    KMem.WDT[60]++;
    KwRunStat.CADDoneCount++;
    KwRunStat.lastAckTime = GetTick();
    KMem.WDT[16] = KwRunStat.lastAckTime - KwRunStat.lastActTime;
    if (channelActivityDetected) {
        KwRunStat.CADNgCount++;
        KMem.WDT[61]++;
        Radio.StartCad();
        KwRunStat.lastActTime = GetTick();
    }
    else {
        KwRunStat.CADOkCount++;
        KMem.WDT[62]++;
        if (WLCfg.bMaster) {
            if (KwRunStat.curStat == 0 ){ //&& stat == RF_IDLE )    //200mS
    //                    Radio.Standby();
                KWireLessStart();
            }else {
                Radio.Standby();
                KWireLessStart();
            }
        }else {
            KWLSlaveSendRplyPkt(1);
        }
    }
    return;
}
int KWLMasterParsePkt(int nChn,int size)
@@ -474,7 +552,7 @@
    pstKLPacket p1 = (pstKLPacket) RX_Buffer;
      if(CRC_OK && p1->STSign == enRplySign ) //memcmp(RX_Buffer,PongMsg,4)==0 
      {
                if (p1->DstAddr == MixAddr(nRadioChannel,nRadioAddr))
                if (p1->DstAddr == MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr))
                    {
                        KwRunStat.bMasterRecved = 1;                        
                        LedToggle();//LED闪烁
@@ -485,6 +563,14 @@
                        if (p1->Stat==0) {
                            KwRunStat.tRSSI = RX_Buffer[10];
                            KwRunStat.tSNR = RX_Buffer[11];
                            if (KwRunStat.tRSSI > -50) {
                                if (KwRunStat.Tx_Power > 0) {KwRunStat.Tx_Power--; SX126xSetRfTxPower(KwRunStat.Tx_Power);}
                            }else if (KwRunStat.tRSSI < -60) {
                                KwRunStat.Tx_Power+=6;
                                if (KwRunStat.Tx_Power > WLCfg.Tx_Power) {KwRunStat.Tx_Power= WLCfg.Tx_Power; }
                                SX126xSetRfTxPower(KwRunStat.Tx_Power);
                            }
                        }else if (p1->Stat==1){
                            KwRunStat.targetSentCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
                        }else if (p1->Stat==2) {
@@ -512,21 +598,22 @@
    if (RX_Buffer[size-2] != (crc_value&0xff) && RX_Buffer[size-1] != (crc_value >> 8))
    {
        KwRunStat.CRCErr++;
        KwRunStat.ErrStat=500;
//        KwRunStat.ErrStat=500;
        CRC_OK = 0;
    }else {
        CRC_OK = 1;
            if (p1->STSign != enReqSign) {
                KwRunStat.PktErr++;
            }else {
            if (p1->DstAddr != MixAddr(nRadioChannel,nRadioAddr)) {
                KwRunStat.ChnErr++;
            }
                if (p1->DstAddr != MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr)) {
                    KwRunStat.ChnErr++;
                    KwRunStat.nErrChn = p1->DstAddr;
                }
            }                
    }
//*/    
      if(CRC_OK && p1->STSign == enReqSign && p1->DstAddr == MixAddr(nRadioChannel,nRadioAddr))// memcmp(RX_Buffer,PingMsg,4)==0 && )
      if(CRC_OK && p1->STSign == enReqSign && p1->DstAddr == MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr))// memcmp(RX_Buffer,PingMsg,4)==0 && )
      {
        LedToggle();//LED闪烁
@@ -537,6 +624,13 @@
                if (p1->Stat==0) {
                    KwRunStat.tRSSI = RX_Buffer[10];
                    KwRunStat.tSNR = RX_Buffer[11];
                            if (KwRunStat.tRSSI > -50) {
                                if (KwRunStat.Tx_Power > 0) {KwRunStat.Tx_Power--; SX126xSetRfTxPower(KwRunStat.Tx_Power);}
                            }else if (KwRunStat.tRSSI < -60) {
                                KwRunStat.Tx_Power+=6;
                                if (KwRunStat.Tx_Power > WLCfg.Tx_Power) {KwRunStat.Tx_Power= WLCfg.Tx_Power; }
                                SX126xSetRfTxPower(KwRunStat.Tx_Power);
                            }
                }else if (p1->Stat==1){
                    KwRunStat.targetSentCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
                }else if (p1->Stat==2) {
@@ -549,15 +643,17 @@
                KMem.WFX[2]=(RX_Buffer[6]) + (RX_Buffer[7]<<8);                
        KWLSlaveSendRplyPkt(1);
            //    Radio.StartCad();
//                KMem.WFY[0]=(RX_Buffer[4]<<8) + RX_Buffer[5];
//                KMem.WFY[1]=(RX_Buffer[6]<<8) + RX_Buffer[7];                
      }
      else
      {
        Radio.Rx( SLAVE_RX_TIMEOUT_VALUE );
                KwRunStat.runStep=RS_RECVING;
                KwRunStat.lastActTime = GetTick();
                KwRunStat.lastRecvtime = GetTick();
                KwStartRecv(1);
//        Radio.Rx( SLAVE_RX_TIMEOUT_VALUE );
//                KwRunStat.runStep=RS_RECVING;
//                KwRunStat.lastActTime = GetTick();
//                KwRunStat.lastRecvtime = GetTick();
                
      }       
    return 0;
@@ -568,7 +664,7 @@
    int len1=12;
    pstKLPacket p1 = (pstKLPacket) TX_Buffer;
    p1->STSign = enReqSign;
    p1->DstAddr = MixAddr(nRadioChannel,nRadioAddr);
    p1->DstAddr = MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr);
    p1->Func = 0x3;
    p1->Stat = (p1->Stat + 1) &0x03;   //0x00;
    p1->Data[0]=KMem.WFY[1]; 
@@ -619,7 +715,7 @@
    int len1=12;
    pstKLPacket p1 = (pstKLPacket) TX_Buffer;
    p1->STSign = enRplySign;
    p1->DstAddr = MixAddr(nRadioChannel,nRadioAddr);
    p1->DstAddr = MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr);
    p1->Func = 0x3;
    p1->Stat = (p1->Stat + 1) &0x03;   //0x00;
    p1->Data[0]=KMem.WFY[1]; 
@@ -661,8 +757,136 @@
}
int KWL_Process(int nChn)
{
    RadioStatus_t rs = SX126xGetStatus();
    KMem.WDT[1] = rs.Value;
    RadioError_t  er = SX126xGetDeviceErrors();
    KMem.WDT[2] = er.Value;
    KMem.WDT[3] = SX126xGetIrqStatus();
    KMem.WDT[4] = SX126xGetRssiInst();
    KMem.WDT[5] = GetRadioBusyPin();
        RadioState_t stat = Radio.GetStatus();
        KMem.WDT[32]=stat;
//        KMem.WDT[38]=Radio.Rssi(MODEM_FSK);
        if (WLCfg.bMaster){
            KWLMasterProc(nChn);
        }else            //slave
        {
                KWLSlaveProc(nChn);
        }
    if (KwRunStat.RunStat) KwRunStat.RunStat--;
    if (KwRunStat.ErrStat) KwRunStat.ErrStat--;
    return 0;
}
int KWLMasterProc(int nChn)
{
            RadioState_t stat = Radio.GetStatus();
            switch (KwRunStat.runStep){
                case RS_IDLE:
                    if (GetTick() - KwRunStat.lastSendtime > (WLCfg.nCycleTime) *10){
                        Radio.StartCad();
                        KwRunStat.runStep = RS_MASTER_CAD;
                        KwRunStat.lastActTime = GetTick();
                    }
                    break;
                case RS_MASTER_CAD:
                    if (GetTick() - KwRunStat.lastActTime > 15) {
                        KwRunStat.CADTimeOut++;
                        KwRunStat.runStep = RS_IDLE;
                        Radio.Standby();
                        KMem.WDT[17]++;
                        KwResetRf();
                    }
                    break;
                case RS_SENDING:
                    break;
                case RS_SENT:
                    break;
                case RS_RECVING:
                    if (GetTick() - KwRunStat.lastSendtime > (WLCfg.nCycleTime) *10){
                        KwRunStat.ErrStat=5000;
                        KwRunStat.LostPackets++;
                        KwRunStat.CtnLstPkts++;
                        KwRunStat.Tx_Power+=6;                //丢包, 增加发射功率
                        if (KwRunStat.Tx_Power > WLCfg.Tx_Power) {KwRunStat.Tx_Power= WLCfg.Tx_Power; }
                        SX126xSetRfTxPower(KwRunStat.Tx_Power);
                        if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
                //        KwRunStat.ErrStat=500;
                        if (KwRunStat.CtnLstPkts > 1) {        KwRunStat.ErrStat=2000;}
                            if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
                        if (KwRunStat.CtnLstPkts > 3) {        KwRunStat.ErrStat=5000;}
                            if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
                        if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
                            if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}
                        if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
                                KMem.WDT[51]++;
                                KwResetRf();
                            //    LoadKwConfig();
                            //    KWireLessInit(RadioEnableMaster,nRadioChannel);
                            //    Radio.Standby();
                            //    KWireLessStart();
                        }
                        KwRunStat.runStep = RS_IDLE;
                    }
                    break;
                case RS_RECVED:
                        KwRunStat.runStep = RS_IDLE;
                    break;
                default:
                    break;
            }
/*
            if (GetTick() - KwRunStat.lastSendtime > (WLCfg.nCycleTime) *10){
                if (!KwRunStat.bMasterRecved) {
                        KwRunStat.ErrStat=5000;
                        KwRunStat.LostPackets++;
                        KwRunStat.CtnLstPkts++;
                        if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
                //        KwRunStat.ErrStat=500;
                        if (KwRunStat.CtnLstPkts > 1) {        KwRunStat.ErrStat=2000;}
                            if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
                        if (KwRunStat.CtnLstPkts > 3) {        KwRunStat.ErrStat=5000;}
                            if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
                        if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
                            if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}
                    if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
                            KMem.WDT[51]++;
                        //    LoadKwConfig();
                        //    KWireLessInit(RadioEnableMaster,nRadioChannel);
                        //    Radio.Standby();
                        //    KWireLessStart();
                    }
                }
            }
*/
/*
                if (KwRunStat.curStat == 0 ){ //&& stat == RF_IDLE )    //200mS
//                    Radio.Standby();
                    KWireLessStart();
                }else {
                    Radio.Standby();
                    KWireLessStart();
                }
*/
    return 0;
}
@@ -683,8 +907,9 @@
                        KWireLessStart();
                    }
                }
                if (stat == RF_CAD && GetTick() - KwRunStat.lastRecvtime > ((1 )*10)){
                if (stat == RF_CAD && GetTick() - KwRunStat.lastRecvtime > ((2 )*10)){
                        KwRunStat.StepErr2++;
                        KwRunStat.CADTimeOut++;
                        KMem.WDT[50]++;
                        Radio.Standby();
                        KWireLessStart();
@@ -700,8 +925,7 @@
                }
                if (GetTick() - KwRunStat.lastRecvtime > 16000){
                        KMem.WDT[52]++;
                        LoadKwConfig();
                        KWireLessInit(RadioEnableMaster,nRadioChannel);
                        KwResetRf();
                        Radio.Standby();
                        KWireLessStart();
                    KwRunStat.lastRecvtime = GetTick();
@@ -709,57 +933,6 @@
    return 0;
}
int KWL_Process(int nChn)
{
        RadioState_t stat = Radio.GetStatus();
        KMem.WDT[32]=stat;
//        KMem.WDT[38]=Radio.Rssi(MODEM_FSK);
        if (RadioEnableMaster){
            if (GetTick() - KwRunStat.lastSendtime > CYCLE_TIME *10 && Radio.IsChannelFree(MODEM_LORA,KwRunStat.RF_Freq,-60,1)){
                if (!KwRunStat.bMasterRecved) {
                        KwRunStat.ErrStat=5000;
                        KwRunStat.LostPackets++;
                        KwRunStat.CtnLstPkts++;
                        if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
                //        KwRunStat.ErrStat=500;
                        if (KwRunStat.CtnLstPkts > 1) {        KwRunStat.ErrStat=2000;}
                            if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
                        if (KwRunStat.CtnLstPkts > 3) {        KwRunStat.ErrStat=5000;}
                            if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
                        if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
                            if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}
                    if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
                            KMem.WDT[51]++;
                            LoadKwConfig();
                            KWireLessInit(RadioEnableMaster,nRadioChannel);
                            Radio.Standby();
                        //    KWireLessStart();
                    }
                }
                if (KwRunStat.curStat == 0 ){ //&& stat == RF_IDLE )    //200mS
//                    Radio.Standby();
                    KWireLessStart();
                }else {
                    Radio.Standby();
                    KWireLessStart();
                }
            }
        }else            //slave
        {
                KWLSlaveProc(nChn);
        }
    if (KwRunStat.RunStat) KwRunStat.RunStat--;
    if (KwRunStat.ErrStat) KwRunStat.ErrStat--;
        KwRunStat.Tx_Power = WLCfg.Tx_Power;
        KwRunStat.LoraBandWidth = WLCfg.LoraBandWidth;
        KwRunStat.LoRaFactor = WLCfg.LoRaFactor;
        KwRunStat.LoRaCodingRate = WLCfg.LoRaCodingRate;
        KwRunStat.LoRaPreambleLen = WLCfg.LoRaPreambleLen;
    return 0;
}
Radio_LLCC68/Radio/KWireLess.h
@@ -56,7 +56,13 @@
    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]
@@ -67,8 +73,8 @@
    uchar bEnableEncrypt;
    uchar bEnableRelay;    
    uchar LoRaPreambleLen;            // 2 - 12
    uchar bAutoPower;                //自动功率
    uchar bAutoReSend;        //自动重发
    
//    uchar ;        //
//    uchar 
@@ -87,9 +93,12 @@
typedef enum tag_runstep{
    RS_IDEL,
    RS_IDLE,
    RS_MASTER_CAD,
    RS_SENDING,
    RS_SENT,
    RS_RECVING,
    RS_RECVED,
}enRunStep;
#pragma anon_unions
@@ -137,15 +146,20 @@
    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 ChnErr;                        //频道错误
    uint16_t PktErr;                        //包错误
    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;                    //微闪报警次数
@@ -217,8 +231,8 @@
int KWSendPkg(void* pPkg, int len);
int KWProcPkg(void);
extern uchar nRadioChannel;
extern uchar nRadioAddr;
//extern uchar nRadioChannel;
//extern uchar nRadioAddr;
extern stWLConfig WLCfg ;
extern stWLRunStat KwRunStat;
Radio_LLCC68/Radio/src/radio.c
@@ -410,7 +410,7 @@
PacketStatus_t RadioPktStatus;
uint8_t RadioRxPayload[32];
uint8_t RadioRxPayload[256];
bool IrqFired = false;
@@ -500,7 +500,7 @@
    RadioEvents = events;
    
    SX126xInit( RadioOnDioIrq );
    SX126xSetStandby( STDBY_RC );
    SX126xSetStandby( STDBY_XOSC );        //STDBY_RC
    SX126xSetRegulatorMode( USE_DCDC );
    
    SX126xSetBufferBaseAddress( 0x00, 0x00 );
@@ -560,8 +560,8 @@
bool RadioIsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime )
{
    bool status = true;
   // int16_t rssi = 0;
   // uint32_t carrierSenseTime = 0;
    int16_t rssi = 0;
    uint32_t carrierSenseTime = 0;
    RadioSetModem( modem );
@@ -577,15 +577,15 @@
     //Perform carrier sense for maxCarrierSenseTime
//    while( TimerGetElapsedTime( carrierSenseTime ) < maxCarrierSenseTime )
//    {
//        rssi = RadioRssi( modem );
        rssi = RadioRssi( modem );
//
//        if( rssi > rssiThresh )
//        {
//            status = false;
        if( rssi > rssiThresh )
        {
            status = false;
//            break;
//        }
        }
//    }
    RadioSleep( );
//    RadioSleep( );
    return status;
}
@@ -1067,7 +1067,7 @@
        IrqFired = false;
        uint16_t irqRegs = SX126xGetIrqStatus( );
        SX126xClearIrqStatus( IRQ_RADIO_ALL );
        SX126xClearIrqStatus( irqRegs);            //IRQ_RADIO_ALL );
        
        if( ( irqRegs & IRQ_TX_DONE ) == IRQ_TX_DONE )
        {
@@ -1090,7 +1090,7 @@
        {
            uint8_t size;
            SX126xGetPayload( RadioRxPayload, &size , 255 );
            SX126xGetPayload( RadioRxPayload, &size , 120 );
            SX126xGetPacketStatus( &RadioPktStatus );
            if( ( RadioEvents != NULL ) && ( RadioEvents->RxDone != NULL ) )
            {
Radio_LLCC68/Radio/src/sx126x-board.c
@@ -24,11 +24,11 @@
void SX126xReset( void )
{
    HAL_Delay_nMS( 10 );
    HAL_Delay_nMS( 1 );
    SetRadionRSTPin_0();
    HAL_Delay_nMS( 20 );
    HAL_Delay_nMS( 2 );
    SetRadionRSTPin_1();
    HAL_Delay_nMS( 10 );
    HAL_Delay_nMS( 1 );
}
void SX126xWaitOnBusy( void )
Radio_LLCC68/Src/BoardType.c
@@ -12,7 +12,7 @@
extern int Region$$Table$$Limit;
#define MAKE_VER(x,y) ((x<<8)|y)
#define APP_VER MAKE_VER(1,14)
#define APP_VER MAKE_VER(1,16)
const stAppInfoBlock AppInfoBlock __attribute__((at(APPINFOBLOCK_ADDR))) =
{
Radio_LLCC68/Src/main.c
@@ -157,6 +157,18 @@
        Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));        
        KMem.WDT[2]++;
    }
        if (Uart1RecvBuf1DataLen >0 && Uart1Stat.bPacketRecved)
        {
            int res1 = -1;
            res1 = ModBusSlaveParsePkg(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
            if (res1 !=0)
            {
                KLParsePacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
            }
            Uart1RecvBuf1DataLen=0;
            Uart1Stat.bPacketRecved=0;
            Uart1IdelTimer = 0;
        }
}
/*
@@ -637,26 +649,14 @@
//        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)
        {
            int res1 = -1;
            res1 = ModBusSlaveParsePkg(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
            if (res1 !=0)
            {
                KLParsePacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen);
            }
            Uart1RecvBuf1DataLen=0;
            Uart1Stat.bPacketRecved=0;
            Uart1IdelTimer = 0;
        }else {
#if (USART1_AUTO_BAUDRATE == 1)
            if (Uart1IdelTimer>300000) { // 超过60秒没有数据传输,重新进入自适应波特率状态
                LL_USART_EnableAutoBaudRate(USART1);
                LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
            }else {
                    Uart1IdelTimer++;
            }
        }
#endif
// if (bKBusSlave)    HAL_Delay(0);
/*
        if (!IsEmpty(&Uart1Stat.QRx))