Merge branch 'development' into dev-raspberrypi
This commit is contained in:
+22
-24
@@ -25,7 +25,7 @@
|
||||
LOCAL uint8_t MY_RF24_BASE_ADDR[MY_RF24_ADDR_WIDTH] = { MY_RF24_BASE_RADIO_ID };
|
||||
LOCAL uint8_t MY_RF24_NODE_ADDRESS = AUTO;
|
||||
|
||||
#ifdef MY_RF24_IRQ_PIN
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
LOCAL RF24_receiveCallbackType RF24_receiveCallback = NULL;
|
||||
#endif
|
||||
|
||||
@@ -66,7 +66,7 @@ LOCAL uint8_t RF24_spiByteTransfer(uint8_t cmd) {
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_RAW_readByteRegister(uint8_t cmd) {
|
||||
uint8_t value = RF24_spiMultiByteTransfer( cmd, NULL, 1, true);
|
||||
const uint8_t value = RF24_spiMultiByteTransfer( cmd, NULL, 1, true);
|
||||
RF24_DEBUG(PSTR("RF24:read register, reg=%d, value=%d\n"), cmd & (R_REGISTER ^ 0xFF), value);
|
||||
return value;
|
||||
}
|
||||
@@ -90,7 +90,7 @@ LOCAL uint8_t RF24_getStatus(void) {
|
||||
return RF24_spiByteTransfer( NOP );
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getFifoStatus(void) {
|
||||
LOCAL uint8_t RF24_getFIFOStatus(void) {
|
||||
return RF24_readByteRegister(FIFO_STATUS);
|
||||
}
|
||||
|
||||
@@ -138,6 +138,10 @@ LOCAL void RF24_setPipeLSB(uint8_t pipe, uint8_t LSB) {
|
||||
RF24_writeByteRegister(pipe, LSB);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getObserveTX(void) {
|
||||
return RF24_readByteRegister(OBSERVE_TX);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setStatus(uint8_t status) {
|
||||
RF24_writeByteRegister(RF24_STATUS, status);
|
||||
}
|
||||
@@ -187,9 +191,8 @@ LOCAL bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
|
||||
// flush TX FIFO
|
||||
RF24_flushTX();
|
||||
// this command is affected in clones (e.g. Si24R1): flipped NoACK bit when using W_TX_PAYLOAD_NO_ACK / W_TX_PAYLOAD
|
||||
// RF24_spiMultiByteTransfer( recipient==BROADCAST_ADDRESS ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD, (uint8_t*)buf, len, false );
|
||||
// we are safe by disabling AutoACK on the broadcasting pipe
|
||||
RF24_spiMultiByteTransfer( W_TX_PAYLOAD, (uint8_t*)buf, len, false );
|
||||
// AutoACK is disabled on the broadcasting pipe - NO_ACK prevents resending
|
||||
RF24_spiMultiByteTransfer(recipient == BROADCAST_ADDRESS ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD, (uint8_t*)buf, len, false );
|
||||
// go, TX starts after ~10us
|
||||
RF24_ce(HIGH);
|
||||
// timeout counter to detect HW issues
|
||||
@@ -207,10 +210,9 @@ LOCAL bool RF24_sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
|
||||
RF24_DEBUG(PSTR("RF24:MAX_RT\n"));
|
||||
RF24_flushTX();
|
||||
}
|
||||
|
||||
RF24_startListening();
|
||||
// true if message sent and not timeout
|
||||
return (status & _BV(TX_DS) && timeout);
|
||||
// true if message sent
|
||||
return ( status & _BV(TX_DS) );
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getDynamicPayloadSize(void) {
|
||||
@@ -224,15 +226,13 @@ LOCAL uint8_t RF24_getDynamicPayloadSize(void) {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
LOCAL bool RF24_isDataAvailable() {
|
||||
uint8_t status = RF24_getStatus();
|
||||
return (((status >> RX_P_NO) & 0b0111) <= 5);
|
||||
return ( !(RF24_getFIFOStatus() & _BV(0)) );
|
||||
}
|
||||
|
||||
|
||||
LOCAL uint8_t RF24_readMessage( void* buf) {
|
||||
uint8_t len = RF24_getDynamicPayloadSize();
|
||||
const uint8_t len = RF24_getDynamicPayloadSize();
|
||||
RF24_DEBUG(PSTR("RF24:read message, len=%d\n"), len);
|
||||
RF24_spiMultiByteTransfer( R_RX_PAYLOAD , (uint8_t*)buf, len, true );
|
||||
// clear RX interrupt
|
||||
@@ -256,12 +256,10 @@ LOCAL uint8_t RF24_getNodeID(void) {
|
||||
|
||||
LOCAL bool RF24_sanityCheck(void) {
|
||||
// detect HW defect, configuration errors or interrupted SPI line, CE disconnect cannot be detected
|
||||
bool status = RF24_readByteRegister(RF_SETUP) == MY_RF24_RF_SETUP;
|
||||
status &= RF24_readByteRegister(RF_CH) == MY_RF24_CHANNEL;
|
||||
return status;
|
||||
return (RF24_readByteRegister(RF_SETUP) == MY_RF24_RF_SETUP) & (RF24_readByteRegister(RF_CH) == MY_RF24_CHANNEL);
|
||||
}
|
||||
|
||||
#ifdef MY_RF24_IRQ_PIN
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
LOCAL void RF24_irqHandler( void )
|
||||
{
|
||||
if (RF24_receiveCallback)
|
||||
@@ -281,7 +279,7 @@ LOCAL void RF24_irqHandler( void )
|
||||
|
||||
// Start checking if RX-FIFO is not empty, as we might end up here from an interrupt
|
||||
// for a message we've already read.
|
||||
while (!(RF24_getFifoStatus() & _BV(0))) {
|
||||
while (RF24_isDataAvailable()) {
|
||||
RF24_receiveCallback(); // Must call RF24_readMessage(), which will clear RX_DR IRQ !
|
||||
}
|
||||
// Restore our interrupt handler.
|
||||
@@ -295,8 +293,7 @@ LOCAL void RF24_irqHandler( void )
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL void RF24_registerReceiveCallback( RF24_receiveCallbackType cb )
|
||||
{
|
||||
LOCAL void RF24_registerReceiveCallback( RF24_receiveCallbackType cb ) {
|
||||
MY_CRITICAL_SECTION {
|
||||
RF24_receiveCallback = cb;
|
||||
}
|
||||
@@ -304,25 +301,26 @@ LOCAL void RF24_registerReceiveCallback( RF24_receiveCallbackType cb )
|
||||
#endif
|
||||
|
||||
LOCAL bool RF24_initialize(void) {
|
||||
// prevent warning
|
||||
(void)RF24_getObserveTX;
|
||||
|
||||
// Initialize pins
|
||||
pinMode(MY_RF24_CE_PIN,OUTPUT);
|
||||
pinMode(MY_RF24_CS_PIN,OUTPUT);
|
||||
#ifdef MY_RF24_IRQ_PIN
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
pinMode(MY_RF24_IRQ_PIN,INPUT);
|
||||
#endif
|
||||
// Initialize SPI
|
||||
_SPI.begin();
|
||||
RF24_ce(LOW);
|
||||
RF24_csn(HIGH);
|
||||
#ifdef MY_RF24_IRQ_PIN
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
// assure SPI can be used from interrupt context
|
||||
// Note: ESP8266 & SoftSPI currently do not support interrupt usage for SPI,
|
||||
// therefore it is unsafe to use MY_RF24_IRQ_PIN with ESP8266/SoftSPI!
|
||||
_SPI.usingInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN));
|
||||
// attach interrupt
|
||||
attachInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN), RF24_irqHandler, FALLING);
|
||||
#else
|
||||
(void)RF24_getFifoStatus; // prevent 'defined but not used' warning
|
||||
#endif
|
||||
// CRC and power up
|
||||
RF24_setRFConfiguration(MY_RF24_CONFIGURATION | _BV(PWR_UP) ) ;
|
||||
|
||||
+28
-7
@@ -34,13 +34,10 @@
|
||||
#define MY_RF24_SPI_DATA_MODE SPI_MODE0
|
||||
|
||||
#if defined (ARDUINO) && !defined (__arm__) && !defined (_SPI)
|
||||
#include <SPI.h>
|
||||
#if defined(MY_SOFTSPI)
|
||||
SoftSPI<MY_SOFT_SPI_MISO_PIN, MY_SOFT_SPI_MOSI_PIN, MY_SOFT_SPI_SCK_PIN, MY_RF24_SPI_DATA_MODE> _SPI;
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#include <SPI.h>
|
||||
#define _SPI SPI1
|
||||
#else
|
||||
#include <SPI.h>
|
||||
#define _SPI SPI
|
||||
#endif
|
||||
#else
|
||||
@@ -61,8 +58,31 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// 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
|
||||
// SoftSPI does not support usingInterrupt()
|
||||
#ifdef MY_SOFTSPI
|
||||
#error RF24 IRQ usage cannot be used with Soft SPI
|
||||
#endif
|
||||
// ESP8266 does not support usingInterrupt()
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#error RF24 IRQ usage cannot be used with ESP8266
|
||||
#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
|
||||
|
||||
|
||||
// RF24 settings
|
||||
#ifdef MY_RF24_IRQ_PIN
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
#define MY_RF24_CONFIGURATION (uint8_t) ((RF24_CRC_16 << 2) | (1 << MASK_TX_DS) | (1 << MASK_MAX_RT))
|
||||
#else
|
||||
#define MY_RF24_CONFIGURATION (uint8_t) (RF24_CRC_16 << 2)
|
||||
@@ -226,7 +246,7 @@ LOCAL uint8_t RF24_RAW_writeByteRegister(uint8_t cmd, uint8_t value);
|
||||
LOCAL void RF24_flushRX(void);
|
||||
LOCAL void RF24_flushTX(void);
|
||||
LOCAL uint8_t RF24_getStatus(void);
|
||||
LOCAL uint8_t RF24_getFifoStatus(void);
|
||||
LOCAL uint8_t RF24_getFIFOStatus(void);
|
||||
LOCAL void RF24_openWritingPipe(uint8_t recipient);
|
||||
LOCAL void RF24_startListening(void);
|
||||
LOCAL void RF24_stopListening(void);
|
||||
@@ -250,10 +270,11 @@ LOCAL void RF24_setDynamicPayload(uint8_t pipe);
|
||||
LOCAL void RF24_setRFConfiguration(uint8_t configuration);
|
||||
LOCAL void RF24_setPipeAddress(uint8_t pipe, uint8_t* address, uint8_t width);
|
||||
LOCAL void RF24_setPipeLSB(uint8_t pipe, uint8_t LSB);
|
||||
LOCAL uint8_t RF24_getObserveTX(void);
|
||||
LOCAL void RF24_setStatus(uint8_t status);
|
||||
LOCAL void RF24_enableFeatures(void);
|
||||
|
||||
#ifdef MY_RF24_IRQ_PIN
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
typedef void (*RF24_receiveCallbackType)(void);
|
||||
/**
|
||||
* Register a callback, which will be called (from interrupt context) for every message received.
|
||||
|
||||
Reference in New Issue
Block a user