Lora_with_GPS integration code - program hangs after a few transmissions

Dependencies:   mbed LoRaWAN-lib SingleFrequencyLora

Fork of Lora_with_GPS by Rishin Amin

Committer:
Rishin
Date:
Wed Nov 22 21:01:30 2017 +0000
Revision:
14:6990bc2f44e9
Parent:
12:7debb1c79a06
still crashing - finding the root cause

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
mluis 0:92bca02df485 8
mluis 0:92bca02df485 9 Description: LoRaMac classA device implementation
mluis 0:92bca02df485 10
mluis 0:92bca02df485 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:92bca02df485 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:7debb1c79a06 19 #include "l86.hpp"
mluis 3:9c6f7f082151 20
Rishin 14:6990bc2f44e9 21
Rishin 12:7debb1c79a06 22 // #define APPDATA_SIZE 55 // size of a GPS_data struct //commented by Rish
joshcurry 10:9364ab3a08eb 23 #define LORAWAN_DEFAULT_DATARATE DR_0
joshcurry 10:9364ab3a08eb 24 #define LORAWAN_DEVICE_ADDRESS ( uint32_t )0x01234567
joshcurry 10:9364ab3a08eb 25 #define LORAWAN_NWKSKEY { 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }
joshcurry 10:9364ab3a08eb 26 #define LORAWAN_APPSKEY { 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }
mluis 1:352f608c3337 27
Rishin 12:7debb1c79a06 28
Rishin 12:7debb1c79a06 29 RawSerial gps(GPS_TX, GPS_RX); // USART1
Rishin 12:7debb1c79a06 30 // RawSerial pc(PC_TX, PC_RX);
Rishin 12:7debb1c79a06 31
Rishin 12:7debb1c79a06 32 DigitalOut Reset(RESET_PIN);
Rishin 12:7debb1c79a06 33 DigitalOut Force_on(FORCE_ON_PIN);
Rishin 12:7debb1c79a06 34 DigitalOut GPS_status(GPS_STATUS_LED);
Rishin 12:7debb1c79a06 35 // DigitalIn OnePPS(ONEPPS_PIN);
Rishin 12:7debb1c79a06 36
Rishin 12:7debb1c79a06 37
Rishin 12:7debb1c79a06 38 // uint8_t AppDatasend[APPDATA_SIZE];
Rishin 12:7debb1c79a06 39 RMC_data RMC;
Rishin 12:7debb1c79a06 40 GPS_data GPS_parse;
Rishin 12:7debb1c79a06 41 volatile int Char_index = 0; // index for char array
Rishin 12:7debb1c79a06 42 char Rx_data[MAX_NMEA_LENGTH] = "0"; // char array to store received bytes
Rishin 12:7debb1c79a06 43 char tx_line[82];
Rishin 14:6990bc2f44e9 44 volatile uint8_t Transmission_in_progress = 0;
Rishin 12:7debb1c79a06 45
Rishin 12:7debb1c79a06 46 void gps_receive(void) {
Rishin 12:7debb1c79a06 47 // Note: you need to actually read from the serial to clear the RX interrupt
Rishin 12:7debb1c79a06 48 while(gps.readable()) {
Rishin 12:7debb1c79a06 49 char incoming = gps.getc();
Rishin 12:7debb1c79a06 50 // #ifdef DEBUGGER
Rishin 12:7debb1c79a06 51 // pc.putc(incoming);
Rishin 12:7debb1c79a06 52 // #endif
Rishin 12:7debb1c79a06 53 Rx_data[Char_index] = incoming;
Rishin 14:6990bc2f44e9 54 if(Rx_data[Char_index] == '$'){
Rishin 14:6990bc2f44e9 55 Transmission_in_progress = 1;
Rishin 14:6990bc2f44e9 56 }
Rishin 14:6990bc2f44e9 57 if(Transmission_in_progress == 1){
Rishin 14:6990bc2f44e9 58 if((Rx_data[Char_index] == '\n')){ // Received the end of an NMEA scentence
Rishin 14:6990bc2f44e9 59 if((strncmp(GPRMC_Tag,Rx_data,(sizeof(GPRMC_Tag)-1)) == 0) || (strncmp(GNRMC_Tag,Rx_data,(sizeof(GNRMC_Tag)-1)) == 0)){
Rishin 14:6990bc2f44e9 60 RMC = Parse_RMC_sentence(Rx_data);
Rishin 14:6990bc2f44e9 61 if(strcmp(RMC.Status, "A") == 0){
Rishin 14:6990bc2f44e9 62 GPS_status = 0; // Turn status LED off
Rishin 14:6990bc2f44e9 63 }
Rishin 14:6990bc2f44e9 64 else{
Rishin 14:6990bc2f44e9 65 //GPS_status = 1;
Rishin 14:6990bc2f44e9 66 }
Rishin 14:6990bc2f44e9 67 GPS_status = !GPS_status; // for debugging
Rishin 14:6990bc2f44e9 68 NVIC_DisableIRQ(USART1_IRQn);
Rishin 14:6990bc2f44e9 69 GPS_parse = Parse_RMC_data(RMC); // Parse into format suitable for Tom (Dinghy_RaceTrak software)
Rishin 14:6990bc2f44e9 70 // Send_GPS_data(GPS_parse); // This doesn't work with LoRa send code but works without :/
Rishin 14:6990bc2f44e9 71 NVIC_EnableIRQ(USART1_IRQn);
Rishin 12:7debb1c79a06 72 }
Rishin 14:6990bc2f44e9 73 Transmission_in_progress = 0;
Rishin 14:6990bc2f44e9 74 Char_index = 0;
Rishin 14:6990bc2f44e9 75 memset(Rx_data,0,sizeof(Rx_data));
Rishin 12:7debb1c79a06 76 }
Rishin 14:6990bc2f44e9 77 else
Rishin 14:6990bc2f44e9 78 Char_index++;
Rishin 12:7debb1c79a06 79 }
Rishin 12:7debb1c79a06 80 }
Rishin 12:7debb1c79a06 81 return;
Rishin 12:7debb1c79a06 82 // store chars into a string then process into LAT, LONG, SOG, COG & DATETIME, VALID/INVLAID
Rishin 12:7debb1c79a06 83 }
Rishin 12:7debb1c79a06 84
mluis 0:92bca02df485 85 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:92bca02df485 86 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:92bca02df485 87
mluis 0:92bca02df485 88
mluis 0:92bca02df485 89
joshcurry 10:9364ab3a08eb 90 void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:92bca02df485 91 {
joshcurry 10:9364ab3a08eb 92 return;
mluis 0:92bca02df485 93 }
mluis 0:92bca02df485 94
joshcurry 10:9364ab3a08eb 95 void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 0:92bca02df485 96 {
joshcurry 10:9364ab3a08eb 97 return;
mluis 3:9c6f7f082151 98 }
mluis 3:9c6f7f082151 99
mluis 3:9c6f7f082151 100
mluis 9:ee9dcbb9708d 101
joshcurry 10:9364ab3a08eb 102
mluis 0:92bca02df485 103 int main( void )
mluis 0:92bca02df485 104 {
joshcurry 10:9364ab3a08eb 105 //Initialise firmware
Rishin 14:6990bc2f44e9 106
mluis 3:9c6f7f082151 107 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 3:9c6f7f082151 108 LoRaMacCallback_t LoRaMacCallbacks;
mluis 3:9c6f7f082151 109 MibRequestConfirm_t mibReq;
mluis 0:92bca02df485 110
joshcurry 10:9364ab3a08eb 111 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
joshcurry 10:9364ab3a08eb 112 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
joshcurry 10:9364ab3a08eb 113 // LoRaMacPrimitives.MacMlmeConfirm = McpsConfirm;
joshcurry 10:9364ab3a08eb 114 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 7:3173f0508a98 115
cdebank 11:32323f490d9e 116 LoRaMacChannelAdd( 3, ( ChannelParams_t ){ 869400000, { ( ( DR_5 << 4 ) | DR_5 ) }, 3 } );
cdebank 11:32323f490d9e 117 //LoRaMacChannelAdd( 4, ( ChannelParams_t ){ 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
cdebank 11:32323f490d9e 118 //LoRaMacChannelAdd( 5, ( ChannelParams_t ){ 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
cdebank 11:32323f490d9e 119 //LoRaMacChannelAdd( 6, ( ChannelParams_t ){ 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
cdebank 11:32323f490d9e 120 //LoRaMacChannelAdd( 7, ( ChannelParams_t ){ 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } );
cdebank 11:32323f490d9e 121 //LoRaMacChannelAdd( 8, ( ChannelParams_t ){ 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } );
cdebank 11:32323f490d9e 122 //LoRaMacChannelAdd( 9, ( ChannelParams_t ){ 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } );
mluis 7:3173f0508a98 123
joshcurry 10:9364ab3a08eb 124 //Join ABP
mluis 0:92bca02df485 125
joshcurry 10:9364ab3a08eb 126 mibReq.Type = MIB_NET_ID;
joshcurry 10:9364ab3a08eb 127 mibReq.Param.NetID = 0;
joshcurry 10:9364ab3a08eb 128 LoRaMacMibSetRequestConfirm( &mibReq );
joshcurry 10:9364ab3a08eb 129
joshcurry 10:9364ab3a08eb 130 mibReq.Type = MIB_DEV_ADDR;
joshcurry 10:9364ab3a08eb 131 mibReq.Param.DevAddr = LORAWAN_DEVICE_ADDRESS;
joshcurry 10:9364ab3a08eb 132 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 133
joshcurry 10:9364ab3a08eb 134 mibReq.Type = MIB_NWK_SKEY;
joshcurry 10:9364ab3a08eb 135 mibReq.Param.NwkSKey = NwkSKey;
joshcurry 10:9364ab3a08eb 136 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 137
joshcurry 10:9364ab3a08eb 138 mibReq.Type = MIB_APP_SKEY;
joshcurry 10:9364ab3a08eb 139 mibReq.Param.AppSKey = AppSKey;
joshcurry 10:9364ab3a08eb 140 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 141
joshcurry 10:9364ab3a08eb 142 mibReq.Type = MIB_NETWORK_JOINED;
joshcurry 10:9364ab3a08eb 143 mibReq.Param.IsNetworkJoined = true;
joshcurry 10:9364ab3a08eb 144 LoRaMacMibSetRequestConfirm( &mibReq );
joshcurry 10:9364ab3a08eb 145
joshcurry 10:9364ab3a08eb 146 mibReq.Type = MIB_CHANNELS_TX_POWER;
joshcurry 10:9364ab3a08eb 147 mibReq.Param.ChannelsTxPower = LORAMAC_MAX_TX_POWER;
joshcurry 10:9364ab3a08eb 148 LoRaMacMibSetRequestConfirm( &mibReq );
joshcurry 10:9364ab3a08eb 149
joshcurry 10:9364ab3a08eb 150 mibReq.Type = MIB_CHANNELS_DEFAULT_TX_POWER;
joshcurry 10:9364ab3a08eb 151 mibReq.Param.ChannelsDefaultTxPower = LORAMAC_MAX_TX_POWER;
joshcurry 10:9364ab3a08eb 152 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 153
Rishin 12:7debb1c79a06 154 /*//Prepareframe
mluis 3:9c6f7f082151 155
cdebank 11:32323f490d9e 156 AppData[0] = 0x43;
cdebank 11:32323f490d9e 157 AppData[1] = 0x68;
cdebank 11:32323f490d9e 158 AppData[2] = 0x72;
cdebank 11:32323f490d9e 159 AppData[3] = 0x69;
cdebank 11:32323f490d9e 160 AppData[4] = 0x73;
cdebank 11:32323f490d9e 161 AppData[5] = 0x21;
Rishin 12:7debb1c79a06 162 AppData[6] = 0x21;*/
joshcurry 10:9364ab3a08eb 163
joshcurry 10:9364ab3a08eb 164 //Sendframe
joshcurry 10:9364ab3a08eb 165
Rishin 14:6990bc2f44e9 166 /*McpsReq_t mcpsReq; // Commented out by Rish
joshcurry 10:9364ab3a08eb 167
joshcurry 10:9364ab3a08eb 168 uint8_t AppPort = 3;
cdebank 11:32323f490d9e 169 mcpsReq.Type = MCPS_UNCONFIRMED;
cdebank 11:32323f490d9e 170 mcpsReq.Req.Unconfirmed.fPort = AppPort;
cdebank 11:32323f490d9e 171 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
cdebank 11:32323f490d9e 172 mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE;
cdebank 11:32323f490d9e 173 mcpsReq.Req.Unconfirmed.Datarate = DR_5;
joshcurry 10:9364ab3a08eb 174
Rishin 14:6990bc2f44e9 175 //LoRaMacMcpsRequest( &mcpsReq );*/ // Commented out by Rish
Rishin 12:7debb1c79a06 176
Rishin 12:7debb1c79a06 177 GPS_status = 1;
Rishin 12:7debb1c79a06 178 gps.attach(&gps_receive, Serial::RxIrq);
Rishin 12:7debb1c79a06 179 gps.printf(PMTK_SET_NMEA_OUTPUT_RMC); // Only output RMC and GPTXT
Rishin 12:7debb1c79a06 180 gps.printf(PMTK_SET_UPDATE_F_2HZ); // Set update Frequency to 2Hz
joshcurry 10:9364ab3a08eb 181
cdebank 11:32323f490d9e 182 while(1){
Rishin 14:6990bc2f44e9 183 wait(3);
Rishin 12:7debb1c79a06 184 //LoRaMacMcpsRequest( &mcpsReq );
Rishin 12:7debb1c79a06 185 // Print_GPS_data(GPS_parse);
Rishin 14:6990bc2f44e9 186 while(Transmission_in_progress==1);
Rishin 14:6990bc2f44e9 187 NVIC_DisableIRQ(USART1_IRQn);
Rishin 12:7debb1c79a06 188 Send_GPS_data(GPS_parse);
Rishin 14:6990bc2f44e9 189 NVIC_EnableIRQ(USART1_IRQn);
cdebank 11:32323f490d9e 190 }
joshcurry 10:9364ab3a08eb 191 }
mluis 3:9c6f7f082151 192