From eaf5d5b7aa6e4155924d97802f581b7de835f0d8 Mon Sep 17 00:00:00 2001 From: QuakeGod <quakegod@sina.com> Date: 星期三, 22 十一月 2023 14:06:17 +0800 Subject: [PATCH] fix Radio --- Radio_LLCC68/Radio/KWireLess.c | 356 +++++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 227 insertions(+), 129 deletions(-) diff --git a/Radio_LLCC68/Radio/KWireLess.c b/Radio_LLCC68/Radio/KWireLess.c index 5efee30..771a2ff 100644 --- a/Radio_LLCC68/Radio/KWireLess.c +++ b/Radio_LLCC68/Radio/KWireLess.c @@ -60,7 +60,7 @@ #endif -#define nChannelBandwidth 200000 // Hz +#define nChannelBandwidth 250000 // Hz #define TX_OUTPUT_POWER 22 // 22 dBm extern bool IrqFired; @@ -82,12 +82,12 @@ // 1: 250 kHz, // 2: 500 kHz, // 3: Reserved] -#define LORA_SPREADING_FACTOR 6 // [SF7..SF12] -#define LORA_CODINGRATE 1 // [1: 4/5, +#define LORA_SPREADING_FACTOR 7 // [SF7..SF12] +#define LORA_CODINGRATE 4 // [1: 4/5, // 2: 4/6, // 3: 4/7, // 4: 4/8] -#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx +#define LORA_PREAMBLE_LENGTH 4 // Same for Tx and Rx #define LORA_SYMBOL_TIMEOUT 0 // Symbols #define LORA_FIX_LENGTH_PAYLOAD_ON false #define LORA_IQ_INVERSION_ON false @@ -115,15 +115,16 @@ TX_TIMEOUT, }States_t; -#define MASTER_RX_TIMEOUT_VALUE 60 -#define SLAVE_RX_TIMEOUT_VALUE 200 +#define MASTER_RX_TIMEOUT_VALUE 80 //mS +#define SLAVE_RX_TIMEOUT_VALUE 250 //mS +#define CYCLE_TIME 200 //mS -#define BUFFER_SIZE 32 // Define the payload size here +#define BUFFER_SIZE 32 // Define the payload size here const uint8_t PingMsg[] = "PING"; const uint8_t PongMsg[] = "PONG"; -uint16_t BufferSize = BUFFER_SIZE; +//uint16_t BufferSize = BUFFER_SIZE; uint8_t TX_Buffer[BUFFER_SIZE]; uint8_t RX_Buffer[BUFFER_SIZE]; @@ -190,9 +191,14 @@ #error "Please define a frequency band in the compiler options." #endif SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS 0x30 -> SD_XOSC 0x20 -> SD_RC + +// SX126xSetCadParams(4,0,1,0,1); + + KwRunStat.runStep=RS_IDEL; return 0; } + int KWireLessStart(void) { if(RadioEnableMaster) @@ -204,8 +210,8 @@ Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); KwRunStat.runStep=RS_RECVING; - KwRunStat.lastActTime = GetuS(); - KwRunStat.lastRecvtime = GetuS(); + KwRunStat.lastActTime = GetTick(); + KwRunStat.lastRecvtime = GetTick(); } // while( 1 ) @@ -219,8 +225,8 @@ { KwRunStat.runStep=RS_IDEL; KMem.WDT[42]++; - KwRunStat.lastSenttime = GetuS(); - KwRunStat.lastAckTime = GetuS(); + KwRunStat.lastSenttime = GetTick(); + KwRunStat.lastAckTime = GetTick(); Radio.Standby(); if (RadioEnableMaster) { Radio.Rx( MASTER_RX_TIMEOUT_VALUE ); //杩涘叆鎺ユ敹 @@ -229,10 +235,10 @@ } KwRunStat.runStep=RS_RECVING; - KwRunStat.lastActTime = GetuS(); - KwRunStat.lastRecvtime = GetuS(); + KwRunStat.lastActTime = GetTick(); + KwRunStat.lastRecvtime = GetTick(); LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14); - int us2=GetuS(); + int us2=GetTick(); KMem.ScanTimeuS=us2-KMem.LastScanTime; KMem.LastScanTime = us2; if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;} @@ -243,23 +249,26 @@ void OnTxTimeout( void ) { KwRunStat.runStep=RS_IDEL; - KwRunStat.lastAckTime = GetuS(); + KwRunStat.lastAckTime = GetTick(); KwRunStat.TXErr++; Radio.Standby(); KMem.WDT[44]++; } bool bThisRxError=0; + void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) { + memcpy( RX_Buffer, payload, size ); Radio.Standby(); + if (bThisRxError) {bThisRxError=0;return;} - KwRunStat.lastRecvdtime=GetuS(); - KwRunStat.lastAckTime = GetuS(); + + KwRunStat.lastRecvdtime=GetTick(); + KwRunStat.lastAckTime = GetTick(); KwRunStat.curStat = 0; - KwRunStat.latancy = GetuS() - KwRunStat.lastSendtime; - BufferSize = size; - memcpy( RX_Buffer, payload, BufferSize ); + KwRunStat.latancy = GetTick() - KwRunStat.lastSendtime; + RssiValue = rssi; SnrValue = snr; @@ -272,18 +281,8 @@ KMem.WDT[43]++; KwRunStat.recvCount++; KwRunStat.CtnLstPkts=0; - bool CRC_OK =1; -/* - crc_value=RadioComputeCRC(RX_Buffer,size-2,CRC_TYPE_IBM);//璁$畻寰楀嚭瑕佸彂閫佹暟鎹寘CRC鍊� - if (RX_Buffer[size-2] != (crc_value&0xff) && RX_Buffer[size-1] != (crc_value >> 8)) - { - KwRunStat.CRCErr++; - KMem.ErrStat=500; - CRC_OK = 0; - }else { - CRC_OK = 1; - } -*/ + +//*/ unsigned char indicator1 = 128+RssiValue; unsigned char indicator2; const unsigned char maxrssi = 100; @@ -296,27 +295,151 @@ unsigned char indicator3 = (1 << indicator2) -1; if(RadioEnableMaster) { - if(memcmp(RX_Buffer,PongMsg,4)==0 && CRC_OK) + KWLMasterParsePkt(1,size); + //KWLMasterSendReqPkt(1); + } + else //slave + { + KWLSlaveParsePkt(1,size); + + } +} + +void OnRxTimeout( void ) +{ + KwRunStat.runStep=RS_IDEL; + + Radio.Standby(); + KwRunStat.lastErrTime = GetTick() - KwRunStat.lastAckTime; + KwRunStat.lastAckTime = GetTick(); + + KMem.WDT[45]++; + KwRunStat.LostPackets++; + KwRunStat.CtnLstPkts++; + if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;} +// KMem.ErrStat=500; + if (KwRunStat.CtnLstPkts > 1) { KMem.ErrStat=500;} + if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;} + if (KwRunStat.CtnLstPkts > 3) { KMem.ErrStat=5000;} + if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;} + if (KwRunStat.CtnLstPkts > 6) { KMem.ErrStat=5000; KMem.WFX[1]=0; } + if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;} + + if(RadioEnableMaster) + { + //KWLMasterSendReqPkt(1); + } + else + { + Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); + KwRunStat.runStep=RS_RECVING; + KwRunStat.lastActTime = GetTick(); + KwRunStat.lastRecvtime = GetTick(); + } +} + + +void OnRxError( void ) +{ + +// Radio.Standby(); + KwRunStat.RXErr++; + KMem.WDT[46]++; + bThisRxError=1; + KwRunStat.lastErrTime = GetTick() - KwRunStat.lastAckTime; + + if(RadioEnableMaster) + { +// SendPingMsg(); + } + else + { +// Radio.Rx( RX_TIMEOUT_VALUE ); + //KwRunStat.lastActTime = GetTick(); + KwRunStat.LostPackets++; + KwRunStat.CtnLstPkts++; + if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;} +// KMem.ErrStat=500; + if (KwRunStat.CtnLstPkts > 1) { KMem.ErrStat=500; } + if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;} + if (KwRunStat.CtnLstPkts > 3) { KMem.ErrStat=5000;} + if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;} + if (KwRunStat.CtnLstPkts > 6) { KMem.ErrStat=5000; KMem.WFX[1]=0; } + if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;} + } +} + +void OnCadDone( bool channelActivityDetected) +{ + KwRunStat.CADDoneCount++; + return; +} +int KWLMasterParsePkt(int nChn,int size) +{ + bool CRC_OK =1; +///* + crc_value=RadioComputeCRC(RX_Buffer,size-2,CRC_TYPE_IBM);//璁$畻寰楀嚭瑕佸彂閫佹暟鎹寘CRC鍊� + if (RX_Buffer[size-2] != (crc_value&0xff) && RX_Buffer[size-1] != (crc_value >> 8)) + { + KwRunStat.CRCErr++; + KMem.ErrStat=500; + CRC_OK = 0; + }else { + CRC_OK = 1; + } +//*/ + pstKLPacket p1 = (pstKLPacket) RX_Buffer; + if(CRC_OK && p1->STSign == enRplySign) //memcmp(RX_Buffer,PongMsg,4)==0 { - KWLMasterParsePkt(1); + LedToggle();//LED闂儊 + if (p1->Stat==0) { KwRunStat.tRSSI = RX_Buffer[10]; KwRunStat.tSNR = RX_Buffer[11]; - KMem.WDT[41]=(-RX_Buffer[10]) + ((-RX_Buffer[11])<<8); + }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) { + KwRunStat.targetRecvdCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24); + }else if (p1->Stat==3) { + } + + KMem.WDT[41]=(-KwRunStat.tRSSI) + ((-KwRunStat.tSNR)<<8); - LedToggle();//LED闂儊 + KMem.WFX[1]=(RX_Buffer[4]) + (RX_Buffer[5]<<8); // KMem.WFX[1]=(RX_Buffer[4]) + (indicator3<<8); KMem.WFX[2]=(RX_Buffer[6]) + (RX_Buffer[7]<<8); } - //KWLMasterSendReqPkt(1); - } - else //slave - { - KWLSlaveParsePkt(1); - if(memcmp(RX_Buffer,PingMsg,4)==0 && CRC_OK) + + return 0; +} +int KWLSlaveParsePkt(int nChn,int size) +{ + bool CRC_OK =1; + pstKLPacket p1 = (pstKLPacket) RX_Buffer; +///* + crc_value=RadioComputeCRC(RX_Buffer,size-2,CRC_TYPE_IBM);//璁$畻寰楀嚭瑕佸彂閫佹暟鎹寘CRC鍊� + if (RX_Buffer[size-2] != (crc_value&0xff) && RX_Buffer[size-1] != (crc_value >> 8)) + { + KwRunStat.CRCErr++; + KMem.ErrStat=500; + CRC_OK = 0; + }else { + CRC_OK = 1; + } +//*/ + if(CRC_OK && p1->STSign == enReqSign)// memcmp(RX_Buffer,PingMsg,4)==0 && ) { LedToggle();//LED闂儊 + if (p1->Stat==0) { + KwRunStat.tRSSI = RX_Buffer[10]; + KwRunStat.tSNR = RX_Buffer[11]; + }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) { + KwRunStat.targetRecvdCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24); + }else if (p1->Stat==3) { + } KMem.WFX[1]=(RX_Buffer[4]) + (RX_Buffer[5]<<8); // KMem.WFX[1]=(RX_Buffer[4]) + (indicator3<<8); @@ -331,78 +454,27 @@ { Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); KwRunStat.runStep=RS_RECVING; - KwRunStat.lastActTime = GetuS(); - KwRunStat.lastRecvtime = GetuS(); + KwRunStat.lastActTime = GetTick(); + KwRunStat.lastRecvtime = GetTick(); - } - } -} - -void OnRxTimeout( void ) -{ - KwRunStat.runStep=RS_IDEL; - - Radio.Standby(); - KwRunStat.lastAckTime = GetuS(); - - KMem.WDT[45]++; - KwRunStat.LostPackets++; - KwRunStat.CtnLstPkts++; - if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;} - KMem.ErrStat=500; - if (KwRunStat.CtnLstPkts > 3) { KMem.ErrStat=5000; } - if (KwRunStat.CtnLstPkts > 10) { KMem.ErrStat=5000; KMem.WFX[1]=0; } - - - if(RadioEnableMaster) - { - //KWLMasterSendReqPkt(1); - } - else - { - Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); - KwRunStat.runStep=RS_RECVING; - KwRunStat.lastActTime = GetuS(); - KwRunStat.lastRecvtime = GetuS(); - } -} - - -void OnRxError( void ) -{ - -// Radio.Standby(); - KwRunStat.RXErr++; - KMem.WDT[46]++; - bThisRxError=1; - - if(RadioEnableMaster) - { -// SendPingMsg(); - } - else - { -// Radio.Rx( RX_TIMEOUT_VALUE ); - //KwRunStat.lastActTime = GetuS(); - KwRunStat.LostPackets++; - KwRunStat.CtnLstPkts++; - if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;} - KMem.ErrStat=500; - if (KwRunStat.CtnLstPkts > 3) { KMem.ErrStat=5000; } - if (KwRunStat.CtnLstPkts > 10) { KMem.ErrStat=5000; KMem.WFX[1]=0; } - - } - -} - -void OnCadDone( bool channelActivityDetected) -{ - return; + } + return 0; } int KWLMasterSendReqPkt(int nChn) { int len1=12; + pstKLPacket p1 = (pstKLPacket) TX_Buffer; + p1->STSign = enReqSign; + p1->DstAddr = (nRadioChannel<<4) || 0x1; + p1->Func = 0x3; + p1->Stat = (p1->Stat + 1) &0x03; //0x00; + p1->Data[0]=KMem.WFY[1]; + p1->Data[1]=KMem.WFY[1]>>8; + p1->Data[2]=KMem.WFY[2]; + p1->Data[3]=KMem.WFY[2]>>8; + +/* TX_Buffer[0] = 'P'; TX_Buffer[1] = 'I'; TX_Buffer[2] = 'N'; @@ -411,7 +483,19 @@ TX_Buffer[5] = KMem.WFY[1]>>8; TX_Buffer[6] = KMem.WFY[2]; TX_Buffer[7] = KMem.WFY[2]>>8; +*/ + if (p1->Stat == 0) + { + TX_Buffer[10] = RssiValue; + TX_Buffer[11] = SnrValue; + }else if (p1->Stat == 1){ + memcpy(TX_Buffer+8,&KwRunStat.sentCount,4); + }else if (p1->Stat == 2) { + memcpy(TX_Buffer+8,&KwRunStat.recvCount,4); + }else if (p1->Stat == 3) { + + } crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//璁$畻寰楀嚭瑕佸彂閫佹暟鎹寘CRC鍊� TX_Buffer[len1]=crc_value; TX_Buffer[len1+1]=crc_value>>8; @@ -419,11 +503,11 @@ KMem.WDT[56]=crc_value; KwRunStat.sentCount++; - KwRunStat.cycleTime = GetuS()- KwRunStat.lastSendtime ; - KwRunStat.lastSendtime = GetuS(); + KwRunStat.cycleTime = GetTick()- KwRunStat.lastSendtime ; + KwRunStat.lastSendtime = GetTick(); Radio.Send( TX_Buffer, len1+2); KwRunStat.runStep=RS_SENDING; - KwRunStat.lastActTime = GetuS(); + KwRunStat.lastActTime = GetTick(); return 0; } @@ -431,6 +515,16 @@ int KWLSlaveSendRplyPkt(int nChn) { int len1=12; + pstKLPacket p1 = (pstKLPacket) TX_Buffer; + p1->STSign = enRplySign; + p1->DstAddr = (nRadioChannel<<4) || 0x1; + p1->Func = 0x3; + p1->Stat = (p1->Stat + 1) &0x03; //0x00; + p1->Data[0]=KMem.WFY[1]; + p1->Data[1]=KMem.WFY[1]>>8; + p1->Data[2]=KMem.WFY[2]; + p1->Data[3]=KMem.WFY[2]>>8; +/* TX_Buffer[0] = 'P'; TX_Buffer[1] = 'O'; TX_Buffer[2] = 'N'; @@ -439,19 +533,28 @@ TX_Buffer[5] = KMem.WFY[1]>>8; TX_Buffer[6] = KMem.WFY[2]; TX_Buffer[7] = KMem.WFY[2]>>8; - +*/ + if (p1->Stat == 0) + { TX_Buffer[10] = RssiValue; TX_Buffer[11] = SnrValue; - + }else if (p1->Stat == 1){ + memcpy(TX_Buffer+8,&KwRunStat.sentCount,4); + }else if (p1->Stat == 2) { + memcpy(TX_Buffer+8,&KwRunStat.recvCount,4); + }else if (p1->Stat == 3) { + + } + crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//璁$畻寰楀嚭瑕佸彂閫佹暟鎹寘CRC鍊� TX_Buffer[len1]=crc_value; TX_Buffer[len1+1]=crc_value>>8; Radio.Send( TX_Buffer, len1+2); KwRunStat.runStep=RS_SENDING; - KwRunStat.lastActTime = GetuS(); + KwRunStat.lastActTime = GetTick(); KwRunStat.sentCount++; - KwRunStat.lastSendtime = GetuS(); + KwRunStat.lastSendtime = GetTick(); return 0; } @@ -472,11 +575,11 @@ KMem.WDT[32]=stat; // KMem.WDT[38]=Radio.Rssi(MODEM_FSK); if (RadioEnableMaster){ - if (GetuS() - KwRunStat.lastSendtime >100000 && KwRunStat.curStat == 0 ) //&& stat == RF_IDLE ) + if (GetTick() - KwRunStat.lastSendtime >2000 && KwRunStat.curStat == 0 ) //&& stat == RF_IDLE ) //200mS { - KWireLessStart(); + KWireLessStart(); } - }else + }else //slave { if (stat == RF_IDLE){ KMem.WDT[48]++; @@ -484,13 +587,16 @@ KWireLessStart(); } if (stat == RF_RX_RUNNING ){ - if (GetuS() - KwRunStat.lastRecvtime > 300000){ + if (GetTick() - KwRunStat.lastRecvtime > ((CYCLE_TIME + 10 )*10)){ + KwRunStat.lastRecvtime = GetTick(); + KwRunStat.StepErr1++; KMem.WDT[49]++; Radio.Standby(); KWireLessStart(); } } - if (stat == RF_CAD && GetuS() - KwRunStat.lastRecvtime > 400000){ + if (stat == RF_CAD && GetTick() - KwRunStat.lastRecvtime > ((1 )*10)){ + KwRunStat.StepErr2++; KMem.WDT[50]++; Radio.Standby(); KWireLessStart(); @@ -500,14 +606,6 @@ return 0; } -int KWLMasterParsePkt(int nChn) -{ - return 0; -} -int KWLSlaveParsePkt(int nChn) -{ - return 0; -} -- Gitblit v1.9.1