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:
14:35e7156e73ff
Parent:
13:a22debeee855
Child:
15:cdc215392e60
diff -r a22debeee855 -r 35e7156e73ff main.cpp
--- a/main.cpp	Tue Jul 14 00:47:41 2015 +0000
+++ b/main.cpp	Tue Sep 01 00:29:36 2015 +0000
@@ -44,7 +44,7 @@
 GPS gps(PB_6, PB_7, PB_11);
 DigitalOut pd2(PD_2);
 
-AnalogIn ain_bat(PA_1); // V3 mote: only when GPS enabled
+AnalogIn* ain_bat;
 #define AIN_VREF        3.3     // stm32 internal refernce
 #define AIN_VBAT_DIV    2       // resistor divider
 
@@ -1329,14 +1329,20 @@
     if (gps.enabled()) {
         if (gps.LatitudeBinary != 0) {
             gps.LatitudeBinary = 0;
-            printf("gps long:%f, lat:%f  vbat:%.2fV\r\n", gps.Longitude, gps.Latitude, ain_bat.read()*AIN_VREF*AIN_VBAT_DIV);
+            printf("gps long:%f, lat:%f  Vbat:%.2fV\r\n", gps.Longitude, gps.Latitude, ain_bat->read()*AIN_VREF*AIN_VBAT_DIV);
         }
     }
 }
 
+RCC_OscInitTypeDef prev_RCC_OscInitStruct;
+
 void all_peripherals_off()
 {
     // TODO: PD2 low, has pulldown
+    
+    HAL_RCC_GetOscConfig(&prev_RCC_OscInitStruct);
+    /*printf("hsiState:%d\r\n", osc_init.HSIState);
+    wait(0.05);*/
 
 #ifndef USE_DEBUGGER
     /* PA13 to undriven JTMS/SWDIO pin (from AF0 to GPIO), and PA2 */
@@ -1354,13 +1360,28 @@
     }
 }
 
+
+
 void restore_from_sleep()
 {
+    RCC_OscInitTypeDef osc_init;
 #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
+
+    if (prev_RCC_OscInitStruct.HSIState == RCC_HSI_ON) {
+        HAL_RCC_GetOscConfig(&osc_init);
+        if (osc_init.HSIState != RCC_HSI_ON) {
+            printf("hsi-restore\r\n");
+            // Enable the HSI (to clock the ADC)
+            osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSI;
+            osc_init.HSIState       = RCC_HSI_ON;
+            osc_init.PLL.PLLState   = RCC_PLL_NONE;
+            HAL_RCC_OscConfig(&osc_init);    
+        }
+    }
 }
 
 int get_kbd_str(char* buf, int size)
@@ -1804,6 +1825,7 @@
                     for (;;) asm("nop");
 #endif /* USE_DEBUGGER */
                 deepsleep();
