end node on synchronous star LoRa network.

Dependencies:   SX127x sx12xx_hal TSL2561

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

This project for use with LoRaWAN_singlechannel_gateway project.

Alternately gateway running on raspberry pi can be used as gateway.

LoRaWAN on single radio channel

Network description is at gateway project page. Synchronous star network.

Hardware Support

This project supports SX1276 and SX1272, sx126x kit, sx126x shield, and sx128x 2.4GHz. The ST board B-L072Z-LRWAN1 is also supported (TypeABZ module). When B-L072Z-LRWAN1 target is selected, TARGET_DISCO_L072CZ_LRWAN1 is defined by tools, allowing correct radio driver configuration for this platform. Alternately, any mbed board that can use LoRa radio shield board should work, but NUCLEO boards are tested.

End-node Unique ID

DevEUI is created from CPU serial number. AppEUI and AppKey are declared as software constants.

End-node Configuration

Data rate definition LORAMAC_DEFAULT_DATARATE configured in LoRaMac-definitions.h. See gateway project page for configuration of gateway.
LoRaWAN addressing is configured in Comissioning.h; only OTA mode is functional.
Header file board/lora_config.h, selects application layer options (i.e. sensors) to be compiled in.

Serial Interface

Serial port operates at 115200bps.
Application layer single_us915_main.cpp User button triggers uplink (i.e. blue button on nucleo board), or jumper enables continuously sends repeated uplink packets. The MAC layer holds each uplink request until the allocated timeslot.

commandargumentsdescription
?-print available commands
. (period)-print status (DevEUI, DevAddr, etc)
ullength integerset payload length of test uplink packets

sensor demo

Selected grove sensors may be plugged into SX1272 shield.
To enable, edit lora_config.h to define SENSORS.

Sensor connections on SX1272MB2xAS:

D8 D9: buttonRX TX: (unused)A3 A4: Rotary Angle Sensor
D6 D7: RGB LEDSCL SDA: digital light sensorA1 A2: Rotary Angle Sensor

Digital input pin, state reported via uplink: PC8
Digital output pin, controlled via downlink: PC6
PWM out: PB_10

Jumper enables auto-repeated transmit: PC10 and PC12 on NUCLEO board, located on end of morpho headers nearby JP4.

Revision:
16:915815632c1f
Parent:
15:9023bf4cf168
Child:
17:3215f12051f9
--- a/mac/LoRaMac.cpp	Thu Jul 13 23:08:58 2017 +0000
+++ b/mac/LoRaMac.cpp	Thu Jul 27 17:53:37 2017 +0000
@@ -24,6 +24,8 @@
 #include "LoRaMac.h"
 #include "LoRaMacTest.h"
 
+#define TX_DEBUG
+
 #define PING_SLOT_RESOLUTION_us         30000
 
 /*!
@@ -122,7 +124,6 @@
  */
 static bool PublicNetwork;
 
-
 /*!
  * Buffer containing the data to be sent or received.
  */
@@ -159,7 +160,7 @@
  * IsPacketCounterFixed enables the MIC field tests by fixing the
  * UpLinkCounter value
  */
-static bool IsUpLinkCounterFixed = false;
+//static bool IsUpLinkCounterFixed = false;
 
 /*!
  * Used for test purposes. Disables the opening of the reception windows.
@@ -244,7 +245,7 @@
 #define BEACON_CHANNEL_BW           2   /* 2=500KHz */
 #define BEACON_CHANNEL_DR           LORAMAC_DEFAULT_DATARATE
 
-/* measured beacon duration (all at bw500)
+/* measured beacon duration (all at bw500, 6 byte fixed payload length)
  * latency assigned for correct rx-before-tx measurement */
 #if (LORAMAC_DEFAULT_DATARATE == DR_8)
     #define BEACON_RXDONE_LATENCY_us        6000
@@ -266,11 +267,9 @@
     #define BEACON_TOA_us                   6560
 #endif
 
-#define BEACON_GUARD_us         2500000     // pre-beacon start
+#define BEACON_GUARD_us         2000000     // pre-beacon start
 #define BEACON_RESERVED_us      2120000     // post-beacon start
 
-//#define BEACON_SYMBOL_TIMEOUT_UNLOCKED      100
-
 #else
     #error "Please define a frequency band in the compiler options."
 #endif
@@ -319,32 +318,6 @@
 //static TimerTime_t LoRaMacInitializationTime = 0;
 
 /*!
- * LoRaMac internal states
- */
-enum eLoRaMacState
-{
-    LORAMAC_IDLE          = 0x00000000,
-    LORAMAC_TX_RUNNING    = 0x00000001,
-    LORAMAC_RX            = 0x00000002,
-    LORAMAC_ACK_REQ       = 0x00000004,
-    LORAMAC_ACK_RETRY     = 0x00000008,
-    LORAMAC_TX_DELAYED    = 0x00000010,
-    LORAMAC_TX_CONFIG     = 0x00000020,
-    LORAMAC_RX_ABORT      = 0x00000040,
-    LORAMAC_TX_SCHED      = 0x00000080,
-};
-
-/*!
- * LoRaMac internal state
- */
-uint32_t LoRaMacState = LORAMAC_IDLE;
-
-/*!
- * LoRaMac timer used to check the LoRaMacState (runs every second)
- */
-static LowPowerTimeout MacStateCheckTimer;
-
-/*!
  * LoRaMac upper layer event functions
  */
 static LoRaMacPrimitives_t *LoRaMacPrimitives;
@@ -362,7 +335,6 @@
 /*!
  * LoRaMac duty cycle delayed Tx timer
  */
-static LowPowerTimeout TxDelayedTimer;
 LowPowerTimeout tx_timeout;
 
 
@@ -380,19 +352,17 @@
     BEACON_STATE_LOCKED_,
 } beacon_state_e;
 
