LoRa節點開發:7、加入打印調試LoRaWAN

一般調試我們用兩種方法,斷點和打印,考慮到射頻和RTC,我們主要用打印調試的方法。

1、實現串口打印

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

PUTCHAR_PROTOTYPE
{
    HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);

    return ch;
}

2、分等級調試打印

typedef enum  
{ 
    LOG_LEVEL_OFF=0,
    LOG_LEVEL_INFO, 
    LOG_LEVEL_DEVELOP, 
    LOG_LEVEL_ALL, 
}LOG_LEVEL; 

#define log_develop(level,...) \
do { \
     if(level>=LOG_LEVEL_DEVELOP) \
     { \
	printf("\nFILE:%s LINE:%d,FUNC:%s  ", __FILE__, __LINE__ ,__func__); \
	printf(##__VA_ARGS__); \
     } \
} while (0) 

#define log_info(level, ...) \
 do { \
	if(level>=LOG_LEVEL_INFO) \
	printf(##__VA_ARGS__ ); \
 } while (0) 

#define log_debug(level, ...) \
do {  \
	if(level>=LOG_LEVEL_ALL) \
	printf(##__VA_ARGS__ ); \
} while (0)

3、定義調試等級

LOG_LEVEL loglevel;//定義打印等級,可以根據自己的實際設定,一般在調試階段我們設定爲LOG_LEVEL_ALL,即所有信息可見。

4、關鍵部分加入打印

以US915頻段說明,其餘頻段類似。

4.1、在RegionUS915TxConfig函數打印發送的參數

bool RegionUS915TxConfig( TxConfigParams_t* txConfig, int8_t* txPower, TimerTime_t* txTimeOnAir )
{
    RadioModems_t modem;
    int8_t phyDr = DataratesUS915[txConfig->Datarate];
    int8_t txPowerLimited = LimitTxPower( txConfig->TxPower, NvmCtx.Bands[NvmCtx.Channels[txConfig->Channel].Band].TxMaxPower, txConfig->Datarate, NvmCtx.ChannelsMask );
    uint32_t bandwidth = GetBandwidth( txConfig->Datarate );
    int8_t phyTxPower = 0;

    // Calculate physical TX power
    phyTxPower = RegionCommonComputeTxPower( txPowerLimited, txConfig->MaxEirp, txConfig->AntennaGain );

    // Setup the radio frequency
    Radio.SetChannel( NvmCtx.Channels[txConfig->Channel].Frequency );

    if( txConfig->Datarate == DR_7 )
    { // High Speed FSK channel
        modem = MODEM_FSK;
        Radio.SetTxConfig( modem, phyTxPower, 25000, bandwidth, phyDr * 1000, 0, 5, false, true, 0, 0, false, 4000 );
    }
    else
    {
        modem = MODEM_LORA;
        Radio.SetTxConfig( modem, phyTxPower, 0, bandwidth, phyDr, 1, 8, false, true, 0, 0, false, 4000 );
    }

    // Setup maximum payload lenght of the radio driver
    Radio.SetMaxPayloadLength( modem, txConfig->PktLen );
    // Get the time-on-air of the next tx frame
    *txTimeOnAir = Radio.TimeOnAir( modem, txConfig->PktLen );

    *txPower = txPowerLimited;
	
    log_info (loglevel, "[%.3fMHz, SF%d ,%ddBm ,%dbyte]",  
                                 Channels[txConfig->Channel].Frequency/1e6, 
				 phyDr, 
				 phyTxPower, 
			         txConfig->PktLen );
    return true;
}

4.2、在SendFrameOnChannel函數裏面打印發送的數據(加密之後的)

LoRaMacStatus_t SendFrameOnChannel( uint8_t channel )
{
    TxConfigParams_t txConfig;
    int8_t txPower = 0;

    txConfig.Channel = channel;
    txConfig.Datarate = MacCtx.NvmCtx->MacParams.ChannelsDatarate;
    txConfig.TxPower = MacCtx.NvmCtx->MacParams.ChannelsTxPower;
    txConfig.MaxEirp = MacCtx.NvmCtx->MacParams.MaxEirp;
    txConfig.AntennaGain = MacCtx.NvmCtx->MacParams.AntennaGain;
    txConfig.PktLen = MacCtx.PktBufferLen;

    RegionTxConfig( MacCtx.NvmCtx->Region, &txConfig, &txPower, &MacCtx.TxTimeOnAir );
	
    MacCtx.McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
    MacCtx.McpsConfirm.Datarate = MacCtx.NvmCtx->MacParams.ChannelsDatarate;
    MacCtx.McpsConfirm.TxPower = txPower;
    MacCtx.McpsConfirm.Channel = channel;
    // Store the time on air
    MacCtx.McpsConfirm.TxTimeOnAir = MacCtx.TxTimeOnAir;
    MacCtx.MlmeConfirm.TxTimeOnAir = MacCtx.TxTimeOnAir;

    if( LoRaMacClassBIsBeaconModeActive( ) == true )
    {
        // Currently, the Time-On-Air can only be computed when the radio is configured with
        // the TX configuration
        TimerTime_t collisionTime = LoRaMacClassBIsUplinkCollision( MacCtx.TxTimeOnAir );

        if( collisionTime > 0 )
        {
            return LORAMAC_STATUS_BUSY_UPLINK_COLLISION;
        }
    }
    if( MacCtx.NvmCtx->DeviceClass == CLASS_B )
    {
        // Stop slots for class b
        LoRaMacClassBStopRxSlots( );
    }

    LoRaMacClassBHaltBeaconing( );

    MacCtx.MacState |= LORAMAC_TX_RUNNING;
    if( MacCtx.NodeAckRequested == false )
    {
        MacCtx.ChannelsNbTransCounter++;
    }
    // Send now
    Radio.Send( MacCtx.PktBuffer, MacCtx.PktBufferLen );
	
    for( uint16_t i = 0; i < MacCtx.PktBufferLen; i++ )
    {
	log_info(loglevel," %02X",MacCtx.PktBuffer[i]);
    }
    log_info(loglevel,"\r\n");

    return LORAMAC_STATUS_OK;
}

4.3、在RegionUS915RxConfig函數裏面打印接收參數

bool RegionUS915RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
{
    RadioModems_t modem;
    int8_t dr = rxConfig->Datarate;
    uint8_t maxPayload = 0;
    int8_t phyDr = 0;
    uint32_t frequency = rxConfig->Frequency;

    if( Radio.GetStatus( ) != RF_IDLE )
    {
        return false;
    }

    if( rxConfig->RxSlot == RX_SLOT_WIN_1 )
    {
        // Apply window 1 frequency
        frequency = NvmCtx.Channels[rxConfig->Channel].Frequency;
        // Apply the alternative RX 1 window frequency, if it is available
        if( NvmCtx.Channels[rxConfig->Channel].Rx1Frequency != 0 )
        {
            frequency = NvmCtx.Channels[rxConfig->Channel].Rx1Frequency;
        }
    }

    // Read the physical datarate from the datarates table
    phyDr = DataratesUS915[dr];

    Radio.SetChannel( frequency );

    // Radio configuration
    if( dr == DR_7 )
    {
        modem = MODEM_FSK;
        Radio.SetRxConfig( modem, 50000, phyDr * 1000, 0, 83333, 5, rxConfig->WindowTimeout, false, 0, true, 0, 0, false, rxConfig->RxContinuous );
    }
    else
    {
        modem = MODEM_LORA;
        Radio.SetRxConfig( modem, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
    }

    if( rxConfig->RepeaterSupport == true )
    {
        maxPayload = MaxPayloadOfDatarateRepeaterEU868[dr];
    }
    else
    {
        maxPayload = MaxPayloadOfDatarateEU868[dr];
    }

    Radio.SetMaxPayloadLength( modem, maxPayload + LORA_MAC_FRMPAYLOAD_OVERHEAD );

    *datarate = (uint8_t) dr;
		
    log_info( loglevel,"Recv[%.3fMHz, SF%d %dK, st:%d]... \r\n", frequency/1e6, 
						 phyDr, 
    ( rxConfig->Bandwidth == 0 ) ? 125 : ( rxConfig->Bandwidth == 1 ) ? 250 : 500, 
						 rxConfig->WindowTimeout );

    return true;
}

4.4、在OnRadioRxDone函數裏面打印接收到的數據(未解密的數據)

static void OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
    RxDoneParams.LastRxDone = TimerGetCurrentTime( );
    RxDoneParams.Payload = payload;
    RxDoneParams.Size = size;
    RxDoneParams.Rssi = rssi;
    RxDoneParams.Snr = snr;

    log_info(loglevel,"\r\nRecv data:%d byte,rssi:%d,snr:%d,data[",size,rssi,snr);
    for(uint8_t i=0;i<size;i++)
    {
	log_info(loglevel," %02x",payload[i]);
    }
    log_info(loglevel,"]\r\n");
	
    LoRaMacRadioEvents.Events.RxDone = 1;

    if( ( MacCtx.MacCallbacks != NULL ) && ( MacCtx.MacCallbacks->MacProcessNotify != NULL ) )
    {
        MacCtx.MacCallbacks->MacProcessNotify( );
    }
}

5、調試

入網調試打印:

發送數據打印調試:

加入打印之後,可以方便的調試,還可以打印入網之後、發送超時、接收超時等,可以根據自己的需要添加。

關注公衆號:“物聯網思考”,獲取更多開發資料、經驗。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章