+
             }
             restore_from_sleep();                   
         } else if (pcbuf[0] == 'r' && pcbuf[1] == 'x') { // RX
@@ -1877,6 +1899,7 @@
                 printf("mm,     print XYZ\r\n");
                 printf("mma     toggle stby/active\r\n");
                 printf("mmtd     configure for transient detection\r\n");
+                printf("mmod     configure for orientation detection\r\n");
             } else if (pcbuf[2] == '.') {
                 printf("active:%d\r\n", mma8451q.get_active());
                 mma8451q.print_regs();
@@ -1887,6 +1910,18 @@
                 mma8451q.out.v.y >>= 4; // 12bit data
                 mma8451q.out.v.z >>= 4; // 12bit data
                 printf("x:%d y:%d z:%d\r\n", mma8451q.out.v.x, mma8451q.out.v.y, mma8451q.out.v.z);
+            } else if (pcbuf[2] == 'o' && pcbuf[3] == 'd') {
+                printf("orientation ");
+                d = mma8451q.read_single(MMA8451_PL_CFG);
+                if (d & 0x40) { // PL_EN?
+                    d &= ~0x40;
+                    mma8451q.write(MMA8451_PL_CFG, d);
+                    printf("off\r\n");
+                } else {
+                    mpl3115a2.write(CTRL_REG4, 0);  // turn off: shares same interrupt pin
+                    mma8451q.orient_detect();
+                    printf("on\r\n");
+                }
             } else if (pcbuf[2] == 't' && pcbuf[3] == 'd') {   
                 printf("transient ");
                 if (mma8451q.transient_cfg.bits.ELE) {
@@ -1896,6 +1931,7 @@
                     mma8451q.write(MMA8451_CTRL_REG4, mma8451q.ctrl_reg4.octet);                    
                     printf("off\r\n");
                 } else {
+                    mpl3115a2.write(CTRL_REG4, 0);  // turn off: shares same interrupt pin
                     mma8451q.transient_detect();
                     //poll_timeout.attach(on_poll, 0.3);
                     printf("on\r\n");
@@ -1915,19 +1951,31 @@
                 printf("mp.     get status\r\n");
                 printf("mpt     get temperature\r\n");
                 printf("mpa     get altitude\r\n");
+                printf("mpb     get barometer\r\n");
+                printf("mpo[%%d]     get/set oversampling (0-7)\r\n");
             } else if (pcbuf[2] == '.') {
                 printf("active:%d\r\n", mpl3115a2.GetModeActive());
                 printf("int src:%02x\r\n", mpl3115a2.read(INT_SOURCE_REG));
+                printf("CTRL1:%02x\r\n", mpl3115a2.read(CTRL_REG1));
                 /* 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("CTRL3:%02x\r\n", mpl3115a2.read(CTRL_REG3));    /* TODO: PP_OD1 for open-drain operation */
+                printf("CTRL4:%02x\r\n", mpl3115a2.read(CTRL_REG4));
+                printf("CTRL5:%02x\r\n", mpl3115a2.read(CTRL_REG5));
+                printf("OFF_H:%02x\r\n", mpl3115a2.read(OFF_H_REG));
+                
                 printf("i2c_int_pin:%d\r\n", i2c_int_pin.read());
             } else if (pcbuf[2] == 't') {
-                printf("temp:%f\r\n", mpl3115a2.ReadTemperature());
+                printf("temp:%.4f\r\n", mpl3115a2.ReadTemperature());
             } else if (pcbuf[2] == 'a') {
-                printf("alt:%f\r\n", mpl3115a2.ReadAltitude());
-                
+                printf("alt:%.4f\r\n", mpl3115a2.ReadAltitude());
+            } else if (pcbuf[2] == 'b') {
+                printf("bar:%.2f\r\n", mpl3115a2.ReadBarometer());
+            } else if (pcbuf[2] == 'o') {
+                if (pcbuf[3] >= '0' && pcbuf[3] <= '9') {
+                    sscanf(pcbuf+3, "%d", &i);
+                    mpl3115a2.setOSR(i);
+                }                       
+                printf("OSR:%d\r\n", mpl3115a2.getOSR());
             }
         }
         /************************** sx9500... **************************************/
@@ -2427,9 +2475,11 @@
     if (first && !pc_1) {
         //printf("v2-mote\r\n");
         mote_version = MOTE_V2;
+        ain_bat = new AnalogIn(PA_0);
     } else {
         //printf("v3-mote\r\n");
         mote_version = MOTE_V3;
+        ain_bat = new AnalogIn(PA_1);
     }
     
 }
@@ -2468,6 +2518,8 @@
     sx9500.write(SX9500_REG_PROXCTRL6, 0x04);
     
     mma8451q.set_active(0);
+    mma8451q.verbose = true;
+    mpl3115a2.init();
     mpl3115a2.SetModeStandby();
     
     GPIOA->MODER |= 0x01415500;     // unused pins as outputs: PA4, PA5, PA6, PA7, PA8, (PA11,PA12 USB)