NAMote72 Utility Application – Serial Terminal Monitor control for NAMote72 (note: this application replaces the previous na_mote1 test code application)

Dependencies:   SX127x lib_gps lib_mma8451q lib_mpl3115a2 lib_sx9500 mbed

Fork of na_mote1 by wayne roberts

See wiki Page for a detailed

This is a link to the wiki page

Revision:
8:e3da1dc7b628
Parent:
7:6d5f193b6c2f
Child:
9:dc02ef560bd1
--- a/main.cpp	Wed May 13 20:59:12 2015 +0000
+++ b/main.cpp	Thu May 21 23:23:58 2015 +0000
@@ -6,6 +6,8 @@
 #include "sx9500.h"
 #include "gps.h"
 
+#define LORA_WAN_ENABLE
+
 #define RADIO_RESET              PC_2 //NorAm_Mote Reset_sx
 #define RADIO_MOSI               PB_15 //NorAm_Mote SPI2 Mosi
 #define RADIO_MISO               PB_14 //NorAm_Mote SPI2 Miso
@@ -83,6 +85,13 @@
 
 app_e app = APP_NONE;
 
+#ifdef LORA_WAN_ENABLE
+#define CFG_us915
+#include "oslmic.h"
+#include "lorabase.h"
+char mic_check;
+#endif /* LORA_WAN_ENABLE */
+
 #define FSK_LARGE_PKT_THRESHOLD  0x3f
 
 /******************************************************************************/
@@ -1075,7 +1084,36 @@
     printf("\r\n");
 }
 
-void print_rx_verbose(uint8_t len)
+#ifdef LORA_WAN_ENABLE
+static const u1_t DEVKEY[16] = {
+        0x03, 0xfb, 0xb9, 0xb1, 0xa0, 0x62, 0xe0, 0x80, 0x8d, 0xa1, 0x9a, 0xf4, 0x05, 0xb1, 0xa1, 0x13
+//<APPKEY>03-   FB   -B9   -B1   -A0   -62   -E0   -80   -8D   -A1   -9A   -F4   -05   -B1   -A1   -13</APPKEY>
+};
+// provide device key (16 bytes)
+void os_getDevKey (u1_t* buf) {
+    memcpy(buf, DEVKEY, 16);
+}
+
+static void aes_encrypt (xref2u1_t pdu, int len) {
+    os_getDevKey(AESkey);
+    os_aes(AES_ENC, pdu, len);
+}
+
+u4_t os_rmsbf4 (xref2cu1_t buf) {
+    return (u4_t)(buf[3] | (buf[2]<<8) | ((u4_t)buf[1]<<16) | ((u4_t)buf[0]<<24));
+}
+u4_t calc_mic;
+u4_t rx_mic;
+static int aes_verifyMic0 (xref2u1_t pdu, int len) {
+    os_getDevKey(AESkey);
+    calc_mic = os_aes(AES_MIC|AES_MICNOAUX, pdu, len);
+    rx_mic = os_rmsbf4(pdu+len);
+    return calc_mic == rx_mic;
+    //return os_aes(AES_MIC|AES_MICNOAUX, pdu, len) == os_rmsbf4(pdu+len);
+}
+#endif /* LORA_WAN_ENABLE */
+
+void print_rx_verbose(uint8_t dlen)
 {
     float dbm;
     printLoraIrqs_(false);
@@ -1083,6 +1121,7 @@
         lora.RegHopChannel.octet = radio.read_reg(REG_LR_HOPCHANNEL);
         printf("HopCH:%d ", lora.RegHopChannel.bits.FhssPresentChannel);
     }
+    printf("%dHz ", lora.get_freq_error_Hz());    
     lora_printCodingRate(true);  // true: of received packet
     dbm = lora.get_pkt_rssi();
     printf(" crc%s %.1fdB  %.1fdBm\r\n",
@@ -1090,7 +1129,36 @@
         lora.RegPktSnrValue / 4.0,
         dbm
     );
