Fix RX message buffering for Rpi
This commit is contained in:
@@ -269,7 +269,7 @@ for opt do
|
|||||||
CXXFLAGS="-DMY_MQTT_SUBSCRIBE_TOPIC_PREFIX=${optarg} $CXXFLAGS"
|
CXXFLAGS="-DMY_MQTT_SUBSCRIBE_TOPIC_PREFIX=${optarg} $CXXFLAGS"
|
||||||
;;
|
;;
|
||||||
--my-rf24-irq-pin=*)
|
--my-rf24-irq-pin=*)
|
||||||
CXXFLAGS="-DMY_RF24_IRQ_PIN=${optarg} $CXXFLAGS"
|
CXXFLAGS="-DMY_RX_MESSAGE_BUFFER_FEATURE -DMY_RF24_IRQ_PIN=${optarg} $CXXFLAGS"
|
||||||
;;
|
;;
|
||||||
--my-rx-message-buffer-size=*)
|
--my-rx-message-buffer-size=*)
|
||||||
CXXFLAGS="-DMY_RX_MESSAGE_BUFFER_SIZE=${optarg} $CXXFLAGS"
|
CXXFLAGS="-DMY_RX_MESSAGE_BUFFER_SIZE=${optarg} $CXXFLAGS"
|
||||||
|
|||||||
+29
-15
@@ -21,6 +21,20 @@
|
|||||||
|
|
||||||
#include "RF24.h"
|
#include "RF24.h"
|
||||||
|
|
||||||
|
// verify RF24 IRQ defs
|
||||||
|
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||||
|
#if !defined(MY_RF24_IRQ_PIN)
|
||||||
|
#error Message buffering feature requires MY_RF24_IRQ_PIN to be defined!
|
||||||
|
#endif
|
||||||
|
#ifndef SPI_HAS_TRANSACTION
|
||||||
|
#error RF24 IRQ usage requires transactional SPI support
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#ifdef MY_RX_MESSAGE_BUFFER_SIZE
|
||||||
|
#error Receive message buffering requires RF24 IRQ usage
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
// pipes
|
// pipes
|
||||||
#define BROADCAST_PIPE 1
|
#define BROADCAST_PIPE 1
|
||||||
#define NODE_PIPE 2
|
#define NODE_PIPE 2
|
||||||
@@ -32,7 +46,7 @@
|
|||||||
#define RF24_DEBUG(x,...)
|
#define RF24_DEBUG(x,...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
typedef void (*RF24_receiveCallbackType)(void);
|
typedef void (*RF24_receiveCallbackType)(void);
|
||||||
|
|
||||||
void (* RF24_receiveCallback)(void) = NULL;
|
void (* RF24_receiveCallback)(void) = NULL;
|
||||||
@@ -52,17 +66,17 @@ static void RF24_startListening(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void RF24_powerDown(void) {
|
static void RF24_powerDown(void) {
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_lock(&rf24_mutex);
|
pthread_mutex_lock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
_rf24.powerDown();
|
_rf24.powerDown();
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_unlock(&rf24_mutex);
|
pthread_mutex_unlock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
|
static bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_lock(&rf24_mutex);
|
pthread_mutex_lock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
// Make sure radio has powered up
|
// Make sure radio has powered up
|
||||||
@@ -75,38 +89,38 @@ static bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len )
|
|||||||
_rf24.openWritingPipe(MY_RF24_BASE_ADDR);
|
_rf24.openWritingPipe(MY_RF24_BASE_ADDR);
|
||||||
bool ok = _rf24.write(buf, len, recipient == BROADCAST_ADDRESS);
|
bool ok = _rf24.write(buf, len, recipient == BROADCAST_ADDRESS);
|
||||||
_rf24.startListening();
|
_rf24.startListening();
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_unlock(&rf24_mutex);
|
pthread_mutex_unlock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool RF24_isDataAvailable() {
|
static bool RF24_isDataAvailable() {
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_lock(&rf24_mutex);
|
pthread_mutex_lock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
uint8_t pipe_num = 255;
|
uint8_t pipe_num = 255;
|
||||||
_rf24.available(&pipe_num);
|
_rf24.available(&pipe_num);
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_unlock(&rf24_mutex);
|
pthread_mutex_unlock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
return (pipe_num <= 5);
|
return (pipe_num <= 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t RF24_readMessage( void* buf) {
|
static uint8_t RF24_readMessage( void* buf) {
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_lock(&rf24_mutex);
|
pthread_mutex_lock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
uint8_t len = _rf24.getDynamicPayloadSize();
|
uint8_t len = _rf24.getDynamicPayloadSize();
|
||||||
_rf24.read(buf, len);
|
_rf24.read(buf, len);
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_unlock(&rf24_mutex);
|
pthread_mutex_unlock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RF24_setNodeAddress(uint8_t address) {
|
static void RF24_setNodeAddress(uint8_t address) {
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_lock(&rf24_mutex);
|
pthread_mutex_lock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
if (address != AUTO){
|
if (address != AUTO){
|
||||||
@@ -117,7 +131,7 @@ static void RF24_setNodeAddress(uint8_t address) {
|
|||||||
// enable autoACK on node pipe
|
// enable autoACK on node pipe
|
||||||
_rf24.setAutoAck(NODE_PIPE, true);
|
_rf24.setAutoAck(NODE_PIPE, true);
|
||||||
}
|
}
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_unlock(&rf24_mutex);
|
pthread_mutex_unlock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -128,7 +142,7 @@ static uint8_t RF24_getNodeID(void) {
|
|||||||
|
|
||||||
bool RF24_sanityCheck(void) {
|
bool RF24_sanityCheck(void) {
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_lock(&rf24_mutex);
|
pthread_mutex_lock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
// detect HW defect ot interrupted SPI line, CE disconnect cannot be detected
|
// detect HW defect ot interrupted SPI line, CE disconnect cannot be detected
|
||||||
@@ -136,13 +150,13 @@ bool RF24_sanityCheck(void) {
|
|||||||
_rf24.getChannel() != MY_RF24_CHANNEL) {
|
_rf24.getChannel() != MY_RF24_CHANNEL) {
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
pthread_mutex_unlock(&rf24_mutex);
|
pthread_mutex_unlock(&rf24_mutex);
|
||||||
#endif
|
#endif
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
void RF24_irqHandler(void) {
|
void RF24_irqHandler(void) {
|
||||||
if (RF24_receiveCallback) {
|
if (RF24_receiveCallback) {
|
||||||
while (RF24_isDataAvailable()) {
|
while (RF24_isDataAvailable()) {
|
||||||
@@ -199,7 +213,7 @@ static bool RF24_initialize(void) {
|
|||||||
|
|
||||||
//_rf24.printDetails();
|
//_rf24.printDetails();
|
||||||
|
|
||||||
#ifdef MY_RF24_IRQ_PIN
|
#ifdef MY_RX_MESSAGE_BUFFER_FEATURE
|
||||||
// Mask all interrupts except the receive interrupt
|
// Mask all interrupts except the receive interrupt
|
||||||
_rf24.maskIRQ(1,1,0);
|
_rf24.maskIRQ(1,1,0);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user