repeat message down a chain, adding to the payload at each repeating device

Dependencies:   sx12xx_hal

radio chip selection

Radio chip driver is not included, because options are available.
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.

/media/uploads/dudmuck/chain.png

network architecture

  • UNIT 0x00 transmitting only device: mandatory.
  • UNIT 0x01: repeating device
  • Uni-directional network: Each unit can only receive message from UNIT_ID - 1 (previous unit)
  • UINT n receiving only device LAST_UNIT: mandatory; prints payload onto UART.

configuration

Each device in the network is uniquely identified by:

  • UNIT_ID: ID byte designating address of this device.
  • UNIT_LAST: If defined, this device prints payload onto serial port instead of re-transmitting payload.

All devices in network must be configured identically with the following:

  • TX_INTERVAL_US: how often to take measurement and send to UNIT_ID+1 (time of complete cycle).
  • MAX_TX_LENGTH: Maximum size of payload, in bytes. Payload is sent in fragments when exceeds this value; aka size of each fragment.
  • TXRX_PADDING_US : Time allotted for RX-TX turnaround and CPU overhead
  • MAX_TX_ATTEMPTS: Count of transmit retries permitted
  • SPREADING_FACTOR LoRa configuration of datarate
  • CF_MHZ : Operating radio frequency


Duration of retry interval is auto-calculated from LoRa modem configuration (bandwidth/sf) and MAX_TX_LENGTH.
Take care that TX_INTERVAL_US value is appropriate relative to total retry interval (interval * MAX_TX_ATTEMPTS)

Revision:
4:f272bf72893a
Parent:
3:a78a70af3403
Child:
5:62c9ddaa5ea6
diff -r a78a70af3403 -r f272bf72893a main.cpp
--- a/main.cpp	Sun Nov 25 17:13:06 2018 -0800
+++ b/main.cpp	Fri Dec 14 14:41:38 2018 -0800
@@ -104,10 +104,13 @@
 } pkt_flags_t;
 
 volatile struct _f_ {
-    uint8_t _sleep_        : 1;   // 0
+    uint8_t _sleep_                     : 1;   // 0
     uint8_t mbedTImeout_forwarderStarted: 1;   // 1
     uint8_t run                         : 1;   // 2
+#ifndef UNIT_LAST
     uint8_t sample                      : 1;   // 3
+    uint8_t fwd                         : 1;   // 4
+#endif /* UNIT_LAST */
 } flags;
 
 
@@ -175,7 +178,9 @@
 
         rxStartAt = 0;  // starting of continuous rx not used
 
+#ifndef UNIT_LAST
         flags.sample = 1;
+#endif /* UNIT_LAST */
         pc.printf("RX ");
     }
 
@@ -223,6 +228,12 @@
     state = STATE_ACK_WAITING;
 }
 
+volatile uint16_t sample;
+
+#ifdef N_SMP
+volatile uint16_t samples[N_SMP];
+#endif
+
 uint8_t _tx_forward()
 {
     unsigned fwdLen;
@@ -259,10 +270,10 @@
         message_t* mptr = (message_t*)(Radio::radio.tx_buf + txCurs);
         mptr->unit_id = UNIT_ID;
         mptr->flags = 0x00;
-        mptr->sample = ain.read_u16();
+        mptr->sample = sample;  // taken from main loop
 #ifdef N_SMP
         for (c = 0; c < N_SMP; c++)
-            mptr->samples[c] = ain.read_u16();
+            mptr->samples[c] = samples[c];  // taken from main loop
 #endif
         txCurs += sizeof(message_t);
 
@@ -307,6 +318,12 @@
     pc.printf(" dur%u\e[0m\r\n", dur);
     prevFwdStart = now;
 }
+
+void sample_ticker_cb()
+{
+    flags.sample = 1;
+}
+
 #else // ..UNIT_LAST:
 void uart_forward_cb()
 {
@@ -334,7 +351,9 @@
 
     pc.printf("nextRxStartCB for %uus\r\n", us);
 
+#ifndef UNIT_LAST
     flags.sample = 1;
+#endif /* UNIT_LAST */
 }
 
 
@@ -393,6 +412,7 @@
             int target_us = us - sinceRxDone;
             // tx to occur after time given for all potential retries
 #ifndef UNIT_LAST
+            flags.sample = 1;   // sample from main loop, to be ready for tx_forward
             mbedTImeout_forwarder.attach_us(tx_forward_cb, target_us);
 #else
             mbedTImeout_forwarder.attach_us(uart_forward_cb, target_us);
@@ -499,7 +519,8 @@
                 f.bits.fragNum++;
                 f.bits.attempt = 0;
                 Radio::radio.tx_buf[1] = f.octet;
-                _tx_forward();
+                flags.sample = 1;
+                flags.fwd = 1;  // tx_forward from main loop
                 pc.printf("ackOk->%u ", f.bits.fragNum);
             }
         } else 
@@ -510,18 +531,6 @@
     pc.printf("\r\n");
 } // ..rxDoneCB()
 
-void sample_ticker_cb()
-{
-    flags.sample = 1;
-}
-
-
-uint16_t sample;
-
-#ifdef N_SMP
-uint16_t samples[N_SMP];
-#endif
-
 #if (UNIT_ID == 0x00) && !defined(UNIT_LAST)
 LowPowerTicker      sampleTicker, txTicker;
 
@@ -635,9 +644,6 @@
             printf(" measuredInterval:%llu\r\n", measuredInterval);
             stateToString(state, str);
             printf("_sleep_:%u %s\r\n", flags._sleep_, str);
-#ifdef SX128x_H
-            print_sx1280();
-#endif
             break;
         case 'r':
             flags.run ^= 1;
@@ -714,6 +720,7 @@
             uart_rx();
         }
 
+#ifndef UNIT_LAST
         if (flags.sample) {
             sample = ain.read_u16();
 #ifdef N_SMP
@@ -721,9 +728,14 @@
                 samples[c] = ain.read_u16();
 #endif
             print_radio_chip();
+            if (flags.fwd) {
+                _tx_forward();
+                flags.fwd = 0;
+            }
 
             flags.sample = 0;
         }
+#endif /* UNIT_LAST */
 
         if (flags._sleep_ == 0) {
             Radio::service();