This program enables the use of the Semtech "SX1276 Starter Kit A" application to be used with the FRDM-KL25Z board. At the time of writing this, this app was available on Semtech's SX1276 page, in the "Docs & Resources"* tab, via the link "PC GUI (1.0.0Beta5)". This program was tested with the FRDM-KL25Z board, with a Modtronix inAir9 SX1276 board mounted in a Modtronix SHD3I shield. The inAir9 module is mounted in iMod port 3 of the SHD3I module. The SHD3I shield is mounted on the FRDM-KL25Z board.

Dependencies:   SX127x_modtronix USBDevice_Semtech_GUI mbed

Fork of hid_test by wayne roberts

Revision:
1:d25ba61cd2f3
Parent:
0:f8bc33804548
Child:
2:1972097c22d1
--- a/main.cpp	Mon Apr 14 22:01:41 2014 +0000
+++ b/main.cpp	Wed Apr 30 23:06:39 2014 +0000
@@ -2,9 +2,15 @@
 #include "USBHID.h"
 #include "sx127x.h"
 
+//#define _DEBUG_ 
+
 //  pin:      3     8     1      7    10    12     5   20    18
 //           mosi, miso, sclk,   cs,  rst,  dio0, dio1, fctx, fcps 
 SX127x radio(PTD2, PTD3, PTD1, PTD0, PTD5, PTA13, PTD4, PTC9, PTC8);
+#ifdef _DEBUG_
+    #include "sx127x_lora.h"
+    SX127x_lora lora(radio);
+#endif /* _DEBUG_ */
 
 //We declare a USBHID device. By default input and output reports are 64 bytes long.
 USBHID hid(64, 64, 0x47a, 0x0b);
@@ -109,7 +115,10 @@
             #endif /* _DEBUG_ */
             radio.init();   //SX1272Init( );
             // Set FSK modem ON
-            radio.SetLoRaOn( false ); //SX1272SetLoRaOn( false ); // Default radio setting
+            radio.set_opmode(RF_OPMODE_SLEEP);  
+            radio.RegOpMode.bits.LongRangeMode = 0;
+            radio.write_reg(REG_OPMODE, radio.RegOpMode.octet);            
+            //radio.SetLoRaOn( false ); //SX1272SetLoRaOn( false ); // Default radio setting
             // Default answer settings
             break;
         case HID_SK_GET_VERSION:
