From e58454968ff3309a429f42bfdac75a9213a142d4 Mon Sep 17 00:00:00 2001 From: davidhewing Date: Tue, 11 Jun 2013 13:59:46 -0400 Subject: [PATCH 1/4] Update RFM12B.cpp --- RFM12B.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/RFM12B.cpp b/RFM12B.cpp index e7eddba..692d6a3 100644 --- a/RFM12B.cpp +++ b/RFM12B.cpp @@ -44,6 +44,8 @@ void RFM12B::SPIInit() { #endif pinMode(RFM_IRQ, INPUT); digitalWrite(RFM_IRQ, 1); // pull-up + + receivedata = NULL; // receivedata callback function set to NULL } uint8_t RFM12B::Byte(uint8_t out) { @@ -225,7 +227,8 @@ void RFM12B::InterruptHandler() { case TXSYN2: out = networkID; 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 + case TXDONE: XFER(RF_IDLE_MODE); out = 0xAA; break; // fall through + case TXIDLE: ReceiveStart(); return; break; // Restart Receive hardware when done default: out = 0xAA; } @@ -263,6 +266,12 @@ void RFM12B::ReceiveStart() { XFER(RF_RECEIVER_ON); } +void RFM12B::ReceiveCallBack(void (*receivefunction)(void)) { + + receivedata = receivefunction; +} + + bool RFM12B::ReceiveComplete() { if (rxstate == TXRECV && (rxfill >= rf12_len + 6 || rxfill >= RF_MAX)) { rxstate = TXIDLE; @@ -273,6 +282,10 @@ bool RFM12B::ReceiveComplete() { crypter(false); else rf12_seq = -1; + + if (receivedata !=NULL) // Callback function if defined + receivedata(); + return true; // it's a broadcast packet or it's addressed to this node } } @@ -309,7 +322,8 @@ void RFM12B::SendStart(uint8_t toNodeID, const void* sendBuf, uint8_t sendLen, b rf12_len = sendLen; memcpy((void*) rf12_data, sendBuf, sendLen); SendStart(toNodeID, requestACK, sendACK); - SendWait(waitMode); + if (waitMode) + SendWait(waitMode); } /// Should be called immediately after reception in case sender wants ACK From 6c652761e02ad873f44c7b92ab69942bc319c0d3 Mon Sep 17 00:00:00 2001 From: davidhewing Date: Tue, 11 Jun 2013 13:01:39 -0500 Subject: [PATCH 2/4] Added support for ReceiveCallBack function call --- RFM12B.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/RFM12B.h b/RFM12B.h index 751b673..fe56d66 100644 --- a/RFM12B.h +++ b/RFM12B.h @@ -170,6 +170,7 @@ class RFM12B static long rf12_seq; // seq number of encrypted packet (or -1) static uint8_t cs_pin; // chip select pin void (*crypter)(bool); // does en-/decryption (null if disabled) + void (*receivedata)(void); // Receive callback function static uint8_t Byte(uint8_t out); static uint16_t XFERSlow(uint16_t cmd); static void XFER(uint16_t cmd); @@ -191,10 +192,11 @@ class RFM12B //Defaults: Group: 0xAA=170, transmit power: 0(max), KBPS: 38.3Kbps (air transmission baud - has to be same on all radios in same group) void Initialize(uint8_t nodeid, uint8_t freqBand, uint8_t groupid=0xAA, uint8_t txPower=0, uint8_t airKbps=0x08, uint8_t lowVoltageThreshold=RF12_2v75); void SetCS(uint8_t pin); - void ReceiveStart(); + static void ReceiveStart(); bool ReceiveComplete(); bool CanSend(); uint16_t Control(uint16_t cmd); + void ReceiveCallBack(void (*receivefunction)(void)); void SendStart(uint8_t toNodeId, bool requestACK=false, bool sendACK=false); void SendStart(uint8_t toNodeId, const void* sendBuf, uint8_t sendLen, bool requestACK=false, bool sendACK=false, uint8_t waitMode=SLEEP_MODE_STANDBY); From 2d6c89276b39c173c4f106de3630d9f51b38449e Mon Sep 17 00:00:00 2001 From: davidhewing Date: Tue, 11 Jun 2013 13:02:26 -0500 Subject: [PATCH 3/4] ReceiveCallBack function support --- keywords.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/keywords.txt b/keywords.txt index a430a0f..8c6da47 100644 --- a/keywords.txt +++ b/keywords.txt @@ -10,7 +10,7 @@ # Methods and Functions (KEYWORD2) ####################################### Initialize KEYWORD2 -SetCS KEYWORD2 +SetCS KEYWORD2 Control KEYWORD2 ReceiveStart KEYWORD2 ReceiveComplete KEYWORD2 @@ -26,6 +26,7 @@ LowBattery KEYWORD2 CryptFunction KEYWORD2 Encrypt KEYWORD2 CRCPass KEYWORD2 +ReceiveCallBack KEYWORD2 ####################################### # Instances (KEYWORD2) From f5f01a4009810e95cb64bf5e83952a80453f1e2c Mon Sep 17 00:00:00 2001 From: David Ewing Date: Sat, 29 Jun 2013 20:43:35 -0500 Subject: [PATCH 4/4] modified: RFM12B.cpp modified: RFM12B.h modified: keywords.txt Added a ClearToSend function to provide information back to the main program as to the current state of the library. If the interrupt routine is busy receiving or sending data, this function returns a false. This allows the main program to go on doing other tasks instead of calling the send routine which in turn would just wait until the RF12 is ready. --- RFM12B.cpp | 10 ++++++++++ RFM12B.h | 1 + keywords.txt | 1 + 3 files changed, 12 insertions(+) diff --git a/RFM12B.cpp b/RFM12B.cpp index 5afea68..81810ca 100644 --- a/RFM12B.cpp +++ b/RFM12B.cpp @@ -308,6 +308,16 @@ bool RFM12B::CanSend() { return false; } +bool RFM12B::ClearToSend() { + // 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) { + return true; + } + return false; +} + + 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); diff --git a/RFM12B.h b/RFM12B.h index 3c11db5..4a8c7f1 100644 --- a/RFM12B.h +++ b/RFM12B.h @@ -195,6 +195,7 @@ class RFM12B static void ReceiveStart(); bool ReceiveComplete(); bool CanSend(); + bool ClearToSend(); uint16_t Control(uint16_t cmd); void ReceiveCallBack(void (*receivefunction)(void)); diff --git a/keywords.txt b/keywords.txt index 8c6da47..d27e91d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -15,6 +15,7 @@ Control KEYWORD2 ReceiveStart KEYWORD2 ReceiveComplete KEYWORD2 CanSend KEYWORD2 +ClearToSend KEYWORD2 SendStart KEYWORD2 SendACK KEYWORD2 SendWait KEYWORD2