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:
3:8924027a8933
Parent:
2:fb41d1c4b299
Child:
4:1efa46cff1b3
diff -r fb41d1c4b299 -r 8924027a8933 main.cpp
--- a/main.cpp	Tue Oct 07 22:16:43 2014 +0000
+++ b/main.cpp	Fri Oct 31 22:52:58 2014 +0000
@@ -4,6 +4,7 @@
 #include "mma8451q.h"
 #include "mpl3115a2.h"
 #include "sx9500.h"
+#include "gps.h"
 
 /*
  *
@@ -19,24 +20,37 @@
     SX9500 sx9500(i2c);
 #endif /* I2C_PIN_TeST */
 
+GPS gps;
 DigitalOut pd2(PD_2);
+DigitalOut hdr_fem_ctx(PC_7);
 
 DigitalOut red_led(PB_1);
 DigitalOut green_led(PB_7);
 #define LED_ON  0
 #define LED_OFF 1
 
+//volatile uint32_t dio2_cnt = 0;
+//InterruptIn dio2(PC_11);
+Timeout hop_timeout;
+
 InterruptIn dio3(PC_8);
 bool clear_valid_header;
 
+typedef enum {
+    HOP_TYPE_NONE = 0,
+    HOP_TYPE_64CH,
+    HOP_TYPE_4CH
+} hop_type_e;
+hop_type_e hop_type;
+float hop_base_MHz = 902.3;
+float hop_step_MHz = 0.2;
+
+
 bool per_en;
 int PacketRxSequencePrev;
-//uint32_t PacketRxSequence;
 uint32_t PacketPerKoCnt;
 uint32_t PacketPerOkCnt;
 uint32_t PacketNormalCnt;
-/*Ticker per_ticker;
-float per_tx_interval = 0.1;*/
 Timeout per_timeout;
 float per_tx_delay = 0.1;
 int per_id;
@@ -52,7 +66,7 @@
 
 app_e app = APP_NONE;
 
-
+#if 0
 const uint32_t frfs[] = {   /* frequency hopping table */
     MHZ_TO_FRF(903.0),
     MHZ_TO_FRF(904.0),
@@ -68,6 +82,7 @@
     MHZ_TO_FRF(914.0),
     MHZ_TO_FRF(915.0)
 };
+#endif /* #if 0 */
 
 #define FSK_LARGE_PKT_THRESHOLD  0x3f
 
@@ -469,8 +484,18 @@
         printf("LowDataRateOptimize:%d\r\n", lora.RegModemConfig3.sx1276bits.LowDataRateOptimize);        
     }
     
+    switch (hop_type) {
+        case HOP_TYPE_NONE:
+            break;
+        case HOP_TYPE_64CH:
+            printf("hop 64ch\n");
+            break;
+        case HOP_TYPE_4CH:
+            printf("hop 4ch\n");
+            break;
+    }
+       
     printf("\r\n");
-    //printf("A %02x\r\n", radio.RegModemConfig2.octet);
 }
 
 uint16_t
@@ -931,7 +956,7 @@
     int i;
     
     PacketTxCnt++;
-    
+
     radio.tx_buf[0] = per_id;
     radio.tx_buf[1] = PacketTxCnt >> 24;
     radio.tx_buf[2] = PacketTxCnt >> 16;
@@ -947,12 +972,53 @@
     lora.start_tx(lora.RegPayloadLength);
 }
 
+
+
 void dio3_cb()
 {
     green_led = LED_ON;
     clear_valid_header = true;
 }
 