-struct beacon_struct {
+static struct beacon_struct {
     int rx_precession_us; // positive: rxing before tx start, negative: rxing after tx start
     unsigned int RxBeaconSetupAt_us;
     unsigned int LastBeaconRx_us;   // updated only at beacon reception
-    unsigned int LastBeaconStart_us;    // updated at beacon reception and beacon reception timeout
-    unsigned int next_beacon_expected_us;
     int last_BeaconRxTimerError_us;
     int known_working_BeaconRxTimerError_us;
 
     float symbol_period_secs;
 
     uint8_t Precess_symbols;    // how many symbols we want to start receiver before expected transmitter
-    uint8_t SymbolTimeout;
+    uint16_t SymbolTimeout;
     float SymbolTimeout_sec;
     uint8_t num_missed;
 
@@ -401,9 +371,11 @@
     uint16_t tx_slot_offset;
     uint16_t periodicity_slots;
 
-    LowPowerTimeout timeout;
-    bool have_beacon;
+    LowPowerTimeout timeout_rx;
+    LowPowerTimeout timeout_guard;
+    bool guard;
 } BeaconCtx;
+bool expecting_beacon;
 
 /*!
  * Rx window parameters
@@ -421,7 +393,7 @@
  */
 static RxConfigParams_t RxWindowsParam;
 
-bool expecting_beacon;
+
 /*!
  * Acknowledge timeout timer. Used for packet retransmissions.
  */
@@ -496,16 +468,6 @@
  */
 static void OnRadioRxTimeout( void );
 
-/*!
- * \brief Function executed on Resend Frame timer event.
- */
-static void OnMacStateCheckTimerEvent( void );
-
-/*!
- * \brief Function executed on duty cycle delayed Tx  timer event
- */
-static void OnTxDelayedTimerEvent( void );
-
 static void OnRxWindowTimerEvent( void );
 
 /*!
@@ -527,15 +489,6 @@
 static bool RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous );
 
 /*!
- * \brief Verifies if the RX window 2 frequency is in range
- *
- * \param [IN] freq window channel frequency
- *
- * \retval status  Function status [1: OK, 0: Frequency not applicable]
- */
-//static bool Rx2FreqInRange( uint32_t freq );
-
-/*!
  * \brief Adds a new MAC command to be sent.
  *
  * \Remark MAC layer internal function
@@ -660,7 +613,11 @@
 void
 loramac_print_status()
 {
-    isr_printf("LoRaMacState:%lx DR%u=sf%u\r\n", LoRaMacState, LoRaMacParams.ChannelsDatarate_fixed, Datarates[LoRaMacParams.ChannelsDatarate_fixed]);
+    isr_printf("DR%u=sf%u guard:%d\r\n",
+        LoRaMacParams.ChannelsDatarate_fixed,
+        Datarates[LoRaMacParams.ChannelsDatarate_fixed],
+        BeaconCtx.guard
+    );
 }
 
 /*
@@ -715,27 +672,11 @@
 
 static void OnRadioTxDone( unsigned int tx_done_us )
 {
-    Radio.Sleep( );
-    TxDone_us = tx_done_us;
-
     // Setup timers
-    if( IsRxWindowsEnabled == true )
+    if (IsRxWindowsEnabled)
     {
-        if (BeaconCtx.state != BEACON_STATE_NONE) {
-            int us_to_beacon, us_since_beacon_start = lp_timer.read_us() - BeaconCtx.LastBeaconStart_us;
-            us_to_beacon = BEACON_INTERVAL_us - us_since_beacon_start;
-    
-            if (us_to_beacon < 500000) {
-                /* would step on beacon reception */
-                isr_printf("<rxwinT %d>\r\n", us_to_beacon);
-                McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
-                MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
-                LoRaMacFlags.Bits.MacDone = 1;
-                return;
-            }
-        }
         rx_timeout.attach_us(&OnRxWindowTimerEvent, RxWindowDelay_us);
-        if( NodeAckRequested == true )
+        if (NodeAckRequested)
         {
             AckTimeoutTimer.attach_us(&OnAckTimeoutTimerEvent, (RxWindowDelay_us/1000) + ACK_TIMEOUT_us + randr(-ACK_TIMEOUT_RND_us, ACK_TIMEOUT_RND_us));
         }
@@ -751,14 +692,14 @@
         }
         LoRaMacFlags.Bits.MacDone = 1;
     }
+    Radio.Sleep( );   
+    TxDone_us = tx_done_us; 
 
-    // Update last tx done time for the current channel
-    //Bands[Channels[Channel].Band].last_tx_done_us = tx_done_us;
     // Update Aggregated last tx done time
     AggregatedLastTxDoneTime_us = tx_done_us;
     // Update Backoff
 
-    if( NodeAckRequested == false )
+    if (!NodeAckRequested)
     {
         McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
         ChannelsNbRepCounter++;
@@ -766,30 +707,46 @@
     
     MlmeIndication.MlmeIndication = MLME_TXDONE;
     MlmeIndication.Status = LORAMAC_EVENT_INFO_STATUS_OK;
-    LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );    
+    LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );  
+}
+
+
+static void application_callbacks()
+{
+    if (LoRaMacFlags.Bits.McpsInd) {
+        LoRaMacFlags.Bits.McpsInd = 0;
+        LoRaMacPrimitives->MacMcpsIndication( &McpsIndication );
+    }
+    
+    if (LoRaMacFlags.Bits.McpsReq) {
+        LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm );
+        LoRaMacFlags.Bits.McpsReq = 0;
+    }    
 }
 
 static void PrepareRxDoneAbort( void )
 {
-    LoRaMacState |= LORAMAC_RX_ABORT;
-
-    if( NodeAckRequested )
+    if (NodeAckRequested)
     {
         OnAckTimeoutTimerEvent( );
     }
 
     LoRaMacFlags.Bits.McpsInd = 1;
     LoRaMacFlags.Bits.MacDone = 1;
-
-    // Trig OnMacCheckTimerEvent call as soon as possible
-    OnMacStateCheckTimerEvent();
+    
+    application_callbacks();
 }
 
