mBed RFM12B module library

Dependents:   _EXAMPLE_RFM12B

Fork of RF12B by Sukkin Pang

RFM12B Library

The main purpose of this library was to implement the RFM12B module in order to be able to establish communication with the Moteino (arduino clone that uses the RFM12B).

In order to achieve my goal I was highly inspired by RF12B library from pangsk https://mbed.org/users/pangsk/ and by RFM12B arduino library made by Felix Rusu (http://lowpowerlab.com/blog/2012/12/28/rfm12b-arduino-library/)

Who/What is Moteino? (http://lowpowerlab.com/moteino/)

Revision:
7:19d9da22271a
Parent:
6:52322349ee10
Child:
8:7d282360721a
--- a/RFM12B.cpp	Thu May 30 22:05:50 2013 +0000
+++ b/RFM12B.cpp	Thu May 30 22:11:42 2013 +0000
@@ -30,7 +30,8 @@
 #include "RFM12B.h"
 
 RFM12B::RFM12B(PinName _SDI, PinName _SDO, PinName _SCK, PinName _NCS, PinName _NIRQ, PinName _NIRQ_LED) :
-spi(_SDI, _SDO, _SCK), NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ), NIRQ_LED(_NIRQ_LED) {
+    spi(_SDI, _SDO, _SCK), NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ), NIRQ_LED(_NIRQ_LED)
+{
 
     useEncryption = false;
 
@@ -45,7 +46,8 @@
     NIRQ.fall(this, &RFM12B::InterruptHandler);
 }
 