+
+float hop_MHz;
+bool new_hop;
+uint8_t hop_ofs = 0;
+
+void hop_cb()
+{
+    static uint8_t prev_ofs;
+    int shift;
+    
+    radio.RegOpMode.octet = radio.read_reg(REG_OPMODE);
+    if (radio.RegOpMode.bits.Mode == RF_OPMODE_TRANSMITTER)
+        hop_timeout.attach(&hop_cb, 0.4);
+    else
+        return;
+    
+    do {
+        shift = rand() & 0x1f;
+        if (hop_type == HOP_TYPE_64CH)
+            hop_ofs = (rand() >> shift) & 0x3f;
+        else if (hop_type == HOP_TYPE_4CH)
+            hop_ofs = (rand() >> shift) & 0x3;
+    } while (hop_ofs == prev_ofs);
+    
+    prev_ofs = hop_ofs; 
+    hop_MHz = hop_base_MHz + (hop_ofs * hop_step_MHz);
+    new_hop = true;
+      
+    radio.set_frf_MHz(hop_MHz);
+
+    radio.set_opmode(RF_OPMODE_STANDBY);              
+    radio.set_opmode(RF_OPMODE_TRANSMITTER);
+    
+    if (hdr_fem_ctx.read())
+        hdr_fem_ctx = 0;
+    else
+        hdr_fem_ctx = 1;
+}
+
 void print_rx_buf(int len)
 {
     int i;
@@ -1101,6 +1167,11 @@
         radio.write_reg(REG_LR_IRQFLAGS, irqs.octet);
         clear_valid_header = false;
     }
+    
+    if (new_hop) {
+        new_hop = false;
+        printf("%02d  %.1f\n", hop_ofs, hop_MHz);
+    }    
 }
 
 int get_kbd_str(char* buf, int size)
@@ -1112,11 +1183,13 @@
     for (i = 0;;) {
         if (pc.readable()) {
             c = pc.getc();
-            if (c == 8 && i > 0) {
-                pc.putc(8);
-                pc.putc(' ');
-                pc.putc(8);
-                i--;
+            if (c == 8) {   // backspace
+                if (i > 0) {
+                    pc.putc(8);
+                    pc.putc(' ');
+                    pc.putc(8);
+                    i--;
+                }
             } else if (c == '\r') {
                 if (i == 0) {
                     return prev_len; // repeat previous
@@ -1135,6 +1208,7 @@
             }
         } else {
             service_radio();
+            gps.service();
         }
     } // ...for()
 }
@@ -1162,6 +1236,8 @@
     }
 }
 
+
+                
 void
 console()
 {
@@ -1203,15 +1279,16 @@
             case 'T':
                 if (radio.RegOpMode.bits.LongRangeMode) {
                     lora.RegModemConfig2.octet = radio.read_reg(REG_LR_MODEMCONFIG2);
-                    //printf("a %02x\r\n", radio.RegModemConfig2.octet);
+                    //printf("a %02x\r\n", lora.RegModemConfig2.octet);
                     lora.RegModemConfig2.sx1276bits.TxContinuousMode ^= 1;   // same for sx1272 and sx1276
-                    //printf("b %02x\r\n", radio.RegModemConfig2.octet);
+                    //printf("b %02x\r\n", lora.RegModemConfig2.octet);
                     radio.write_reg(REG_LR_MODEMCONFIG2, lora.RegModemConfig2.octet);
-                    lora.RegModemConfig2.octet = radio.read_reg(REG_LR_MODEMCONFIG);
-                    //printf("c %02x\r\n", radio.RegModemConfig2.octet);
+                    lora.RegModemConfig2.octet = radio.read_reg(REG_LR_MODEMCONFIG2);
+                    //printf("c %02x\r\n", lora.RegModemConfig2.octet);
                     lora_printTxContinuousMode();
                     printf("\r\n");
-                }
+                } else
+                    printf("(fsk)\n");
                 break;
             case 'C':
                 if (radio.RegOpMode.bits.LongRangeMode) {
@@ -1298,6 +1375,7 @@
                 }
                 break;
             case '?':
+                printf("ge          toggle GPS enable\r\n");
                 printf("L           toggle LongRangeMode/FSK\r\n");
                 printf("i           radio_init\r\n");
                 printf("h           hw_reset\r\n");
@@ -1312,6 +1390,9 @@
                 printf("per         toggle PER enable\n");
                 printf("pin[%%f]         get/set per_tx_delay (seconds)\n");    
                 printf("pid[%%d]        get/set PER device ID\n");
