Port of lowpowerlab RFM69 Packet radio library for HopeRF RFM69H with hacks from debugging with a FRDM-KL25Z

Dependents:   chuk

Fork of RFM69 by B Rey

Revision:
1:b6d925d3740d
Parent:
0:db6e4ce9dc02
Child:
2:574f229e8182
--- a/RFM69.cpp	Sun Mar 08 17:13:43 2015 +0000
+++ b/RFM69.cpp	Mon Jun 08 19:53:20 2015 +0000
@@ -56,12 +56,12 @@
 volatile int16_t RFM69::RSSI;          // most accurate RSSI during reception (closest to the reception)
 
 RFM69::RFM69(PinName mosi, PinName miso, PinName sclk, PinName slaveSelectPin, PinName interrupt): 
-      _slaveSelectPin(slaveSelectPin) ,  _spi(mosi, miso, sclk), _interrupt(interrupt) {
+      _slaveSelectPin(slaveSelectPin) ,  _spi(mosi, miso, sclk)/*, _interrupt(interrupt)*/ {
  
     // Setup the spi for 8 bit data, high steady state clock,
     // second edge capture, with a 1MHz clock rate
     _spi.format(8,0);
-    _spi.frequency(4000000);
+    _spi.frequency(1000000);
     _mode = RF69_MODE_STANDBY;
     _promiscuousMode = false;
     _powerLevel = 31;
@@ -88,14 +88,14 @@
     // +13dBm formula: Pout = -18 + OutputPower (with PA0 or PA1**)
     // +17dBm formula: Pout = -14 + OutputPower (with PA1 and PA2)**
     // +20dBm formula: Pout = -11 + OutputPower (with PA1 and PA2)** and high power PA settings (section 3.3.7 in datasheet)
-    ///* 0x11 */ { REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | RF_PALEVEL_OUTPUTPOWER_11111},
-    ///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, // over current protection (default is 95mA)
+    /* 0x11 */ { REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_ON | RF_PALEVEL_PA2_ON | RF_PALEVEL_OUTPUTPOWER_11111},
+    /* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, // over current protection (default is 95mA)
 
     // RXBW defaults are { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4KHz)
     /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2 }, // (BitRate < 2 * RxBw)
     //for BR-19200: /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_3 },
     /* 0x25 */ { REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01 }, // DIO0 is the only IRQ we're using
-    /* 0x26 */ { REG_DIOMAPPING2, RF_DIOMAPPING2_CLKOUT_OFF }, // DIO5 ClkOut disable for power saving
+    ///* 0x26 */ { REG_DIOMAPPING2, RF_DIOMAPPING2_CLKOUT_OFF }, // DIO5 ClkOut disable for power saving
     /* 0x28 */ { REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN }, // writing to this bit ensures that the FIFO & status flags are reset
     /* 0x29 */ { REG_RSSITHRESH, 220 }, // must be set to dBm = (-Sensitivity / 2), default is 0xE4 = 228 so -114dBm
     ///* 0x2D */ { REG_PREAMBLELSB, RF_PREAMBLESIZE_LSB_VALUE } // default 3 preamble bytes 0xAAAAAA
@@ -117,19 +117,19 @@
 
     // Setup the spi for 8 bit data : 1RW-bit 7 adressbit and  8 databit
     // second edge capture, with a 10MHz clock rate
-    _spi.format(8,0);
-    _spi.frequency(4000000);
+    //_spi.format(8,0);
+    //_spi.frequency(1000000);
 
 #define TIME_OUT 50
   
   start_to = t.read_ms() ;
 
-  do writeReg(REG_SYNCVALUE1, 0xaa); while (readReg(REG_SYNCVALUE1) != 0xaa && t.read_ms()-start_to < TIME_OUT);
+  do writeReg(REG_SYNCVALUE1, 0xaa); while (readReg(REG_SYNCVALUE1) != 0xaa);// && t.read_ms()-start_to < TIME_OUT);
   if (t.read_ms()-start_to >= TIME_OUT) return (false);
     
   // Set time out 
   start_to = t.read_ms()  ;  
-	do writeReg(REG_SYNCVALUE1, 0x55); while (readReg(REG_SYNCVALUE1) != 0x55 && t.read_ms()-start_to < TIME_OUT);
+	do writeReg(REG_SYNCVALUE1, 0x55); while (readReg(REG_SYNCVALUE1) != 0x55);// && t.read_ms()-start_to < TIME_OUT);
   if (t.read_ms()-start_to >= TIME_OUT) return (false);
   for (uint8_t i = 0; CONFIG[i][0] != 255; i++)
     writeReg(CONFIG[i][0], CONFIG[i][1]);
@@ -138,14 +138,14 @@
   // Disable it during initialization so we always start from a known state.
   encrypt(0);
 
-  setHighPower(_isRFM69HW); // called regardless if it's a RFM69W or RFM69HW
+  //setHighPower(_isRFM69HW); // called regardless if it's a RFM69W or RFM69HW
   setMode(RF69_MODE_STANDBY);
     // Set up interrupt handler
 	start_to = t.read_ms() ;
-	while (((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) && t.read_ms()-start_to < TIME_OUT); // Wait for ModeReady
+	while (((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00));// && t.read_ms()-start_to < TIME_OUT); // Wait for ModeReady
   if (t.read_ms()-start_to >= TIME_OUT) return (false);
 
-    _interrupt.rise(this, &RFM69::isr0);
+    //_interrupt.rise(this, &RFM69::isr0);
  
   _address = nodeID;
    return true;
@@ -242,8 +242,8 @@
 void RFM69::send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK)
 {
   writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
-  uint32_t now = t.read_ms();
-  while (!canSend() && t.read_ms() - now < RF69_CSMA_LIMIT_MS) receiveDone();
+  //uint32_t now = t.read_ms();
+  //while (!canSend() && t.read_ms() - now < RF69_CSMA_LIMIT_MS) receiveDone();
   sendFrame(toAddress, buffer, bufferSize, requestACK, false);
 }
 
@@ -299,7 +299,7 @@
 {
   setMode(RF69_MODE_STANDBY); // turn off receiver to prevent reception while filling fifo
   while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
-  writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent"
+  //writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent"
   if (bufferSize > RF69_MAX_DATA_LEN) bufferSize = RF69_MAX_DATA_LEN;
 
  // control byte
@@ -323,8 +323,8 @@
   // no need to wait for transmit mode to be ready since its handled by the radio
   setMode(RF69_MODE_TX);
   uint32_t txStart = t.read_ms();
-  while (_interrupt == 0 && t.read_ms() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish
-  //while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // wait for ModeReady
+  //while (_interrupt == 0 && t.read_ms() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish
+  while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // wait for ModeReady
   setMode(RF69_MODE_STANDBY);
 }
 // ON  = disable filtering to capture all frames on network
@@ -354,6 +354,7 @@
     _slaveSelectPin = 1;
 }
 */
+extern Serial pc;
 // for debugging
 void RFM69::readAllRegs()
 {
@@ -365,11 +366,7 @@
     _spi.write(regAddr & 0x7F); // send address + r/w bit
     regVal = _spi.write(0);
  
- /*   Serial.print(regAddr, HEX);
-    Serial.print(" - ");
-    Serial.print(regVal,HEX);
-    Serial.print(" - ");
-    Serial.println(regVal,BIN);*/
+    pc.printf("%d - %d - %d\r\n", regAddr, regVal, regVal);
   }
   unselect();
 }
@@ -448,11 +445,11 @@
     writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
   writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); // set DIO0 to "PAYLOADREADY" in receive mode
   setMode(RF69_MODE_RX);
-  _interrupt.enable_irq();
+  //_interrupt.enable_irq();
 }
 
 bool RFM69::receiveDone() {
-  _interrupt.disable_irq();  // re-enabled in unselect() via setMode() or via receiveBegin()
+  //_interrupt.disable_irq();  // re-enabled in unselect() via setMode() or via receiveBegin()
   if (_mode == RF69_MODE_RX && PAYLOADLEN > 0)
   {
     setMode(RF69_MODE_STANDBY); // enables interrupts
@@ -460,7 +457,7 @@
   }
   else if (_mode == RF69_MODE_RX) // already in RX no payload yet
   {
-   _interrupt.enable_irq(); // explicitly re-enable interrupts
+   //_interrupt.enable_irq(); // explicitly re-enable interrupts
     return false;
   }
   receiveBegin();
@@ -515,7 +512,7 @@
 
 // select the transceiver
 void RFM69::select() {
-   _interrupt.disable_irq();    // Disable Interrupts
+   //_interrupt.disable_irq();    // Disable Interrupts
 /*  // set RFM69 SPI settings
   SPI.setDataMode(SPI_MODE0);
   SPI.setBitOrder(MSBFIRST);
@@ -526,5 +523,5 @@
 // UNselect the transceiver chip
 void RFM69::unselect() {
     _slaveSelectPin = 1;
-    _interrupt.enable_irq();     // Enable Interrupts
+    //_interrupt.enable_irq();     // Enable Interrupts
 }