-int us_at_sched;
 void send_callback()
-{
-    LoRaMacState &= ~LORAMAC_TX_SCHED;
-                    
+{      
+    if (BeaconCtx.guard)
+        return;
+    
+    tx_timeout.attach_us(&send_callback, BeaconCtx.periodicity_slots * 30000);
+                
+    if (!LoRaMacFlags.Bits.pending_tx)
+        return;
+        
     Radio.SetTxConfig(
         /* RadioModems_t modem */ MODEM_LORA,
         /* int8_t power */ TxPowers[LoRaMacParams.ChannelsTxPower],
@@ -805,24 +762,18 @@
         /* bool iqInverted */ false,
         /* uint32_t timeout */ 3e3
     );
-    Radio.Send( LoRaMacBuffer, LoRaMacBufferPktLen );
-
-    LoRaMacState |= LORAMAC_TX_RUNNING;
-
-    // Starts the MAC layer status check timer
-    MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
-    
-    /*
-    unsigned int now_us = lp_timer.read_us();
-    int us_since_beacon_start = now_us - BeaconCtx.LastBeaconStart_us;
-    int now_slot = (us_since_beacon_start - BEACON_RESERVED_us) / 30000;
-    isr_printf("send now slot:%u\r\n", now_slot);
-    */
+    Radio.Send( LoRaMacBuffer, LoRaMacBufferPktLen );        
+        
+    LoRaMacFlags.Bits.pending_tx = 0;
 }
 
 void OnRxBeaconSetup()
 {
-    BeaconCtx.RxBeaconSetupAt_us = lp_timer.read_us();
+    unsigned int prevRxBeaconSetupAt_us;
+    unsigned now_us = lp_timer.read_us();
+    
+    prevRxBeaconSetupAt_us = BeaconCtx.RxBeaconSetupAt_us;
+    BeaconCtx.RxBeaconSetupAt_us = now_us;
     expecting_beacon = true;
     
     Radio.SetRxConfig(
@@ -843,15 +794,31 @@
     );
 
     Radio.Rx(2000);
+    
+    unsigned int us_since_last_beacon_start = BeaconCtx.RxBeaconSetupAt_us - prevRxBeaconSetupAt_us;
+    isr_printf("OnRxBeaconSetup() %u since-last:%u\r\n", BeaconCtx.SymbolTimeout, us_since_last_beacon_start);
+}
+
+void guard_callback()
+{
+    static unsigned prev_guard_at;
+    unsigned now_us = lp_timer.read_us();
+    
+    /* occurs BEACON_GUARD_us prior to OnRxBeaconSetup() */
+    BeaconCtx.guard = true;
+    isr_printf("guard since-last:%u\r\n", now_us - prev_guard_at);
+    prev_guard_at = now_us;
 }
 
 static void set_beacon_symbol_timeout(float secs)
 {
+    isr_printf("symTo:%.1f ", secs);
     BeaconCtx.SymbolTimeout = secs / BeaconCtx.symbol_period_secs;
     if (BeaconCtx.SymbolTimeout < (BEACON_MIN_SYMBOL_TIMEOUT+BeaconCtx.Precess_symbols)) {
         BeaconCtx.SymbolTimeout = BEACON_MIN_SYMBOL_TIMEOUT+BeaconCtx.Precess_symbols;  
     }
     BeaconCtx.SymbolTimeout_sec = BeaconCtx.SymbolTimeout * BeaconCtx.symbol_period_secs;
+    isr_printf("%u\r\n", BeaconCtx.SymbolTimeout);
 }
 
 static uint16_t beacon_crc( uint8_t *buffer, uint16_t length )
@@ -888,10 +855,10 @@
     int32_t compensation = 0;
     unsigned ThisBeaconRx_us = rx_us - (BEACON_TOA_us + BEACON_RXDONE_LATENCY_us);
 
+    BeaconCtx.guard = false;
     BeaconCtx.rx_precession_us = ThisBeaconRx_us - BeaconCtx.RxBeaconSetupAt_us;
     if (BeaconCtx.state != BEACON_STATE_FIRST_ACQ) {
         unsigned int us_since_last = ThisBeaconRx_us - BeaconCtx.LastBeaconRx_us;
-        //unsigned int intervals_since_last = (ThisBeaconRx_us / BEACON_INTERVAL_us) - (BeaconCtx.LastBeaconRx_us / BEACON_INTERVAL_us);
         unsigned int intervals_since_last = us_since_last / BEACON_INTERVAL_us;
         BeaconCtx.known_working_BeaconRxTimerError_us = BeaconCtx.last_BeaconRxTimerError_us;
         /* get average of error history */
@@ -925,7 +892,6 @@
     isr_printf("err%d=%u-%u ", BeaconCtx.last_BeaconRxTimerError_us, ThisBeaconRx_us, BeaconCtx.LastBeaconRx_us);
     isr_printf(" rx-before-tx:%d ", BeaconCtx.rx_precession_us);
     BeaconCtx.LastBeaconRx_us = ThisBeaconRx_us;
-    BeaconCtx.LastBeaconStart_us = BeaconCtx.LastBeaconRx_us;
 
     if (BeaconCtx.state == BEACON_STATE_LOCKED_) {
         if (compensate_precession) {
@@ -934,12 +900,19 @@
         }
     }
 
-    isr_printf("\r\n");
-
-    BeaconCtx.next_beacon_expected_us = BEACON_INTERVAL_us + compensation;
+    unsigned int next_beacon_expected_us = BEACON_INTERVAL_us + compensation;
     unsigned now_us = lp_timer.read_us();
     unsigned us_since_rx_setup = now_us - BeaconCtx.RxBeaconSetupAt_us;
-    BeaconCtx.timeout.attach_us(&OnRxBeaconSetup, BeaconCtx.next_beacon_expected_us - us_since_rx_setup);
+    unsigned timeout_us = next_beacon_expected_us - us_since_rx_setup;
+    BeaconCtx.timeout_rx.attach_us(&OnRxBeaconSetup, timeout_us);
+    BeaconCtx.timeout_guard.attach_us(&guard_callback, timeout_us - BEACON_GUARD_us);
+    
+    /***************************************/
+    unsigned us_since_beacon_start = now_us - ThisBeaconRx_us;
+    timeout_us = BEACON_RESERVED_us + (BeaconCtx.tx_slot_offset * 30000) - us_since_beacon_start;
+    tx_timeout.attach_us(&send_callback, timeout_us);
+    /***************************************/    
+    isr_printf(" nextTx:%u\r\n", timeout_us);
 
     if (BeaconCtx.num_missed > 0) {
         /* restore rx symbol timeout */
@@ -947,12 +920,10 @@
     }
 
     BeaconCtx.num_missed = 0;
-    BeaconCtx.have_beacon = true;
 
     MlmeIndication.MlmeIndication = MLME_BEACON;
     MlmeIndication.Status = LORAMAC_EVENT_INFO_STATUS_BEACON_LOCKED;
     LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );
-    //LoRaMacFlags.Bits.MlmeInd = 1;
 
     /* check beacon payload */
     uint16_t calc_crc = beacon_crc(payload, 4);
@@ -974,8 +945,7 @@
             McpsIndication.Buffer = LoRaMacRxPayload;
             McpsIndication.BufferSize = 4;
             McpsIndication.RxData = true;
-            LoRaMacFlags.Bits.McpsInd = 1;
-            MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, 50000);
+            LoRaMacPrimitives->MacMcpsIndication( &McpsIndication );
         }
     } else
         isr_printf("calc_crc:%04x rx_crc:%04x\r\n", calc_crc, rx_crc);
