Simple SX1272 Test Application

Dependencies:   SX1272Lib mbed

Revision:
1:4c82bff12ad0
Parent:
0:7e7575bda256
Child:
2:61713a461cf1
--- a/main.cpp	Fri Apr 21 15:36:26 2017 +0000
+++ b/main.cpp	Mon Apr 24 09:36:52 2017 +0000
@@ -4,35 +4,37 @@
 #include "radio.h"
 
 Serial pc(USBTX, USBRX); // tx, rx
-
 InterruptIn DatarateButton(USER_BUTTON);
 
-static uint8_t LoRaMacBuffer[255];
+// Global variables
+static uint8_t LoRaWANBuffer[255];
+static uint16_t FCnt = 0;
 
-uint32_t TxFreq;
-uint8_t CurrentDatarate = 12;
-bool ButtonPressed = false;
+// ISR variables
+static volatile bool SingleDatarate = true;
+static volatile uint8_t CurrentDatarate = 12;
+static volatile bool ButtonPressed = false;
 
 /* -------------- */
 
 void UserButtonPressed( void )
 {
-    if( CurrentDatarate == 7 )
+    if( SingleDatarate == false )
     {
-        CurrentDatarate = 12;
+        // Increase datarate by 1
+        CurrentDatarate = (CurrentDatarate == 7) ? 12 : CurrentDatarate-1;
     }
-    else
-    {
-        CurrentDatarate -= 1;
-    }
-    
+   
     ButtonPressed = true;
 }
 
 /* -------------- */
 
 int main() {
-    pc.printf("HelLoRa !\n");
+    Timer t;
+    uint32_t TxFreq;
+
+    pc.printf("HelLo Ra !\n");
     
     // Get USER button pressed
     DatarateButton.fall( &UserButtonPressed );
@@ -44,18 +46,33 @@
     {
         if( ButtonPressed == true )
         {
-            // Send one packet
-            TxFreq = 868100000;
+            // Configure radio
+            TxFreq = 867720000;
             Radio.SetChannel( TxFreq );
+            Radio.Write( REG_LR_SYNCWORD, 0x34 ); // public: to be replaced with SetPublicNetwork() when available
             Radio.SetTxConfig( MODEM_LORA, 14, 0, 0, CurrentDatarate, 1, 8, false, true, 0, 0, false, 3e3 ); 
-            Radio.Send( LoRaMacBuffer, 10 );
+            
+            // Set packet payload
+            //LoRaWANBuffer[0] = 0x40; // Unconfirmed Data Up
+            LoRaWANBuffer[0] = 0x80; // Confirmed Data Up
+            LoRaWANBuffer[1] = 0x03; // DevAddr
+            LoRaWANBuffer[2] = 0x00;
+            LoRaWANBuffer[3] = 0xFE;
+            LoRaWANBuffer[4] = 0xCA;
+            LoRaWANBuffer[5] = 0x0; // FCtrl
+            LoRaWANBuffer[6] = FCnt;
+            LoRaWANBuffer[7] = FCnt >> 8;
+            LoRaWANBuffer[8] = 1; // FPort
+            LoRaWANBuffer[9] = 0x0; // FRMPayload
+            
+            // Send packet
+            Radio.Send( LoRaWANBuffer, 14 );
+            FCnt += 1;
             pc.printf( "LoRa packet: Freq=%u, SF%u\n", TxFreq, CurrentDatarate );
 
             // Stop sending
             ButtonPressed = false;
         }
     }
-    
-    return 0;
 }
  
\ No newline at end of file