LoRa send is causing hang-up when a gps fix is acquired

Dependencies:   mbed LoRaWAN-lib_publishing_testing_UART_bug SingleFrequencyLora

Fork of simple-demo-76_revised_20171113_copy by Rishin Amin

Revision:
14:539ef77197e0
Parent:
12:dc90bd3ac7d8
--- a/app/main.cpp	Mon Dec 04 14:48:01 2017 +0000
+++ b/app/main.cpp	Mon Dec 04 21:25:19 2017 +0000
@@ -34,10 +34,10 @@
 
 
 // #ifdef DEBUGGER
-// RawSerial pc(PC_TX, PC_RX);    // USART2
+RawSerial pc(USBTX, USBRX);    // USART2
 // #endif
 
-Serial gps(GPS_TX, GPS_RX); // USART1
+RawSerial gps(GPS_TX, GPS_RX); // USART1
 
 // DigitalOut Reset(RESET_PIN);
 // DigitalOut Force_on(FORCE_ON_PIN);
@@ -48,111 +48,143 @@
 GPS_data GPS_parse;
 volatile int Char_index = 0;                  // index for char array
 char Rx_data[MAX_NMEA_LENGTH] = "0";          // char array to store received bytes
-char tx_line[82];
 // uint8_t valid = 0;
-volatile uint8_t GPS_data_ready = 0;
+//volatile uint8_t GPS_data_ready = 0;
 volatile uint8_t sending = 0;
 
-volatile bool keepOut = false;
+volatile bool timerset = false;
 
-uint8_t miss_count =0;
+volatile bool firstfix = false;
+
+volatile bool keepOut = false; 
 
 Ticker LoRaSend_timer;
 