@@ -360,7 +369,7 @@
 void
 printCodingRate(bool from_rx)
 {
-    uint8_t d = radio.getCodingRate(from_rx);
+    uint8_t d = lora.getCodingRate(from_rx);
     printf("CodingRate:");
     switch (d) {
         case 1: printf("4/5 "); break;
@@ -377,7 +386,7 @@
 #ifdef _DEBUG_
 void printHeaderMode()
 {
-    if (radio.getHeaderMode())
+    if (lora.getHeaderMode())
         printf("implicit ");
     else
         printf("explicit ");
@@ -387,11 +396,11 @@
 #ifdef _DEBUG_
 void printBw()
 {
-    uint8_t bw = radio.getBw();
+    uint8_t bw = lora.getBw();
     
     printf("Bw:");
     if (radio.type == SX1276) {
-        switch (radio.RegModemConfig.sx1276bits.Bw) {
+        switch (lora.RegModemConfig.sx1276bits.Bw) {
             case 0: printf("7.8KHz "); break;
             case 1: printf("10.4KHz "); break;
             case 2: printf("15.6KHz "); break;
@@ -402,10 +411,10 @@
             case 7: printf("125KHz "); break;
             case 8: printf("250KHz "); break;
             case 9: printf("500KHz "); break;
-            default: printf("%x ", radio.RegModemConfig.sx1276bits.Bw); break;
+            default: printf("%x ", lora.RegModemConfig.sx1276bits.Bw); break;
         }
     } else if (radio.type == SX1272) {
-        switch (radio.RegModemConfig.sx1272bits.Bw) {
+        switch (lora.RegModemConfig.sx1272bits.Bw) {
             case 0: printf("125KHz "); break;
             case 1: printf("250KHz "); break;
             case 2: printf("500KHz "); break;
@@ -419,28 +428,28 @@
 void printSf()
 {
     // spreading factor same between sx127[26]
-    printf("sf:%d ", radio.getSf());
+    printf("sf:%d ", lora.getSf());
 }
 #endif /* _DEBUG_ */
 
 #ifdef _DEBUG_
 void printTxContinuousMode()
 {
-    printf("TxContinuousMode:%d ", radio.RegModemConfig2.sx1276bits.TxContinuousMode);    // same for sx1272 and sx1276
+    printf("TxContinuousMode:%d ", lora.RegModemConfig2.sx1276bits.TxContinuousMode);    // same for sx1272 and sx1276
 }
 #endif /* _DEBUG_ */
 
 #ifdef _DEBUG_
 void printAgcAutoOn()
 {
-    printf("AgcAutoOn:%d", radio.getAgcAutoOn());
+    printf("AgcAutoOn:%d", lora.getAgcAutoOn());
 }
 #endif /* _DEBUG_ */
 
 #ifdef _DEBUG_
 void printRxPayloadCrcOn()
 {
-    bool on = radio.getRxPayloadCrcOn();
+    bool on = lora.getRxPayloadCrcOn();
     //printf("RxPayloadCrcOn:%s ", on ? "on" : "off");
     if (on)
         printf("RxPayloadCrcOn:1 = Tx CRC Enabled\r\n");
@@ -456,29 +465,29 @@
 
     //already read RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS);
     printf("\r\nIrqFlags:");
-    if (radio.RegIrqFlags.bits.CadDetected)
+    if (lora.RegIrqFlags.bits.CadDetected)
         printf("CadDetected ");
-    if (radio.RegIrqFlags.bits.FhssChangeChannel) {
+    if (lora.RegIrqFlags.bits.FhssChangeChannel) {
         //radio.RegHopChannel.octet = radio.read_reg(REG_LR_HOPCHANNEL);
-        printf("FhssChangeChannel:%d ", radio.RegHopChannel.bits.FhssPresentChannel);
+        printf("FhssChangeChannel:%d ", lora.RegHopChannel.bits.FhssPresentChannel);
     }
-    if (radio.RegIrqFlags.bits.CadDone)
+    if (lora.RegIrqFlags.bits.CadDone)
         printf("CadDone ");
-    if (radio.RegIrqFlags.bits.TxDone)
+    if (lora.RegIrqFlags.bits.TxDone)
         printf("TxDone ");
-    if (radio.RegIrqFlags.bits.ValidHeader)
+    if (lora.RegIrqFlags.bits.ValidHeader)
         printf("ValidHeader ");
-    if (radio.RegIrqFlags.bits.PayloadCrcError)
+    if (lora.RegIrqFlags.bits.PayloadCrcError)
         printf("PayloadCrcError ");
-    if (radio.RegIrqFlags.bits.RxDone)
+    if (lora.RegIrqFlags.bits.RxDone)
         printf("RxDone ");  
-    if (radio.RegIrqFlags.bits.RxTimeout)
+    if (lora.RegIrqFlags.bits.RxTimeout)
         printf("RxTimeout ");
 
     printf("\r\n");
 
     if (clear)
-        radio.write_reg(REG_LR_IRQFLAGS, radio.RegIrqFlags.octet);
+        radio.write_reg(REG_LR_IRQFLAGS, lora.RegIrqFlags.octet);
 
 }
 #endif /* _DEBUG_ */
@@ -504,8 +513,8 @@
     
     // printing LoRa registers at 0x0d -> 0x3f
 
-    radio.RegModemConfig.octet = radio.read_reg(REG_LR_MODEMCONFIG);
-    radio.RegModemConfig2.octet = radio.read_reg(REG_LR_MODEMCONFIG2);
+    lora.RegModemConfig.octet = radio.read_reg(REG_LR_MODEMCONFIG);
+    lora.RegModemConfig2.octet = radio.read_reg(REG_LR_MODEMCONFIG2);
 
     printCodingRate(false); // false: transmitted coding rate
     printHeaderMode();
@@ -516,28 +525,28 @@
     printf("ModemStat:0x%02x\r\n", radio.read_reg(REG_LR_MODEMSTAT));
 
     // fifo ptrs:
-    radio.RegPayloadLength = radio.read_reg(REG_LR_PAYLOADLENGTH);
-    radio.RegRxMaxPayloadLength = radio.read_reg(REG_LR_RX_MAX_PAYLOADLENGTH);
+    lora.RegPayloadLength = radio.read_reg(REG_LR_PAYLOADLENGTH);
+    lora.RegRxMaxPayloadLength = radio.read_reg(REG_LR_RX_MAX_PAYLOADLENGTH);
     printf("fifoptr=0x%02x txbase=0x%02x rxbase=0x%02x payloadLength=0x%02x maxlen=0x%02x",
         radio.read_reg(REG_LR_FIFOADDRPTR),
         radio.read_reg(REG_LR_FIFOTXBASEADDR),
         radio.read_reg(REG_LR_FIFORXBASEADDR),
-        radio.RegPayloadLength,
-        radio.RegRxMaxPayloadLength
+        lora.RegPayloadLength,
+        lora.RegRxMaxPayloadLength
     );
 
-    radio.RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS);
+    lora.RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS);
     printLoraIrqs_(false);
 
-    radio.RegHopPeriod = radio.read_reg(REG_LR_HOPPERIOD);
-    if (radio.RegHopPeriod != 0) {
-        printf("\r\nHopPeriod:0x%02x\r\n", radio.RegHopPeriod);
+    lora.RegHopPeriod = radio.read_reg(REG_LR_HOPPERIOD);
+    if (lora.RegHopPeriod != 0) {
+        printf("\r\nHopPeriod:0x%02x\r\n", lora.RegHopPeriod);
     }
 
     printf("SymbTimeout:0x%03x ", radio.read_u16(REG_LR_MODEMCONFIG2) & 0x3ff);
 
-    radio.RegPreamble = radio.read_u16(REG_LR_PREAMBLEMSB);
-    printf("PreambleLength:0x%03x ", radio.RegPreamble);
+    lora.RegPreamble = radio.read_u16(REG_LR_PREAMBLEMSB);
+    printf("PreambleLength:0x%03x ", lora.RegPreamble);
     
 
     if (radio.RegOpMode.bits.Mode == RF_OPMODE_RECEIVER || radio.RegOpMode.bits.Mode == RF_OPMODE_RECEIVER_SINGLE) {
@@ -550,22 +559,22 @@
     printf("\r\n");
     printAgcAutoOn();
     if (radio.type == SX1272) {
-        printf(" LowDataRateOptimize:%d\r\n", radio.RegModemConfig.sx1272bits.LowDataRateOptimize);
+        printf(" LowDataRateOptimize:%d\r\n", lora.RegModemConfig.sx1272bits.LowDataRateOptimize);
     }
 
     printf("\r\nHeaderCount:%d PacketCount:%d, ",
         radio.read_u16(REG_LR_RXHEADERCNTVALUE_MSB), radio.read_u16(REG_LR_RXPACKETCNTVALUE_MSB));
 
     printf("Lora detection threshold:%02x\r\n", radio.read_reg(REG_LR_DETECTION_THRESHOLD));
-    radio.RegTest31.octet = radio.read_reg(REG_LR_TEST31);
-    printf("detect_trig_same_peaks_nb:%d\r\n", radio.RegTest31.bits.detect_trig_same_peaks_nb);
+    lora.RegTest31.octet = radio.read_reg(REG_LR_TEST31);
+    printf("detect_trig_same_peaks_nb:%d\r\n", lora.RegTest31.bits.detect_trig_same_peaks_nb);
 
     if (radio.type == SX1272) {
-        radio.RegModemConfig.octet = radio.read_reg(REG_LR_MODEMCONFIG);
-        printf("LowDataRateOptimize:%d\r\n", radio.RegModemConfig.sx1272bits.LowDataRateOptimize);
+        lora.RegModemConfig.octet = radio.read_reg(REG_LR_MODEMCONFIG);
+        printf("LowDataRateOptimize:%d\r\n", lora.RegModemConfig.sx1272bits.LowDataRateOptimize);
     } else if (radio.type == SX1276) {
-        radio.RegModemConfig3.octet = radio.read_reg(REG_LR_MODEMCONFIG3);
-        printf("LowDataRateOptimize:%d\r\n", radio.RegModemConfig3.sx1276bits.LowDataRateOptimize);        
+        lora.RegModemConfig3.octet = radio.read_reg(REG_LR_MODEMCONFIG3);
+        printf("LowDataRateOptimize:%d\r\n", lora.RegModemConfig3.sx1276bits.LowDataRateOptimize);        
     }
     
     printf("\r\n");
@@ -573,7 +582,7 @@
 }
 #endif /* _DEBUG_ */
 
-void
+/*void
 service_radio()
 {
     service_action_e act = radio.service();
@@ -591,7 +600,7 @@
             printf("error\r\n");
             break;
     } // ...switch (act)
-}
+}*/
         
 int
 main(void)
@@ -636,23 +645,23 @@
                 }
             } else if (c == 'T') {
                 printf("start_tx\r\n");
-                radio.RegPayloadLength = 8;
-                radio.write_reg(REG_LR_PAYLOADLENGTH, radio.RegPayloadLength);
-                radio.lora_start_tx(8);
-            } else if (c == 'e') {
+                lora.RegPayloadLength = 8;
+                radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
+                lora.start_tx(8);
+            } /*else if (c == 'e') {
                 printf("service_radio\r\n");
                 service_radio();
-            } else if (c == 's') {
+            }*/ else if (c == 's') {
                 radio.set_opmode(RF_OPMODE_STANDBY);
                 printf("standby\r\n");
             } else if (c == 'h') {
                 printf("hwreset\r\n");
                 radio.hw_reset();
                 radio.init();   //SX1272Init( );
-            } else if (c == 'l') {
+            } /*else if (c == 'l') {
                 radio.SetLoRaOn(!radio.RegOpMode.bits.LongRangeMode);
                 printf("LongRangeMode:%d\r\n", radio.RegOpMode.bits.LongRangeMode);
-            } else if (c == '?') {
+            }*/ else if (c == '?') {
                 printf("s   standby\r\n");
                 printf("T   lora_start_tx(8)\r\n");
                 printf(".   print status\r\n");