Simple SX1272 Test Application

Dependencies:   SX1272Lib mbed

Revision:
3:e42efecbbe1b
Parent:
2:61713a461cf1
Child:
4:fac0fc95a644
--- a/main.cpp	Tue Apr 25 07:31:14 2017 +0000
+++ b/main.cpp	Wed Apr 26 14:23:44 2017 +0000
@@ -1,24 +1,53 @@
 #include "mbed.h"
 
-#include "board.h"
+#include "sx1272-hal.h"
 #include "radio.h"
+#include "main.h"
 
+#define TX_LOOP 1
+#define TX_FREQ 866700000
+#define PAYLOAD_SIZE_LORA 97
+#define PAYLOAD_SIZE_FSK 14
+
+/*!
+ * Serial communication for debug logs
+ */
 Serial pc(USBTX, USBRX); // tx, rx
-InterruptIn DatarateButton(USER_BUTTON);
+
+/*!
+ * Interrupt handler for nucleo user button
+ */
+InterruptIn DatarateButton( USER_BUTTON );
 
-// Global variables
+/*!
+ * Radio events function pointer
+ */
+static RadioEvents_t RadioEvents;
+ 
+/*
+ *  Global variables declarations
+ */
+SX1272MB2xAS Radio( NULL );
+
 static uint8_t LoRaWANBuffer[255];
 static uint16_t FCnt = 0;
 
-// ISR variables
+/*
+ *  ISR variables declarations
+ */
 static volatile bool SingleDatarate = true;
-static volatile uint8_t CurrentDatarate = 12;
+static volatile uint8_t CurrentDatarate = 7;
 static volatile bool ButtonPressed = false;
 
 /* -------------- */
 
 void UserButtonPressed( void )
 {
+    if( ButtonPressed == true )
+    {
+        return;
+    }
+
     if( SingleDatarate == false )
     {
         // Increase datarate by 1
@@ -34,14 +63,31 @@
     Timer t;
     uint32_t TxFreq;
     double PktToA;
+    int i;
 
-    pc.printf("HelLo Ra !\n");
+    pc.printf( "\n > Initializing... < \n" );
     
     // Get USER button pressed
     DatarateButton.fall( &UserButtonPressed );
     
-    // Radio board init
-    BoardInit( );
+    // Initialize Radio driver
+    RadioEvents.TxDone = OnTxDone;
+    RadioEvents.RxDone = OnRxDone;
+    RadioEvents.RxError = OnRxError;
+    RadioEvents.TxTimeout = OnTxTimeout;
+    RadioEvents.RxTimeout = OnRxTimeout;
+    Radio.Init( &RadioEvents );
+ 
+    // verify the connection with the board
+    while( Radio.Read( REG_VERSION ) == 0x00  )
+    {
+        pc.printf( "Radio could not be detected!\n", NULL );
+        wait( 1 );
+    }
+ 
+    pc.printf( "\n > Board Type: SX1272MB2xAS < \n" );
+    
+    Radio.SetPublicNetwork( true );
     
     while( 1 )
     {
@@ -49,38 +95,84 @@
         if( ButtonPressed == true )
         {
             // Configure radio
-            TxFreq = 867720000;
+            TxFreq = (uint32_t)TX_FREQ;
             Radio.SetChannel( TxFreq );
-            Radio.SetPublicNetwork( true );
-            Radio.SetTxConfig( MODEM_LORA, 14, 0, 0, CurrentDatarate, 1, 8, false, true, 0, 0, false, 3e3 );
-            
+
             // Set packet payload
-            //LoRaWANBuffer[0] = 0x40; // Unconfirmed Data Up
-            LoRaWANBuffer[0] = 0x80; // Confirmed Data Up
+            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[6] = 0; // FCnt, filled later
+            LoRaWANBuffer[7] = 0; // FCnt, filled later
             LoRaWANBuffer[8] = 1; // FPort
             LoRaWANBuffer[9] = 0x0; // FRMPayload
             
-            // Send packet
-            PktToA = Radio.TimeOnAir( MODEM_LORA, 14 );
-            Radio.Send( LoRaWANBuffer, 14 );
-            pc.printf( "sending LoRa packet: Freq=%u, SF%u (%.1lfus)...\n", TxFreq, CurrentDatarate, PktToA );
-            wait_us( PktToA );
-            pc.printf( "sent.\n");
-            FCnt += 1;
+            FCnt = 0;
+            for( i = 0; i < (int)TX_LOOP; i++ )
+            {
+                LoRaWANBuffer[6] = FCnt;
+                LoRaWANBuffer[7] = FCnt >> 8;
+                
+                // Send LoRa packet
+                Radio.SetTxConfig( MODEM_LORA, 2, 0, 0, CurrentDatarate, 1, 8, false, true, 0, 0, false, 10e3 );
+                PktToA = Radio.TimeOnAir( MODEM_LORA, PAYLOAD_SIZE_LORA );
+                Radio.Send( LoRaWANBuffer, PAYLOAD_SIZE_LORA );
+                pc.printf( "(%d) Sending LoRa packet: Freq=%u, SF%u (%.1lfms), FCnt=%u...\n", i, TxFreq, CurrentDatarate, PktToA, FCnt );
+                wait_ms( PktToA );
+                //wait_ms( 10 );
+                
+                FCnt += 1;
+                LoRaWANBuffer[6] = FCnt;
+                LoRaWANBuffer[7] = FCnt >> 8;
+                
+                // Send FSK packet
+                Radio.SetTxConfig( MODEM_FSK, 2, 25e3, 0, 50e3, 0, 5, false, true, 0, 0, false, 3e3 );
+                PktToA = Radio.TimeOnAir( MODEM_FSK, PAYLOAD_SIZE_FSK );
+                Radio.Send( LoRaWANBuffer, PAYLOAD_SIZE_FSK );
+                pc.printf( "(%d) Sending FSK packet: Freq=%u, FCnt=%u...\n", i, TxFreq, PktToA, FCnt );
+                wait_ms( PktToA );
+                FCnt += 1;
+            }
 
             // Stop sending
             ButtonPressed = false;
         }
         
         // Receive packets
-        
+        // TODO
     }
 }
- 
\ No newline at end of file
+
+void OnTxDone( void )
+{
+    Radio.Sleep( );
+    pc.printf( "> OnTxDone\n\r" );
+}
+ 
+void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
+{
+    Radio.Sleep( );
+    pc.printf( "> OnRxDone\n\r" );
+}
+ 
+void OnTxTimeout( void )
+{
+    Radio.Sleep( );
+    pc.printf( "> OnTxTimeout\n\r" );
+}
+ 
+void OnRxTimeout( void )
+{
+    Radio.Sleep( );
+    pc.printf( "> OnRxTimeout\n\r" );
+}
+ 
+void OnRxError( void )
+{
+    Radio.Sleep( );
+    pc.printf( "> OnRxError\n\r" );
+}
\ No newline at end of file