@@ -1083,7 +1053,7 @@
     switch( macHdr.Bits.MType )
     {
         case FRAME_TYPE_JOIN_ACCEPT:
-            if( IsLoRaMacNetworkJoined == true )
+            if (IsLoRaMacNetworkJoined)
             {
                 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_JOIN_ACCEPT;
                 PrepareRxDoneAbort( );
@@ -1103,10 +1073,6 @@
             if( micRx == mic )
             {
                 LoRaMacJoinComputeSKeys( LoRaMacAppKey, LoRaMacRxPayload + 1, LoRaMacDevNonce, LoRaMacNwkSKey, LoRaMacAppSKey );
-                isr_printf("decr%u:", size);
-                for (mic = 0; mic < size; mic++)
-                    isr_printf("%02x ", LoRaMacRxPayload[mic]);
-                isr_printf("\r\n");
 
                 LoRaMacNetID = ( uint32_t )LoRaMacRxPayload[4];
                 LoRaMacNetID |= ( ( uint32_t )LoRaMacRxPayload[5] << 8 );
@@ -1130,16 +1096,32 @@
                 beaconTimingDelay |= LoRaMacRxPayload[14] << 8;
 
                 isr_printf("%lx slots:%x (rxdelay %lu)", LoRaMacDevAddr, beaconTimingDelay, LoRaMacParams.ReceiveDelay_us);
-                // how long from tx-done of join-request is beacon going to occur at
-                uint32_t us_to_beacon = ( PING_SLOT_RESOLUTION_us * beaconTimingDelay ); 
-                // get time elapsed since last tx-done
                 unsigned int now_us = lp_timer.read_us();
                 unsigned int us_since_TxDone = now_us - TxDone_us;
-                BeaconCtx.timeout.attach_us(&OnRxBeaconSetup, us_to_beacon - us_since_TxDone);
-                BeaconCtx.next_beacon_expected_us = now_us + us_to_beacon;
-                isr_printf("us_to_beacon:%lu, since_tx_done:%u\r\n", us_to_beacon, us_since_TxDone);
-                BeaconCtx.have_beacon = false;
+                unsigned us_to_beacon = ( PING_SLOT_RESOLUTION_us * beaconTimingDelay );
+                unsigned timeout_us = us_to_beacon - us_since_TxDone;
+                BeaconCtx.timeout_rx.attach_us(&OnRxBeaconSetup, timeout_us);
+                BeaconCtx.timeout_guard.attach_us(&guard_callback, timeout_us - BEACON_GUARD_us);
+                
+                BeaconCtx.tx_slot_offset = LoRaMacRxPayload[15];
+                BeaconCtx.tx_slot_offset |= LoRaMacRxPayload[16] << 8;
+                BeaconCtx.periodicity_slots = LoRaMacRxPayload[17];
+                BeaconCtx.periodicity_slots |= LoRaMacRxPayload[18] << 8;                     
+                
+                BeaconCtx.LastBeaconRx_us = (us_to_beacon + TxDone_us) - BEACON_INTERVAL_us;
+                unsigned us_since_beacon_start = now_us - BeaconCtx.LastBeaconRx_us;
+                int now_slot = (us_since_beacon_start - BEACON_RESERVED_us) / 30000;
+                int use_slot = BeaconCtx.tx_slot_offset;
+                while (use_slot < now_slot)
+                    use_slot += BeaconCtx.periodicity_slots;                
+                int slots_now_to_next = use_slot - now_slot;
+                timeout_us = slots_now_to_next * 30000;
+                tx_timeout.attach_us(&send_callback, timeout_us);
+
+                isr_printf(" now_slot:%d use_slot:%d timeout_us:%u\r\n", now_slot, use_slot, timeout_us);
+                isr_printf("us_to_beacon:%u, since_txDone:%u\r\n", us_to_beacon, us_since_TxDone);
                 BeaconCtx.state = BEACON_STATE_FIRST_ACQ;
+                BeaconCtx.guard = false;
                 BeaconCtx.num_missed = 0;
                 BeaconCtx.rx_precession_us = 0;
                 BeaconCtx.last_BeaconRxTimerError_us = -PPM_100_BEACON_INTERVAL;
@@ -1150,13 +1132,6 @@
                 BeaconCtx.SymbolTimeout_sec = 0.1 + (BEACON_MIN_SYMBOL_TIMEOUT * BeaconCtx.symbol_period_secs);
                 BeaconCtx.SymbolTimeout = BeaconCtx.SymbolTimeout_sec / BeaconCtx.symbol_period_secs;
 
-                isr_printf("sp:%f sto:%d\r\n", BeaconCtx.symbol_period_secs, BeaconCtx.SymbolTimeout);
-                BeaconCtx.tx_slot_offset = LoRaMacRxPayload[15];
-                BeaconCtx.tx_slot_offset |= LoRaMacRxPayload[16] << 8;
-                BeaconCtx.periodicity_slots = LoRaMacRxPayload[17];
-                BeaconCtx.periodicity_slots |= LoRaMacRxPayload[18] << 8;
-                isr_printf("tso:%u, PS:%u\r\n", BeaconCtx.tx_slot_offset, BeaconCtx.periodicity_slots);
-
                 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
                 IsLoRaMacNetworkJoined = true;
                 LoRaMacParams.ChannelsDatarate_fixed = LoRaMacParamsDefaults.ChannelsDatarate_fixed;
@@ -1167,6 +1142,8 @@
                 isr_printf("join-mic-fail\r\n");
                 JoinRequestTrials = MaxJoinRequestTrials; // stop trying
             }
+            LoRaMacPrimitives->MacMlmeConfirm( &MlmeConfirm );
+            LoRaMacFlags.Bits.MlmeReq = 0;  // MacMlmeConfirm() called
             break;
         case FRAME_TYPE_DATA_CONFIRMED_DOWN:
         case FRAME_TYPE_DATA_UNCONFIRMED_DOWN:
@@ -1443,10 +1420,12 @@
     }
     LoRaMacFlags.Bits.MacDone = 1;
 
-    /* run mac check quickly, but not immedately (for isr_printf) */
-    MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, 50000);
+    application_callbacks();
+
 } // ..OnRadioRxDone();
 
