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:
25:fed9d5b77183
Parent:
23:a862b5601663
Child:
29:ad409c68c0a6
--- a/mac/LoRaMac.cpp	Wed Aug 09 23:41:10 2017 +0000
+++ b/mac/LoRaMac.cpp	Thu Aug 10 14:47:29 2017 -0700
@@ -610,25 +610,6 @@
  */
 LoRaMacStatus_t PrepareFrame( LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
 
-/*
- * \brief Schedules the frame according to the duty cycle
- *
- * \retval Status of the operation
- */
-static LoRaMacStatus_t ScheduleTx( void );
-
-
-/*!
- * \brief LoRaMAC layer prepared frame buffer transmission with channel specification
- *
- * \remark PrepareFrame must be called at least once before calling this
- *         function.
- *
- * \param [IN] channel     Channel parameters
- * \retval status          Status of the operation.
- */
-LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel );
-
 /*!
  * \brief Sets the radio in continuous transmission mode
  *
@@ -785,6 +766,57 @@
 
 void send_bh()
 {      
+    int8_t datarate = Datarates[LoRaMacParams.ChannelsDatarate_fixed];
+    int8_t txPowerIndex = 0;
+    int8_t txPower = 0;
+
+    txPowerIndex = LoRaMacParams.ChannelsTxPower;
+    txPower = TxPowers[txPowerIndex];
+
+    MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_SEND;
+    McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_SEND;
+    McpsConfirm.TxPower = txPowerIndex;
+    McpsConfirm.UpLinkFrequency = Channels[Channel].Frequency;
+
+    if (!IsLoRaMacNetworkJoined)    // joining is channel hunting
+        Radio.SetChannel( Channels[Channel].Frequency );
+
+#if defined( USE_BAND_915_SINGLE )
+    // High speed LoRa channel BW500 kHz
+    Radio.SetTxConfig( MODEM_LORA, txPower, 0, 2, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
+    TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
+#elif defined(USE_BAND_433) && defined (ENABLE_SX1276)
+    if( LoRaMacParams.ChannelsDatarate_fixed == DR_7 )
+    { // High Speed FSK channel
+        //Radio.SetMaxPayloadLength( MODEM_FSK, LoRaMacBufferPktLen );
+        Radio.SetTxConfig( MODEM_FSK, txPower, 25e3, 0, datarate * 1e3, 0, 5, false, true, 0, 0, false, 3e3 );
+        TxTimeOnAir = Radio.TimeOnAir( MODEM_FSK, LoRaMacBufferPktLen );
+
+    }
+    else if( LoRaMacParams.ChannelsDatarate_fixed == DR_6 )
+    { // High speed LoRa channel
+        //Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
+        Radio.SetTxConfig( MODEM_LORA, txPower, 0, 1, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
+        TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
+    }
+    else
+    { // Normal LoRa channel
+        //Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
+        Radio.SetTxConfig( MODEM_LORA, txPower, 0, 0, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
+        TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
+    }
+#endif
+
+    // Store the time on air
+    McpsConfirm.TxTimeOnAir = TxTimeOnAir;
+    MlmeConfirm.TxTimeOnAir = TxTimeOnAir;
+
+    if (!IsLoRaMacNetworkJoined)
+    {
+        JoinRequestTrials++;
+        isr_printf("join %luhz try%u DR%u\r\n", Channels[Channel].Frequency, JoinRequestTrials, LoRaMacParams.ChannelsDatarate_fixed);
+    }
+
     Radio.SetTxConfig(
         /* RadioModems_t modem */ MODEM_LORA,
         /* int8_t power */ TxPowers[LoRaMacParams.ChannelsTxPower],
@@ -802,28 +834,28 @@
     );
     Radio.Send( LoRaMacBuffer, LoRaMacBufferPktLen );        
         
