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:
13:a22debeee855
Parent:
12:6afe8fed5385
Child:
14:35e7156e73ff
diff -r 6afe8fed5385 -r a22debeee855 main.cpp
--- a/main.cpp	Thu Jul 02 00:14:33 2015 +0000
+++ b/main.cpp	Tue Jul 14 00:47:41 2015 +0000
@@ -44,6 +44,10 @@
 GPS gps(PB_6, PB_7, PB_11);
 DigitalOut pd2(PD_2);
 
+AnalogIn ain_bat(PA_1); // V3 mote: only when GPS enabled
+#define AIN_VREF        3.3     // stm32 internal refernce
+#define AIN_VBAT_DIV    2       // resistor divider
+
 typedef enum {
     MOTE_NONE = 0,
     MOTE_V2,
@@ -52,10 +56,13 @@
 
 mote_version_e mote_version = MOTE_NONE;   
 DigitalOut pc_7(PC_7);
+
 DigitalIn pc_1(PC_1);
 DigitalOut hdr_fem_csd(PC_0);
 
 DigitalOut red_led(PB_1);
+DigitalOut led2(PC_3); // green
+DigitalOut yellow_led(PB_10);
 #define LED_ON  0
 #define LED_OFF 1
 
@@ -64,6 +71,7 @@
 InterruptIn dio3(PC_8);
 bool clear_valid_header;
 
+#ifdef FCC_TEST
 typedef enum {
     HOP_TYPE_NONE = 0,
     HOP_TYPE_64CH,
@@ -72,6 +80,7 @@
 hop_type_e hop_type;
 float hop_base_MHz = 902.3;
 float hop_step_MHz = 0.2;
+#endif /* #ifdef FCC_TEST */
 
 uint32_t repeated_sleep;
 
@@ -136,7 +145,7 @@
         } else {
             // todo: sx1276
         }
-        hdr_fem_csd = 1;    //debug
+        //hdr_fem_csd = 1;    //debug
     } else if (radio.RegOpMode.bits.Mode == RF_OPMODE_RECEIVER || radio.RegOpMode.bits.Mode == RF_OPMODE_CAD) { // start of reception
         if (radio.HF) {
             rfsw2 = 1;
@@ -144,11 +153,11 @@
         } else {
             // todo: sx1276
         }
-        hdr_fem_csd = 0;    //debug
+        //hdr_fem_csd = 0;    //debug
     } else { // RF switch shutdown
         rfsw2 = 0;
         rfsw1 = 0;     
-        hdr_fem_csd = 0;    //debug         
+        //hdr_fem_csd = 0;    //debug         
     }
 }
 
@@ -526,6 +535,7 @@
     
     printf(" invert: rx=%d tx=%d\r\n", lora.RegTest33.bits.invert_i_q, !lora.RegTest33.bits.chirp_invert_tx);
     
+#ifdef FCC_TEST
     switch (hop_type) {
         case HOP_TYPE_NONE:
             break;
@@ -536,7 +546,7 @@
             printf("hop 4ch\r\n");
             break;
     }
-       
+#endif /* #ifdef FCC_TEST */         
     printf("\r\n");
 }
 
@@ -977,9 +987,9 @@
     if (radio.RegOcp.bits.OcpOn) {
         int imax = 0;
         if (radio.RegOcp.bits.OcpTrim < 16)
-            imax = 45 + (5 * radio.RegOcp.bits.OcpTrim);
+            imax = (5 * radio.RegOcp.bits.OcpTrim) + 45;
         else if (radio.RegOcp.bits.OcpTrim < 28)
-            imax = -30 + (10 * radio.RegOcp.bits.OcpTrim);
+            imax = (10 * radio.RegOcp.bits.OcpTrim) - 30;
         else
             imax = 240;
         printf(" OcpOn %dmA ", imax);
@@ -1052,6 +1062,7 @@
     lora.start_tx(lora.RegPayloadLength);
 }
 
+#ifdef FCC_TEST  
 float hop_MHz;
 bool new_hop;
 uint8_t hop_ofs = 0;
@@ -1089,6 +1100,7 @@
     else
         pc_7 = 1;
 }
