mbed 5.4 with sleep mode

Dependencies:  

Revision:
39:4f3f7463e55f
Parent:
37:43d48521d4d7
diff -r f1bc6e867fcf -r 4f3f7463e55f main.cpp
--- a/main.cpp	Wed May 24 07:51:33 2017 +0000
+++ b/main.cpp	Thu May 25 11:53:45 2017 +0000
@@ -7,10 +7,11 @@
 #include "GPS.h"
 #include "MDM.h"
 #include "main.h"
-
+#include "coap_msg.h"
+#include "coap_client.h"
+    
 extern "C" {
-    #include "coap_msg.h"
-    #include "coap_client.h"
+    
     #include "rtc_api.h"
 }
 //
@@ -31,7 +32,7 @@
 unsigned long           ulGSMRxCntr=0, ulGSMTxCntr=0 ;      //-- Asset
 unsigned long           ulTotalRxCntr=0,ulTotalTxCntr=0;    //-- Asset
 char                    caLssFinalAssetMsg[150];
-double                  dLatitude=0,dLongitude=0;
+double                  dLatitude=31.47691,dLongitude=74.34259;
 static unsigned int     uiWakeupCounter=0,uiWatchdogCounter=0;
 unsigned char           ucBtteryLevel=0;
 MDMParser::DevStatus    devStatus={};
@@ -43,6 +44,9 @@
 
 //unsigned int          uiAlarmIntervalCounter = 0;
 //unsigned int          uiHeartBeatCounter = 0;
+
+char ucReturnCode[10];     //Return code array
+int alarmtime = 1495698480;//2017-3-29-11:56:7
 //----------------------------------------------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------------------------------------------
 /*
@@ -50,7 +54,7 @@
 {
     LPC_SC->PCONP |=1<1;            //timer0 power on
     LPC_TIM0->MR0 = sec * 23980000; //1sec * sec
-    LPC_TIM0->MCR = 3;              //interrupt and reset control
+    LPC_TIM0->MCR = 1;              //interrupt and reset control
                                     //3 = Interrupt & reset timer0 on match
                                     //1 = Interrupt only, no reset of timer0
     NVIC_EnableIRQ(TIMER0_IRQn);    //enable timer0 interrupt
@@ -63,33 +67,34 @@
     if((LPC_TIM0->IR & 0x01) == 0x01)   // if MR0 interrupt, proceed
     {
         LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
-        
-        uiAlarmIntervalCounter++;
-        if(uiAlarmIntervalCounter >= 10)//20 sec
-        {
-            bAlarmIntervalFlag = true;
-            uiAlarmIntervalCounter = 0;
-            printf("Alarm Packet time...\r\n");
-        }
-        
-        uiHeartBeatCounter++;
-        if(uiHeartBeatCounter >= 60)//2 min
-        {
-            bHeartBeatFlag = true;
-            uiHeartBeatCounter = 0;
-            printf("Heartbeat Packet time...\r\n");
-        } 
+        printf("Timer 0 interrupt generated 10 sec\r\n");
+        LPC_TIM0->TCR = 0; 
     }
 }
 */
 //----------------------------------------------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------------------------------------------
 
-void rtc_setup(void)
+void rtc_setup(time_t t, unsigned int sec)
 {
+    struct tm *timeinfo = localtime(&t);
+    unsigned int min;
+    
     rtc_init();
 
-    rtc_write(1490788567);//2017-3-29-11:56:7
+    rtc_write(alarmtime);//2017-3-29-11:56:7
+    
+    //min = sec/60;
+    //sec = sec%60;
+     //set alarm time
+    LPC_RTC->ALSEC = timeinfo->tm_sec+sec;//10 sec
+    //LPC_RTC->ALMIN = timeinfo->tm_min+min;//10 sec
+
+    LPC_RTC->AMR &= (~(1 << 0)) & 0xFF;
+
+    LPC_RTC->ILR = (1 << 0) | (1 << 1);
+    
+    printf("RTC Alarm Time Set\r\n");
 
     NVIC_EnableIRQ(RTC_IRQn);
 
@@ -130,7 +135,7 @@
     MDMSerial mdm;            
     gmdm = &mdm;
     us_ticker_init();
-    //timer0_init(2);
+    
 
 while(1)
 {
@@ -142,10 +147,8 @@
             if(bSendingDataFlag)//&& bAlarmIntervalFlag)
             {
                 //bAlarmIntervalFlag = false;
-                unsigned int counter = 50;
-                while( (ret = gps.getMessage(buf, sizeof(buf))) > 0 || counter > 0)
+                while( (ret = gps.getMessage(buf, sizeof(buf))) > 0)
                 {
-                    counter--;
                     int len = LENGTH(ret);
                     ulGPSRxCntr = len;
                     if( (PROTOCOL(ret)==GPSParser::NMEA) && (len > 6) )
@@ -200,13 +203,13 @@
                 sprintf( caLssFinalAssetMsg, "$AQLSS,02,%s,%s*xx",devStatus.imei,devStatus.imsi);
                 printf( "HeartBeat: %s\r\n", caLssFinalAssetMsg );
             }
-                
+            
             ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport);
             if( ssocket >= 0 ) 
             {
                 mdm.socketSetBlocking(ssocket, 10);
                 ipp = 0x97092263;//0x052753CE;
-                if (PASS == test_exchange_func(caLssFinalAssetMsg,strlen(caLssFinalAssetMsg)))
+                if (PASS == test_exchange_func(caLssFinalAssetMsg,strlen(caLssFinalAssetMsg),ucReturnCode))
                 {
                     printf("\r\n-----------------<pass>-----------------\n");           
                 }                
@@ -215,22 +218,26 @@
 
                 mdm.socketFree(ssocket);
                 responce_checker(); 
-            }   
+            }
 
             mdm.disconnect();
-            mdm.powerOff();
+            /*mdm.powerOff();*/
+        
             if(bSleepModeFlag)
             {
-                rtc_setup();
-                hal_deepsleep();
-                //sleep_mode();
-                //uiWakeupCounter++;
+                 rtc_setup(alarmtime,10);
+                printf("Enter Sleep Mode\r\n");
+                hal_sleep();
+                printf("Exit Sleep Mode\r\n");
+                myled = !myled;
+                uiWakeupCounter++;
             }
-            if(bSendingDataFlag)
+            
+            /*if(bSendingDataFlag)
                 wait( 10 );
             else
                 wait( 60 );
-            myled = !myled;
+            myled = !myled;*/
         }
     //}
 }
@@ -295,17 +302,9 @@
        printf("Ussd Got Answer: \"%s\"\r\n", buffer);
 }
 */
-void sleep_mode(void)
-{
-    /* Deep-Sleep Mode, set SLEEPDEEP bit */
-    //SCB->SCR |= 0x4;
-    LPC_SC->PCON = 0x9;
-    __WFI();
-    //SystemInit();
-} 
 
 void responce_checker(void)
-{/*
+{    
     if(!strcmp((const char*)ucReturnCode,"RC 000"))
     {
         printf("Acknowledged RC 000\r\n");
@@ -350,5 +349,5 @@
     else if(!strcmp((const char*)ucReturnCode,"EC 002"))
     {
         printf("Error Code 002 Received Payload is corrupted\r\n");
-    }*/
+    }
 }