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:
10:a3e2c558c003
Parent:
9:dc02ef560bd1
Child:
11:0f62a500aedc
--- a/main.cpp	Thu May 28 01:21:03 2015 +0000
+++ b/main.cpp	Thu Jun 25 22:29:28 2015 +0000
@@ -5,6 +5,7 @@
 #include "mpl3115a2.h"
 #include "sx9500.h"
 #include "gps.h"
+#include "rtc_alarm.h"
 
 #define LORA_WAN_ENABLE
 
@@ -63,6 +64,8 @@
 float hop_base_MHz = 902.3;
 float hop_step_MHz = 0.2;
 
+uint32_t repeated_sleep;
+
 bool abort_key;
 bool per_en;
 int PacketRxSequencePrev;
@@ -85,6 +88,8 @@
 
 app_e app = APP_NONE;
 
+char service_en = 1;
+
 #ifdef LORA_WAN_ENABLE
 #define CFG_us915
 #include "oslmic.h"
@@ -1302,14 +1307,65 @@
     }
 }
 
+void all_peripherals_off()
+{
+    // TODO: PD2 low, has pulldown
+
+#ifndef USE_DEBUGGER
+    /* PA13 to undriven JTMS/SWDIO pin (from AF0 to GPIO), and PA2 */
+    GPIOA->MODER &= 0xf7ffffdf;
+    GPIOB->MODER &= 0xffffdfff; // PB6 UART_TX to input
+#endif
+
+    sx9500.set_active(false);
+    mpl3115a2.SetModeStandby();
+    mma8451q.set_active(0);
+    gps.enable(false);
+
+    if (GPIOB->ODR & 0x1000) {  // if SX1272_NSS (PB12) wasnt forced low
+        radio.set_opmode(RF_OPMODE_SLEEP);
+    }
+}
+
+void restore_from_sleep()
+{
+#ifndef USE_DEBUGGER
+    /* PA13 back to JTMS/SWDIO pin (from GPIO to AF0), and PA2 */
+    GPIOA->MODER |= 0x08000020;
+    GPIOB->MODER |= 0x00002000; // PB6 input to UART_TX
+#endif
+}
+
 int get_kbd_str(char* buf, int size)
 {
     char c;
     int i;
     static int prev_len;
+    static int sleep_start_cnt_dn = 0;
     
     for (i = 0;;) {
+        if (sleep_start_cnt_dn > 0) {
+            if (--sleep_start_cnt_dn == 0) {
+                if (repeated_sleep != 0) {
+                    all_peripherals_off();
+                    rtc_alarm_start(repeated_sleep);
+#ifdef USE_DEBUGGER
+                    HAL_EnableDBGStopMode();
+                    if (!DBGMCU->CR & DBGMCU_CR_DBG_STOP)
+                        for (;;) asm("nop");
+#endif /* USE_DEBUGGER */
+                    deepsleep();
+                    restore_from_sleep();
+                }
+            }
+        }        
+        
         if (pc.readable()) {
+            if (sleep_start_cnt_dn > 0) {
+                repeated_sleep = 0;
+                printf("repeated_sleep abort\r\n");
+            }
+                        
             c = pc.getc();
             if (c == 8) {   // backspace
                 if (i > 0) {
@@ -1336,11 +1392,19 @@
                 pc.putc(c);
             }
         } else {
-            service_radio();
+            if (service_en) {
+                service_radio();
+            }
             gps_service();
             sx9500.service();
             mma8451q.service();
             mpl3115a2.service();
+
+            if (sleep_start_cnt_dn == 0 && repeated_sleep != 0) {
+                printf("Alarm Start %d\r\n", repeated_sleep);
+                sleep_start_cnt_dn = 3000;
+            }
+                        
         }
     } // ...for()
 }
@@ -1370,6 +1434,9 @@
 
 uint8_t last_RxCurrentAddr;
 uint8_t last_RxNbBytes;
+
+
+
                 
 void
 console()
@@ -1508,6 +1575,10 @@
                 }
                 break;
             case '?':
+                printf("G<portABCD><MO><pin0-f>[0123]       pin control (M=MODER, O=BSRR) see RM0038 section 7.4\r\n");
+                printf("cs%%d        deepsleep for seconds\r\n");
+                printf("cl%%d        sleep for seconds\r\n");
+                printf("CS%%d        repeated sleep for seconds (hold down key to stop)\r\n");
                 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");
@@ -1669,7 +1740,34 @@
                     printf("none\r\n");
                     break;
                 }