-void flushSerialBuffer(void) { char char1 = 0; while (gps.readable()) { char1 = gps.getc(); } return; }
+//void flushSerialBuffer(void) { char char1 = 0; while (gps.readable()) { char1 = gps.getc(); } return; }
+
+void LoRaSend(){
+    //GPS_status = !GPS_status;
+    //if (keepOut)
+    //    return;
+    __disable_irq();//NVIC_DisableIRQ(USART1_IRQn);
+    //if(GPS_data_ready == 1){
+        // pc.putc('1');
+        sending = 1;
+        // pc.putc('3');
+        char AppDatas[APPDATA_SIZE];
+        // pc.putc('4');
+        strcpy(AppDatas, GPS_parse.Latitude); 
+        // pc.putc('5');
+        strcat(AppDatas, ",");
+        // pc.putc('6');
+        strcat(AppDatas, GPS_parse.Longitude);
+        // pc.putc('7');
+        strcat(AppDatas, ",");
+        // pc.putc('8');
+        strcat(AppDatas, GPS_parse.Course_Over_Ground);
+        // pc.putc('9'); 
+        strcat(AppDatas, ",");
+        // pc.putc('a');
+        strcat(AppDatas, GPS_parse.Speed_Over_Ground);
+        // pc.putc('b');
+        strcat(AppDatas, ",");
+        //strcat(AppDatas, GPS_parse.Date);
+        //strcat(AppDatas, ",");
+        // pc.putc('c');
+        strcat(AppDatas, GPS_parse.UTC_Time);
+        // pc.putc('d');
+        strcat(AppDatas, ",");
+        // pc.putc('e');
+        strcat(AppDatas, GPS_parse.Valid);
+        // pc.putc('f');
+        // McpsReq_t mcpsReq;
+        // pc.putc('g');
+        // uint8_t AppPort = 3;
+        // pc.putc('h');
+        //  mcpsReq.Type = MCPS_UNCONFIRMED;
+        // pc.putc('i');
+        // mcpsReq.Req.Unconfirmed.fPort = AppPort;
+        // pc.putc('j');
+        // mcpsReq.Req.Unconfirmed.fBuffer = AppDatas;
+        // pc.putc('k');
+        // mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE+2;
+        // pc.putc('l');
+        // mcpsReq.Req.Unconfirmed.Datarate = DR_5;
+        // status = !status;
+        //GPS_status = !GPS_status;
+        // pc.putc('m');
+        // LoRaMacMcpsRequest( &mcpsReq );
+        // GPS_data_ready = 0;
+        // Ready_for_more = 1;
+    //}
+    sending = 0;
+    __enable_irq();//NVIC_EnableIRQ(USART1_IRQn);
+    pc.putc('N');
+}
+
 
 void gps_receive(void) {
     // Note: you need to actually read from the serial to clear the RX interrupt
-    //while(gps.readable()) {
-    GPS_status= !GPS_status;
-    if(sending==1){
-        flushSerialBuffer();
-        return;
-    }
-    if(gps.readable()){
-        __disable_irq();
-        keepOut = true;
+    __disable_irq();
+    keepOut = true;     //
+    while(gps.readable()) {
         char incoming = gps.getc();
         // #ifdef DEBUGGER
         // pc.putc(incoming);
         // #endif
         Rx_data[Char_index] = incoming;
     
-       if((Rx_data[Char_index] == '\n'))
-       {    // Received the end of an NMEA scentence
-            if((strncmp(GPRMC_Tag,Rx_data,(sizeof(GPRMC_Tag)-1)) == 0) || (strncmp(GNRMC_Tag,Rx_data,(sizeof(GNRMC_Tag)-1)) == 0))
-            {
+        if((Rx_data[Char_index] == '\n')){    // Received the end of an NMEA scentence
+            if((strncmp(GPRMC_Tag,Rx_data,(sizeof(GPRMC_Tag)-1)) == 0) || (strncmp(GNRMC_Tag,Rx_data,(sizeof(GNRMC_Tag)-1)) == 0)){
                 RMC = Parse_RMC_sentence(Rx_data);
-                if(strcmp(RMC.Status, "A") == 0)
-                {
-                     // GPS_status = 0;    // Turn status LED off
+                if(strcmp(RMC.Status, "A") == 0){
+                    // valid = 1;
+                    // GPS_status = 0;    // Turn status LED off
+                    // GPS_parse = Parse_RMC_data(RMC); // Parse into format suitable for Tom (Dinghy_RaceTrak software)
+                    // Call function to send using LoRa module
+                    // LoRaSend();
+                    // pc.putc('O');
+                    if(timerset == false)
+                        firstfix = true;
+                    /*
+                    if(firstfix == false)                       //
+                    {                                           //
+                        //LoRaSend_timer.attach(&LoRaSend, 1.0);  //
+                        firstfix = true;                        //                
+                    }   */                                   //                
                 }
-                else
-                {
+                else{
                     // GPS_status = 1;
+                    // Send "No GPS fix" over LoRa?
                 }
-                if (sending)
-                {
-                    Char_index = 0;
-                    memset(Rx_data,0,sizeof(Rx_data));
-                    __enable_irq();
-                    keepOut = false;
-                    return;
-                }
-                GPS_data_ready = 0;
+                ///*
+                if (sending)                            //
+                {                                       // 
+                    Char_index = 0;                     //
+                    memset(Rx_data,0,sizeof(Rx_data));  //
+                    __enable_irq();                     //
+                    keepOut = false;                    //
+                    return;                             //
+                }   //*/                                //
+                GPS_status=!GPS_status;
+                // delete this line
+                //GPS_data_ready = 0;
                 GPS_parse = Parse_RMC_data(RMC); // Parse into format suitable for Tom (Dinghy_RaceTrak software)
-                GPS_data_ready = 1;
+                //GPS_data_ready = 1;
+
+
             }
             Char_index = 0;
             memset(Rx_data,0,sizeof(Rx_data));
-             
         }
         else
             Char_index++;
     }
     keepOut = false;
     __enable_irq();
+    // put first fix code here if above doesn't work
     return;
-}
-
-void LoRaSend(){
-    //GPS_status = !GPS_status;
-    if (keepOut)
-        return;
-    __disable_irq();//NVIC_DisableIRQ(USART1_IRQn);
-    if(GPS_data_ready == 1){
-        sending = 1;
-        char AppDatas[APPDATA_SIZE];
-        strcpy(AppDatas, GPS_parse.Latitude);
-        strcat(AppDatas, ",");
-        strcat(AppDatas, GPS_parse.Longitude);
-        strcat(AppDatas, ",");
-        strcat(AppDatas, GPS_parse.Course_Over_Ground);
-        strcat(AppDatas, ",");
-        strcat(AppDatas, GPS_parse.Speed_Over_Ground);
-        strcat(AppDatas, ",");
-        //strcat(AppDatas, GPS_parse.Date);
-        //strcat(AppDatas, ",");
-        strcat(AppDatas, GPS_parse.UTC_Time);
-        strcat(AppDatas, ",");
-        strcat(AppDatas, GPS_parse.Valid);
-        
-        McpsReq_t mcpsReq;
-        
-        uint8_t AppPort = 3;
-        mcpsReq.Type = MCPS_UNCONFIRMED;
-        mcpsReq.Req.Unconfirmed.fPort = AppPort;
-        mcpsReq.Req.Unconfirmed.fBuffer = AppDatas;
-        mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE+2;
-        mcpsReq.Req.Unconfirmed.Datarate = DR_5;
-        // status = !status;
-        //GPS_status = !GPS_status;
-        LoRaMacMcpsRequest( &mcpsReq );
-        GPS_data_ready = 0;
-        // Ready_for_more = 1;
-    }
-    sending = 0;
-    __enable_irq();//NVIC_EnableIRQ(USART1_IRQn);
+    // store chars into a string then process into LAT, LONG, SOG, COG & DATETIME, VALID/INVLAID
 }
 
 void McpsConfirm( McpsConfirm_t *mcpsConfirm )
@@ -230,7 +262,7 @@
     //Sendframe
     
     McpsReq_t mcpsReq;
-    
+     
     uint8_t AppPort = 3;
     mcpsReq.Type = MCPS_UNCONFIRMED;
     mcpsReq.Req.Unconfirmed.fPort = AppPort;
@@ -239,7 +271,6 @@
     mcpsReq.Req.Unconfirmed.Datarate = DR_5;
         
     LoRaMacMcpsRequest( &mcpsReq );*/
-    
     GPS_status = 1;
     gps.attach(&gps_receive, Serial::RxIrq);
     gps.printf(PMTK_SET_NMEA_OUTPUT_RMC); // Only output RMC and GPTXT
@@ -253,9 +284,16 @@
     gps.printf(PMTK_SET_NMEA_OUTPUT_RMC); // Only output RMC and GPTXT
     wait(1.0); 
     gps.printf(PMTK_SET_UPDATE_F_2HZ);    // Set update Frequency to 2Hz
-    LoRaSend_timer.attach(&LoRaSend, 1.0);
+    //LoRaSend_timer.attach(&LoRaSend, 1.0);
     while(1){
-        //Print_RMC_data(&RMC);
-        //wait(0.5);
+        if(sending==0){
+            Print_GPS_data(GPS_parse);
+        }
+        if(firstfix){
+            LoRaSend_timer.attach(&LoRaSend, 1.0);
+            firstfix = false;
+            timerset = true;
+        }
+        wait(0.5);
         }
 }
\ No newline at end of file