+
+
 static void OnRadioTxTimeout( void )
 {
     Radio.Sleep( );
@@ -1460,7 +1439,7 @@
 {
     Radio.Sleep( );
 
-    if( NodeAckRequested == true )
+    if (NodeAckRequested)
     {
         McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_ERROR;
     }
@@ -1472,223 +1451,115 @@
     }
 }
 
+static void
+join_send()
+{
+    if (JoinRequestTrials < MaxJoinRequestTrials) {
+        LoRaMacHeader_t macHdr;
+        LoRaMacFrameCtrl_t fCtrl;
+        
+        if (++Channel == LORA_MAX_NB_CHANNELS)
+            Channel = 0;
+        isr_printf("<join-ch%u>", Channel);
+    
+        macHdr.Value = 0;
+        macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ;
+    
+        fCtrl.Value = 0;
+        fCtrl.Bits.Adr = 0;
+    
+        /* In case of join request retransmissions, the stack must prepare
+         * the frame again, because the network server keeps track of the random
+         * LoRaMacDevNonce values to prevent reply attacks. */
+        PrepareFrame( &macHdr, &fCtrl, 0, NULL, 0 );
+        
+        ScheduleTx();
+    } else {
+        MlmeConfirm.MlmeRequest = MLME_JOIN;
+        MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL;
+        LoRaMacPrimitives->MacMlmeConfirm( &MlmeConfirm );
+    }
+}
+
 static void OnRadioRxTimeout( void )
 {
+    static unsigned LastBeaconRx_us;
+    
     Radio.Sleep( );
 
     if (expecting_beacon) {
-        /* generate simulated last beacon start */
-        BeaconCtx.LastBeaconStart_us += BEACON_INTERVAL_us + BeaconCtx.known_working_BeaconRxTimerError_us;
-
-        BeaconCtx.next_beacon_expected_us = BEACON_INTERVAL_us;
-        BeaconCtx.num_missed++;
-        unsigned now_us = lp_timer.read_us();
+        unsigned next_beacon_expected_us = BEACON_INTERVAL_us;
         if (BeaconCtx.state == BEACON_STATE_FIRST_ACQ) {
-            BeaconCtx.next_beacon_expected_us -= 1000000;
-            set_beacon_symbol_timeout(2.000);
+            next_beacon_expected_us -= 1000000;
+            set_beacon_symbol_timeout(1.000);
         } else {
-            BeaconCtx.next_beacon_expected_us += BeaconCtx.known_working_BeaconRxTimerError_us;
+            next_beacon_expected_us += BeaconCtx.known_working_BeaconRxTimerError_us;
             // for measurement resolution and temperature drift while missing beacons:
-            BeaconCtx.next_beacon_expected_us -= 5000;
-            set_beacon_symbol_timeout(BeaconCtx.SymbolTimeout_sec + 0.010);
+            next_beacon_expected_us -= 10000;
+            set_beacon_symbol_timeout(BeaconCtx.SymbolTimeout_sec + 0.025);
         }
+        unsigned now_us = lp_timer.read_us();
         unsigned us_since_rx_setup = now_us - BeaconCtx.RxBeaconSetupAt_us;
-        BeaconCtx.timeout.attach_us(&OnRxBeaconSetup, BeaconCtx.next_beacon_expected_us - us_since_rx_setup);
+        unsigned b_timeout_us = next_beacon_expected_us - us_since_rx_setup;
+        BeaconCtx.timeout_rx.attach_us(&OnRxBeaconSetup, b_timeout_us);
+        BeaconCtx.timeout_guard.attach_us(&guard_callback, b_timeout_us - BEACON_GUARD_us);
+
+        if (BeaconCtx.num_missed == 0)  /* first missed beacon, init our local LastBeaconRx_us */
+            LastBeaconRx_us = BeaconCtx.LastBeaconRx_us;
+
+        BeaconCtx.num_missed++;
+        /***************************************/
+        unsigned fake_ThisBeaconRx_us = LastBeaconRx_us + BEACON_INTERVAL_us + BeaconCtx.known_working_BeaconRxTimerError_us;
+        unsigned us_since_beacon_start = now_us - fake_ThisBeaconRx_us;
+        unsigned timeout_us = BEACON_RESERVED_us + (BeaconCtx.tx_slot_offset * 30000) - us_since_beacon_start;
+        tx_timeout.attach_us(&send_callback, timeout_us);
+        isr_printf("fake %u\r\n", fake_ThisBeaconRx_us - LastBeaconRx_us);
+        LastBeaconRx_us = fake_ThisBeaconRx_us; // update our local for next missed beacon
+        /***************************************/         
 
 #ifdef DEBUG_GWTX_JUMPER
         isr_printf("rx-before-gwtx:%d ", gwtx_rise_us - BeaconCtx.RxBeaconSetupAt_us);
-#endif /* DEBUG_GWTX_JUMPER */        
-        isr_printf("beacon-rx-timeout %u %u next in %uus (rxing for %u)\r\n", BeaconCtx.num_missed, BeaconCtx.SymbolTimeout, BeaconCtx.next_beacon_expected_us, us_since_rx_setup);
+#endif /* DEBUG_GWTX_JUMPER */    
+            
+       isr_printf("beacon-rx-timeout %u %u next in %uus (rxing for %u) nextTx:%u\r\n", BeaconCtx.num_missed, BeaconCtx.SymbolTimeout, b_timeout_us, us_since_rx_setup, timeout_us);
 
         MlmeIndication.MlmeIndication = MLME_BEACON;
         MlmeIndication.Status = LORAMAC_EVENT_INFO_STATUS_BEACON_LOST;
         LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );
-        //LoRaMacFlags.Bits.MlmeInd = 1;
 
+        BeaconCtx.guard = false;
         expecting_beacon = false;
+    } else {
+        if (LoRaMacFlags.Bits.MlmeReq && ( MlmeConfirm.MlmeRequest == MLME_JOIN ))
+             tx_timeout.attach_us(&join_send, (JoinRequestTrials*20000) + randr(0, 70000));
     }
 