-            radio.write_reg(REG_FSK_RXCONFIG, fsk.RegRxConfig.octet);            
+            radio.write_reg(REG_FSK_RXCONFIG, fsk.RegRxConfig.octet);     
+        } else if (pcbuf[0] == 'C' && pcbuf[1] == 'S' && pcbuf[2] >= '0' && pcbuf[2] <= '9') {
+            all_peripherals_off();
+            sscanf(pcbuf+2, "%d", &i);
+            repeated_sleep = i;
+        } else if (pcbuf[0] == 'c' && (pcbuf[1] == 's' || pcbuf[1] == 'l') && pcbuf[2] >= '0' && pcbuf[2] <= '9') {
+             // rtc wakeup test
+            all_peripherals_off();
+            sscanf(pcbuf+2, "%d", &i);
+            printf("alarm_start %d\r\n", i);
+            wait(0.1);
+            rtc_alarm_start(i);
+            if (pcbuf[1] == 'l') {
+#ifdef USE_DEBUGGER
+                HAL_EnableDBGSleepMode();
+                if (!DBGMCU->CR & DBGMCU_CR_DBG_SLEEP)
+                    for (;;) asm("nop");
+#endif /* USE_DEBUGGER */
+                sleep();
+            } else {
+#ifdef USE_DEBUGGER
+                HAL_EnableDBGStopMode();
+                if (!DBGMCU->CR & DBGMCU_CR_DBG_STOP)
+                    for (;;) asm("nop");
+#endif /* USE_DEBUGGER */
+                deepsleep();
+            }
+            restore_from_sleep();                   
         } else if (pcbuf[0] == 'r' && pcbuf[1] == 'x') { // RX
             if (per_en) {
                 red_led = LED_OFF;
@@ -1989,6 +2087,9 @@
             for (i = 0; i <= fsk.RegSyncConfig.bits.SyncSize; i++)
                 printf("%02x ", radio.read_reg(REG_FSK_SYNCVALUE1+i));
             printf("\r\n");
+        } else if (pcbuf[0] == 's' && pcbuf[1] == 'e' && pcbuf[2] == 'r') {
+            service_en ^= 1;
+            printf("service_en:%d\r\n", service_en);
         } else if (pcbuf[0] == 's' && pcbuf[1] == 's') {
             if (radio.RegOpMode.bits.LongRangeMode) { // sweep test on lora sync
                 if (pcbuf[2] == 'h') { // sweep high nibble
@@ -2132,7 +2233,15 @@
             printf("chirp_invert_tx :%d\r\n", lora.RegTest33.bits.chirp_invert_tx);          
         } else if (pcbuf[0] == 'r' && pcbuf[1] == 'i' && pcbuf[2] == 'n' && radio.RegOpMode.bits.LongRangeMode) {
             lora.invert_rx(!lora.RegTest33.bits.invert_i_q);
-            printf("rx invert_i_q:%d\r\n", lora.RegTest33.bits.invert_i_q);      
+            printf("rx invert_i_q:%d\r\n", lora.RegTest33.bits.invert_i_q);   
+        } else if (pcbuf[0] == 'd' && pcbuf[1] == 'i' && pcbuf[2] == 'o') {
+            if (radio.RegOpMode.bits.LongRangeMode)
+                lora_print_dio();
+            else
+                fsk_print_dio();            
+            printf("dio0:%d, dio1:%d\r\n", radio.dio0.read(), radio.dio0.read());
+            lora.RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS);
+            printLoraIrqs_(false);            
         } else if (pcbuf[0] == 'd' && pcbuf[1] >= '0' && pcbuf[1] <= '5') {
             switch (pcbuf[1]) {
                 case '0':
@@ -2178,6 +2287,53 @@
                 gps.verbose ^= 1;
                 printf("gps.verbose:%d\r\n", gps.verbose);
             }
+        } else if (pcbuf[0] == 'G') {   // gpio registers.  G<portABCD><MO><pin0-f>[0123]
+            GPIO_TypeDef *gpiox;
+            char bp[2];
+            bp[0] = pcbuf[3];
+            bp[1] = 0;
+            sscanf(bp, "%x", &i);
+            switch (pcbuf[1]) {
+                case 'A': gpiox = GPIOA; break;
+                case 'B': gpiox = GPIOB; break;
+                case 'C': gpiox = GPIOC; break;
+                case 'D': gpiox = GPIOD; break;
+                default: gpiox = NULL; break;
+            }
+            if (gpiox != NULL) {
+                if (pcbuf[2] == 'M') {  // GPIOx->MODER
+                    if (pcbuf[4] >= '0' && pcbuf[4] <= '3') {
+                        a = i << 1;
+                        ui = 3 << a;
+                        d = pcbuf[4] - '0';
+                        //gpiox->MODER &= ~ui;
+                        gpiox->MODER &= ~ui;
+                        gpiox->MODER |= d << a;;
+                        printf("ui:%08x, d<<a:%08x\r\n", ui, d<<a);
+                    }
+                    for (len = 0; len < 16; len++) {
+                        i = len << 1;
+                        ui = gpiox->MODER >> i;
+                        printf("%02d:", len);
+                        switch (ui & 3) {
+                            case 0: printf("input\r\n"); break;
+                            case 1: printf("output\r\n"); break;
+                            case 2: printf("AF\r\n"); break;
+                            case 3: printf("analog\r\n"); break;
+                        }
+                    }
+                    printf("MODER:%08x\r\n", gpiox->MODER);
+                } else if (pcbuf[2] == 'O') {   // GPIOx->BSRR
+                    ui = 1 << i;
+                    if (pcbuf[4] == '0') {
+                        gpiox->BSRR = ui << 16;
+                    } else if (pcbuf[4] == '1') {
+                        gpiox->BSRR = ui;
+                    }
+                    printf("ODR:%08x\r\n", gpiox->ODR);
+                }
+            }
+                        
         } else if (pcbuf[0] == 's' && pcbuf[1] == 't' && pcbuf[2] == 'b') {
             radio.set_opmode(RF_OPMODE_STANDBY);
             //green_led = LED_OFF;
@@ -2199,7 +2355,9 @@
 
 int main()
 {  
-
+    set_time(0);    // start RTC
+    rtc_alarm_init();
+    
     pc.baud(57600);
     gps.init();