David Eisner / RFM69

Dependents:   chuk

Fork of RFM69 by B Rey

Files at this revision

API Documentation at this revision

Comitter:
eisd
Date:
Fri Jun 19 13:03:45 2015 +0000
Parent:
2:574f229e8182
Child:
4:a6b314434ab4
Commit message:
Busy, non-IRQ receive

Changed in this revision

RFM69.cpp Show annotated file Show diff for this revision Revisions of this file
RFM69.h Show annotated file Show diff for this revision Revisions of this file
--- a/RFM69.cpp	Mon Jun 08 20:53:46 2015 +0000
+++ b/RFM69.cpp	Fri Jun 19 13:03:45 2015 +0000
@@ -465,6 +465,76 @@
   return false;
 }
 
+void RFM69::receive() {
+  unsigned long timeout = 10000;
+  char i;
+
+  DATALEN = 0;
+  SENDERID = 0;
+  TARGETID = 0;
+  PAYLOADLEN = 0;
+  ACK_REQUESTED = 0;
+  ACK_RECEIVED = 0;
+  RSSI = 0;
+  for(i = 0; i < 63; i++) DATA[i] = 0;
+
+  // Receive Data until timeout or valid data
+  while(1) {
+    if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)
+      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);
+
+    //printf("Ya estoy en modo RX\n\r");
+
+    // Receive Data until timeout (aprox 2s)
+    while((readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY) == 0) {
+      timeout--;
+      if(timeout == 0) {
+        //printf("Timeout!\n\r");
+        setMode(RF69_MODE_STANDBY);
+        return;
+      }
+      //usleep(1);
+    }
+
+    //printf("He recibido algo!!\n\r");
+
+    // Received any packet!
+    setMode(RF69_MODE_STANDBY);
+    select();
+
+    _spi.write(REG_FIFO & 0x7F);
+    PAYLOADLEN = _spi.write(0);
+    PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution
+    TARGETID = _spi.write(0);
+
+    //printf("Payload length: %i\n\r", PAYLOADLEN);
+    //printf("Target id: %i\n\r", TARGETID);
+
+    if(!(_promiscuousMode || TARGETID==_address || TARGETID==RF69_BROADCAST_ADDR) //match this node's address, or $
+      || PAYLOADLEN < 3) { // address situation could receive packets that are malformed and don't fit this libraries extra fields
+      PAYLOADLEN = 0;
+    } else {
+      DATALEN = PAYLOADLEN - 3;
+      SENDERID = _spi.write(0);
+      uint8_t CTLbyte = _spi.write(0);
+
+      ACK_RECEIVED = CTLbyte & 0x80; // extract ACK-received flag
+      ACK_REQUESTED = CTLbyte & 0x40; // extract ACK-requested flag
+
+      for (uint8_t i = 0; i < DATALEN; i++)
+      {
+        DATA[i] = _spi.write(0);
+      }
+      if (DATALEN < RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string
+    }
+    unselect();
+    //setMode(RF69_MODE_RX);
+    RSSI = readRSSI(0);
+  }
+}
+
 // To enable encryption: radio.encrypt("ABCDEFGHIJKLMNOP");
 // To disable encryption: radio.encrypt(null) or radio.encrypt(0)
 // KEY HAS TO BE 16 bytes !!!
--- a/RFM69.h	Mon Jun 08 20:53:46 2015 +0000
+++ b/RFM69.h	Fri Jun 19 13:03:45 2015 +0000
@@ -75,6 +75,7 @@
     bool canSend();
     void send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK=false);
     bool sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=40); // 40ms roundtrip req for 61byte packets
+    void receive();
     bool receiveDone();
     bool ACKReceived(uint8_t fromNodeID);
     bool ACKRequested();