lmic fork for Senet NAMote

Dependents:   Senet NAMote

Fork of lmic_MOTE_L152RC_2 by Dave Kjendal

Files at this revision

API Documentation at this revision

Comitter:
dudmuck
Date:
Wed Oct 21 01:03:34 2015 +0000
Parent:
7:9095e54e381f
Child:
9:83ae7f34e88c
Commit message:
parse channel mask in join accept, tx power table, channel search in nextJoinState()

Changed in this revision

lmic.cpp Show annotated file Show diff for this revision Revisions of this file
lmic.h Show annotated file Show diff for this revision Revisions of this file
radio.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/lmic.cpp	Mon Sep 21 17:59:42 2015 +0000
+++ b/lmic.cpp	Wed Oct 21 01:03:34 2015 +0000
@@ -11,6 +11,7 @@
 
 //! \file
 #include "lmic.h"
+#include "debug.h"  // tmp wbr
 
 #if !defined(MINRX_SYMS)
 #define MINRX_SYMS 5
@@ -496,8 +497,11 @@
                         e_.prevdr    = LMIC.datarate|DR_PAGE,
                         e_.prevtxpow = LMIC.adrTxPow));
     
-    if( pow != KEEP_TXPOW )
+    if( pow != KEEP_TXPOW ) {
         LMIC.adrTxPow = pow;
+        if (pow < LMIC.txpow_limit)
+            LMIC.txpow = pow;
+    }
     if( LMIC.datarate != dr ) {
         LMIC.datarate = dr;
         DO_DEVDB(LMIC.datarate,datarate);
@@ -675,7 +679,7 @@
 
 #define setRx1Params() /*LMIC.freq/rps remain unchanged*/
 
-static void initJoinLoop (void) {
+static void initJoinLoop (void) {       // eu868
     LMIC.txChnl = os_getRndU1() % 6;
     LMIC.adrTxPow = 14;
     setDrJoin(DRCHG_SET, DR_SF7);
@@ -750,6 +754,9 @@
     
     LMIC.txpow_limit = 30;
 #endif
+
+    LMIC.txpow = LMIC.txpow_limit;
+    LMIC.adrTxPow = LMIC.txpow_limit;
 }
 
 static u4_t convFreq (xref2u1_t ptr) {
@@ -790,12 +797,18 @@
 
 static void updateTx (ostime_t txbeg) {
     u1_t chnl = LMIC.txChnl;
+#ifdef JOIN_REQ_DEBUG    
+    printf("chnl%d ", chnl);
+#endif /* JOIN_REQ_DEBUG */  
     if( chnl < 64 ) {
         LMIC.freq = US915_125kHz_UPFBASE + chnl*US915_125kHz_UPFSTEP;
-        LMIC.txpow = LMIC.txpow_limit;
+#ifdef JOIN_REQ_DEBUG
+    printf("%d (125khz)\r\n", LMIC.freq);
+#endif /* JOIN_REQ_DEBUG */    
         return;
     }
     
+    /* using 500KHz channel */
     if (LMIC.txpow_limit >= 26)
         LMIC.txpow = 26;
     else
@@ -803,9 +816,15 @@
         
     if( chnl < 64+8 ) {
         LMIC.freq = US915_500kHz_UPFBASE + (chnl-64)*US915_500kHz_UPFSTEP;
+#ifdef JOIN_REQ_DEBUG
+    printf("%d (500k)\r\n", LMIC.freq);
+#endif /* JOIN_REQ_DEBUG */            
     } else {
         ASSERT(chnl < 64+8+MAX_XCHANNELS);
         LMIC.freq = LMIC.xchFreq[chnl-72];
+#ifdef JOIN_REQ_DEBUG
+    printf("%d (x)\r\n", LMIC.freq);
+#endif /* JOIN_REQ_DEBUG */           
     }
 
     // Update global duty cycle stats
@@ -900,25 +919,28 @@
 
 static void initJoinLoop (void) {
     LMIC.chRnd = 0;
-    _nextTx();  //LMIC.txChnl = 0;
-    LMIC.adrTxPow = 20;
+    LMIC.txChnl = 0;
+    LMIC.joinBlockChnl = 0;
+    LMIC.joinBlock = 0;
+    LMIC.datarate = DR_SF10;
+    LMIC.adrTxPow = LMIC.txpow_limit;
     ASSERT((LMIC.opmode & OP_NEXTCHNL)==0);
     LMIC.txend = os_getTime();
     setDrJoin(DRCHG_SET, DR_SF7);
 }
 
 static ostime_t nextJoinState (void) {
+    u1_t failed = 0;
+    
+#if 0
     // Try the following:
     //   SF7/8/9/10  on a random channel 0..63
     //   SF8C        on a random channel 64..71
     //
-    u1_t failed = 0;
     if( LMIC.datarate != DR_SF8C ) {
-        //LMIC._txChnl = 64+(LMIC.txChnl&7);
         _nextTx();
         setDrJoin(DRCHG_SET, DR_SF8C);
     } else {
-        //LMIC._txChnl = os_getRndU1() & 0x3F;
         _nextTx();
         s1_t dr = DR_SF7 - ++LMIC.txCnt;
         if( dr < DR_SF10 ) {
@@ -927,6 +949,26 @@
         }
         setDrJoin(DRCHG_SET, dr);
     }
+#endif /* #if 0 */
+
+    if( LMIC.datarate == DR_SF8C ) {
+        // attempted 500khz channel, try 125khz channel in next block
+        LMIC.datarate = DR_SF10;
+        if (++LMIC.joinBlock == 8) {
+            LMIC.joinBlock = 0;
+            if (++LMIC.joinBlockChnl == 8)
+                LMIC.joinBlockChnl = 0;
+        }
+        LMIC.txChnl = (LMIC.joinBlock << 3) + LMIC.joinBlockChnl;
+    } else {
+        // attempted 125khz channel, try 500khz channel
+        LMIC.datarate = DR_SF8C;
+        LMIC.txChnl = LMIC.joinBlock + 64;
+    }
+#ifdef JOIN_REQ_DEBUG
+    printf("njs blk%d, dr%d, txChnl%d ", LMIC.joinBlock, LMIC.datarate, LMIC.txChnl); // crlf in updateTx()
+#endif /* JOIN_REQ_DEBUG */
+    
     LMIC.opmode &= ~OP_NEXTCHNL;
     LMIC.txend = os_getTime() +
         (isTESTMODE()
@@ -1419,6 +1461,7 @@
                            e_.info   = dlen < 4 ? 0 : mic,
                            e_.info2  = hdr + (dlen<<8)));
       badframe:
+        printf("pja badframe dlen:%d, hdr:%02x\r\n", dlen, hdr);
         if( (LMIC.txrxFlags & TXRX_DNW1) != 0 )
             return 0;
         goto nojoinframe;
@@ -1436,11 +1479,7 @@
 
 #if defined(CFG_eu868)
     initDefaultChannels(0);
-#endif
     if( dlen > LEN_JA ) {
-#if defined(CFG_us915)
-        goto badframe;
-#endif
         dlen = OFF_CFLIST;
         for( u1_t chidx=3; chidx<8; chidx++, dlen+=3 ) {
             u4_t freq = convFreq(&LMIC.frame[dlen]);
@@ -1448,6 +1487,29 @@
                 LMIC_setupChannel(chidx, freq, 0, -1);
         }
     }
+#elif defined(CFG_us915)
+#ifdef JA_DEBUG
+    debug_buf(LMIC.frame, dlen);
+#endif /* JA_DEBUG */
+    /*                                                     11 12  13 14  15 16  17 18  19 1a  1b 1c  1d 1e 1f  20 */
+    /* 20 27 2a b1 01 00 00 d0 44 76 03 00 00 0c c5 59 6c, xx xx, xx xx, xx xx, xx xx, xx xx, xx xx, xx xx xx, xx */
+    /*  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 10,  {0} ,  {1} ,  {2} ,  {3} ,  {4} ,  {5} ,   res   , type */
+    /* join accept: [0] to [16], CFList: [17] to [32] */
+    if (dlen == LEN_JAEXT) {
+#ifdef JA_DEBUG        
+        printf("cflistType:%d\r\n", LMIC.frame[32]);
+#endif /* JA_DEBUG */
+        if (LMIC.frame[32] == 1) {
+            u2_t *u2_ptr = (u2_t *)&LMIC.frame[17]; // LoRaWAN is little endian
+            for (u1_t idx = 0; idx < 5; idx++)
+                LMIC.channelMap[idx] = u2_ptr[idx];
+#ifdef JA_DEBUG                
+            for (u1_t idx = 0; idx < 5; idx++)
+                printf("%d map %04x\r\n", idx, LMIC.channelMap[idx]);
+#endif /* JA_DEBUG */                
+        }
+    }
+#endif /* CFG_us915 */
 
     // already incremented when JOIN REQ got sent off
     aes_sessKeys(LMIC.devNonce-1, &LMIC.frame[OFF_JA_ARTNONCE], LMIC.nwkKey, LMIC.artKey);
@@ -1627,6 +1689,7 @@
 
     if( LMIC.txCnt == 0 ) {
         LMIC.seqnoUp += 1;
+        //printf("seqnoUp inc %d\r\n", LMIC.seqnoUp);
         DO_DEVDB(LMIC.seqnoUp,seqnoUp);
     } else {
         EV(devCond, INFO, (e_.reason = EV::devCond_t::RE_TX,
@@ -2121,7 +2184,7 @@
     LMIC.devNonce     =  os_getRndU2();
     LMIC.opmode       =  OP_NONE;
     LMIC.errcr        =  CR_4_5;
-    LMIC.adrEnabled   =  FCT_ADREN;
+    LMIC.adrEnabled   =  0; /* FCT_ADREN */
     LMIC.dn2Dr        =  DR_DNW2;   // we need this for 2nd DN window of join accept
     LMIC.dn2Freq      =  FREQ_DNW2; // ditto
     LMIC.ping.freq    =  FREQ_PING; // defaults for ping
@@ -2241,4 +2304,11 @@
     LMIC.adrAckReq = enabled ? LINK_CHECK_INIT : LINK_CHECK_OFF;
 }
 
+void LMIC_reverse_memcpy(u1_t *dst, const u1_t *src, size_t n)
+{
+    size_t i;
+
+    for (i=0; i < n; ++i)
+        dst[n-1-i] = src[i];    
+}
  
--- a/lmic.h	Mon Sep 21 17:59:42 2015 +0000
+++ b/lmic.h	Wed Oct 21 01:03:34 2015 +0000
@@ -174,6 +174,8 @@
     u1_t        datarate;     // current data rate
     u1_t        errcr;        // error coding rate (used for TX only)
     u1_t        rejoinCnt;    // adjustment for rejoin datarate
+    u1_t        joinBlock;    // during join attempt: current channel block
+    u1_t        joinBlockChnl;    // during join attempt: current 125KHz channel
     s2_t        drift;        // last measured drift
     s2_t        lastDriftDiff;
     s2_t        maxDriftDiff;
@@ -259,6 +261,7 @@
 void LMIC_setSession (u4_t netid, devaddr_t devaddr, xref2u1_t nwkKey, xref2u1_t artKey);
 void LMIC_setLinkCheckMode (bit_t enabled);
 
+void LMIC_reverse_memcpy(u1_t *dst, const u1_t *src, size_t len);
 // Special APIs - for development or testing
 // !!!See implementation for caveats!!!
 
--- a/radio.cpp	Mon Sep 21 17:59:42 2015 +0000
+++ b/radio.cpp	Wed Oct 21 01:03:34 2015 +0000
@@ -399,7 +399,33 @@
 
 DigitalOut pd2(PD_2);   // power amplifier voltage control pin
 
+/*     pd2=0   pd2=1
+op pab  rfo     rfo
+0  4.6  18.5    27.0
+1  5.6  21.1    28.1
+2  6.7  23.3    29.1
+3  7.7  25.3    30.1
+4  8.8  26.2    30.7
+5  9.8  27.3    31.2
+6  10.7 28.1    31.6
+7  11.7 28.6    32.2
+8  12.8 29.2    32.4
+9  13.7 29.9    32.9
+10 14.7 30.5    33.1
+11 15.6 30.8    33.4
+12 16.4 30.9    33.6
+13 17.1 31.0    33.7
+14 17.8 31.1    33.7
+15 18.4 31.1    33.7
+*/
+//                     txpow:   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14  15  16  17  18  19
+static const u1_t boost_table[20] = { 0, 0, 0, 0, 0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15 };
+
+//                     txpow:  20 21 22 23 24 25 26 27 28 29 30
+static const u1_t   rfo_table[11] = { 1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 9 }; 
+
 static void configPower () {
+    printf("txpow:%d ", LMIC.txpow);
 #ifdef CFG_sx1276_radio
     // no boost used for now
     s1_t pw = (s1_t)LMIC.txpow;
@@ -413,30 +439,17 @@
     writeReg(RegPaDac, readReg(RegPaDac)|0x4);
 
 #elif defined(CFG_sx1272_radio)
-    // set PA config (2-17 dBm using PA_BOOST)
-    s1_t pw = (s1_t)LMIC.txpow;
-    /*if(pw > 17) {
-        pw = 17;
-    } else if(pw < 2) {
-        pw = 2;
-    }*/
+
     /* NA-mote TX power config: */
-    if (pw > 23) {
-        // use RFO with external amplifier
+    if (LMIC.txpow > 19) {
         PaSelect = 0x00;    // use RFO
-        writeReg(RegPaConfig, (u1_t)(PaSelect|(pw-25)));
-        if (pw > 26)
-            pd2 = 1;
-        else
-            pd2 = 0;
+        writeReg(RegPaConfig, PaSelect | rfo_table[LMIC.txpow-20]);
+        pd2 = 0;
     } else {
-        // use PA_BOOST direct to antenna
         PaSelect = 0x80;    // use PA_BOOST 
-        if (pw > 17)
-            pw = 17;
-        writeReg(RegPaConfig, (u1_t)(PaSelect|(pw-2)));
+        writeReg(RegPaConfig, PaSelect | boost_table[LMIC.txpow]);
     }
-    
+    //printf("PaConfig:%02x\r\n", readReg(RegPaConfig));
     
 #else
 #error Missing CFG_sx1272_radio/CFG_sx1276_radio