-    print_rx_buf(/*lora.RegRxNbBytes*/len);    
+    print_rx_buf(/*lora.RegRxNbBytes*/dlen);  
+    
+#ifdef LORA_WAN_ENABLE
+    if (mic_check) {    /* LoraWAN MIC check (join accept check) */
+        int a, d;
+        u1_t hdr;
+        printf("mic_check ");
+        if (dlen != LEN_JA && dlen != LEN_JAEXT) {
+            printf("dlen fail\r\n");
+            return;
+        }
+        hdr = radio.rx_buf[0];
+        if ((hdr & (HDR_FTYPE|HDR_MAJOR)) != (HDR_FTYPE_JACC|HDR_MAJOR_V1) ) {   
+            printf("hdr fail\r\n");
+            return;
+        } 
+        
+        aes_encrypt(radio.rx_buf+1, dlen-1);
+        if (!aes_verifyMic0(radio.rx_buf, dlen-4) ) {       
+            printf("%08x != %08x fail\r\n", calc_mic, rx_mic);
+        } else
+            printf("%08x == %08x\r\n", calc_mic, rx_mic);
+            
+        for (a = 0x0d; a < 0x40; a++) {
+            d = radio.read_reg(a);
+            //update_shadow_regs(selected_radio, a, d); 
+            printf("%02x: %02x\r\n", a, d);
+        }            
+    }
+#endif /* LORA_WAN_ENABLE */
 }
     
 void
@@ -1437,6 +1505,8 @@
                 }
                 break;
             case '?':
+                printf("r %%x       read sx1272 register (addr)\r\n");
+                printf("w %%x %%x   write sx1272 register (addr data)\r\n");
                 printf("ge          toggle GPS enable\r\n");
                 printf("L           toggle LongRangeMode/FSK\r\n");
                 printf("i           radio_init\r\n");
@@ -1449,7 +1519,7 @@
                 printf("frf[%%f]    get/set operating frequency (MHz)\r\n");
                 printf("pd2         toggle PA_High_Power\r\n");
                 printf("bgr[%%d]        get/set prog_txdac BGR bias for TXDAC (7=+20dBm)\r\n"); 
-                printf("per         toggle PER enable\r\n");
+                printf("per         toggle PER enable (\"tx\" to start, ctrl-C to stop)\r\n");
                 printf("pin[%%f]         get/set per_tx_delay (seconds)\r\n");    
                 printf("pid[%%d]        get/set PER device ID\r\n");
                 printf("hop         change hop type (off, 64ch, 4ch)\r\n");
@@ -1463,6 +1533,7 @@
                     printf("T           LORA toggle TxContinuousMode\r\n");
                     printf("hp[%%d]     LORA get/set hop period\r\n");
                     printf("hm          LORA toggle explicit/explicit header mode\r\n");
+                    printf("in          LORA toggle invert_i_q\r\n");
                     printf("ld          LORA toggle LowDataRateOptimize\r\n");
                 } else {
                     printf("bw[a][%%d] FSK get-set rxbw (bwa=afcbw)\r\n");
@@ -1484,6 +1555,9 @@
                     printf("ag          FSK toggle AgcAutoOn\r\n");
                     printf("pre[%%d}    FSK get-set TX preamble length\r\n");
                 }
+                printf("mp?     MPL3115 help (tempurature/pressure sensor)\r\n");
+                printf("mm?     MMA8451 help (accelerometer)\r\n");
+                printf("95?     SX9500 help (touch sensor)\r\n");
                 break;
             case '.':
                 if (radio.RegOpMode.bits.LongRangeMode)
@@ -1772,6 +1846,12 @@
             }
         } 
 #endif /* !I2C_PIN_TEST */
+#ifdef LORA_WAN_ENABLE
+        else if (pcbuf[0] == 'm' && pcbuf[1] == 'i') {
+            mic_check ^= 1;
+            printf("mic_check:%d\r\n", mic_check);
+        }
+#endif /* LORA_WAN_ENABLE */        
         else if (pcbuf[0] == 'm' && pcbuf[1] == 'p' && !radio.RegOpMode.bits.LongRangeMode) {
             radio.RegDioMapping2.bits.MapPreambleDetect ^= 1;
             radio.write_reg(REG_DIOMAPPING2, radio.RegDioMapping2.octet);
@@ -2043,6 +2123,12 @@
                 printf("LowDataRateOptimize:%d\r\n", lora.RegModemConfig3.sx1276bits.LowDataRateOptimize); 
                 radio.write_reg(REG_LR_MODEMCONFIG3, lora.RegModemConfig3.octet);
             }            
+        } else if (pcbuf[0] == 'i' && pcbuf[1] == 'n' && radio.RegOpMode.bits.LongRangeMode) {
+            RegTest33_t test33;
+            test33.octet = radio.read_reg(REG_LR_TEST33);
+            test33.bits.invert_i_q ^= 1;
+            radio.write_reg(REG_LR_TEST33, test33.octet);
+            printf("invert_i_q:%d\r\n", test33.bits.invert_i_q);            
         } else if (pcbuf[0] == 'd' && pcbuf[1] >= '0' && pcbuf[1] <= '5') {
             switch (pcbuf[1]) {
                 case '0':