-    if( NodeAckRequested == true )
+    if (NodeAckRequested)
     {
         McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
     }
     MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
     LoRaMacFlags.Bits.MacDone = 1;
-
-    /* run mac check quickly, but not immedately (for isr_printf) */
-    MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, 50000 + randr(0, 50000));
+    
+    application_callbacks();
 } // ..OnRadioRxTimeout();
 
-static void OnMacStateCheckTimerEvent( void )
-{
-    bool txTimeout = false;
-
-    if( LoRaMacFlags.Bits.MacDone == 1 )
-    {
-        if( ( LoRaMacState & LORAMAC_RX_ABORT ) == LORAMAC_RX_ABORT )
-        {
-            LoRaMacState &= ~LORAMAC_RX_ABORT;
-            LoRaMacState &= ~LORAMAC_TX_RUNNING;
-        }
-
-        if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
-        {
-            if( ( McpsConfirm.Status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) ||
-                ( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) )
-            {
-                // Stop transmit cycle due to tx timeout.
-                LoRaMacState &= ~LORAMAC_TX_RUNNING;
-                MacCommandsBufferIndex = 0;
-                McpsConfirm.AckReceived = false;
-                McpsConfirm.TxTimeOnAir = 0;
-                txTimeout = true;
-            }
-        }
-
-        if( ( NodeAckRequested == false ) && ( txTimeout == false ) )
-        {
-            if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
-            {
-                if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) && ( MlmeConfirm.MlmeRequest == MLME_JOIN ) )
-                { // Procedure for the join request
-
-                    if( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_OK )
-                    { // Node joined successfully
-                        isr_printf("mac-check-join ok ");
-                        UpLinkCounter = 0;
-                        ChannelsNbRepCounter = 0;
-                        LoRaMacState &= ~LORAMAC_TX_RUNNING;
-                    }
-                    else
-                    {
-                        isr_printf("jrt:%u, maxjrt:%u\r\n", JoinRequestTrials, MaxJoinRequestTrials);
-                        if( JoinRequestTrials >= MaxJoinRequestTrials )
-                        {
-                            LoRaMacState &= ~LORAMAC_TX_RUNNING;
-                            isr_printf("tx-run-D\r\n");
-                        }
-                        else
-                        {
-                            LoRaMacFlags.Bits.MacDone = 0;
-                            // Sends the same frame again
-                            OnTxDelayedTimerEvent( );
-                        }
-                    }
-                }
-            } // ..if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
-        } // ..if( ( NodeAckRequested == false ) && ( txTimeout == false ) )
-
-        if( LoRaMacFlags.Bits.McpsInd == 1 )
-        {// Procedure if we received a frame
-            if( ( McpsConfirm.AckReceived == true ) /*|| ( AckTimeoutRetriesCounter > AckTimeoutRetries )*/ )
-            {
-                NodeAckRequested = false;
-                if( IsUpLinkCounterFixed == false )
-                {
-                    UpLinkCounter++;
-                }
-
-                LoRaMacState &= ~LORAMAC_TX_RUNNING;
-            }
-        }
-
-    } // ...if( LoRaMacFlags.Bits.MacDone == 1 )
-
-    // Handle reception for Class B and Class C
-    if( ( LoRaMacState & LORAMAC_RX ) == LORAMAC_RX )
-    {
-        LoRaMacState &= ~LORAMAC_RX;
-    }
-    if( LoRaMacState == LORAMAC_IDLE )
-    {
-        if( LoRaMacFlags.Bits.McpsReq == 1 )
-        {
-            LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm );
-            LoRaMacFlags.Bits.McpsReq = 0;
-        }
-
-        if( LoRaMacFlags.Bits.MlmeReq == 1 )
-        {
-            LoRaMacPrimitives->MacMlmeConfirm( &MlmeConfirm );
-            LoRaMacFlags.Bits.MlmeReq = 0;
-        }
-
-        if( LoRaMacFlags.Bits.MlmeInd == 1 )
-        {
-            LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );
-            LoRaMacFlags.Bits.MlmeInd = 0;
-        }
-
-        // Procedure done. Reset variables.
-        LoRaMacFlags.Bits.MacDone = 0;
-    }
-    else
-    {
-        // Operation not finished restart timer
-        MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
-    }
-
-    if( LoRaMacFlags.Bits.McpsInd == 1 )
-    {
-        if( LoRaMacFlags.Bits.McpsIndSkip == 0 )
-        {
-            LoRaMacPrimitives->MacMcpsIndication( &McpsIndication );
-        }
-        LoRaMacFlags.Bits.McpsIndSkip = 0;
-        LoRaMacFlags.Bits.McpsInd = 0;
-    }
-} // ..OnMacStateCheckTimerEvent( void )
-
-static void OnTxDelayedTimerEvent( void )
-{
-    LoRaMacHeader_t macHdr;
-    LoRaMacFrameCtrl_t fCtrl;
-
-    LoRaMacState &= ~LORAMAC_TX_DELAYED;
-
-    if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) && ( MlmeConfirm.MlmeRequest == MLME_JOIN ) )
-    {
-        ResetMacParameters( );
-        if (++Channel == LORA_MAX_NB_CHANNELS)
-            Channel = 0;
-        isr_printf("<ch%u>", Channel);
-        isr_printf("tx-delayed join ch%u\r\n", Channel);
-
-        macHdr.Value = 0;
-        macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ;
-
-        fCtrl.Value = 0;
-        fCtrl.Bits.Adr = 0;
-
-        /* In case of join request retransmissions, the stack must prepare
-         * the frame again, because the network server keeps track of the random
-         * LoRaMacDevNonce values to prevent reply attacks. */
-        PrepareFrame( &macHdr, &fCtrl, 0, NULL, 0 );
-    }
-
-    ScheduleTx( );
-}
-
 static void OnRxWindowTimerEvent( void )
 {
     Radio.Standby( );
     if (expecting_beacon) {
         isr_printf("rxwin-during-beacon\r\n");
-        /* TODO: report failure */
+        McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_ERROR;
+        MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_ERROR;
+        LoRaMacFlags.Bits.MacDone = 1;
+        LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm );
     } else {
         RxWindowSetup( LORAMAC_FIRST_CHANNEL + ( Channel * LORAMAC_STEPWIDTH_CHANNEL), RxWindowsParam.Datarate, RxWindowsParam.Bandwidth, RxWindowsParam.RxWindowTimeout, false );
     }
