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:
12:dc90bd3ac7d8
Parent:
11:32323f490d9e
Child:
14:539ef77197e0
diff -r 32323f490d9e -r dc90bd3ac7d8 app/main.cpp
--- a/app/main.cpp	Thu Nov 16 14:30:39 2017 +0000
+++ b/app/main.cpp	Mon Dec 04 14:46:58 2017 +0000
@@ -5,43 +5,166 @@
  _____) ) ____| | | || |_| ____( (___| | | |
 (______/|_____)_|_|_| \__)_____)\____)_| |_|
     (C)2015 Semtech
-
+ 
 Description: LoRaMac classA device implementation
-
+ 
 License: Revised BSD License, see LICENSE.TXT file include in the project
-
+ 
 Maintainer: Miguel Luis and Gregory Cristian
 */
 #include "mbed.h"
 #include "board.h"
 #include "radio.h"
 #include "LoRaMac.h"
-
-
-#define APPDATA_SIZE 7
+ 
+ 
+#define APPDATA_SIZE 45
 #define LORAWAN_DEFAULT_DATARATE DR_0
 #define LORAWAN_DEVICE_ADDRESS                      ( uint32_t )0x01234567
 #define LORAWAN_NWKSKEY                             { 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }
 #define LORAWAN_APPSKEY                             { 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }
-
+ 
 uint8_t AppData[APPDATA_SIZE];
 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
+ 
+
+#include "mbed.h"
+#include "l86.hpp"
 
 
+// #ifdef DEBUGGER
+// RawSerial pc(PC_TX, PC_RX);    // USART2
+// #endif
+
+Serial gps(GPS_TX, GPS_RX); // USART1
+
+// DigitalOut Reset(RESET_PIN);
+// DigitalOut Force_on(FORCE_ON_PIN);
+DigitalOut GPS_status(GPS_STATUS_LED);
+// DigitalIn  OnePPS(ONEPPS_PIN);
+
+RMC_data RMC;
+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 sending = 0;
+
+volatile bool keepOut = false;
+
+uint8_t miss_count =0;
+
+Ticker LoRaSend_timer;
+
+void flushSerialBuffer(void) { char char1 = 0; while (gps.readable()) { char1 = gps.getc(); } return; }
+
+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;
+        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))
+            {
+                RMC = Parse_RMC_sentence(Rx_data);
+                if(strcmp(RMC.Status, "A") == 0)
+                {
+                     // GPS_status = 0;    // Turn status LED off
+                }
+                else
+                {
+                    // GPS_status = 1;
+                }
+                if (sending)
+                {
+                    Char_index = 0;
+                    memset(Rx_data,0,sizeof(Rx_data));
+                    __enable_irq();
+                    keepOut = false;
+                    return;
+                }
+                GPS_data_ready = 0;
+                GPS_parse = Parse_RMC_data(RMC); // Parse into format suitable for Tom (Dinghy_RaceTrak software)
+                GPS_data_ready = 1;
+            }
+            Char_index = 0;
+            memset(Rx_data,0,sizeof(Rx_data));
+             
+        }
+        else
+            Char_index++;
+    }
+    keepOut = false;
+    __enable_irq();
+    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);
+}
 
 void McpsConfirm( McpsConfirm_t *mcpsConfirm )
 {
     return;
 }
-
+ 
 void McpsIndication( McpsIndication_t *mcpsIndication )
 {
     return;
 }
-
-
-
+ 
  
 int main( void )
 {
@@ -50,38 +173,38 @@
     LoRaMacPrimitives_t LoRaMacPrimitives;
     LoRaMacCallback_t LoRaMacCallbacks;
     MibRequestConfirm_t mibReq;
-
+ 
     LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
     LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
 //    LoRaMacPrimitives.MacMlmeConfirm = McpsConfirm;
     LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
-
-    LoRaMacChannelAdd( 3, ( ChannelParams_t ){ 869400000, { ( ( DR_5 << 4 ) | DR_5 ) }, 3 } );
+ 
+    LoRaMacChannelAdd( 3, ( ChannelParams_t ){ 868100000, { ( ( DR_5 << 4 ) | DR_5 ) }, 3 } );
     //LoRaMacChannelAdd( 4, ( ChannelParams_t ){ 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
     //LoRaMacChannelAdd( 5, ( ChannelParams_t ){ 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
     //LoRaMacChannelAdd( 6, ( ChannelParams_t ){ 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
     //LoRaMacChannelAdd( 7, ( ChannelParams_t ){ 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
     //LoRaMacChannelAdd( 8, ( ChannelParams_t ){ 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } );
     //LoRaMacChannelAdd( 9, ( ChannelParams_t ){ 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } );
-
+ 
     //Join ABP
-
+ 
     mibReq.Type = MIB_NET_ID;
     mibReq.Param.NetID = 0;
     LoRaMacMibSetRequestConfirm( &mibReq );
-
+ 
     mibReq.Type = MIB_DEV_ADDR;
     mibReq.Param.DevAddr = LORAWAN_DEVICE_ADDRESS;
     LoRaMacMibSetRequestConfirm( &mibReq );
-
+ 
     mibReq.Type = MIB_NWK_SKEY;
     mibReq.Param.NwkSKey = NwkSKey;
     LoRaMacMibSetRequestConfirm( &mibReq );
-
+ 
     mibReq.Type = MIB_APP_SKEY;
     mibReq.Param.AppSKey = AppSKey;
     LoRaMacMibSetRequestConfirm( &mibReq );
-
+ 
     mibReq.Type = MIB_NETWORK_JOINED;
     mibReq.Param.IsNetworkJoined = true;
     LoRaMacMibSetRequestConfirm( &mibReq );
@@ -93,9 +216,9 @@
     mibReq.Type = MIB_CHANNELS_DEFAULT_TX_POWER;
     mibReq.Param.ChannelsDefaultTxPower = LORAMAC_MAX_TX_POWER;
     LoRaMacMibSetRequestConfirm( &mibReq );
-
-    //Prepareframe
-
+ 
+    /*//Prepareframe
+ 
     AppData[0] = 0x43;
     AppData[1] = 0x68;
     AppData[2] = 0x72;
@@ -115,11 +238,24 @@
     mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE;
     mcpsReq.Req.Unconfirmed.Datarate = DR_5;
         
-    LoRaMacMcpsRequest( &mcpsReq );
+    LoRaMacMcpsRequest( &mcpsReq );*/
     
+    GPS_status = 1;
+    gps.attach(&gps_receive, Serial::RxIrq);
+    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
+    wait(1.0); 
+    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
+    wait(1.0); 
+    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);
     while(1){
-        wait(1);
-        LoRaMacMcpsRequest( &mcpsReq );
+        //Print_RMC_data(&RMC);
+        //wait(0.5);
         }
-}
-
+}
\ No newline at end of file