+                printf("hop         change hop type (off, 64ch, 4ch)\n");
+                printf("hb[%%f]     get/set hop base MHz\n");
+                printf("hs[%%f]     get/set hop step MHz\n");
                 if (radio.RegOpMode.bits.LongRangeMode) {
                     printf("pl[%%d]     LORA get/set RegPayloadLength\r\n");
                     printf("cr[1234]    LORA set coding rate \r\n");
@@ -1353,6 +1434,7 @@
         if (pcbuf[0] == 't' && pcbuf[1] == 'x') { // TX
             if (radio.RegOpMode.bits.LongRangeMode) {
                 if (per_en) {
+                    printf("timeout attach %f\n", per_tx_delay);
                     PacketTxCnt = 0;
                     per_timeout.attach(&per_cb, per_tx_delay);
                 } else {
@@ -1379,6 +1461,38 @@
                     fsk.start_tx(fsk_tx_length);
                 }
             }
+            
+            if (hop_type != HOP_TYPE_NONE)
+                hop_timeout.attach(&hop_cb, 0.4);
+        } else if (pcbuf[0] == 'r' && pcbuf[1] == 'n' && pcbuf[2] == 'd') {
+            uint8_t of = rand() & 0x3f;
+
+            printf("%02d %.2f\n", of, 902.3 + (of * 0.2));
+        } else if (pcbuf[0] == 'h' && pcbuf[1] == 'b') {
+            if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
+                sscanf(pcbuf+2, "%f", &hop_base_MHz);
+            }
+            printf("hop_base:%f\n", hop_base_MHz);
+        } else if (pcbuf[0] == 'h' && pcbuf[1] == 's') {
+            if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
+                sscanf(pcbuf+2, "%f", &hop_step_MHz);
+            }
+            printf("hop_step:%f\n", hop_step_MHz);                           
+        } else if (pcbuf[0] == 'h' && pcbuf[1] == 'o' && pcbuf[2] == 'p') {
+            switch (hop_type) {
+                case HOP_TYPE_NONE:
+                    hop_type = HOP_TYPE_64CH;
+                    printf("64ch hop\n");                 
+                    break;
+                case HOP_TYPE_64CH:
+                    hop_type = HOP_TYPE_4CH;
+                    printf("4ch hop\n");                                 
+                    break;
+                case HOP_TYPE_4CH:
+                    hop_type = HOP_TYPE_NONE;
+                    printf("hop off\n");                 
+                    break;
+            }
         } else if (pcbuf[0] == 'h' && pcbuf[1] == 'p' && radio.RegOpMode.bits.LongRangeMode) {
             if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
                 sscanf(pcbuf+2, "%d", &i);
@@ -1473,8 +1587,10 @@
             }
             printf("\n");                      
         }
-#else        
-        else if (pcbuf[0] == 'm' && pcbuf[1] == 'm') {
+#else    
+        else if (pcbuf[0] == 'm' && pcbuf[1] >= '0' && pcbuf[1] <= '9') {    
+
+        } else if (pcbuf[0] == 'm' && pcbuf[1] == 'm') {
             if (pcbuf[2] == 's')
                 mma8451q.status();
             else if (pcbuf[2] == 'a') {
@@ -1775,6 +1891,13 @@
                 lora_print_dio();
             else
                 fsk_print_dio();
+        } else if (pcbuf[0] == 'g') {   /******* GPS... **********/
+            if (pcbuf[1] == 'e') {
+                gps.enable(!gps.enabled());
+            } else if (pcbuf[1] == 's') {
+                gps.send();
+                printf("gps send\n");
+            }
         } else if (pcbuf[0] == 's' && pcbuf[1] == 't' && pcbuf[2] == 'b') {
             radio.set_opmode(RF_OPMODE_STANDBY);
             green_led = LED_OFF;
@@ -1798,11 +1921,14 @@
 {  
 
     pc.baud(57600);
+    gps.init();
     
-    radio.frfs = frfs;
-    
+    //radio.frfs = frfs;
+
+    printf("\nreset\n");    
     green_led = LED_OFF;
     red_led = LED_OFF;
+    
 
     while(1) {
         switch (app) {