Simple SX1272 Test Application

Dependencies:   SX1272Lib mbed

Revision:
4:fac0fc95a644
Parent:
3:e42efecbbe1b
Child:
5:8d3e18491667
--- a/main.cpp	Wed Apr 26 14:23:44 2017 +0000
+++ b/main.cpp	Fri Apr 28 15:24:27 2017 +0000
@@ -5,9 +5,34 @@
 #include "main.h"
 
 #define TX_LOOP 1
-#define TX_FREQ 866700000
-#define PAYLOAD_SIZE_LORA 97
-#define PAYLOAD_SIZE_FSK 14
+#define TX_FREQ 867100000
+#define PAYLOAD_SIZE_LONG 112 //97
+#define PAYLOAD_SIZE_SHORT 12
+
+#define CHANNEL_0_FREQ_HZ 867100000
+#define CHANNEL_1_FREQ_HZ 867300000
+#define CHANNEL_2_FREQ_HZ 867500000
+#define CHANNEL_3_FREQ_HZ 867700000
+#define CHANNEL_4_FREQ_HZ 867900000
+#define CHANNEL_5_FREQ_HZ 868100000
+#define CHANNEL_6_FREQ_HZ 868300000
+#define CHANNEL_7_FREQ_HZ 868500000
+#define CHANNEL_8_FREQ_HZ 868300000
+#define CHANNEL_9_FREQ_HZ 868800000
+
+typedef enum Modulation
+{
+    MOD_LORA,
+    MOD_FSK
+} Modulation_t;
+
+typedef enum LoRaBandwidth
+{
+    LORA_BW_125K,
+    LORA_BW_250K,
+    LORA_BW_500K,
+    LORA_BW_NA
+} LoRaBandwidth_t;
 
 /*!
  * Serial communication for debug logs
@@ -59,9 +84,35 @@
 
 /* -------------- */
 
+uint32_t SendPacket( uint32_t freq_hz, RadioModems_t modulation, LoRaBandwidth_t bw, uint8_t pkt_size )
+{
+    uint32_t TimeOnAir;
+
+    Radio.SetChannel( freq_hz );
+
+    switch( modulation )
+    {
+        case MODEM_LORA:
+            Radio.SetTxConfig( MODEM_LORA, 2, 0, bw, CurrentDatarate, 1, 8, false, true, 0, 0, false, 10e3 );
+            TimeOnAir = Radio.TimeOnAir( MODEM_LORA, pkt_size );
+            break;
+        case MODEM_FSK:
+            Radio.SetTxConfig( MODEM_FSK, 2, 25e3, 0, 50e3, 0, 5, false, true, 0, 0, false, 3e3 );
+            TimeOnAir = Radio.TimeOnAir( MODEM_FSK, pkt_size );
+            break;
+        default:
+            break;
+    }
+    
+    Radio.Send( LoRaWANBuffer, pkt_size );
+    
+    return TimeOnAir;
+}
+
+/* -------------- */
+
 int main() {
     Timer t;
-    uint32_t TxFreq;
     double PktToA;
     int i;
 
@@ -94,10 +145,6 @@
         // Check if there is a packet to be sent
         if( ButtonPressed == true )
         {
-            // Configure radio
-            TxFreq = (uint32_t)TX_FREQ;
-            Radio.SetChannel( TxFreq );
-
             // Set packet payload
             LoRaWANBuffer[0] = 0x40; // Unconfirmed Data Up
             //LoRaWANBuffer[0] = 0x80; // Confirmed Data Up
@@ -118,23 +165,35 @@
                 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 );
+                PktToA = SendPacket( CHANNEL_0_FREQ_HZ, MODEM_LORA, LORA_BW_125K, PAYLOAD_SIZE_LONG );
+                //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 );
+                Radio.Sleep( );
                 
+#if 1
                 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 );
+                PktToA = SendPacket( CHANNEL_9_FREQ_HZ, MODEM_FSK, LORA_BW_NA, PAYLOAD_SIZE_SHORT );
+                //pc.printf( "(%d) Sending FSK packet: Freq=%u, FCnt=%u...\n", i, TxFreq, PktToA, FCnt );
                 wait_ms( PktToA );
+                Radio.Sleep( );
+#endif
+                
+#if 1
+                FCnt += 1;
+                LoRaWANBuffer[6] = FCnt;
+                LoRaWANBuffer[7] = FCnt >> 8;
+                
+                // Send LoRa packet
+                PktToA = SendPacket( CHANNEL_8_FREQ_HZ, MODEM_LORA, LORA_BW_250K, PAYLOAD_SIZE_SHORT );
+                //pc.printf( "(%d) Sending LoRa packet: Freq=%u, SF%u (%.1lfms), FCnt=%u...\n", i, TxFreq, CurrentDatarate, PktToA, FCnt );
+                wait_ms( PktToA );
+                Radio.Sleep( );
+#endif
+                
                 FCnt += 1;
             }
 
@@ -149,8 +208,8 @@
 
 void OnTxDone( void )
 {
-    Radio.Sleep( );
-    pc.printf( "> OnTxDone\n\r" );
+    //Radio.Sleep( );
+    //pc.printf( "> OnTxDone\n\r" );
 }
  
 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )