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:
6:8defa260bc10
Parent:
5:329557e0625c
Child:
7:6d5f193b6c2f
diff -r 329557e0625c -r 8defa260bc10 main.cpp
--- a/main.cpp	Mon Apr 13 23:05:15 2015 +0000
+++ b/main.cpp	Fri May 08 01:44:19 2015 +0000
@@ -6,6 +6,22 @@
 #include "sx9500.h"
 #include "gps.h"
 
+#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
+#define RADIO_SCLK               PB_13 //NorAm_Mote  SPI2 Clk
+#define RADIO_NSS                PB_12 //NorAm_Mote SPI2 Nss
+
+#define RADIO_DIO_0              PC_6 //NorAm_Mote DIO0 
+#define RADIO_DIO_1              PC_10 //NorAm_Mote DIO1 
+#define RADIO_DIO_2              PC_8 //NorAm_Mote DIO2 
+#define RADIO_DIO_3              PB_4 //NorAm_Mote DIO3 
+#define RADIO_DIO_4              PB_5 //NorAm_Mote DIO4 
+#define RADIO_DIO_5              PB_6 //NorAm_Mote DIO5
+
+#define RFSW1                    PC_4 //NorAm_Mote RFSwitch_CNTR_1
+#define RFSW2                    PC_13 //NorAm_Mote RFSwitch_CNTR_2
+
 /*
  *
  */
@@ -15,19 +31,19 @@
     DigitalInOut pb9(PB_9);
 #else
     I2C i2c(I2C_SDA, I2C_SCL);
-    MMA8451Q mma8451q(i2c);
-    //MPL3115A2 mpl3115a2(i2c);
-    SX9500 sx9500(i2c);
+    DigitalIn i2c_int_pin(RADIO_DIO_3);
+    MMA8451Q mma8451q(i2c, i2c_int_pin);
+    MPL3115A2 mpl3115a2(i2c, i2c_int_pin);
+    SX9500 sx9500(i2c, PA_9, PA_10);
 #endif /* I2C_PIN_TeST */
 
-GPS gps;
+/*  gps(tx, rx, en); */
+GPS gps(PB_6, PB_7, PB_11);
 DigitalOut pd2(PD_2);
 DigitalOut hdr_fem_ctx(PC_7);
 DigitalOut hdr_fem_csd(PC_0);
-DigitalOut sx9500_txen(PA_9);
 
 DigitalOut red_led(PB_1);
-DigitalOut green_led(PB_7);
 #define LED_ON  0
 #define LED_OFF 1
 
@@ -58,6 +74,7 @@
 
 uint8_t tx_cnt;
 char pcbuf[64];
+bool rx_after_tx;
 
 typedef enum {
     APP_NONE = 0,
@@ -69,21 +86,7 @@
 #define FSK_LARGE_PKT_THRESHOLD  0x3f
 
 /******************************************************************************/
-#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
-#define RADIO_SCLK               PB_13 //NorAm_Mote  SPI2 Clk
-#define RADIO_NSS                PB_12 //NorAm_Mote SPI2 Nss
 
-#define RADIO_DIO_0              PC_6 //NorAm_Mote DIO0 
-#define RADIO_DIO_1              PC_10 //NorAm_Mote DIO1 
-#define RADIO_DIO_2              PC_8 //NorAm_Mote DIO2 
-#define RADIO_DIO_3              PB_4 //NorAm_Mote DIO3 
-#define RADIO_DIO_4              PB_5 //NorAm_Mote DIO4 
-#define RADIO_DIO_5              PB_6 //NorAm_Mote DIO5
-
-#define RFSW1                    PC_4 //NorAm_Mote RFSwitch_CNTR_1
-#define RFSW2                    PC_13 //NorAm_Mote RFSwitch_CNTR_2
 
 //  pin:       3     8     1      7    10    12     5   20    18
 //             mosi,      miso,      sclk,       cs,        rst,        dio0,         dio1
@@ -94,6 +97,8 @@
 DigitalOut rfsw1(RFSW1);
 DigitalOut rfsw2(RFSW2);
 
+
+
 void rfsw_callback()
 {
     if (radio.RegOpMode.bits.Mode == RF_OPMODE_TRANSMITTER) {  // start of transmission
@@ -961,7 +966,7 @@
         printf("per_tx_delay:%f\n", per_tx_delay);
         printf("PER device ID:%d\n", per_id);
     }
-
+    printf("GPS enabled:%d\r\n", gps.enabled());
 }
 
 void per_cb()
@@ -989,7 +994,7 @@
 
 void dio3_cb()
 {
-    green_led = LED_ON;
+    //green_led = LED_ON;
     clear_valid_header = true;
 }
 
@@ -1099,7 +1104,7 @@
     
         switch (act) {
             case SERVICE_READ_FIFO:
-                green_led = LED_OFF; // ValidHeader indication
+                //green_led = LED_OFF; // ValidHeader indication
                                    
                 if (app == APP_NONE) {   
                     if (per_en) {
@@ -1163,6 +1168,8 @@
                     lora.start_rx();
                 } else if (per_en)
                     per_timeout.attach(&per_cb, per_tx_delay); // start next TX
+                else if (rx_after_tx)
+                    lora.start_rx();
                 break;
             case SERVICE_ERROR:
                 printf("error\r\n");
@@ -1212,6 +1219,18 @@
     }    
 }
 
+void
+gps_service()
+{
+    gps.service();
+    if (gps.enabled()) {
+        if (gps.LatitudeBinary != 0) {
+            gps.LatitudeBinary = 0;
+            printf("gps long:%f, lat:%f\r\n", gps.Longitude, gps.Latitude);
+        }
+    }
+}
+
 int get_kbd_str(char* buf, int size)
 {
     char c;
@@ -1247,7 +1266,10 @@
             }
         } else {
             service_radio();
-            gps.service();
+            gps_service();
+            sx9500.service();
+            mma8451q.service();
+            mpl3115a2.service();
         }
     } // ...for()
 }
@@ -1488,6 +1510,10 @@
                         radio.tx_buf[i] = tx_cnt;
                     red_led = LED_ON;
                     lora.start_tx(lora.RegPayloadLength);
+                    if (pcbuf[2] == 'r')
+                        rx_after_tx = true;
+                    else
+                        rx_after_tx = false;
                 }
             } else {    // FSK:
                 if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
@@ -1569,7 +1595,7 @@
         } else if (pcbuf[0] == 'r' && pcbuf[1] == 'x') { // RX
             if (per_en) {
                 red_led = LED_OFF;
-                green_led = LED_OFF;
+                //green_led = LED_OFF;
                 PacketNormalCnt = 0;
                 PacketRxSequencePrev = -1;
                 PacketPerKoCnt = 0;
@@ -1630,28 +1656,76 @@
             printf("\n");                      
         }
 #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') {
-                if (pcbuf[3] == '0' || pcbuf[3] == '1')
-                    mma8451q.set_active(pcbuf[3] - '0');
-                else  
-                    printf("active:%d\n", mma8451q.get_active());
-            } else
-                mma8451q.try_read();
-        } /*else if (pcbuf[0] == 'm' && pcbuf[1] == 'p' && pcbuf[2] == 'l') {
-            mpl3115a2.try_read();
-        }*/ else if (pcbuf[0] == '9' && pcbuf[1] == '5') {
+        /************************** MMA8451Q... **************************************/
+        else if (pcbuf[0] == 'm' && pcbuf[1] == 'm') {
+            if (pcbuf[2] == '?') {
+                printf("mm.     get status\r\n");
+                printf("mm,     print XYZ\n");
+                printf("mma     toggle stby/active\n");
+                printf("mmtd     configure for transient detection\n");
+            } else if (pcbuf[2] == '.') {
+                printf("active:%d\n", mma8451q.get_active());
+                mma8451q.print_regs();
+                printf("i2c_int_pin:%d\r\n", i2c_int_pin.read());
+            } else if (pcbuf[2] == ',') {
+                mma8451q.read(MMA8451_OUT_X_MSB, mma8451q.out.octets, 6);
+                mma8451q.out.v.x >>= 4; // 12bit data
+                mma8451q.out.v.y >>= 4; // 12bit data
+                mma8451q.out.v.z >>= 4; // 12bit data
+                printf("x:%d y:%d z:%d\n", mma8451q.out.v.x, mma8451q.out.v.y, mma8451q.out.v.z);
+            } else if (pcbuf[2] == 't' && pcbuf[3] == 'd') {   
+                printf("transient ");
+                if (mma8451q.transient_cfg.bits.ELE) {
+                    mma8451q.transient_cfg.octet = 0;
+                    mma8451q.write(MMA8451_TRANSIENT_CFG, mma8451q.transient_cfg.octet);
+                    mma8451q.ctrl_reg4.bits.INT_EN_TRANS = 0;
+                    mma8451q.write(MMA8451_CTRL_REG4, mma8451q.ctrl_reg4.octet);                    
+                    printf("off\n");
+                } else {
+                    mma8451q.transient_detect();
+                    //poll_timeout.attach(on_poll, 0.3);
+                    printf("on\n");
+                }                         
+            } else if (pcbuf[2] == 'a') {
+                if (mma8451q.get_active()) {
+                    mma8451q.set_active(0);
+                } else {
+                    mma8451q.set_active(1);
+                }
+                printf("active:%d\n", mma8451q.get_active());
+            }
+        }
+        /************************** MPL3115... **************************************/
+        else if (pcbuf[0] == 'm' && pcbuf[1] == 'p') {
+            if (pcbuf[2] == '?') {
+                printf("mp.     get status\r\n");
+                printf("mpt     get temperature\r\n");
+                printf("mpa     get altitude\r\n");
+            } else if (pcbuf[2] == '.') {
+                printf("active:%d\r\n", mpl3115a2.GetModeActive());
+                printf("int src:%02x\r\n", mpl3115a2.read(INT_SOURCE_REG));
+                /* only INT1 is connected */
+                printf("CTRL3 int ctl:%02x\r\n", mpl3115a2.read(CTRL_REG3));    /* TODO: PP_OD1 for open-drain operation */
+                printf("CTRL4 int  en:%02x\r\n", mpl3115a2.read(CTRL_REG4));
+                printf("CTRL5 int cfg:%02x\r\n", mpl3115a2.read(CTRL_REG5));
+                printf("i2c_int_pin:%d\r\n", i2c_int_pin.read());
+            } else if (pcbuf[2] == 't') {
+                printf("temp:%f\r\n", mpl3115a2.ReadTemperature());
+            } else if (pcbuf[2] == 'a') {
+                printf("alt:%f\r\n", mpl3115a2.ReadAltitude());
+                
+            }
+        }
+        /************************** sx9500... **************************************/
+        else if (pcbuf[0] == '9' && pcbuf[1] == '5') {
             if (pcbuf[2] == '?') {
                 printf("95R     reset\n");
-                printf("95s     read sensor\n");
                 printf("95.     read status\n");
-                printf("95S     put to sleep\n");
+                printf("95t[%%d]   get/set PROXTHRESH\r\n");
+                printf("95s[%%d]   get/set SCANPERIOD\r\n");
+                printf("95a         toggle txen\r\n");
             } else if (pcbuf[2] == '.') {
-                printf("RegIrqSrc:%02x\n", sx9500.read_single(SX9500_REG_IRQSRC));
+                printf("(txen) active:%d\r\n", sx9500.get_active());
                 printf("RegStat:%02x\n", sx9500.read_single(SX9500_REG_STAT));
                 printf("RegProxCtrl0:%02x\n", sx9500.read_single(SX9500_REG_PROXCTRL0));
                 printf("RegProxCtrl1:%02x\n", sx9500.read_single(SX9500_REG_PROXCTRL1));
@@ -1660,14 +1734,42 @@
                 printf("RegProxCtrl4:%02x\n", sx9500.read_single(SX9500_REG_PROXCTRL4));
                 printf("RegProxCtrl5:%02x\n", sx9500.read_single(SX9500_REG_PROXCTRL5));
                 printf("RegProxCtrl6:%02x\n", sx9500.read_single(SX9500_REG_PROXCTRL6));
-            } else if (pcbuf[2] == 's') {
-                printf("sensor:%d\n", sx9500.get_sensor());
+            } else if (pcbuf[2] == 'a') {
+                if (sx9500.get_active()) {
+                    sx9500.set_active(false);
+                } else {
+                    sx9500.RegProxCtrl0.octet = sx9500.read_single(SX9500_REG_PROXCTRL0);
+                    sx9500.RegProxCtrl0.bits.sensor_en = 3;    // CS0 and CS1 on
+                    sx9500.write(SX9500_REG_PROXCTRL0, sx9500.RegProxCtrl0.octet);
+                    sx9500.write(SX9500_REG_PROXCTRL6, 1);  // threshold to 20 for CS1 release
+                    sx9500.write(SX9500_REG_IRQMSK, 0x60);     // enable near and far interrupts
+                    sx9500.set_active(true);
+                    printf("RegProxCtrl0:%02x\n", sx9500.read_single(SX9500_REG_PROXCTRL0));    /* sensor-enable and scan period */
+                }
+                printf("(txen) active:%d\r\n", sx9500.get_active());
             } else if (pcbuf[2] == 'R') {
                 sx9500.reset();
-            } else if (pcbuf[2] == 'S') {
-                sx9500.write(SX9500_REG_PROXCTRL0, 0);  // put SENSOREN=0
-                printf("sx9500 sleep\r\n");
-            }        
+            } else if (pcbuf[2] == 'i') {
+                if (pcbuf[3] != 0) {
+                    sscanf(pcbuf+3, "%x", &i);
+                    sx9500.write(SX9500_REG_IRQMSK, i);
+                }
+                printf("irqmsk:%02x\r\n", sx9500.read_single(SX9500_REG_IRQMSK));
+            } else if (pcbuf[2] == 't') {
+                if (pcbuf[3] >= '0' && pcbuf[3] <= '9') {
+                    sscanf(pcbuf+3, "%d", &i);
+                    sx9500.write(SX9500_REG_PROXCTRL6, i);           
+                }   
+                printf("proxthresh:0x%x\r\n", sx9500.read_single(SX9500_REG_PROXCTRL6));
+            } else if (pcbuf[2] == 'p') {
+                if (pcbuf[3] >= '0' && pcbuf[3] <= '9') {
+                    sscanf(pcbuf+2, "%d", &i);
+                    sx9500.RegProxCtrl0.bits.scan_period = i;
+                    sx9500.write(SX9500_REG_PROXCTRL0, sx9500.RegProxCtrl0.octet);
+                }
+                sx9500.RegProxCtrl0.octet = sx9500.read_single(SX9500_REG_PROXCTRL0);
+                printf("scan period:%d\r\n", sx9500.RegProxCtrl0.bits.scan_period);
+            }
         } 
 #endif /* !I2C_PIN_TEST */
         else if (pcbuf[0] == 'm' && pcbuf[1] == 'p' && !radio.RegOpMode.bits.LongRangeMode) {
@@ -1974,18 +2076,25 @@
                 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");
+                //gps.enable(!gps.enabled());
+                if (gps.enabled()) {
+                    gps.enable(false);
+                    printf("gps disabled\r\n");
+                } else {
+                    gps.enable(true);
+                    printf("GPS enabled\r\n");
+                }
+            } else if (pcbuf[1] == 'v') {
+                gps.verbose ^= 1;
+                printf("gps.verbose:%d\r\n", gps.verbose);
             }
         } else if (pcbuf[0] == 's' && pcbuf[1] == 't' && pcbuf[2] == 'b') {
             radio.set_opmode(RF_OPMODE_STANDBY);
-            green_led = LED_OFF;
+            //green_led = LED_OFF;
             red_led = LED_OFF;
         } else if (pcbuf[0] == 's' && pcbuf[1] == 'l' && pcbuf[2] == 'e') {
             radio.set_opmode(RF_OPMODE_SLEEP);
-            green_led = LED_OFF;
+            //green_led = LED_OFF;
             red_led = LED_OFF;
         } else if (pcbuf[0] == 'c' && pcbuf[1] == 'h' && pcbuf[2] == 'a') {
             app = APP_CHAT;
@@ -2005,15 +2114,24 @@
     gps.init();
 
     printf("\nreset\n");    
-    green_led = LED_OFF;
+    //green_led = LED_OFF;
     red_led = LED_OFF;
     
     radio.rf_switch.attach(rfsw_callback);
     
     if (radio.RegOpMode.bits.LongRangeMode)
         last_RxCurrentAddr = radio.read_reg(REG_LR_FIFORXCURRENTADDR);
-        
-    sx9500_txen = 0;
+    
+    gps.enable(false);
+    
+    sx9500.RegProxCtrl0.bits.sensor_en = 3; // only CS0 and CS1 connected
+    sx9500.write(SX9500_REG_PROXCTRL0, sx9500.RegProxCtrl0.octet);
+    
+    // set PROXTHRESH to 80 because CS1 has 48 showing always on PROXDIFF
+    sx9500.write(SX9500_REG_PROXCTRL6, 0x04);
+    
+    mma8451q.set_active(0);
+    mpl3115a2.SetModeStandby();
 
     while(1) {
         switch (app) {