standalone sx1276 demo program

Dependencies:   SX1276Lib mbed

Fork of SX1276_GPS by CaryCoders

Committer:
ftagius
Date:
Thu Sep 03 14:33:55 2015 +0000
Revision:
33:319cbac3b6eb
Parent:
32:a2472bbe7c92
sx1276 demo program requiring only the freedom board and a semtech shield

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:88d87b1c1f8b 1 #include "mbed.h"
ftagius 33:319cbac3b6eb 2 #include "main.h"
ftagius 33:319cbac3b6eb 3 #ifndef STANDALONE
vtraveller 25:24654d08a99a 4 #include "lcdadafruit.h"
ftagius 33:319cbac3b6eb 5 #include "GPS.h"
ftagius 33:319cbac3b6eb 6 #endif
ftagius 29:0ea07cc7124b 7 #include "sx1276-hal.h"
ftagius 29:0ea07cc7124b 8 #include "debug.h"
ftagius 29:0ea07cc7124b 9 #include "serial_api.h"
ftagius 32:a2472bbe7c92 10 #include "datetime.h"
ftagius 32:a2472bbe7c92 11 #include "time.h"
ftagius 32:a2472bbe7c92 12 #include <string>
wim 0:88d87b1c1f8b 13
ftagius 29:0ea07cc7124b 14
ftagius 29:0ea07cc7124b 15 #define USE_MODEM_LORA 1
ftagius 33:319cbac3b6eb 16 #define RF_FREQUENCY 868000000 // Hz
ftagius 29:0ea07cc7124b 17 //#define RF_FREQUENCY 880030000
ftagius 33:319cbac3b6eb 18 //#define RF_FREQUENCY 914000000.0 // Hz
ftagius 31:2c813f321db7 19 //#define RF_FREQUENCY 413000000.0 // Hz
ftagius 32:a2472bbe7c92 20
ftagius 29:0ea07cc7124b 21
ftagius 29:0ea07cc7124b 22 //#define BUFFER_SIZE 64 // Define the payload size here
ftagius 29:0ea07cc7124b 23
ftagius 29:0ea07cc7124b 24 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
ftagius 32:a2472bbe7c92 25 //DigitalOut led(LED2);
ftagius 32:a2472bbe7c92 26 DigitalOut led(LED_RED);
ftagius 29:0ea07cc7124b 27 #else
ftagius 29:0ea07cc7124b 28 DigitalOut led(LED1);
ftagius 29:0ea07cc7124b 29 #endif
ftagius 29:0ea07cc7124b 30 Serial pc(USBTX, USBRX);
ftagius 29:0ea07cc7124b 31
ftagius 29:0ea07cc7124b 32 float Frequency = RF_FREQUENCY;
ftagius 32:a2472bbe7c92 33 float distance = -1;
ftagius 32:a2472bbe7c92 34 float r_latitude = 0;
ftagius 33:319cbac3b6eb 35 float r_latitude_last = 0;
ftagius 32:a2472bbe7c92 36 float r_longitude = 0;
ftagius 33:319cbac3b6eb 37 float r_longitude_last = 0;
ftagius 29:0ea07cc7124b 38 int TxPower = TX_OUTPUT_POWER;
ftagius 29:0ea07cc7124b 39 int Bandwidth = LORA_BANDWIDTH;
ftagius 29:0ea07cc7124b 40 int SpreadingFactor = LORA_SPREADING_FACTOR;
ftagius 29:0ea07cc7124b 41 int CodingRate = LORA_CODINGRATE;
ftagius 32:a2472bbe7c92 42 struct tm Clock;
ftagius 29:0ea07cc7124b 43
ftagius 29:0ea07cc7124b 44 /*
ftagius 29:0ea07cc7124b 45 * Global variables declarations
ftagius 29:0ea07cc7124b 46 */
ftagius 29:0ea07cc7124b 47 typedef RadioState States_t;
ftagius 29:0ea07cc7124b 48 volatile States_t State = LOWPOWER;
ftagius 29:0ea07cc7124b 49
ftagius 33:319cbac3b6eb 50 //SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, OnCadDone );
ftagius 29:0ea07cc7124b 51 SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, NULL );
ftagius 29:0ea07cc7124b 52 // for hand wired I2C cI2C(PTC9, PTC8);
ftagius 29:0ea07cc7124b 53 I2C cI2C(PTC9, PTC8);
ftagius 29:0ea07cc7124b 54 // for stacked shield I2C cI2C(PTC2, PTC1);
ftagius 29:0ea07cc7124b 55 //I2C cI2C(PTC2, PTC1);
ftagius 33:319cbac3b6eb 56 #ifndef STANDALONE
ftagius 29:0ea07cc7124b 57 LCDadafruit cLCD(cI2C);
ftagius 29:0ea07cc7124b 58 GPS gpsd(PTE0,PTE1);
ftagius 33:319cbac3b6eb 59 #endif
ftagius 29:0ea07cc7124b 60
ftagius 29:0ea07cc7124b 61 // for other board GPS gpsd(PTE20, PTE21);
ftagius 29:0ea07cc7124b 62
ftagius 29:0ea07cc7124b 63 uint8_t PingMsg[256];
ftagius 29:0ea07cc7124b 64 uint8_t PongMsg[256];
ftagius 29:0ea07cc7124b 65 uint8_t HelloMsg[256];
ftagius 29:0ea07cc7124b 66 char pcbuf[PCBUF_SIZE];
ftagius 29:0ea07cc7124b 67 uint16_t BufferSize = BUFFER_SIZE;
ftagius 29:0ea07cc7124b 68 uint8_t BufferTx[BUFFER_SIZE];
ftagius 29:0ea07cc7124b 69 uint8_t BufferRx[BUFFER_SIZE];
ftagius 29:0ea07cc7124b 70 int16_t RssiValue = 0.0;
ftagius 29:0ea07cc7124b 71 int8_t SnrValue = 0.0;
ftagius 29:0ea07cc7124b 72 int txLen = 0;
ftagius 29:0ea07cc7124b 73 int pkt_count = 0;
ftagius 29:0ea07cc7124b 74 int max_pkts = 20;
ftagius 29:0ea07cc7124b 75 int pkt_data[20];
ftagius 29:0ea07cc7124b 76 int per = 0;
ftagius 33:319cbac3b6eb 77 int tx_rate_us = 1000000; // 1 second
ftagius 29:0ea07cc7124b 78 app_e app = APP_NONE;
ftagius 29:0ea07cc7124b 79 bool isMaster = true;
ftagius 29:0ea07cc7124b 80 bool AlwaysMaster = false;
ftagius 29:0ea07cc7124b 81 bool AlwaysSlave = false;
ftagius 29:0ea07cc7124b 82 bool ackRcvd = true;
ftagius 29:0ea07cc7124b 83 bool rxTimeout = false;
ftagius 29:0ea07cc7124b 84 bool gpsEnabled = true;
ftagius 33:319cbac3b6eb 85 InterruptIn PPS(PTD7); //pps IRQ
ftagius 33:319cbac3b6eb 86 DigitalOut RXin(PTD6);
ftagius 32:a2472bbe7c92 87 Ticker usec_tick;
ftagius 32:a2472bbe7c92 88
ftagius 32:a2472bbe7c92 89 void tx_pkt() {
ftagius 32:a2472bbe7c92 90 led = !led;
ftagius 33:319cbac3b6eb 91
ftagius 32:a2472bbe7c92 92 BufferSize=strlen((char *)BufferTx);
ftagius 33:319cbac3b6eb 93 #if 1
ftagius 32:a2472bbe7c92 94 // printf("Buffersize = %d\r\n",BufferSize);
ftagius 33:319cbac3b6eb 95 // Radio.Send( BufferTx, BufferSize );
ftagius 33:319cbac3b6eb 96 Radio.Rx( RX_TIMEOUT_VALUE / 1 );
ftagius 33:319cbac3b6eb 97 #else
ftagius 33:319cbac3b6eb 98 // printf("calling start cat\r\n");
ftagius 33:319cbac3b6eb 99 // Radio.Send( BufferTx, BufferSize );
ftagius 33:319cbac3b6eb 100 // Radio.Rx( RX_TIMEOUT_VALUE / 1 );
ftagius 33:319cbac3b6eb 101 // Radio.StartCad();
ftagius 33:319cbac3b6eb 102 #endif
ftagius 32:a2472bbe7c92 103 }
ftagius 29:0ea07cc7124b 104
ftagius 32:a2472bbe7c92 105 void ppsSync()
ftagius 29:0ea07cc7124b 106 {
ftagius 32:a2472bbe7c92 107 static int pps_count=0;
ftagius 32:a2472bbe7c92 108
ftagius 32:a2472bbe7c92 109 ++pps_count;
ftagius 32:a2472bbe7c92 110 if (pps_count == 1)
ftagius 29:0ea07cc7124b 111 {
ftagius 32:a2472bbe7c92 112 // detach the timer
ftagius 32:a2472bbe7c92 113 usec_tick.detach();
ftagius 33:319cbac3b6eb 114
ftagius 33:319cbac3b6eb 115 wait_ms(500);
ftagius 32:a2472bbe7c92 116 usec_tick.attach_us(&tx_pkt, tx_rate_us);
ftagius 33:319cbac3b6eb 117 // printf("call attach\r\n");
ftagius 29:0ea07cc7124b 118 }
ftagius 29:0ea07cc7124b 119 else
ftagius 29:0ea07cc7124b 120 {
ftagius 32:a2472bbe7c92 121 if (pps_count > 10)
ftagius 32:a2472bbe7c92 122 {
ftagius 33:319cbac3b6eb 123 pps_count = 0;
ftagius 32:a2472bbe7c92 124 }
ftagius 29:0ea07cc7124b 125 }
ftagius 29:0ea07cc7124b 126
ftagius 32:a2472bbe7c92 127 //printf("got a pps\r\n");
ftagius 32:a2472bbe7c92 128 //ShowDateTime();
ftagius 32:a2472bbe7c92 129
ftagius 29:0ea07cc7124b 130 }
vtraveller 3:ed09f95739df 131 int main()
vtraveller 21:c44cfd3259c0 132 {
ftagius 29:0ea07cc7124b 133
ftagius 29:0ea07cc7124b 134 int i;
ftagius 33:319cbac3b6eb 135 RXin=0;
ftagius 32:a2472bbe7c92 136 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
ftagius 32:a2472bbe7c92 137 const int refresh_Time = 1000; //refresh time in ms
ftagius 32:a2472bbe7c92 138 bool gpsFix=false;
ftagius 33:319cbac3b6eb 139
ftagius 29:0ea07cc7124b 140 pc.baud(9600);
vtraveller 21:c44cfd3259c0 141 cI2C.frequency(400000); // I2C can handle two different frequencies - switch to high speed if asked
ftagius 33:319cbac3b6eb 142 #ifndef STANDALONE
ftagius 33:319cbac3b6eb 143 gps_setup();
ftagius 29:0ea07cc7124b 144 cLCD.clear();
ftagius 29:0ea07cc7124b 145 cLCD.home();
ftagius 29:0ea07cc7124b 146 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 147 cLCD.printf(" HOMER ");
ftagius 29:0ea07cc7124b 148 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 149 cLCD.printf(" DOH! ");
ftagius 33:319cbac3b6eb 150 #endif
ftagius 29:0ea07cc7124b 151
ftagius 29:0ea07cc7124b 152 rxTimeout = false;
ftagius 29:0ea07cc7124b 153 // PTD1 (the SCK pin for the one SPI interface onboard the KL25z) is actually an output to the Blue LED.
ftagius 29:0ea07cc7124b 154 // the lora shield drives SCK, which turns on the blue led. use PTC5 instead of the default PTD1 for SCK
ftagius 29:0ea07cc7124b 155
ftagius 29:0ea07cc7124b 156 if (RADIO_INSTALLED)
ftagius 29:0ea07cc7124b 157 debug( " SX1276 Test Applications \r\n\n" );
ftagius 29:0ea07cc7124b 158 led = 1;
ftagius 29:0ea07cc7124b 159
ftagius 29:0ea07cc7124b 160 // init pkt_data array to 0
ftagius 29:0ea07cc7124b 161 for (i=0;i<pkt_count;i++)
ftagius 29:0ea07cc7124b 162 pkt_data[pkt_count]=0;
ftagius 29:0ea07cc7124b 163
ftagius 29:0ea07cc7124b 164 // verify the connection with the board
ftagius 29:0ea07cc7124b 165 if (RADIO_INSTALLED)
ftagius 29:0ea07cc7124b 166 {
ftagius 29:0ea07cc7124b 167
ftagius 29:0ea07cc7124b 168 while( Radio.Read( REG_VERSION ) == 0x00 )
ftagius 29:0ea07cc7124b 169 {
ftagius 29:0ea07cc7124b 170 debug( "Radio could not be detected!\r\n", NULL );
ftagius 29:0ea07cc7124b 171 wait( 1 );
ftagius 29:0ea07cc7124b 172 }
ftagius 29:0ea07cc7124b 173
ftagius 29:0ea07cc7124b 174
ftagius 29:0ea07cc7124b 175 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "> Board Type: SX1276MB1LAS < \r\n" );
ftagius 29:0ea07cc7124b 176 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "> Board Type: SX1276MB1MAS < \r\n" );
ftagius 29:0ea07cc7124b 177 Radio.SetChannel( Frequency );
ftagius 33:319cbac3b6eb 178 // Radio.StartCad();
ftagius 33:319cbac3b6eb 179 // printf("start cad\r\n");
ftagius 29:0ea07cc7124b 180 debug_if( !LORA_FHSS_ENABLED, "> LORA Mode < \r\n");
ftagius 29:0ea07cc7124b 181 configRxTx();
ftagius 29:0ea07cc7124b 182
ftagius 29:0ea07cc7124b 183 //print_status();
ftagius 29:0ea07cc7124b 184 debug_if( DEBUG_MESSAGE, "Starting Application loop\r\n" );
ftagius 29:0ea07cc7124b 185 wait(1);
ftagius 29:0ea07cc7124b 186
ftagius 29:0ea07cc7124b 187 // led = 0;
ftagius 29:0ea07cc7124b 188
ftagius 29:0ea07cc7124b 189 strcat((char *)PingMsg,"PING");
ftagius 29:0ea07cc7124b 190 strcat((char *)PongMsg,"PONG");
ftagius 29:0ea07cc7124b 191 strcat((char *)HelloMsg,"HELLO_7");
ftagius 29:0ea07cc7124b 192
ftagius 29:0ea07cc7124b 193 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 194 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 195
ftagius 29:0ea07cc7124b 196 app = APP_HELLO;
ftagius 33:319cbac3b6eb 197 //app = APP_CHAT;
ftagius 29:0ea07cc7124b 198 }
ftagius 29:0ea07cc7124b 199 else
ftagius 29:0ea07cc7124b 200 {
ftagius 29:0ea07cc7124b 201 app = APP_GPS;
ftagius 29:0ea07cc7124b 202 pc.printf("Starting GPS App\r\n");
ftagius 29:0ea07cc7124b 203 }
ftagius 29:0ea07cc7124b 204
ftagius 33:319cbac3b6eb 205 refresh_Timer.start(); //starts the clock on the timer used for gps refreshes
ftagius 33:319cbac3b6eb 206 // usec_tick.attach_us(&tx_pkt, tx_rate_us); // attach a timer to periodically call tx. this timer will be restarted in ppsSync to synronize timer accross systems
ftagius 33:319cbac3b6eb 207 // PPS.rise(&ppsSync); // call ppsSync when the PPS irq is detected
ftagius 33:319cbac3b6eb 208 // printf("start cad 2\r\n");
ftagius 33:319cbac3b6eb 209 // Radio.StartCad();
ftagius 33:319cbac3b6eb 210
ftagius 29:0ea07cc7124b 211 while( 1 )
ftagius 29:0ea07cc7124b 212 {
ftagius 33:319cbac3b6eb 213 #ifndef STANDALONE
ftagius 33:319cbac3b6eb 214 //printf("call check \r\n");
ftagius 32:a2472bbe7c92 215 check_gps();
ftagius 32:a2472bbe7c92 216 if (refresh_Timer.read_ms() >= refresh_Time)
ftagius 32:a2472bbe7c92 217 {
ftagius 33:319cbac3b6eb 218 //printf("reset timer popped\r\n");
ftagius 33:319cbac3b6eb 219 if (gpsd.fix)
ftagius 33:319cbac3b6eb 220 {
ftagius 32:a2472bbe7c92 221 // got a gps fix
ftagius 32:a2472bbe7c92 222 if (gpsFix == false)
ftagius 32:a2472bbe7c92 223 {
ftagius 32:a2472bbe7c92 224 // first time fix obtained
ftagius 32:a2472bbe7c92 225 gpsFix = true;
ftagius 32:a2472bbe7c92 226 // set the rtc with the latest gps time
ftagius 32:a2472bbe7c92 227
ftagius 32:a2472bbe7c92 228 pc.printf("GPS fix obtained on %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds);
ftagius 32:a2472bbe7c92 229 // update rtc with the latest gps time stamp
ftagius 32:a2472bbe7c92 230 SetDateTime(gpsd.year+2000,
ftagius 32:a2472bbe7c92 231 gpsd.month,
ftagius 32:a2472bbe7c92 232 gpsd.day,
ftagius 32:a2472bbe7c92 233 gpsd.hour,
ftagius 32:a2472bbe7c92 234 gpsd.minute,
ftagius 32:a2472bbe7c92 235 gpsd.seconds);
ftagius 32:a2472bbe7c92 236 }
ftagius 32:a2472bbe7c92 237 #if 0
ftagius 32:a2472bbe7c92 238 else
ftagius 32:a2472bbe7c92 239 {
ftagius 32:a2472bbe7c92 240 printf("radfta - ");
ftagius 32:a2472bbe7c92 241 ShowDateTime();
ftagius 32:a2472bbe7c92 242 // pc.printf("GPS %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds);
ftagius 32:a2472bbe7c92 243 }
ftagius 32:a2472bbe7c92 244 #endif
ftagius 32:a2472bbe7c92 245
ftagius 32:a2472bbe7c92 246 }
ftagius 32:a2472bbe7c92 247 else
ftagius 32:a2472bbe7c92 248 {
ftagius 32:a2472bbe7c92 249 gpsFix = false;
ftagius 32:a2472bbe7c92 250 cLCD.setCursor(0,1);
ftagius 32:a2472bbe7c92 251 cLCD.printf("No GPS. per:%d ",per);
ftagius 32:a2472bbe7c92 252 //pc.printf("Waiting for GPS FIX\r\n");
ftagius 32:a2472bbe7c92 253
ftagius 32:a2472bbe7c92 254 }
ftagius 32:a2472bbe7c92 255
ftagius 32:a2472bbe7c92 256 refresh_Timer.reset();
ftagius 32:a2472bbe7c92 257 }
ftagius 32:a2472bbe7c92 258
ftagius 33:319cbac3b6eb 259 #endif
ftagius 29:0ea07cc7124b 260 switch (app) {
ftagius 29:0ea07cc7124b 261 case APP_PING:
ftagius 29:0ea07cc7124b 262 start_ping_pong();
ftagius 29:0ea07cc7124b 263 break;
ftagius 29:0ea07cc7124b 264 case APP_CHAT:
ftagius 29:0ea07cc7124b 265 console_chat();
ftagius 29:0ea07cc7124b 266 break;
ftagius 29:0ea07cc7124b 267 case APP_GPS:
ftagius 33:319cbac3b6eb 268 #ifndef STANDALONE
ftagius 29:0ea07cc7124b 269 check_gps();
ftagius 33:319cbac3b6eb 270 #endif
ftagius 29:0ea07cc7124b 271 break;
ftagius 29:0ea07cc7124b 272 case APP_HELLO:
ftagius 33:319cbac3b6eb 273 wait(1);
ftagius 29:0ea07cc7124b 274 start_hello();
ftagius 29:0ea07cc7124b 275 break;
ftagius 29:0ea07cc7124b 276 case APP_CONSOLE:
ftagius 29:0ea07cc7124b 277 // printf("-chat-> x");
ftagius 29:0ea07cc7124b 278 fflush(stdout);
ftagius 29:0ea07cc7124b 279 console();
ftagius 29:0ea07cc7124b 280 break;
ftagius 29:0ea07cc7124b 281 default:
ftagius 29:0ea07cc7124b 282 printf("unknown app %d\r\n", app);
ftagius 29:0ea07cc7124b 283 break;
ftagius 29:0ea07cc7124b 284 } // ...switch (app)
ftagius 29:0ea07cc7124b 285
ftagius 29:0ea07cc7124b 286 }
ftagius 29:0ea07cc7124b 287
wim 0:88d87b1c1f8b 288 }