+    LoRaMacFlags.Bits.uplink_pending = 0;   // sent
+
+    // Compute Rx1 windows parameters, taken at TxDone
+    if (!IsLoRaMacNetworkJoined)
+        RxWindowDelay_us = LoRaMacParams.JoinAcceptDelay_us + RxWindowsParam.RxOffset;
+    else
+        RxWindowDelay_us = LoRaMacParams.ReceiveDelay_us + RxWindowsParam.RxOffset;
+
+    RxWindowDelay_us -= DIO0_LAG_us;
 }
 
 void send_callback()
 {      
     if (BeaconCtx.guard) {
-        if (LoRaMacFlags.Bits.pending_tx) {
-            McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_INCOMPLETE;
-            MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_INCOMPLETE;
-            LoRaMacFlags.Bits.MacDone = 1;
-            LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm );
-        }
+        /* last send, this will be restarted after beacon sent */
         return;
     }
     
     tx_timeout.attach_us(&send_callback, BeaconCtx.periodicity_slots * 30000);
                 
-    if (!LoRaMacFlags.Bits.pending_tx)
-        return;
-
-    flags.send = 1;
-        
-    LoRaMacFlags.Bits.pending_tx = 0;
+    if (LoRaMacFlags.Bits.uplink_pending)
+        flags.send = 1;
 }
 
 void OnRxBeaconSetup()
@@ -1537,6 +1569,14 @@
     flags.join_send = 1;
 }
 
+static void ScheduleTx( void )
+{
+    if (!IsLoRaMacNetworkJoined)
+        flags.send = 1; // immediately send join asychronously
+    else
+        LoRaMacFlags.Bits.uplink_pending = 1;   // send synchronously
+}
+
 static void
 join_send_bh()
 {
@@ -1617,8 +1657,10 @@
         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 (LoRaMacFlags.Bits.MlmeReq && ( MlmeConfirm.MlmeRequest == MLME_JOIN )) {
+            /* no join accept received: join retry */
+            tx_timeout.attach_us(&join_send, (JoinRequestTrials*20000) + randr(0, 70000));
+        }
     }
 
     if (NodeAckRequested)
@@ -1641,7 +1683,8 @@
         LoRaMacFlags.Bits.MacDone = 1;
         LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm );
     } else {
-        RxWindowSetup( LORAMAC_FIRST_CHANNEL + ( Channel * LORAMAC_STEPWIDTH_CHANNEL), RxWindowsParam.Datarate, RxWindowsParam.Bandwidth, RxWindowsParam.RxWindowTimeout, false );
+        //RxWindowSetup( LORAMAC_FIRST_CHANNEL + ( Channel * LORAMAC_STEPWIDTH_CHANNEL), RxWindowsParam.Datarate, RxWindowsParam.Bandwidth, RxWindowsParam.RxWindowTimeout, false );
+        RxWindowSetup( Channels[Channel].Frequency, RxWindowsParam.Datarate, RxWindowsParam.Bandwidth, RxWindowsParam.RxWindowTimeout, false );
     }
 }
 
@@ -1856,27 +1899,11 @@
     McpsConfirm.AckReceived = false;
     McpsConfirm.UpLinkCounter = UpLinkCounter;
 
-    return ScheduleTx();
+    ScheduleTx();
+
+    return LORAMAC_STATUS_OK;
 }
 
-static LoRaMacStatus_t ScheduleTx( void )
-{
-    // Compute Rx1 windows parameters
-    if (!IsLoRaMacNetworkJoined)
-    {
-        RxWindowDelay_us = LoRaMacParams.JoinAcceptDelay_us + RxWindowsParam.RxOffset;  // dont care
-    }
-    else
-    {
-        RxWindowDelay_us = LoRaMacParams.ReceiveDelay_us + RxWindowsParam.RxOffset;
-    }
-    RxWindowDelay_us -= DIO0_LAG_us;
-
-    // Schedule transmission of frame
-
-    // Try to send now
-    return SendFrameOnChannel( Channels[Channel] );
-}
 
 static void ResetMacParameters( void )
 {
@@ -2057,88 +2084,6 @@
     return LORAMAC_STATUS_OK;
 }
 
-LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel )
-{
-    int8_t datarate = Datarates[LoRaMacParams.ChannelsDatarate_fixed];
-    int8_t txPowerIndex = 0;
-    int8_t txPower = 0;
-
-    if (BeaconCtx.guard) {
-        unsigned now_us = lp_timer.read_us();
-        if (now_us - BeaconCtx.guard_at > 10000000) {
-            isr_printf("beacon fault\r\n");
-            IsLoRaMacNetworkJoined = false;
-            BeaconCtx.timeout_rx.detach();
-            BeaconCtx.timeout_guard.detach();
-            BeaconCtx.state = BEACON_STATE_NONE;
-            BeaconCtx.guard = false;
-            return LORAMAC_STATUS_NO_NETWORK_JOINED;
-        }
-        return LORAMAC_STATUS_BUSY;
-    }
-
-    txPowerIndex = LoRaMacParams.ChannelsTxPower;
-    txPower = TxPowers[txPowerIndex];
-
-    MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_SEND;
-    McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_SEND;
-    McpsConfirm.TxPower = txPowerIndex;
-    McpsConfirm.UpLinkFrequency = channel.Frequency;
-
-    Radio.SetChannel( channel.Frequency );
-
-#if defined( USE_BAND_915_SINGLE )
-    if( LoRaMacParams.ChannelsDatarate_fixed >= DR_8 )
-    { // High speed LoRa channel BW500 kHz
-        Radio.SetTxConfig( MODEM_LORA, txPower, 0, 2, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
-        TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
-    } else
-        return LORAMAC_STATUS_DATARATE_INVALID;
-#elif defined(USE_BAND_433) && defined (ENABLE_SX1276)
-    if( LoRaMacParams.ChannelsDatarate_fixed == DR_7 )
-    { // High Speed FSK channel
-        //Radio.SetMaxPayloadLength( MODEM_FSK, LoRaMacBufferPktLen );
-        Radio.SetTxConfig( MODEM_FSK, txPower, 25e3, 0, datarate * 1e3, 0, 5, false, true, 0, 0, false, 3e3 );
-        TxTimeOnAir = Radio.TimeOnAir( MODEM_FSK, LoRaMacBufferPktLen );
-
-    }
-    else if( LoRaMacParams.ChannelsDatarate_fixed == DR_6 )
-    { // High speed LoRa channel
-        //Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
-        Radio.SetTxConfig( MODEM_LORA, txPower, 0, 1, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
-        TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
-    }
-    else
-    { // Normal LoRa channel
-        //Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
-        Radio.SetTxConfig( MODEM_LORA, txPower, 0, 0, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
-        TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
-    }
-#endif
-
-    // Store the time on air
-    McpsConfirm.TxTimeOnAir = TxTimeOnAir;
-    MlmeConfirm.TxTimeOnAir = TxTimeOnAir;
-
-
-    if (!IsLoRaMacNetworkJoined)
-    {
-        JoinRequestTrials++;
-        isr_printf("join %luhz try%u DR%u\r\n", channel.Frequency, JoinRequestTrials, LoRaMacParams.ChannelsDatarate_fixed);
-    }
-
-    /* anything not join request is sent at permitted time slot */
-    LoRaMacHeader_t* macHdr = (LoRaMacHeader_t*)&LoRaMacBuffer[0];
-    if (macHdr->Bits.MType == FRAME_TYPE_JOIN_REQ) {
-        // Send now
-        Radio.Send( LoRaMacBuffer, LoRaMacBufferPktLen );
-    }
-    else {
-        LoRaMacFlags.Bits.pending_tx = 1;
-    }
-
-    return LORAMAC_STATUS_OK;
-}
 
 LoRaMacStatus_t SetTxContinuousWave( uint16_t timeout )
 {