Driver library for SX1272/SX1276 transceivers

Dependents:   LORA_RX LORA_TX WindConcentrator hid_test ... more

/media/uploads/dudmuck/lora.png

Driver library for SX1272 and SX1276 radio transceivers.

This device uses CSS modulation to provide much improved link budget. The RF hardware is same as in FSK devices, just with added LoRa spread-spectrum modem.

This library provides functions to configure radio chip and transmit & receive packets.

Using This Library

Library function service_radio() must be called continuously from main loop, to service interrupts from radio.

/media/uploads/dudmuck/sx1272rf1_connector_300.jpg

Board Specific implementation

FunctionPointer for rf_switch callback allows the program to implement control of RF switch unique to their board. Example options are:

  • SKY13373 for external power amplifier implementation. Requires two DigitalOut pins.
  • SKY13350 using PA_BOOST. requires two DigitalOut pins.
  • PE4259-63: controlled directly by radio chip, no software function needed. However, in the case of SX1276MB1xAS, the RXTX pin on IO2 should be driven by this callback function when R16 is installed (without R15) on this shield board.

Some configurations may need to force the use of RFO or PA_BOOST, or a board could offer both options. The rf_switch function pointer callback should support the implementation choice on the board.

further reading

Revision:
1:7dc60eb4c7ec
Parent:
0:27aa8733f85d
Child:
2:fdae76e1215e
--- a/sx127x.cpp	Wed Mar 26 00:56:09 2014 +0000
+++ b/sx127x.cpp	Mon Apr 14 21:57:41 2014 +0000
@@ -22,7 +22,7 @@
     reset_pin.input();
     m_cs = 1;
     m_spi.format(8, 0);
-    m_spi.frequency(1000000);
+    m_spi.frequency(3000000);
     
     init();
 }
@@ -78,6 +78,24 @@
             type = SX1272;
     }
 }
+
+void
+SX127x::ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
+{
+    uint8_t i;
+    
+    m_cs = 0;
+    
+    m_spi.write(addr); // bit7 is low for reading from radio
+
+    for( i = 0; i < size; i++ )
+    {
+        buffer[i] = m_spi.write(0x00);
+    }
+    
+    m_cs = 1;
+}
+
 uint8_t SX127x::read_reg(uint8_t addr)
 {
     uint8_t ret;
@@ -143,6 +161,21 @@
     m_cs = 1;   // Deselect the device
 }
 
+void SX127x::WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
+{
+    uint8_t i;
+
+    m_cs = 0;   // Select the device by seting chip select low
+
+    m_spi.write(addr | 0x80); // bit7 is high for writing to radio
+    for( i = 0; i < size; i++ )
+    {
+        m_spi.write(buffer[i]);
+    }
+
+    m_cs = 1;   // Deselect the device
+}
+
 void SX127x::lora_write_fifo(uint8_t len)
 {
     int i;
@@ -167,6 +200,48 @@
     m_cs = 1;
 }
 
+void
+SX127x::SetLoRaOn( bool enable )
+{   
+    set_opmode(RF_OPMODE_SLEEP);
+
+    if (enable)
+    {   
+        RegOpMode.bits.LongRangeMode = 1;
+        write_reg(REG_OPMODE, RegOpMode.octet);
+        
+        /*RegOpMode.octet = read_reg(REG_OPMODE);
+        printf("setloraon:%02x\r\n", RegOpMode.octet);*/
+        
+
+        /*                                // RxDone               RxTimeout                   FhssChangeChannel           CadDone
+        SX1272LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_00 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_00;
+                                        // CadDetected          ModeReady
+        SX1272LR->RegDioMapping2 = RFLR_DIOMAPPING2_DIO4_00 | RFLR_DIOMAPPING2_DIO5_00;
+        SX1272WriteBuffer( REG_LR_DIOMAPPING1, &SX1272LR->RegDioMapping1, 2 );*/
+        RegDioMapping1.bits.Dio0Mapping = 0;    // DIO0 to RxDone
+        RegDioMapping1.bits.Dio1Mapping = 0;
+        write_reg(REG_DIOMAPPING1, RegDioMapping1.octet);
+        
+        // todo: read LoRa regsiters
+        //SX1272ReadBuffer( REG_LR_OPMODE, SX1272Regs + 1, 0x70 - 1 );
+    }
+    else
+    {
+        RegOpMode.bits.LongRangeMode = 0;
+        write_reg(REG_OPMODE, RegOpMode.octet);
+        
+        /*RegOpMode.octet = read_reg(REG_OPMODE);
+        printf("setloraoff:%02x\r\n", RegOpMode.octet);*/
+        
+        // todo: read FSK regsiters
+        //SX1272ReadBuffer( REG_OPMODE, SX1272Regs + 1, 0x70 - 1 );
+    }
+    
+    set_opmode(RF_OPMODE_STANDBY);
+}
+
+
 void SX127x::set_opmode(chip_mode_e mode)
 {
     RegOpMode.bits.Mode = mode;