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:
1:7dbf0926e146
Parent:
0:d88677306896
Child:
2:534be88a25dc
--- a/main.cpp	Wed Jun 13 17:49:58 2018 -0700
+++ b/main.cpp	Thu Jul 05 17:43:35 2018 -0700
@@ -1,6 +1,6 @@
 #include "radio.h" 
 
-#define UNIT_ID     0x01            /* 0x00: first unit */
+#define UNIT_ID     0x03            /* 0x00: first unit */
 //#define UNIT_LAST
 
 // test large sample, large pkt size  #define N_SMP    10
@@ -29,6 +29,8 @@
     #define RX_STARTUP_US           1500
 #elif defined(SX126x_H)
     #define RX_STARTUP_US           1000
+#elif defined(SX128x_H)
+    #define RX_STARTUP_US           1000
 #endif
 unsigned rxStartup_us = RX_STARTUP_US;
 
@@ -36,6 +38,8 @@
 
 #ifdef TARGET_DISCO_L072CZ_LRWAN1
         AnalogIn ain(A0);
+#elif defined(TARGET_MOTE_L152RC)
+        AnalogIn ain(A0);
 #else
     #ifdef TARGET_FF_MORPHO
         AnalogIn ain(PC_4); // pin unused by arduino shields
@@ -155,7 +159,7 @@
         measuredInterval = 0; // single use
     } else {
         flags._sleep_ = 0;
-        Radio::Rx();
+        Radio::Rx(0);
 
         rxStartAt = 0;  // starting of continuous rx not used
 
@@ -167,22 +171,28 @@
 
 LowPowerTimeout mbedTImeout_forwarder;
 
+
 #ifndef UNIT_LAST
 volatile uint8_t txCurs;
 
 LowPowerTicker tickerRetry;
+volatile us_timestamp_t txStartAt;
 
 void retry_cb()
 {
     unsigned c;
     pkt_flags_t f;
 
+    Radio::Standby();
+
     f.octet = Radio::radio.tx_buf[1];
     pc.printf("attempt%u", f.bits.attempt);
     if (++f.bits.attempt >= MAX_TX_ATTEMPTS) {
         pc.printf(" lastTry");
         tickerRetry.detach();
-#if (UNIT_ID != 0x00)
+#if (UNIT_ID == 0x00)
+        flags._sleep_ = 1;
+#else
         setupNext();
 #endif /* UNIT_ID != 0x00 */
         pc.printf("\r\n");
@@ -195,6 +205,7 @@
     Radio::radio.tx_buf[txCurs-2] = c >> 8;
     Radio::radio.tx_buf[txCurs-1] = c;
 
+    txStartAt = Radio::lpt.read_us();
     Radio::Send(txCurs, 0, 0, 0);
     state = STATE_ACK_WAITING;
 }
@@ -251,6 +262,8 @@
     Radio::radio.tx_buf[txCurs++] = c >> 8;
     Radio::radio.tx_buf[txCurs++] = c;
 
+    /*Radio::set_tx_dbm(17);
+        Radio::PrintStatus();*/
     Radio::Send(txCurs, 0, 0, 0);
     state = STATE_ACK_WAITING;
     flags._sleep_ = 0;
@@ -302,7 +315,7 @@
 {
     unsigned us;
 
-    Radio::Rx();
+    Radio::Rx(0);
     rxStartAt = Radio::lpt.read_us();
     flags._sleep_ = 0;
 
@@ -316,7 +329,7 @@
 {
     char str[32];
 
-    Radio::Rx();
+    Radio::Rx(0);
     stateToString(state, str);
     pc.printf("%s:txDone->Rx\r\n", str);
 }
@@ -377,6 +390,8 @@
         }
 
         Radio::radio.tx_buf[0] = UNIT_ID;   // OK, send ACK
+        /*YYY Radio::set_tx_dbm(17);
+        Radio::PrintStatus();*/
         Radio::Send(1, 0, 0, 0);
 
 
@@ -498,6 +513,8 @@
     f.bits.fragNum = 0;
     f.bits.fragLast = 1;
 
+    Radio::Standby();
+
     txCurs = 0;
     Radio::radio.tx_buf[txCurs++] = UNIT_ID;
     Radio::radio.tx_buf[txCurs++] = f.octet;
@@ -517,12 +534,14 @@
     Radio::radio.tx_buf[txCurs++] = c;
 
     Radio::Send(txCurs, 0, 0, 0);
+    txStartAt = Radio::lpt.read_us();
     state = STATE_ACK_WAITING;
-    pc.printf("tx_ticker_cb:%u \r\n", mptr->sample);
+    pc.printf("tx_ticker_cb:%u\r\n", mptr->sample);
     flags._sleep_ = 0;
 }
 #endif /* UNIT_ID == 0x00 */
 
+
 void uart_rx()
 {
     char str[32];
@@ -539,7 +558,7 @@
             pc.printf("rxStartup_us:%u\r\n", rxStartup_us);
             break;
         case '.':
-            Radio::PrintStatus();
+            //Radio::PrintStatus();
             printf("UNIT_ID:%02x ", UNIT_ID);
             printf(" measuredInterval:%llu\r\n", measuredInterval);
             stateToString(state, str);
@@ -562,15 +581,23 @@
     } // ..switch (ch)
 }
 
+RadioEvents_t rev = { 0 };
+
 int main()
 {
     flags.run = 1;
     pc.baud(115200);
     pc.printf("\r\nreset\r\n");
-    Radio::Init(txDoneCB, rxDoneCB);
+
+    rev.TxDone_botHalf = txDoneCB;
+    rev.RxDone = rxDoneCB;
+    Radio::Init(&rev);
 
-    Radio::setFrfMHz(CF_MHZ);
-    Radio::Config(500, SPREADING_FACTOR);
+    Radio::SetChannel(CF_MHZ * 1000000);
+    Radio::LoRaModemConfig(500, SPREADING_FACTOR, 1);
+    Radio::LoRaPacketConfig(8, false, true, false);  // preambleLen, fixLen, crcOn, invIQ
+
+    Radio::set_tx_dbm(17);
 
     /* max TX length + turnaround + ACK length */
     retryInterval_us = Radio::lora_toa_us(MAX_TX_LENGTH) + TXRX_PADDING_US + Radio::lora_toa_us(1);
@@ -585,7 +612,7 @@
 #if (UNIT_ID == 0x00) && !defined(UNIT_LAST)
     lpTicker.attach_us(tx_ticker_cb, TX_INTERVAL_US);
 #else
-    Radio::PrintStatus();
+    //Radio::PrintStatus();
 
     setupNext();