+#endif /* #ifdef FCC_TEST */  
 
 void print_rx_buf(int len)
 {
@@ -1302,10 +1314,12 @@
         clear_valid_header = false;
     }
     
+#ifdef FCC_TEST    
     if (new_hop) {
         new_hop = false;
         printf("%02d  %.1f\r\n", hop_ofs, hop_MHz);
     }    
+#endif /* #ifdef FCC_TEST */  
 }
 
 void
@@ -1315,7 +1329,7 @@
     if (gps.enabled()) {
         if (gps.LatitudeBinary != 0) {
             gps.LatitudeBinary = 0;
-            printf("gps long:%f, lat:%f\r\n", gps.Longitude, gps.Latitude);
+            printf("gps long:%f, lat:%f  vbat:%.2fV\r\n", gps.Longitude, gps.Latitude, ain_bat.read()*AIN_VREF*AIN_VBAT_DIV);
         }
     }
 }
@@ -1598,10 +1612,13 @@
                 printf("L           toggle LongRangeMode/FSK\r\n");
                 printf("i           radio_init\r\n");
                 printf("h           hw_reset\r\n");
-                printf("tx[%%d]     transmit\r\n");
-                printf("rx          receive\r\n");   
+                printf("tx[%%d]     transmit [length]\r\n");
+                printf("rx          receive\r\n");
+                printf("stb         put radio into standby\r\n");
+                printf("sle         put radio to sleep\r\n");
                 printf("C           toggle crcOn\r\n");
                 printf("op[%%d]     get/set output power\r\n");
+                printf("ocp[%%d]     get/set over-current protection (mA)\r\n");
                 printf("d[0-5]      change DIO pin assignment\r\n");
                 printf("frf[%%f]    get/set operating frequency (MHz)\r\n");
                 printf("pd2         toggle PA_High_Power\r\n");
@@ -1609,9 +1626,11 @@
                 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");
+#ifdef FCC_TEST                
                 printf("hop         change hop type (off, 64ch, 4ch)\r\n");
                 printf("hb[%%f]     get/set hop base MHz\r\n");
                 printf("hs[%%f]     get/set hop step MHz\r\n");
+#endif /* #ifdef FCC_TEST */                 
                 if (radio.RegOpMode.bits.LongRangeMode) {
                     printf("pl[%%d]     LORA get/set RegPayloadLength\r\n");
                     printf("cr[1234]    LORA set coding rate \r\n");
@@ -1666,6 +1685,7 @@
                     if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
                         sscanf(pcbuf+2, "%d", &i);
                         lora.RegPayloadLength = i;
+                        radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
                     }
                     tx_cnt++;
                     for (i = 0; i < lora.RegPayloadLength; i++)
@@ -1690,14 +1710,17 @@
                     fsk.start_tx(fsk_tx_length);
                 }
             }
-            
+#ifdef FCC_TEST            
             if (hop_type != HOP_TYPE_NONE)
                 hop_timeout.attach(&hop_cb, 0.4);
+#endif /* #ifdef FCC_TEST */
         } else if (pcbuf[0] == 'r' && pcbuf[1] == 'n' && pcbuf[2] == 'd') {
             uint8_t of = rand() & 0x3f;
 
             printf("%02d %.2f\r\n", of, 902.3 + (of * 0.2));
-        } else if (pcbuf[0] == 'h' && pcbuf[1] == 'b') {
+        }
+#ifdef FCC_TEST
+        else if (pcbuf[0] == 'h' && pcbuf[1] == 'b') {
             if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
                 sscanf(pcbuf+2, "%f", &hop_base_MHz);
             }
@@ -1722,7 +1745,9 @@
                     printf("hop off\r\n");                 
                     break;
             }
-        } else if (pcbuf[0] == 'h' && pcbuf[1] == 'p' && radio.RegOpMode.bits.LongRangeMode) {
+        }
+#endif /* #ifdef FCC_TEST */         
+        else if (pcbuf[0] == 'h' && pcbuf[1] == 'p' && radio.RegOpMode.bits.LongRangeMode) {
             if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
                 sscanf(pcbuf+2, "%d", &i);
                 lora.RegHopPeriod = i;
@@ -1983,6 +2008,23 @@
             }
             radio.RegPaConfig.octet = radio.read_reg(REG_PACONFIG);
             printf("OutputPower:%d\r\n", radio.RegPaConfig.bits.OutputPower);