-
-    /* no retrying TX in this class of operation */
-    LoRaMacState &= ~LORAMAC_TX_RUNNING;
 }
 
 static void OnAckTimeoutTimerEvent( void )
 {
-    if( NodeAckRequested == true )
-    {
-        LoRaMacState &= ~LORAMAC_ACK_REQ;
-    }
 }
 
 static bool RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous )
@@ -1893,15 +1764,13 @@
     McpsConfirm.AckReceived = false;
     McpsConfirm.UpLinkCounter = UpLinkCounter;
 
-    status = ScheduleTx( );
-
-    return status;
+    return ScheduleTx();
 }
 
 static LoRaMacStatus_t ScheduleTx( void )
 {
     // Compute Rx1 windows parameters
-    if( IsLoRaMacNetworkJoined == false )
+    if (!IsLoRaMacNetworkJoined)
     {
         RxWindowDelay_us = LoRaMacParams.JoinAcceptDelay_us + RxWindowsParam.RxOffset;  // dont care
     }
@@ -1998,7 +1867,7 @@
             NodeAckRequested = true;
             //Intentional fallthrough
         case FRAME_TYPE_DATA_UNCONFIRMED_UP:
-            if( IsLoRaMacNetworkJoined == false )
+            if (!IsLoRaMacNetworkJoined)
             {
                 return LORAMAC_STATUS_NO_NETWORK_JOINED; // No network has been joined yet
             }
@@ -2095,15 +1964,13 @@
     return LORAMAC_STATUS_OK;
 }
 
-
-//TxPowers[LoRaMacParams.ChannelsTxPower]
 LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel )
 {
     int8_t datarate = Datarates[LoRaMacParams.ChannelsDatarate_fixed];
     int8_t txPowerIndex = 0;
     int8_t txPower = 0;
 
-    if (LoRaMacState & LORAMAC_TX_SCHED) {
+    if (BeaconCtx.guard) {
         return LORAMAC_STATUS_BUSY;
     }
 
@@ -2129,7 +1996,7 @@
     MlmeConfirm.TxTimeOnAir = TxTimeOnAir;
 
 
-    if( IsLoRaMacNetworkJoined == false )
+    if (!IsLoRaMacNetworkJoined)
     {
         JoinRequestTrials++;
         isr_printf("join %luhz try%u DR%u\r\n", channel.Frequency, JoinRequestTrials, LoRaMacParams.ChannelsDatarate_fixed);
@@ -2141,47 +2008,11 @@
         // Send now
         Radio.SetTxConfig( MODEM_LORA, txPower, 0, 2, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
         Radio.Send( LoRaMacBuffer, LoRaMacBufferPktLen );
-
-        LoRaMacState |= LORAMAC_TX_RUNNING;
-
-        // Starts the MAC layer status check timer
-        MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
-    } else if (BeaconCtx.have_beacon) {
-        /* find now ping slot */
-        unsigned int target_us, now_us = lp_timer.read_us();
-        int us_to_guard, us_since_beacon_start = now_us - BeaconCtx.LastBeaconStart_us;
-        int use_slot = BeaconCtx.tx_slot_offset;
-        int now_slot = (us_since_beacon_start - BEACON_RESERVED_us) / 30000;
-        int use_slot_us, txing_in_us;
-
-        while (use_slot < now_slot)
-            use_slot += BeaconCtx.periodicity_slots;
+    }
+    else {
+        LoRaMacFlags.Bits.pending_tx = 1;
+    }
 
-        use_slot_us = (use_slot * 30000);
-        us_to_guard = BeaconCtx.next_beacon_expected_us - use_slot_us;
-        target_us = (use_slot * 30000) + BEACON_RESERVED_us;
-        txing_in_us = target_us - us_since_beacon_start;
-#ifdef TX_DEBUG
-        isr_printf("now_slot:%d, to-guard:%d use_slot:%u (txing_in %d) ", now_slot, us_to_guard, use_slot, txing_in_us);
-#endif
-        if ((us_to_guard - txing_in_us) > BEACON_GUARD_us) {
-            us_at_sched = us_to_guard - txing_in_us;
-            tx_timeout.attach_us(&send_callback, txing_in_us);
-#ifdef TX_DEBUG
-            isr_printf(" : tx in %uus\r\n", target_us - us_since_beacon_start);
-#endif
-        } else {
-#ifdef TX_DEBUG
-            isr_printf("sf-busy\r\n");
-#endif
-            return LORAMAC_STATUS_BUSY;
-        }
-
-        LoRaMacState |= LORAMAC_TX_SCHED;
-    } else {
-        isr_printf("dropped tx\r\n");
-        return LORAMAC_STATUS_SERVICE_UNKNOWN;  // TODO use correct failure
-    }
 
     return LORAMAC_STATUS_OK;
 }
@@ -2193,14 +2024,8 @@
 
     txPowerIndex = LoRaMacParams.ChannelsTxPower;
     txPower = TxPowers[txPowerIndex];
-
-    // Starts the MAC layer status check timer
-    MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
-
     Radio.SetTxContinuousWave( Channels[Channel].Frequency, txPower, timeout );
 
-    LoRaMacState |= LORAMAC_TX_RUNNING;
-
     return LORAMAC_STATUS_OK;
 }
 
