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

Committer:
Rishin
Date:
Mon Dec 04 14:46:58 2017 +0000
Revision:
12:dc90bd3ac7d8
Parent:
11:32323f490d9e
Child:
14:539ef77197e0
commit for publishing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:92bca02df485 1 /*
mluis 0:92bca02df485 2 / _____) _ | |
mluis 0:92bca02df485 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:92bca02df485 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:92bca02df485 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:92bca02df485 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:92bca02df485 7 (C)2015 Semtech
Rishin 12:dc90bd3ac7d8 8
mluis 0:92bca02df485 9 Description: LoRaMac classA device implementation
Rishin 12:dc90bd3ac7d8 10
mluis 0:92bca02df485 11 License: Revised BSD License, see LICENSE.TXT file include in the project
Rishin 12:dc90bd3ac7d8 12
mluis 0:92bca02df485 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:92bca02df485 14 */
mluis 0:92bca02df485 15 #include "mbed.h"
mluis 0:92bca02df485 16 #include "board.h"
mluis 0:92bca02df485 17 #include "radio.h"
mluis 0:92bca02df485 18 #include "LoRaMac.h"
Rishin 12:dc90bd3ac7d8 19
Rishin 12:dc90bd3ac7d8 20
Rishin 12:dc90bd3ac7d8 21 #define APPDATA_SIZE 45
joshcurry 10:9364ab3a08eb 22 #define LORAWAN_DEFAULT_DATARATE DR_0
joshcurry 10:9364ab3a08eb 23 #define LORAWAN_DEVICE_ADDRESS ( uint32_t )0x01234567
joshcurry 10:9364ab3a08eb 24 #define LORAWAN_NWKSKEY { 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }
joshcurry 10:9364ab3a08eb 25 #define LORAWAN_APPSKEY { 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }
Rishin 12:dc90bd3ac7d8 26
joshcurry 10:9364ab3a08eb 27 uint8_t AppData[APPDATA_SIZE];
mluis 0:92bca02df485 28 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:92bca02df485 29 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
Rishin 12:dc90bd3ac7d8 30
Rishin 12:dc90bd3ac7d8 31
Rishin 12:dc90bd3ac7d8 32 #include "mbed.h"
Rishin 12:dc90bd3ac7d8 33 #include "l86.hpp"
mluis 0:92bca02df485 34
mluis 0:92bca02df485 35
Rishin 12:dc90bd3ac7d8 36 // #ifdef DEBUGGER
Rishin 12:dc90bd3ac7d8 37 // RawSerial pc(PC_TX, PC_RX); // USART2
Rishin 12:dc90bd3ac7d8 38 // #endif
Rishin 12:dc90bd3ac7d8 39
Rishin 12:dc90bd3ac7d8 40 Serial gps(GPS_TX, GPS_RX); // USART1
Rishin 12:dc90bd3ac7d8 41
Rishin 12:dc90bd3ac7d8 42 // DigitalOut Reset(RESET_PIN);
Rishin 12:dc90bd3ac7d8 43 // DigitalOut Force_on(FORCE_ON_PIN);
Rishin 12:dc90bd3ac7d8 44 DigitalOut GPS_status(GPS_STATUS_LED);
Rishin 12:dc90bd3ac7d8 45 // DigitalIn OnePPS(ONEPPS_PIN);
Rishin 12:dc90bd3ac7d8 46
Rishin 12:dc90bd3ac7d8 47 RMC_data RMC;
Rishin 12:dc90bd3ac7d8 48 GPS_data GPS_parse;
Rishin 12:dc90bd3ac7d8 49 volatile int Char_index = 0; // index for char array
Rishin 12:dc90bd3ac7d8 50 char Rx_data[MAX_NMEA_LENGTH] = "0"; // char array to store received bytes
Rishin 12:dc90bd3ac7d8 51 char tx_line[82];
Rishin 12:dc90bd3ac7d8 52 // uint8_t valid = 0;
Rishin 12:dc90bd3ac7d8 53 volatile uint8_t GPS_data_ready = 0;
Rishin 12:dc90bd3ac7d8 54 volatile uint8_t sending = 0;
Rishin 12:dc90bd3ac7d8 55
Rishin 12:dc90bd3ac7d8 56 volatile bool keepOut = false;
Rishin 12:dc90bd3ac7d8 57
Rishin 12:dc90bd3ac7d8 58 uint8_t miss_count =0;
Rishin 12:dc90bd3ac7d8 59
Rishin 12:dc90bd3ac7d8 60 Ticker LoRaSend_timer;
Rishin 12:dc90bd3ac7d8 61
Rishin 12:dc90bd3ac7d8 62 void flushSerialBuffer(void) { char char1 = 0; while (gps.readable()) { char1 = gps.getc(); } return; }
Rishin 12:dc90bd3ac7d8 63
Rishin 12:dc90bd3ac7d8 64 void gps_receive(void) {
Rishin 12:dc90bd3ac7d8 65 // Note: you need to actually read from the serial to clear the RX interrupt
Rishin 12:dc90bd3ac7d8 66 //while(gps.readable()) {
Rishin 12:dc90bd3ac7d8 67 GPS_status= !GPS_status;
Rishin 12:dc90bd3ac7d8 68 if(sending==1){
Rishin 12:dc90bd3ac7d8 69 flushSerialBuffer();
Rishin 12:dc90bd3ac7d8 70 return;
Rishin 12:dc90bd3ac7d8 71 }
Rishin 12:dc90bd3ac7d8 72 if(gps.readable()){
Rishin 12:dc90bd3ac7d8 73 __disable_irq();
Rishin 12:dc90bd3ac7d8 74 keepOut = true;
Rishin 12:dc90bd3ac7d8 75 char incoming = gps.getc();
Rishin 12:dc90bd3ac7d8 76 // #ifdef DEBUGGER
Rishin 12:dc90bd3ac7d8 77 // pc.putc(incoming);
Rishin 12:dc90bd3ac7d8 78 // #endif
Rishin 12:dc90bd3ac7d8 79 Rx_data[Char_index] = incoming;
Rishin 12:dc90bd3ac7d8 80
Rishin 12:dc90bd3ac7d8 81 if((Rx_data[Char_index] == '\n'))
Rishin 12:dc90bd3ac7d8 82 { // Received the end of an NMEA scentence
Rishin 12:dc90bd3ac7d8 83 if((strncmp(GPRMC_Tag,Rx_data,(sizeof(GPRMC_Tag)-1)) == 0) || (strncmp(GNRMC_Tag,Rx_data,(sizeof(GNRMC_Tag)-1)) == 0))
Rishin 12:dc90bd3ac7d8 84 {
Rishin 12:dc90bd3ac7d8 85 RMC = Parse_RMC_sentence(Rx_data);
Rishin 12:dc90bd3ac7d8 86 if(strcmp(RMC.Status, "A") == 0)
Rishin 12:dc90bd3ac7d8 87 {
Rishin 12:dc90bd3ac7d8 88 // GPS_status = 0; // Turn status LED off
Rishin 12:dc90bd3ac7d8 89 }
Rishin 12:dc90bd3ac7d8 90 else
Rishin 12:dc90bd3ac7d8 91 {
Rishin 12:dc90bd3ac7d8 92 // GPS_status = 1;
Rishin 12:dc90bd3ac7d8 93 }
Rishin 12:dc90bd3ac7d8 94 if (sending)
Rishin 12:dc90bd3ac7d8 95 {
Rishin 12:dc90bd3ac7d8 96 Char_index = 0;
Rishin 12:dc90bd3ac7d8 97 memset(Rx_data,0,sizeof(Rx_data));
Rishin 12:dc90bd3ac7d8 98 __enable_irq();
Rishin 12:dc90bd3ac7d8 99 keepOut = false;
Rishin 12:dc90bd3ac7d8 100 return;
Rishin 12:dc90bd3ac7d8 101 }
Rishin 12:dc90bd3ac7d8 102 GPS_data_ready = 0;
Rishin 12:dc90bd3ac7d8 103 GPS_parse = Parse_RMC_data(RMC); // Parse into format suitable for Tom (Dinghy_RaceTrak software)
Rishin 12:dc90bd3ac7d8 104 GPS_data_ready = 1;
Rishin 12:dc90bd3ac7d8 105 }
Rishin 12:dc90bd3ac7d8 106 Char_index = 0;
Rishin 12:dc90bd3ac7d8 107 memset(Rx_data,0,sizeof(Rx_data));
Rishin 12:dc90bd3ac7d8 108
Rishin 12:dc90bd3ac7d8 109 }
Rishin 12:dc90bd3ac7d8 110 else
Rishin 12:dc90bd3ac7d8 111 Char_index++;
Rishin 12:dc90bd3ac7d8 112 }
Rishin 12:dc90bd3ac7d8 113 keepOut = false;
Rishin 12:dc90bd3ac7d8 114 __enable_irq();
Rishin 12:dc90bd3ac7d8 115 return;
Rishin 12:dc90bd3ac7d8 116 }
Rishin 12:dc90bd3ac7d8 117
Rishin 12:dc90bd3ac7d8 118 void LoRaSend(){
Rishin 12:dc90bd3ac7d8 119 //GPS_status = !GPS_status;
Rishin 12:dc90bd3ac7d8 120 if (keepOut)
Rishin 12:dc90bd3ac7d8 121 return;
Rishin 12:dc90bd3ac7d8 122 __disable_irq();//NVIC_DisableIRQ(USART1_IRQn);
Rishin 12:dc90bd3ac7d8 123 if(GPS_data_ready == 1){
Rishin 12:dc90bd3ac7d8 124 sending = 1;
Rishin 12:dc90bd3ac7d8 125 char AppDatas[APPDATA_SIZE];
Rishin 12:dc90bd3ac7d8 126 strcpy(AppDatas, GPS_parse.Latitude);
Rishin 12:dc90bd3ac7d8 127 strcat(AppDatas, ",");
Rishin 12:dc90bd3ac7d8 128 strcat(AppDatas, GPS_parse.Longitude);
Rishin 12:dc90bd3ac7d8 129 strcat(AppDatas, ",");
Rishin 12:dc90bd3ac7d8 130 strcat(AppDatas, GPS_parse.Course_Over_Ground);
Rishin 12:dc90bd3ac7d8 131 strcat(AppDatas, ",");
Rishin 12:dc90bd3ac7d8 132 strcat(AppDatas, GPS_parse.Speed_Over_Ground);
Rishin 12:dc90bd3ac7d8 133 strcat(AppDatas, ",");
Rishin 12:dc90bd3ac7d8 134 //strcat(AppDatas, GPS_parse.Date);
Rishin 12:dc90bd3ac7d8 135 //strcat(AppDatas, ",");
Rishin 12:dc90bd3ac7d8 136 strcat(AppDatas, GPS_parse.UTC_Time);
Rishin 12:dc90bd3ac7d8 137 strcat(AppDatas, ",");
Rishin 12:dc90bd3ac7d8 138 strcat(AppDatas, GPS_parse.Valid);
Rishin 12:dc90bd3ac7d8 139
Rishin 12:dc90bd3ac7d8 140 McpsReq_t mcpsReq;
Rishin 12:dc90bd3ac7d8 141
Rishin 12:dc90bd3ac7d8 142 uint8_t AppPort = 3;
Rishin 12:dc90bd3ac7d8 143 mcpsReq.Type = MCPS_UNCONFIRMED;
Rishin 12:dc90bd3ac7d8 144 mcpsReq.Req.Unconfirmed.fPort = AppPort;
Rishin 12:dc90bd3ac7d8 145 mcpsReq.Req.Unconfirmed.fBuffer = AppDatas;
Rishin 12:dc90bd3ac7d8 146 mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE+2;
Rishin 12:dc90bd3ac7d8 147 mcpsReq.Req.Unconfirmed.Datarate = DR_5;
Rishin 12:dc90bd3ac7d8 148 // status = !status;
Rishin 12:dc90bd3ac7d8 149 //GPS_status = !GPS_status;
Rishin 12:dc90bd3ac7d8 150 LoRaMacMcpsRequest( &mcpsReq );
Rishin 12:dc90bd3ac7d8 151 GPS_data_ready = 0;
Rishin 12:dc90bd3ac7d8 152 // Ready_for_more = 1;
Rishin 12:dc90bd3ac7d8 153 }
Rishin 12:dc90bd3ac7d8 154 sending = 0;
Rishin 12:dc90bd3ac7d8 155 __enable_irq();//NVIC_EnableIRQ(USART1_IRQn);
Rishin 12:dc90bd3ac7d8 156 }
mluis 0:92bca02df485 157
joshcurry 10:9364ab3a08eb 158 void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:92bca02df485 159 {
joshcurry 10:9364ab3a08eb 160 return;
mluis 0:92bca02df485 161 }
Rishin 12:dc90bd3ac7d8 162
joshcurry 10:9364ab3a08eb 163 void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 0:92bca02df485 164 {
joshcurry 10:9364ab3a08eb 165 return;
mluis 3:9c6f7f082151 166 }
Rishin 12:dc90bd3ac7d8 167
joshcurry 10:9364ab3a08eb 168
mluis 0:92bca02df485 169 int main( void )
mluis 0:92bca02df485 170 {
joshcurry 10:9364ab3a08eb 171 //Initialise firmware
joshcurry 10:9364ab3a08eb 172
mluis 3:9c6f7f082151 173 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 3:9c6f7f082151 174 LoRaMacCallback_t LoRaMacCallbacks;
mluis 3:9c6f7f082151 175 MibRequestConfirm_t mibReq;
Rishin 12:dc90bd3ac7d8 176
joshcurry 10:9364ab3a08eb 177 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
joshcurry 10:9364ab3a08eb 178 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
joshcurry 10:9364ab3a08eb 179 // LoRaMacPrimitives.MacMlmeConfirm = McpsConfirm;
joshcurry 10:9364ab3a08eb 180 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
Rishin 12:dc90bd3ac7d8 181
Rishin 12:dc90bd3ac7d8 182 LoRaMacChannelAdd( 3, ( ChannelParams_t ){ 868100000, { ( ( DR_5 << 4 ) | DR_5 ) }, 3 } );
cdebank 11:32323f490d9e 183 //LoRaMacChannelAdd( 4, ( ChannelParams_t ){ 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
cdebank 11:32323f490d9e 184 //LoRaMacChannelAdd( 5, ( ChannelParams_t ){ 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
cdebank 11:32323f490d9e 185 //LoRaMacChannelAdd( 6, ( ChannelParams_t ){ 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
cdebank 11:32323f490d9e 186 //LoRaMacChannelAdd( 7, ( ChannelParams_t ){ 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
cdebank 11:32323f490d9e 187 //LoRaMacChannelAdd( 8, ( ChannelParams_t ){ 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } );
cdebank 11:32323f490d9e 188 //LoRaMacChannelAdd( 9, ( ChannelParams_t ){ 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } );
Rishin 12:dc90bd3ac7d8 189
joshcurry 10:9364ab3a08eb 190 //Join ABP
Rishin 12:dc90bd3ac7d8 191
joshcurry 10:9364ab3a08eb 192 mibReq.Type = MIB_NET_ID;
joshcurry 10:9364ab3a08eb 193 mibReq.Param.NetID = 0;
joshcurry 10:9364ab3a08eb 194 LoRaMacMibSetRequestConfirm( &mibReq );
Rishin 12:dc90bd3ac7d8 195
joshcurry 10:9364ab3a08eb 196 mibReq.Type = MIB_DEV_ADDR;
joshcurry 10:9364ab3a08eb 197 mibReq.Param.DevAddr = LORAWAN_DEVICE_ADDRESS;
joshcurry 10:9364ab3a08eb 198 LoRaMacMibSetRequestConfirm( &mibReq );
Rishin 12:dc90bd3ac7d8 199
joshcurry 10:9364ab3a08eb 200 mibReq.Type = MIB_NWK_SKEY;
joshcurry 10:9364ab3a08eb 201 mibReq.Param.NwkSKey = NwkSKey;
joshcurry 10:9364ab3a08eb 202 LoRaMacMibSetRequestConfirm( &mibReq );
Rishin 12:dc90bd3ac7d8 203
joshcurry 10:9364ab3a08eb 204 mibReq.Type = MIB_APP_SKEY;
joshcurry 10:9364ab3a08eb 205 mibReq.Param.AppSKey = AppSKey;
joshcurry 10:9364ab3a08eb 206 LoRaMacMibSetRequestConfirm( &mibReq );
Rishin 12:dc90bd3ac7d8 207
joshcurry 10:9364ab3a08eb 208 mibReq.Type = MIB_NETWORK_JOINED;
joshcurry 10:9364ab3a08eb 209 mibReq.Param.IsNetworkJoined = true;
joshcurry 10:9364ab3a08eb 210 LoRaMacMibSetRequestConfirm( &mibReq );
joshcurry 10:9364ab3a08eb 211
joshcurry 10:9364ab3a08eb 212 mibReq.Type = MIB_CHANNELS_TX_POWER;
joshcurry 10:9364ab3a08eb 213 mibReq.Param.ChannelsTxPower = LORAMAC_MAX_TX_POWER;
joshcurry 10:9364ab3a08eb 214 LoRaMacMibSetRequestConfirm( &mibReq );
joshcurry 10:9364ab3a08eb 215
joshcurry 10:9364ab3a08eb 216 mibReq.Type = MIB_CHANNELS_DEFAULT_TX_POWER;
joshcurry 10:9364ab3a08eb 217 mibReq.Param.ChannelsDefaultTxPower = LORAMAC_MAX_TX_POWER;
joshcurry 10:9364ab3a08eb 218 LoRaMacMibSetRequestConfirm( &mibReq );
Rishin 12:dc90bd3ac7d8 219
Rishin 12:dc90bd3ac7d8 220 /*//Prepareframe
Rishin 12:dc90bd3ac7d8 221
cdebank 11:32323f490d9e 222 AppData[0] = 0x43;
cdebank 11:32323f490d9e 223 AppData[1] = 0x68;
cdebank 11:32323f490d9e 224 AppData[2] = 0x72;
cdebank 11:32323f490d9e 225 AppData[3] = 0x69;
cdebank 11:32323f490d9e 226 AppData[4] = 0x73;
cdebank 11:32323f490d9e 227 AppData[5] = 0x21;
cdebank 11:32323f490d9e 228 AppData[6] = 0x21;
joshcurry 10:9364ab3a08eb 229
joshcurry 10:9364ab3a08eb 230 //Sendframe
joshcurry 10:9364ab3a08eb 231
joshcurry 10:9364ab3a08eb 232 McpsReq_t mcpsReq;
joshcurry 10:9364ab3a08eb 233
joshcurry 10:9364ab3a08eb 234 uint8_t AppPort = 3;
cdebank 11:32323f490d9e 235 mcpsReq.Type = MCPS_UNCONFIRMED;
cdebank 11:32323f490d9e 236 mcpsReq.Req.Unconfirmed.fPort = AppPort;
cdebank 11:32323f490d9e 237 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
cdebank 11:32323f490d9e 238 mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE;
cdebank 11:32323f490d9e 239 mcpsReq.Req.Unconfirmed.Datarate = DR_5;
joshcurry 10:9364ab3a08eb 240
Rishin 12:dc90bd3ac7d8 241 LoRaMacMcpsRequest( &mcpsReq );*/
joshcurry 10:9364ab3a08eb 242
Rishin 12:dc90bd3ac7d8 243 GPS_status = 1;
Rishin 12:dc90bd3ac7d8 244 gps.attach(&gps_receive, Serial::RxIrq);
Rishin 12:dc90bd3ac7d8 245 gps.printf(PMTK_SET_NMEA_OUTPUT_RMC); // Only output RMC and GPTXT
Rishin 12:dc90bd3ac7d8 246 wait(1.0);
Rishin 12:dc90bd3ac7d8 247 gps.printf(PMTK_SET_UPDATE_F_2HZ); // Set update Frequency to 2Hz
Rishin 12:dc90bd3ac7d8 248 wait(1.0);
Rishin 12:dc90bd3ac7d8 249 gps.printf(PMTK_SET_NMEA_OUTPUT_RMC); // Only output RMC and GPTXT
Rishin 12:dc90bd3ac7d8 250 wait(1.0);
Rishin 12:dc90bd3ac7d8 251 gps.printf(PMTK_SET_UPDATE_F_2HZ); // Set update Frequency to 2Hz
Rishin 12:dc90bd3ac7d8 252 wait(1.0);
Rishin 12:dc90bd3ac7d8 253 gps.printf(PMTK_SET_NMEA_OUTPUT_RMC); // Only output RMC and GPTXT
Rishin 12:dc90bd3ac7d8 254 wait(1.0);
Rishin 12:dc90bd3ac7d8 255 gps.printf(PMTK_SET_UPDATE_F_2HZ); // Set update Frequency to 2Hz
Rishin 12:dc90bd3ac7d8 256 LoRaSend_timer.attach(&LoRaSend, 1.0);
cdebank 11:32323f490d9e 257 while(1){
Rishin 12:dc90bd3ac7d8 258 //Print_RMC_data(&RMC);
Rishin 12:dc90bd3ac7d8 259 //wait(0.5);
cdebank 11:32323f490d9e 260 }
Rishin 12:dc90bd3ac7d8 261 }