mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Revision:
10:d2da2028a233
Parent:
9:26f694bc31b4
Child:
11:b8505cbbd55c
diff -r 26f694bc31b4 -r d2da2028a233 main.cpp
--- a/main.cpp	Thu Apr 17 15:10:09 2014 +0000
+++ b/main.cpp	Mon May 05 15:45:31 2014 +0000
@@ -1,5 +1,4 @@
 #include "mbed.h"
-#include "C027.h"
 #include "GPS.h"
 #include "MDM.h"
 
@@ -23,52 +22,81 @@
 */
 #define PASSWORD    NULL 
 
-//----------------------------------------------------------------------
 
-C027 c027;
+//----------------------------------------------------------------------
+#if defined(TARGET_UBLOX_C027) || defined(TARGET_LPC1768)
+  //#define C027_USEONBOARD // remove this if you have a GSM/GPS shield
+  #ifdef C027_USEONBOARD
+    #include "C027.h"
+    C027 c027;
+  #elif defined(TARGET_LPC1768) 
+    // We assume we have a C027 (not an Arch Pro)
+    #define D15  P0_1
+    #define D14  P0_0
+    #define D4   P2_12
+    #define D1   P4_28
+    #define D0   P4_29
+    #define LED1 P3_25
+  #endif
+#endif
+
+#ifndef C027_USEONBOARD
+  #define GPSSCL    D15
+  #define GPSSDA    D14
+  #define GPSADR   (66<<1) // GPS I2C Address
+  #define MDMPWRON  D4
+  #define MDMTXD    D1
+  #define MDMRXD    D0
+  #define MDMCTS    NC
+  #define MDMRTS    NC
+  #define MDMBAUD   115200
+#endif
+
+// no tracing if serial is shared with the VCP
+#define TRACE     ((USBRX==MDMRXD)||(USBTX==MDMTXD))?:printf
 
 void printDeviceStatus(MDMParser::DevStatus* status) {
-    printf("Device Status:\r\n");
+    TRACE("Device Status:\r\n");
     const char* txtDev[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200" };
     if (status->dev < sizeof(txtDev)/sizeof(*txtDev) && (status->dev != MDMParser::DEV_UNKNOWN))
-        printf("  Device:       %s\r\n", txtDev[status->dev]);
+        TRACE("  Device:       %s\r\n", txtDev[status->dev]);
     const char* txtLpm[] = { "Disabled", "Enabled", "Active" };
     if (status->lpm < sizeof(txtLpm)/sizeof(*txtLpm))
-        printf("  Power Save:   %s\r\n", txtLpm[status->lpm]);
+        TRACE("  Power Save:   %s\r\n", txtLpm[status->lpm]);
     const char* txtSim[] = { "Unknown", "Pin", "Ready" };
     if (status->sim < sizeof(txtSim)/sizeof(*txtSim) && (status->sim != MDMParser::SIM_UNKNOWN))
-        printf("  SIM:          %s\r\n", txtSim[status->sim]);
+        TRACE("  SIM:          %s\r\n", txtSim[status->sim]);
     if (*status->ccid)  
-        printf("  CCID:         %s\r\n", status->ccid);
+        TRACE("  CCID:         %s\r\n", status->ccid);
     if (*status->imei) 
-        printf("  IMEI:         %s\r\n", status->imei);
+        TRACE("  IMEI:         %s\r\n", status->imei);
     if (*status->imsi)  
-        printf("  IMSI:         %s\r\n", status->imsi);
+        TRACE("  IMSI:         %s\r\n", status->imsi);
     if (*status->meid) 
-        printf("  MEID:         %s\r\n", status->meid);
+        TRACE("  MEID:         %s\r\n", status->meid);
     if (*status->manu) 
-        printf("  Manufacturer: %s\r\n", status->manu);
+        TRACE("  Manufacturer: %s\r\n", status->manu);
     if (*status->model)  
-        printf("  Model:        %s\r\n", status->model);
+        TRACE("  Model:        %s\r\n", status->model);
     if (*status->ver)  
-        printf("  Version:      %s\r\n", status->ver);
+        TRACE("  Version:      %s\r\n", status->ver);
 }
 
 void printNetStatus(MDMParser::NetStatus *status)
 {
-    printf("Network Status:\r\n");
+    TRACE("Network Status:\r\n");
     const char* txtReg[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
     if (status->reg < sizeof(txtReg)/sizeof(*txtReg) && (status->reg != MDMParser::REG_UNKNOWN))
-        printf("  Registration:       %s\r\n", txtReg[status->reg]);
+        TRACE("  Registration:       %s\r\n", txtReg[status->reg]);
     const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
     if (status->act < sizeof(txtAct)/sizeof(*txtAct) && (status->act != MDMParser::ACT_UNKNOWN))
-        printf("  Access Technology:  %s\r\n", txtAct[status->act]);
+        TRACE("  Access Technology:  %s\r\n", txtAct[status->act]);
     if (status->rssi) 
-        printf("  Signal Strength:    %d dBm\r\n", status->rssi);
+        TRACE("  Signal Strength:    %d dBm\r\n", status->rssi);
     if (*status->opr)  
-        printf("  Operator:           %s\r\n", status->opr);
+        TRACE("  Operator:           %s\r\n", status->opr);
     if (*status->num)  
-        printf("  Phone Number:       %s\r\n", status->num);
+        TRACE("  Phone Number:       %s\r\n", status->num);
 }
 
 int main(void)
@@ -79,54 +107,70 @@
     Serial pc(USBTX,USBRX);
     pc.baud(115200);
     
-    printf("C027 Support Example\r\n");
+    TRACE("GSM/GPS Support Example\r\n");
 
+#ifdef C027_USEONBOARD
+    // turn on the supplies of the Modem and the GPS
     c027.mdmPower(true);
     c027.gpsPower(true);
+#else
+    // turn on the Modem
+    DigitalOut mdmPwrOn(MDMPWRON, 0);
+    wait_ms(150);
+    mdmPwrOn = 1;
+#endif
     wait(2);
 
+    // Create the GPS object
+#if defined(GPSSCL) && defined(GPSSDA) && defined(GPSADR)
     GPSI2C gps(GPSSDA,GPSSCL,GPSADR);     // use GPSI2C class
-    // GPSSerial gps(GPSTXD,GPSRXD,GPSBAUD); // or GPSSerial class 
+#elif defined(GPSTXD) && defined(GPSRXD) && defined(GPSBAUD)
+    GPSSerial gps(GPSTXD,GPSRXD,GPSBAUD); // or GPSSerial class 
+#else
+    #warning "please define the pins for the GPS"
+#endif
+    // Create the modem object
     MDMSerial mdm(MDMTXD,MDMRXD,MDMBAUD
- #if DEVICE_SERIAL_FC
+#if DEVICE_SERIAL_FC
                 ,MDMRTS,MDMCTS
- #endif
+#endif
                 );
                 
     // initialize the modem 
-    printf("Device Init\r\n");
+    TRACE("Device Init\r\n");
     MDMParser::DevStatus devStatus;
     MDMParser::NetStatus netStatus;
-    if (mdm.init(SIMPIN, &devStatus))
+    bool mdmOk = mdm.init(SIMPIN, &devStatus);
+    if (mdmOk)
     {
         printDeviceStatus(&devStatus);
         
         // wait until we are connected
-        printf("Network Check\r\n");
+        TRACE("Network Check\r\n");
         while (!mdm.checkNetStatus(&netStatus))
             wait_ms(1000);
     
         printNetStatus(&netStatus);
-        printf("Network Join\r\n");
+        TRACE("Network Join\r\n");
         // join the internet connection 
         MDMParser::IP ip = 
             mdm.join(APN,USERNAME,PASSWORD);
         if (ip != NOIP)
         {
-            printf("  IP Address: " IPSTR "\r\n", IPNUM(ip));
-            printf("Socket Create\r\n");
+            TRACE("  IP Address: " IPSTR "\r\n", IPNUM(ip));
+            TRACE("Socket Create\r\n");
             int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
             if (socket >= 0)
             {
-                printf("Socket Connect\r\n");
+                TRACE("Socket Connect\r\n");
                 if (mdm.socketConnect(socket, "mbed.org", 80))
                 {
-                    printf("Make a Http Post Request\r\n");
+                    TRACE("Make a Http Post Request\r\n");
                     const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
-                    printf("Socket Send\r\n");
+                    TRACE("Socket Send\r\n");
                     mdm.socketSend(socket, http, sizeof(http)-1);
                 
-                    printf("Socket Recving\r\n");
+                    TRACE("Socket Recving\r\n");
                     while (true) {
                         ret = mdm.socketReadable(socket);
                         if (ret > 0)
@@ -134,88 +178,89 @@
                         if (ret < 0)
                             break;
                         else if (ret > 0)
-                            printf("Socket Recv \"%*s\"\r\n", ret, buf);
+                            TRACE("Socket Recv \"%*s\"\r\n", ret, buf);
                     }
-                    printf("Socket Close\r\n");
+                    TRACE("Socket Close\r\n");
                     mdm.socketClose(socket);
                 }
-                printf("Socket Free\r\n");
+                TRACE("Socket Free\r\n");
                 mdm.socketFree(socket);
             }
             
             // disconnect  
-            printf("Network Disconnect\r\n");
+            TRACE("Network Disconnect\r\n");
             mdm.disconnect();
         }
         
         const char* ussd = "*#134#"; // You may get answer "UNKNOWN APPLICATION"
-        printf("Send Ussd Command %s\r\n", ussd);
+        TRACE("Send Ussd Command %s\r\n", ussd);
         ret = mdm.ussdCommand(ussd, buf);
         if (ret > 0) 
-            printf("Got Ussd Answer: \"%*s\"\r\n", ret, buf);
-            
-        printf("Checking SMS and GPS\r\n");
-        char link[128] = "";
-        unsigned int i = 0xFFFFFFFF;
-        const int wait = 100;
-        while (1) {
-            
-            while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
+            TRACE("Got Ussd Answer: \"%*s\"\r\n", ret, buf);
+    }
+    TRACE("Checking SMS and GPS\r\n");
+    char link[128] = "";
+    unsigned int i = 0xFFFFFFFF;
+    const int wait = 100;
+    bool abort = false;
+    DigitalOut led(LED1);
+    while (!abort) {
+        led = !led;        
+        while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
+        {
+            int len = LENGTH(ret);
+            //TRACE("NMEA: %.*s\r\n", len-2, msg); 
+            if ((PROTOCOL(ret) == NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6))
             {
-                int len = LENGTH(ret);
-                //printf("NMEA: %.*s\r\n", len-2, msg); 
-                if ((PROTOCOL(ret) == NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6))
+                double la = 0, lo = 0;
+                char ch;
+                if (gps.getNmeaAngle(1,buf,len,la) && 
+                    gps.getNmeaAngle(3,buf,len,lo) && 
+                    gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
                 {
-                    double la = 0, lo = 0;
-                    char ch;
-                    if (gps.getNmeaAngle(1,buf,len,la) && 
-                        gps.getNmeaAngle(3,buf,len,lo) && 
-                        gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
-                    {
-                        printf("GPS Location: %.5f %.5f\r\n", la, lo); 
-                        sprintf(link, "I am here!\n"
-                                      "https://maps.google.com/?q=%.5f,%.5f", la, lo); 
-                    }
+                    TRACE("GPS Location: %.5f %.5f\r\n", la, lo); 
+                    sprintf(link, "I am here!\n"
+                                  "https://maps.google.com/?q=%.5f,%.5f", la, lo); 
                 }
             }
-            
-            if (i++ == 10000/wait) {
-                i = 0;
-                // check the network status
-                if (mdm.checkNetStatus(&netStatus))
-                    printNetStatus(&netStatus);
-                    
-                // checking unread sms
-                int ix[8];
-                int n = mdm.smsList("REC UNREAD", ix, 8);
-                if (8 < n) n = 8;
-                while (0 < n--)
-                {
-                    char num[32];
-                    printf("Unread SMS at index %d\r\n", ix[n]);
-                    if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
-                        printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
-                        printf("Delete SMS at index %d\r\n", ix[n]);
-                        mdm.smsDelete(ix[n]);
-                        // provide a reply
-                        const char* reply = "Hello my friend";
-                        if (strstr(buf, /*w*/"here are you"))
-                            reply = *link ? link : "I don't know"; // reply wil location link
-                        printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
-                        mdm.smsSend(num, reply);
-                    }
+        }
+        if (mdmOk && (i++ == 10000/wait)) {
+            i = 0;
+            // check the network status
+            if (mdm.checkNetStatus(&netStatus))
+                printNetStatus(&netStatus);
+                
+            // checking unread sms
+            int ix[8];
+            int n = mdm.smsList("REC UNREAD", ix, 8);
+            if (8 < n) n = 8;
+            while (0 < n--)
+            {
+                char num[32];
+                TRACE("Unread SMS at index %d\r\n", ix[n]);
+                if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
+                    TRACE("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
+                    TRACE("Delete SMS at index %d\r\n", ix[n]);
+                    mdm.smsDelete(ix[n]);
+                    // provide a reply
+                    const char* reply = "Hello my friend";
+                    if (strstr(buf, /*w*/"here are you"))
+                        reply = *link ? link : "I don't know"; // reply wil location link
+                    TRACE("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
+                    mdm.smsSend(num, reply);
                 }
             }
-            wait_ms(wait);
         }
-        mdm.powerOff();
-        gps.powerOff();
+        wait_ms(wait);
     }
-    printf("Shutdown\r\n");
-    // now it is safe to switch off
+    mdm.powerOff();
+    gps.powerOff();
+    TRACE("Shutdown\r\n");
+#ifdef C027_USEONBOARD
+    // now it is safe to switch LDOs off
     c027.mdmPower(false);
     c027.gpsPower(false);
-    
+#endif    
     return 0;
 }