@@ -2208,11 +2033,6 @@
 {
     Radio.SetTxContinuousWave( frequency, power, timeout );
 
-    // Starts the MAC layer status check timer
-    MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
-
-    LoRaMacState |= LORAMAC_TX_RUNNING;
-
     return LORAMAC_STATUS_OK;
 }
 
@@ -2241,7 +2061,6 @@
     LoRaMacFlags.Value = 0;
 
     LoRaMacDeviceClass = CLASS_A;
-    LoRaMacState = LORAMAC_IDLE;
 
     JoinRequestTrials = 0;
     MaxJoinRequestTrials = 255;
@@ -2438,10 +2257,6 @@
     {
         return LORAMAC_STATUS_PARAMETER_INVALID;
     }
-    if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
-    {
-        return LORAMAC_STATUS_BUSY;
-    }
 
     switch( mibSet->Type )
     {
@@ -2469,6 +2284,11 @@
         case MIB_NETWORK_JOINED:
         {
             IsLoRaMacNetworkJoined = mibSet->Param.IsNetworkJoined;
+            if (!IsLoRaMacNetworkJoined) {
+                BeaconCtx.timeout_rx.detach();
+                BeaconCtx.timeout_guard.detach();
+                BeaconCtx.state = BEACON_STATE_NONE;
+            }
             break;
         }
         case MIB_NET_ID:
@@ -2591,10 +2411,6 @@
     {
         return LORAMAC_STATUS_PARAMETER_INVALID;
     }
-    if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
-    {
-        return LORAMAC_STATUS_BUSY;
-    }
 
     // Reset downlink counter
     channelParam->DownLinkCounter = 0;
@@ -2626,10 +2442,6 @@
     {
         return LORAMAC_STATUS_PARAMETER_INVALID;
     }
-    if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
-    {
-        return LORAMAC_STATUS_BUSY;
-    }
 
     if( MulticastChannels != NULL )
     {
@@ -2668,10 +2480,6 @@
     {
         return LORAMAC_STATUS_PARAMETER_INVALID;
     }
-    if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
-    {
-        return LORAMAC_STATUS_BUSY;
-    }
 
     memset1( ( uint8_t* ) &MlmeConfirm, 0, sizeof( MlmeConfirm ) );
 
@@ -2681,11 +2489,6 @@
     {
         case MLME_JOIN:
         {
-            if( ( LoRaMacState & LORAMAC_TX_DELAYED ) == LORAMAC_TX_DELAYED )
-            {
-                return LORAMAC_STATUS_BUSY;
-            }
-
             if( ( mlmeRequest->Req.Join.DevEui == NULL ) ||
                 ( mlmeRequest->Req.Join.AppEui == NULL ) ||
                 ( mlmeRequest->Req.Join.AppKey == NULL ) ||
@@ -2716,10 +2519,11 @@
             macHdr.Bits.MType  = FRAME_TYPE_JOIN_REQ;
 
             ResetMacParameters( );
+            expecting_beacon = false;
+            BeaconCtx.state = BEACON_STATE_NONE;
 
             Channel = 0;    // start with first channel
             isr_printf("<ch0>");
-
             isr_printf("mlme-join-send ch%u\r\n", Channel);
             status = Send( &macHdr, 0, NULL, 0 );
             break;
@@ -2773,11 +2577,6 @@
     {
         return LORAMAC_STATUS_PARAMETER_INVALID;
     }
-    if( ( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING ) ||
-        ( ( LoRaMacState & LORAMAC_TX_DELAYED ) == LORAMAC_TX_DELAYED ) )
-    {
-        return LORAMAC_STATUS_BUSY;
-    }
 
     macHdr.Value = 0;
     memset1 ( ( uint8_t* ) &McpsConfirm, 0, sizeof( McpsConfirm ) );
@@ -2843,7 +2642,7 @@
 void LoRaMacTestSetMic( uint16_t txPacketCounter )
 {
     UpLinkCounter = txPacketCounter;
-    IsUpLinkCounterFixed = true;
+    //IsUpLinkCounterFixed = true;
 }
 
 void LoRaMacTestSetChannel( uint8_t channel )