-int RFM12B::writeCmd(int cmd) {
+int RFM12B::writeCmd(int cmd)
+{
     NCS = 0;
     int recv = spi.write(cmd >> 8);
     recv = spi.write(cmd);
@@ -53,7 +55,8 @@
     return recv;
 }
 
-uint16_t RFM12B::crc16_update(uint16_t crc, uint8_t data) {
+uint16_t RFM12B::crc16_update(uint16_t crc, uint8_t data)
+{
     int i;
 
     crc ^= data;
@@ -67,11 +70,13 @@
     return crc;
 }
 
-uint8_t RFM12B::byte(uint8_t out) {
+uint8_t RFM12B::byte(uint8_t out)
+{
     return spi.write(out);
 }
 
-uint16_t RFM12B::xfer(uint16_t cmd) {
+uint16_t RFM12B::xfer(uint16_t cmd)
+{
     NCS = 0;
     uint16_t reply = byte(cmd >> 8) << 8;
     reply |= byte(cmd);
@@ -85,7 +90,8 @@
 // - networkid [optional - default = 170] (0-255 for RF12B, only 212 allowed for RF12)
 // - txPower [optional - default = 0 (max)] (7 is min value)
 // - airKbps [optional - default = 38.31Kbps]
-void RFM12B::Initialize(uint8_t nodeid, uint8_t freqBand, uint8_t groupid, uint8_t txPower, uint8_t airKbps) {
+void RFM12B::Initialize(uint8_t nodeid, uint8_t freqBand, uint8_t groupid, uint8_t txPower, uint8_t airKbps)
+{
 
     nodeID = nodeid;
     networkID = groupid;
@@ -123,7 +129,8 @@
     rxstate = TXIDLE;
 }
 
-void RFM12B::InterruptHandler() {
+void RFM12B::InterruptHandler()
+{
 
     NIRQ_LED = 1;
 
@@ -150,25 +157,25 @@
             rf12_crc = crc16_update(rf12_crc, out);
         } else {
             switch (rxstate++) {
-            case TXSYN1:
-                out = 0x2D;
-                break;
-            case TXSYN2:
-                out = rf12_grp;
-                rxstate = - (3 + rf12_len);
-                break;
-            case TXCRC1:
-                out = rf12_crc;
-                break;
-            case TXCRC2:
-                out = rf12_crc >> 8;
-                break;
-            case TXDONE:
-                xfer(RF_IDLE_MODE); // fall through
-                out = 0xAA;
-                break;
-            default:
-                out = 0xAA;
+                case TXSYN1:
+                    out = 0x2D;
+                    break;
+                case TXSYN2:
+                    out = rf12_grp;
+                    rxstate = - (3 + rf12_len);
+                    break;
+                case TXCRC1:
+                    out = rf12_crc;
+                    break;
+                case TXCRC2:
+                    out = rf12_crc >> 8;
+                    break;
+                case TXDONE:
+                    xfer(RF_IDLE_MODE); // fall through
+                    out = 0xAA;
+                    break;
+                default:
+                    out = 0xAA;
             }
         }
         xfer(RF_TXREG_WRITE + out);
@@ -176,7 +183,8 @@
     NIRQ_LED = 0;
 }
 
-void RFM12B::ReceiveStart(void) {
+void RFM12B::ReceiveStart(void)
+{
     rxfill = rf12_len= 0;
     rf12_crc = ~0;
 
@@ -187,7 +195,8 @@
     xfer(RF_RECEIVER_ON);
 }
 
-bool RFM12B::ReceiveComplete(void) {
+bool RFM12B::ReceiveComplete(void)
+{
     if (rxstate == TXRECV && (rxfill >= rf12_len+ 6 || rxfill >= RF_MAX)) {
         rxstate = TXIDLE;
 
@@ -218,7 +227,8 @@
     return false;
 }
 
-bool RFM12B::CanSend() {
+bool RFM12B::CanSend()
+{
     // no need to test with interrupts disabled: state TXRECV is only reached
     // outside of ISR and we don't care if rxfill jumps from 0 to 1 here
     if (rxstate == TXRECV && rxfill == 0 && (byte(0x00) & (RF_RSSI_BIT >> 8)) == 0) {
@@ -229,7 +239,8 @@
     return false;
 }
 
-void RFM12B::SendStart(uint8_t toNodeID, bool requestACK, bool sendACK) {
+void RFM12B::SendStart(uint8_t toNodeID, bool requestACK, bool sendACK)
+{
 
     rf12_hdr1= toNodeID | (sendACK ? RF12_HDR_ACKCTLMASK : 0);
     rf12_hdr2= nodeID | (requestACK ? RF12_HDR_ACKCTLMASK : 0);
@@ -248,68 +259,78 @@
     xfer(RF_XMITTER_ON); // bytes will be fed via interrupts
 }
 
-void RFM12B::SendStart(uint8_t toNodeID, const void* sendBuf, uint8_t sendLen, bool requestACK, bool sendACK) {
-    
+void RFM12B::SendStart(uint8_t toNodeID, const void* sendBuf, uint8_t sendLen, bool requestACK, bool sendACK)
+{
+
     rf12_len = sendLen;
     memcpy((void*) rf12_data, sendBuf, sendLen);
-    
+
 #ifdef DEBUG
-            printf("\nSending message from [%d]; crc:%x,  len: %d, message: ", nodeID, rf12_crc, rf12_len);
-            for (int i=0; i<rf12_len; i++) {
-                printf("%c", rf12_data[i]);
-            }
+    printf("\nSending message from [%d]; crc:%x,  len: %d, message: ", nodeID, rf12_crc, rf12_len);
+    for (int i=0; i<rf12_len; i++) {
+        printf("%c", rf12_data[i]);
+    }
 #endif
-    
+
 
     SendStart(toNodeID, requestACK, sendACK);
 }
 
 /// Should be called immediately after reception in case sender wants ACK
-void RFM12B::SendACK(const void* sendBuf, uint8_t sendLen) {
+void RFM12B::SendACK(const void* sendBuf, uint8_t sendLen)
+{
     while (!CanSend())
         ReceiveComplete();
     SendStart(RF12_SOURCEID, sendBuf, (sendLen > 0) ? sendLen: strlen((const char*)sendBuf), false, true);
 }
 
-void RFM12B::Send(uint8_t toNodeID, const void* sendBuf, uint8_t sendLen, bool requestACK) {
+void RFM12B::Send(uint8_t toNodeID, const void* sendBuf, uint8_t sendLen, bool requestACK)
+{
     while (!CanSend())
         ReceiveComplete();
     SendStart(toNodeID, sendBuf, (sendLen > 0) ? sendLen: strlen((const char*)sendBuf) , requestACK, false);
 }
 
-uint8_t RFM12B::GetSender(void) {
+uint8_t RFM12B::GetSender(void)
+{
     return RF12_SOURCEID;
 }
 
-volatile uint8_t * RFM12B::GetData(void) {
+volatile uint8_t * RFM12B::GetData(void)
+{
 
     return (uint8_t*) rf12_data;
 }
 
-uint8_t RFM12B::GetDataLen(void) {
+uint8_t RFM12B::GetDataLen(void)
+{
     return rf12_len;
 }
 
-bool RFM12B::ACKRequested() {
+bool RFM12B::ACKRequested()
+{
     return RF12_WANTS_ACK;
 }
 
 /// Should be polled immediately after sending a packet with ACK request
-bool RFM12B::ACKReceived(uint8_t fromNodeID) {
+bool RFM12B::ACKReceived(uint8_t fromNodeID)
+{
     if (ReceiveComplete())
         return CRC_Pass() && RF12_DESTID == nodeID && (RF12_SOURCEID == fromNodeID || fromNodeID == 0) && (rf12_hdr1&RF12_HDR_ACKCTLMASK) &&
-                !(rf12_hdr2 & RF12_HDR_ACKCTLMASK);
+               !(rf12_hdr2 & RF12_HDR_ACKCTLMASK);
     return false;
 }
 
-bool RFM12B::CRC_Pass(void) {
+bool RFM12B::CRC_Pass(void)
+{
     return (rf12_crc == 0);
 }
 
 // XXTEA by David Wheeler, adapted from http://en.wikipedia.org/wiki/XXTEA
 #define DELTA 0x9E3779B9
 #define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (cryptKey[(uint8_t)((p&3)^e)] ^ z)))
-void RFM12B::Encryption(bool encrypt) {
+void RFM12B::Encryption(bool encrypt)
+{
 
     uint32_t y, z, sum, *v = (uint32_t*) rf12_data;
     uint8_t p, e, rounds = 6;
@@ -334,7 +355,7 @@
                     y = v[p+1], z = v[p] += MX;
                 y = v[0];
                 z = v[n-1] += MX;
-            }while (--rounds);
+            } while (--rounds);
         }
     } else if (rf12_crc == 0) {
         // actual decoding
@@ -348,7 +369,7 @@
                     z = v[p-1], y = v[p] -= MX;
                 z = v[n-1];
                 y = v[0] -= MX;
-            }while ((sum -= DELTA) != 0);
+            } while ((sum -= DELTA) != 0);
         }
         // strip sequence number from the end again
         if (n > 0) {
@@ -361,7 +382,8 @@
 
 }
 
-void RFM12B::SetEncryptionKey(const uint8_t* key) {
+void RFM12B::SetEncryptionKey(const uint8_t* key)
+{
     if (key != 0) {
         for (uint8_t i = 0; i < sizeof cryptKey; ++i)
             ((uint8_t*) cryptKey)[i] = key[i];