Transport update (#722)

* Major transport rework

* Adjust signal report calculations

* Minor driver optimizations

* Implement sanity check in legacy RFM69 driver

* Additional RFM69 rework

* RFM95 encryption

* Polishing
This commit is contained in:
Olivier
2017-03-28 20:31:08 +02:00
committed by Henrik Ekblad
parent 63b8d935c8
commit b132a8a81e
37 changed files with 5986 additions and 1063 deletions
+150 -81
View File
@@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
@@ -17,13 +17,20 @@
* version 2 as published by the Free Software Foundation.
*
* Based on maniacbug's RF24 library, copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
* RF24 driver refactored and optimized for speed and size, copyright (C) 2016 Olivier Mauti <olivier@mysensors.org>
* RF24 driver refactored and optimized for speed and size, copyright (C) 2017 Olivier Mauti <olivier@mysensors.org>
*/
#include "RF24.h"
LOCAL uint8_t MY_RF24_BASE_ADDR[MY_RF24_ADDR_WIDTH] = { MY_RF24_BASE_RADIO_ID };
LOCAL uint8_t MY_RF24_NODE_ADDRESS = AUTO;
// debug output
#if defined(MY_DEBUG_VERBOSE_RF24)
#define RF24_DEBUG(x,...) DEBUG_OUTPUT(x, ##__VA_ARGS__) //!< DEBUG
#else
#define RF24_DEBUG(x,...) //!< DEBUG null
#endif
LOCAL uint8_t RF24_BASE_ID[MY_RF24_ADDR_WIDTH] = { MY_RF24_BASE_RADIO_ID };
LOCAL uint8_t RF24_NODE_ADDRESS = RF24_BROADCAST_ADDRESS;
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
LOCAL RF24_receiveCallbackType RF24_receiveCallback = NULL;
@@ -45,13 +52,13 @@ LOCAL void RF24_ce(const bool level)
}
LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t len,
const bool aReadMode)
const bool readMode)
{
uint8_t status;
uint8_t* current = buf;
#if !defined(MY_SOFTSPI)
_SPI.beginTransaction(SPISettings(MY_RF24_SPI_MAX_SPEED, MY_RF24_SPI_DATA_ORDER,
MY_RF24_SPI_DATA_MODE));
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
_SPI.beginTransaction(SPISettings(MY_RF24_SPI_SPEED, RF24_SPI_DATA_ORDER,
RF24_SPI_DATA_MODE));
#endif
RF24_csn(LOW);
// timing
@@ -63,14 +70,14 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t
*ptx++ = cmd;
while ( len-- ) {
if (aReadMode) {
*ptx++ = RF24_NOP;
if (readMode) {
*ptx++ = RF24_CMD_NOP;
} else {
*ptx++ = *current++;
}
}
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
if (aReadMode) {
if (readMode) {
if (size == 2) {
status = *++prx; // result is 2nd byte of receive buffer
} else {
@@ -86,8 +93,8 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t
#else
status = _SPI.transfer(cmd);
while ( len-- ) {
if (aReadMode) {
status = _SPI.transfer(RF24_NOP);
if (readMode) {
status = _SPI.transfer(RF24_CMD_NOP);
if (buf != NULL) {
*current++ = status;
}
@@ -97,7 +104,7 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t
}
#endif
RF24_csn(HIGH);
#if !defined(MY_SOFTSPI)
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
_SPI.endTransaction();
#endif
// timing
@@ -113,86 +120,87 @@ LOCAL uint8_t RF24_spiByteTransfer(const uint8_t cmd)
LOCAL uint8_t RF24_RAW_readByteRegister(const uint8_t cmd)
{
const uint8_t value = RF24_spiMultiByteTransfer(cmd, NULL, 1, true);
RF24_DEBUG(PSTR("RF24:read register, reg=%d, value=%d\n"), cmd & RF24_REGISTER_MASK, value);
RF24_DEBUG(PSTR("RF24:RBR:REG=%d,VAL=%d\n"), cmd & RF24_REGISTER_MASK, value);
return value;
}
LOCAL uint8_t RF24_RAW_writeByteRegister(const uint8_t cmd, uint8_t value)
{
RF24_DEBUG(PSTR("RF24:write register, reg=%d, value=%d\n"), cmd & RF24_REGISTER_MASK, value);
RF24_DEBUG(PSTR("RF24:WBR:REG=%d,VAL=%d\n"), cmd & RF24_REGISTER_MASK, value);
return RF24_spiMultiByteTransfer( cmd , &value, 1, false);
}
LOCAL void RF24_flushRX(void)
{
RF24_DEBUG(PSTR("RF24:flushRX\n"));
RF24_spiByteTransfer(RF24_FLUSH_RX);
RF24_DEBUG(PSTR("RF24:FRX\n"));
RF24_spiByteTransfer(RF24_CMD_FLUSH_RX);
}
LOCAL void RF24_flushTX(void)
{
RF24_DEBUG(PSTR("RF24:flushTX\n"));
RF24_spiByteTransfer(RF24_FLUSH_TX);
RF24_DEBUG(PSTR("RF24:FTX\n"));
RF24_spiByteTransfer(RF24_CMD_FLUSH_TX);
}
LOCAL uint8_t RF24_getStatus(void)
{
return RF24_spiByteTransfer(RF24_NOP);
return RF24_spiByteTransfer(RF24_CMD_NOP);
}
LOCAL uint8_t RF24_getFIFOStatus(void)
{
return RF24_readByteRegister(RF24_FIFO_STATUS);
return RF24_readByteRegister(RF24_REG_FIFO_STATUS);
}
LOCAL void RF24_setChannel(const uint8_t channel)
{
RF24_writeByteRegister(RF24_RF_CH,channel);
RF24_writeByteRegister(RF24_REG_RF_CH,channel);
}
LOCAL void RF24_setRetries(const uint8_t retransmitDelay, const uint8_t retransmitCount)
{
RF24_writeByteRegister(RF24_SETUP_RETR, retransmitDelay << RF24_ARD | retransmitCount << RF24_ARC);
RF24_writeByteRegister(RF24_REG_SETUP_RETR,
retransmitDelay << RF24_ARD | retransmitCount << RF24_ARC);
}
LOCAL void RF24_setAddressWidth(const uint8_t width)
LOCAL void RF24_setAddressWidth(const uint8_t addressWidth)
{
RF24_writeByteRegister(RF24_SETUP_AW, width - 2);
RF24_writeByteRegister(RF24_REG_SETUP_AW, addressWidth - 2);
}
LOCAL void RF24_setRFSetup(const uint8_t RFsetup)
{
RF24_writeByteRegister(RF24_RF_SETUP, RFsetup);
RF24_writeByteRegister(RF24_REG_RF_SETUP, RFsetup);
}
LOCAL void RF24_setFeature(const uint8_t feature)
{
RF24_writeByteRegister(RF24_FEATURE, feature);
RF24_writeByteRegister(RF24_REG_FEATURE, feature);
}
LOCAL void RF24_setPipe(const uint8_t pipe)
{
RF24_writeByteRegister(RF24_EN_RXADDR, pipe);
RF24_writeByteRegister(RF24_REG_EN_RXADDR, pipe);
}
LOCAL void RF24_setAutoACK(const uint8_t pipe)
{
RF24_writeByteRegister(RF24_EN_AA, pipe);
RF24_writeByteRegister(RF24_REG_EN_AA, pipe);
}
LOCAL void RF24_setDynamicPayload(const uint8_t pipe)
{
RF24_writeByteRegister(RF24_DYNPD, pipe);
RF24_writeByteRegister(RF24_REG_DYNPD, pipe);
}
LOCAL void RF24_setRFConfiguration(const uint8_t configuration)
{
RF24_writeByteRegister(RF24_NRF_CONFIG, configuration);
RF24_writeByteRegister(RF24_REG_NRF_CONFIG, configuration);
}
LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t* address, const uint8_t width)
LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t* address, const uint8_t addressWidth)
{
RF24_writeMultiByteRegister(pipe, address, width);
RF24_writeMultiByteRegister(pipe, address, addressWidth);
}
LOCAL void RF24_setPipeLSB(const uint8_t pipe, const uint8_t LSB)
@@ -202,34 +210,34 @@ LOCAL void RF24_setPipeLSB(const uint8_t pipe, const uint8_t LSB)
LOCAL uint8_t RF24_getObserveTX(void)
{
return RF24_readByteRegister(RF24_OBSERVE_TX);
return RF24_readByteRegister(RF24_REG_OBSERVE_TX);
}
LOCAL void RF24_setStatus(const uint8_t status)
{
RF24_writeByteRegister(RF24_STATUS, status);
RF24_writeByteRegister(RF24_REG_STATUS, status);
}
LOCAL void RF24_enableFeatures(void)
{
RF24_RAW_writeByteRegister(RF24_ACTIVATE, 0x73);
RF24_RAW_writeByteRegister(RF24_CMD_ACTIVATE, 0x73);
}
LOCAL void RF24_openWritingPipe(const uint8_t recipient)
{
RF24_DEBUG(PSTR("RF24:OPEN WPIPE,RCPT=%d\n"), recipient); // open writing pipe
RF24_DEBUG(PSTR("RF24:OWP:RCPT=%d\n"), recipient); // open writing pipe
// only write LSB of RX0 and TX pipe
RF24_setPipeLSB(RF24_RX_ADDR_P0, recipient);
RF24_setPipeLSB(RF24_TX_ADDR, recipient);
RF24_setPipeLSB(RF24_REG_RX_ADDR_P0, recipient);
RF24_setPipeLSB(RF24_REG_TX_ADDR, recipient);
}
LOCAL void RF24_startListening(void)
{
RF24_DEBUG(PSTR("RF24:STRT LIS\n")); // start listening
RF24_DEBUG(PSTR("RF24:STL\n")); // start listening
// toggle PRX
RF24_setRFConfiguration(MY_RF24_CONFIGURATION | _BV(RF24_PWR_UP) | _BV(RF24_PRIM_RX) );
// all RX pipe addresses must be unique, therefore skip if node ID is AUTO
if(MY_RF24_NODE_ADDRESS!=AUTO) {
RF24_setPipeLSB(RF24_RX_ADDR_P0, MY_RF24_NODE_ADDRESS);
RF24_setRFConfiguration(RF24_CONFIGURATION | _BV(RF24_PWR_UP) | _BV(RF24_PRIM_RX) );
// all RX pipe addresses must be unique, therefore skip if node ID is RF24_BROADCAST_ADDRESS
if(RF24_NODE_ADDRESS!= RF24_BROADCAST_ADDRESS) {
RF24_setPipeLSB(RF24_REG_RX_ADDR_P0, RF24_NODE_ADDRESS);
}
// start listening
RF24_ce(HIGH);
@@ -237,35 +245,59 @@ LOCAL void RF24_startListening(void)
LOCAL void RF24_stopListening(void)
{
RF24_DEBUG(PSTR("RF24:STP LIS\n")); // stop listening
RF24_DEBUG(PSTR("RF24:SPL\n")); // stop listening
RF24_ce(LOW);
// timing
delayMicroseconds(130);
RF24_setRFConfiguration(MY_RF24_CONFIGURATION | _BV(RF24_PWR_UP) );
RF24_setRFConfiguration(RF24_CONFIGURATION | _BV(RF24_PWR_UP) );
// timing
delayMicroseconds(100);
}
LOCAL void RF24_powerDown(void)
{
RF24_ce(LOW);
RF24_setRFConfiguration(MY_RF24_CONFIGURATION);
RF24_DEBUG(PSTR("RF24:PD\n")); // power down
#if defined(MY_RF24_POWER_PIN)
hwDigitalWrite(MY_RF24_POWER_PIN, LOW);
#endif
}
LOCAL bool RF24_sendMessage(const uint8_t recipient, const void* buf, const uint8_t len)
LOCAL void RF24_powerUp(void)
{
#if defined(MY_RF24_POWER_PIN)
hwDigitalWrite(MY_RF24_POWER_PIN, HIGH);
delay(RF24_POWERUP_DELAY_MS); // allow VCC to settle
#endif
}
LOCAL void RF24_sleep(void)
{
RF24_DEBUG(PSTR("RF24:SLP\n")); // put radio to sleep
RF24_ce(LOW);
RF24_setRFConfiguration(RF24_CONFIGURATION);
}
LOCAL void RF24_standBy(void)
{
RF24_DEBUG(PSTR("RF24:SBY\n")); // put radio to standby
RF24_ce(LOW);
RF24_setRFConfiguration(RF24_CONFIGURATION | _BV(RF24_PWR_UP));
}
LOCAL bool RF24_sendMessage(const uint8_t recipient, const void* buf, const uint8_t len,
const bool noACK)
{
uint8_t RF24_status;
RF24_stopListening();
RF24_openWritingPipe( recipient );
RF24_DEBUG(PSTR("RF24:SND:TO=%d,LEN=%d\n"),recipient,len); // send message
RF24_DEBUG(PSTR("RF24:TXM:TO=%d,LEN=%d\n"),recipient,len); // send message
// 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
// AutoACK is disabled on the broadcasting pipe - NO_ACK prevents resending
RF24_spiMultiByteTransfer(recipient == BROADCAST_ADDRESS ? RF24_WRITE_TX_PAYLOAD_NO_ACK :
RF24_WRITE_TX_PAYLOAD, (uint8_t*)buf, len, false );
// go, TX starts after ~10us
RF24_spiMultiByteTransfer((recipient == RF24_BROADCAST_ADDRESS ||
noACK) ? RF24_CMD_WRITE_TX_PAYLOAD_NO_ACK :
RF24_CMD_WRITE_TX_PAYLOAD, (uint8_t*)buf, len, false );
// go, TX starts after ~10us, CE high also enables PA+LNA on supported HW
RF24_ce(HIGH);
// timeout counter to detect HW issues
uint16_t timeout = 0xFFFF;
@@ -279,20 +311,20 @@ LOCAL bool RF24_sendMessage(const uint8_t recipient, const void* buf, const uint
// Max retries exceeded
if(RF24_status & _BV(RF24_MAX_RT)) {
// flush packet
RF24_DEBUG(PSTR("!RF24:SND:MAX_RT\n")); // max retries, no ACK
RF24_DEBUG(PSTR("!RF24:TXM:MAX_RT\n")); // max retries, no ACK
RF24_flushTX();
}
RF24_startListening();
// true if message sent
return (RF24_status & _BV(RF24_TX_DS));
return (RF24_status & _BV(RF24_TX_DS) || noACK);
}
LOCAL uint8_t RF24_getDynamicPayloadSize(void)
{
uint8_t result = RF24_spiMultiByteTransfer(RF24_READ_RX_PL_WID, NULL, 1, true);
uint8_t result = RF24_spiMultiByteTransfer(RF24_CMD_READ_RX_PL_WID, NULL, 1, true);
// check if payload size invalid
if(result > 32) {
RF24_DEBUG(PSTR("!RF24:GDP:PAYL LEN INVALID=%d\n"),result); // payload len invalid
RF24_DEBUG(PSTR("!RF24:GDP:PYL INV\n")); // payload len invalid
RF24_flushRX();
result = 0;
}
@@ -308,8 +340,8 @@ LOCAL bool RF24_isDataAvailable(void)
LOCAL uint8_t RF24_readMessage(void* buf)
{
const uint8_t len = RF24_getDynamicPayloadSize();
RF24_DEBUG(PSTR("RF24:RDM:MSG LEN=%d\n"), len); // read message
RF24_spiMultiByteTransfer(RF24_READ_RX_PAYLOAD,(uint8_t*)buf,len,true);
RF24_DEBUG(PSTR("RF24:RXM:LEN=%d\n"), len); // read message
RF24_spiMultiByteTransfer(RF24_CMD_READ_RX_PAYLOAD,(uint8_t*)buf,len,true);
// clear RX interrupt
RF24_setStatus(_BV(RF24_RX_DR));
return len;
@@ -317,8 +349,8 @@ LOCAL uint8_t RF24_readMessage(void* buf)
LOCAL void RF24_setNodeAddress(const uint8_t address)
{
if(address!=AUTO) {
MY_RF24_NODE_ADDRESS = address;
if(address!= RF24_BROADCAST_ADDRESS) {
RF24_NODE_ADDRESS = address;
// enable node pipe
RF24_setPipe(_BV(RF24_ERX_P0 + RF24_BROADCAST_PIPE) | _BV(RF24_ERX_P0));
// enable autoACK on pipe 0
@@ -328,14 +360,47 @@ LOCAL void RF24_setNodeAddress(const uint8_t address)
LOCAL uint8_t RF24_getNodeID(void)
{
return MY_RF24_NODE_ADDRESS;
return RF24_NODE_ADDRESS;
}
LOCAL bool RF24_sanityCheck(void)
{
// detect HW defect, configuration errors or interrupted SPI line, CE disconnect cannot be detected
return (RF24_readByteRegister(RF24_RF_SETUP) == MY_RF24_RF_SETUP) & (RF24_readByteRegister(
RF24_RF_CH) == MY_RF24_CHANNEL);
return (RF24_readByteRegister(RF24_REG_RF_SETUP) == RF24_RF_SETUP) & (RF24_readByteRegister(
RF24_REG_RF_CH) == MY_RF24_CHANNEL);
}
LOCAL int16_t RF24_getTxPowerLevel(void)
{
// in dBm
return (int16_t)((-6) * (3-((RF24_readByteRegister(RF24_REG_RF_SETUP) >> 1) & 3)));
}
LOCAL uint8_t RF24_getTxPowerPercent(void)
{
// report TX level in %, 0 (LOW) = 25%, 3 (MAX) = 100
const uint8_t result = 25 + (((RF24_readByteRegister(RF24_REG_RF_SETUP) >> 2) & 3) * 25);
return result;
}
LOCAL bool RF24_setTxPowerLevel(const uint8_t newPowerLevel)
{
const uint8_t registerContent = RF24_readByteRegister(RF24_REG_RF_SETUP);
RF24_writeByteRegister(RF24_REG_RF_SETUP, (registerContent & 0xF9) | ((newPowerLevel & 3) << 1));
RF24_DEBUG(PSTR("RF24:STX:LEVEL=%d\n"), newPowerLevel);
return true;
}
LOCAL bool RF24_setTxPowerPercent(const uint8_t newPowerPercent)
{
const uint8_t newPowerLevel = static_cast<uint8_t>(RF24_MIN_POWER_LEVEL + (RF24_MAX_POWER_LEVEL
- RF24_MIN_POWER_LEVEL) * (newPowerPercent / 100.0f));
return RF24_setTxPowerLevel(newPowerLevel);
}
LOCAL int16_t RF24_getSendingRSSI(void)
{
// calculate pseudo-RSSI based on retransmission counter (ARC)
// min -104dBm at 250kBps
// Arbitrary definition: ARC 0 == -29, ARC 15 = -104
return static_cast<int16_t>(-29 - (8 * (RF24_getObserveTX() & 0xF)));
}
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
@@ -381,19 +446,23 @@ LOCAL void RF24_registerReceiveCallback(RF24_receiveCallbackType cb)
LOCAL bool RF24_initialize(void)
{
// prevent warning
(void)RF24_getObserveTX;
// Initialize pins
hwPinMode(MY_RF24_CE_PIN,OUTPUT);
hwPinMode(MY_RF24_CS_PIN,OUTPUT);
RF24_DEBUG(PSTR("RF24:INIT\n"));
RF24_DEBUG(PSTR("RF24:INIT:PIN,CE=%d,CS=%d\n"), MY_RF24_CE_PIN, MY_RF24_CS_PIN);
// Initialize pins & HW
#if defined(MY_RF24_POWER_PIN)
hwPinMode(MY_RF24_POWER_PIN, OUTPUT);
#endif
RF24_powerUp();
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
hwPinMode(MY_RF24_IRQ_PIN,INPUT);
#endif
// Initialize SPI
_SPI.begin();
hwPinMode(MY_RF24_CE_PIN, OUTPUT);
hwPinMode(MY_RF24_CS_PIN, OUTPUT);
RF24_ce(LOW);
RF24_csn(HIGH);
// Initialize SPI
_SPI.begin();
#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,
@@ -403,7 +472,7 @@ LOCAL bool RF24_initialize(void)
attachInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN), RF24_irqHandler, FALLING);
#endif
// CRC and power up
RF24_setRFConfiguration(MY_RF24_CONFIGURATION | _BV(RF24_PWR_UP)) ;
RF24_setRFConfiguration(RF24_CONFIGURATION | _BV(RF24_PWR_UP)) ;
// settle >2ms
delay(5);
// set address width
@@ -413,11 +482,11 @@ LOCAL bool RF24_initialize(void)
// set channel
RF24_setChannel(MY_RF24_CHANNEL);
// set data rate and pa level
RF24_setRFSetup(MY_RF24_RF_SETUP);
RF24_setRFSetup(RF24_RF_SETUP);
// toggle features (necessary on some clones and non-P versions)
RF24_enableFeatures();
// enable ACK payload and dynamic payload
RF24_setFeature(MY_RF24_FEATURE);
RF24_setFeature(RF24_FEATURE);
// sanity check (this function is P/non-P independent)
if (!RF24_sanityCheck()) {
RF24_DEBUG(PSTR("!RF24:INIT:SANCHK FAIL\n")); // sanity check failed, check wiring or replace module
@@ -430,12 +499,12 @@ LOCAL bool RF24_initialize(void)
// enable dynamic payloads on used pipes
RF24_setDynamicPayload(_BV(RF24_DPL_P0 + RF24_BROADCAST_PIPE) | _BV(RF24_DPL_P0));
// listen to broadcast pipe
MY_RF24_BASE_ADDR[0] = BROADCAST_ADDRESS;
RF24_setPipeAddress(RF24_RX_ADDR_P0 + RF24_BROADCAST_PIPE, (uint8_t*)&MY_RF24_BASE_ADDR,
RF24_BASE_ID[0] = RF24_BROADCAST_ADDRESS;
RF24_setPipeAddress(RF24_REG_RX_ADDR_P0 + RF24_BROADCAST_PIPE, (uint8_t*)&RF24_BASE_ID,
RF24_BROADCAST_PIPE > 1 ? 1 : MY_RF24_ADDR_WIDTH);
// pipe 0, set full address, later only LSB is updated
RF24_setPipeAddress(RF24_RX_ADDR_P0, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
RF24_setPipeAddress(RF24_TX_ADDR, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
RF24_setPipeAddress(RF24_REG_RX_ADDR_P0, (uint8_t*)&RF24_BASE_ID, MY_RF24_ADDR_WIDTH);
RF24_setPipeAddress(RF24_REG_TX_ADDR, (uint8_t*)&RF24_BASE_ID, MY_RF24_ADDR_WIDTH);
// reset FIFO
RF24_flushRX();
RF24_flushTX();