+        } else if (pcbuf[0] == 'o' && pcbuf[1] == 'c' && pcbuf[2] == 'p') {
+            if (pcbuf[3] >= '0' && pcbuf[3] <= '9') {
+                sscanf(pcbuf+3, "%d", &i);
+                if (i < 130)
+                    radio.RegOcp.bits.OcpTrim = (i - 45) / 5;
+                else
+                    radio.RegOcp.bits.OcpTrim = (i + 30) / 10;
+                radio.write_reg(REG_OCP, radio.RegOcp.octet);
+            }            
+            radio.RegOcp.octet = radio.read_reg(REG_OCP);
+            if (radio.RegOcp.bits.OcpTrim < 16)
+                i = 45 + (5 * radio.RegOcp.bits.OcpTrim);
+            else if (radio.RegOcp.bits.OcpTrim < 28)
+                i = (10 * radio.RegOcp.bits.OcpTrim) - 30;
+            else
+                i = 240;
+            printf("Ocp: %dmA\r\n", i);            
         } else if (pcbuf[0] == 'c' && pcbuf[1] == 'r' && radio.RegOpMode.bits.LongRangeMode) {
             if (pcbuf[2] >= '0' && pcbuf[2] <= '9')
                 lora.setCodingRate(pcbuf[2] - '0');
@@ -2033,6 +2075,12 @@
             }
             radio.write_reg(REG_PDSTRIM1, pds_trim.octet);
             printf("prog_txdac:%.1fuA\r\n", 2.5 + (pds_trim.bits.prog_txdac * 0.625));
+            /* increase OCP threshold to allow more power */
+            radio.RegOcp.octet = radio.read_reg(REG_OCP);
+            if (radio.RegOcp.bits.OcpTrim < 16) {
+                radio.RegOcp.bits.OcpTrim = 16;
+                radio.write_reg(REG_OCP, radio.RegOcp.octet);
+            }           
         } else if (pcbuf[0] == 'b' && pcbuf[1] == 'r' && !radio.RegOpMode.bits.LongRangeMode) {
             if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
                 sscanf(&pcbuf[2], "%d", &i);
@@ -2157,7 +2205,7 @@
             printf("PER device ID:%d\r\n", per_id);
         } else if (pcbuf[0] == 'p' && pcbuf[1] == 'i' && pcbuf[2] == 'n') {
             if (pcbuf[3] >= '0' && pcbuf[3] <= '9') {
-                sscanf(pcbuf+2, "%f", &per_tx_delay);
+                sscanf(pcbuf+3, "%f", &per_tx_delay);
             }
             printf("per_tx_delay:%f\r\n", per_tx_delay);
         } else if (pcbuf[0] == 'p' && pcbuf[1] == 'e' && pcbuf[2] == 'r') {
@@ -2397,6 +2445,8 @@
     printf("\nreset\r\n");    
     //green_led = LED_OFF;
     red_led = LED_OFF;
+    led2 = LED_OFF;
+    yellow_led = LED_OFF;
     
     radio.rf_switch.attach(rfsw_callback);
     
@@ -2426,11 +2476,14 @@
     GPIOB->MODER |= 0x00000401;     // unused pins as outputs: PB0(HDR_DIO1), PB5 (PB10 pulled hi by LED), PB3-T_SWO
     printf("GPIOB->MODER:%08x\r\n", GPIOB->MODER);
 
-    GPIOC->MODER |= 0x00000041;    // unused pins as outputs: PC0(HDR_FEM_CSD), PC3(SPI3_enable)
+    GPIOC->MODER |= 0x00000045;    // unused pins as outputs: PC0(hdr_fem_csd) PC1(hdr_fem_ctx) PC3(SPI3_enable)
     printf("GPIOC->MODER:%08x\r\n", GPIOC->MODER);
 
- 
-
+    /*wait(0.1);
+    all_peripherals_off();
+    //repeated_sleep = 2;
+    deepsleep();*/
+            
     while(1) {
         switch (app) {
             case APP_NONE: