mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 20:09:14 +03:00
gatewayTransportSend
This commit is contained in:
166
lib/MySensors/hal/transport/RF24/MyTransportRF24.cpp
Normal file
166
lib/MySensors/hal/transport/RF24/MyTransportRF24.cpp
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2019 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "hal/transport/RF24/driver/RF24.h"
|
||||
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
#include "drivers/CircularBuffer/CircularBuffer.h"
|
||||
|
||||
typedef struct _transportQueuedMessage {
|
||||
uint8_t m_len; // Length of the data
|
||||
uint8_t m_data[MAX_MESSAGE_SIZE]; // The raw data
|
||||
} transportQueuedMessage;
|
||||
|
||||
/** Buffer to store queued messages in. */
|
||||
static transportQueuedMessage transportRxQueueStorage[MY_RX_MESSAGE_BUFFER_SIZE];
|
||||
/** Circular buffer, which uses the transportRxQueueStorage and administers stored messages. */
|
||||
static CircularBuffer<transportQueuedMessage> transportRxQueue(transportRxQueueStorage,
|
||||
MY_RX_MESSAGE_BUFFER_SIZE);
|
||||
|
||||
static volatile uint8_t transportLostMessageCount = 0;
|
||||
|
||||
static void transportRxCallback(void)
|
||||
{
|
||||
// Called for each message received by radio, from interrupt context.
|
||||
// This function _must_ call RF24_readMessage() to de-assert interrupt line!
|
||||
if (!transportRxQueue.full()) {
|
||||
transportQueuedMessage* msg = transportRxQueue.getFront();
|
||||
msg->m_len = RF24_readMessage(msg->m_data); // Read payload & clear RX_DR
|
||||
(void)transportRxQueue.pushFront(msg);
|
||||
} else {
|
||||
// Queue is full. Discard message.
|
||||
(void)RF24_readMessage(NULL); // Read payload & clear RX_DR
|
||||
// Keep track of messages lost. Max 255, prevent wrapping.
|
||||
if (transportLostMessageCount < 255) {
|
||||
++transportLostMessageCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool transportInit(void)
|
||||
{
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
RF24_registerReceiveCallback( transportRxCallback );
|
||||
#endif
|
||||
return RF24_initialize();
|
||||
}
|
||||
|
||||
void transportSetAddress(const uint8_t address)
|
||||
{
|
||||
RF24_setNodeAddress(address);
|
||||
RF24_startListening();
|
||||
}
|
||||
|
||||
uint8_t transportGetAddress(void)
|
||||
{
|
||||
return RF24_getNodeID();
|
||||
}
|
||||
|
||||
bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
|
||||
{
|
||||
return RF24_sendMessage(to, data, len, noACK);
|
||||
}
|
||||
|
||||
bool transportDataAvailable(void)
|
||||
{
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
(void)RF24_isDataAvailable; // Prevent 'defined but not used' warning
|
||||
return !transportRxQueue.empty();
|
||||
#else
|
||||
return RF24_isDataAvailable();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool transportSanityCheck(void)
|
||||
{
|
||||
return RF24_sanityCheck();
|
||||
}
|
||||
|
||||
uint8_t transportReceive(void *data)
|
||||
{
|
||||
uint8_t len = 0;
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
transportQueuedMessage* msg = transportRxQueue.getBack();
|
||||
if (msg) {
|
||||
len = msg->m_len;
|
||||
(void)memcpy(data, msg->m_data, len);
|
||||
(void)transportRxQueue.popBack();
|
||||
}
|
||||
#else
|
||||
len = RF24_readMessage(data);
|
||||
#endif
|
||||
return len;
|
||||
}
|
||||
|
||||
void transportSleep(void)
|
||||
{
|
||||
RF24_sleep();
|
||||
}
|
||||
|
||||
void transportStandBy(void)
|
||||
{
|
||||
RF24_standBy();
|
||||
}
|
||||
|
||||
void transportPowerDown(void)
|
||||
{
|
||||
RF24_powerDown();
|
||||
}
|
||||
|
||||
void transportPowerUp(void)
|
||||
{
|
||||
RF24_powerUp();
|
||||
}
|
||||
|
||||
int16_t transportGetSendingRSSI(void)
|
||||
{
|
||||
return RF24_getSendingRSSI();
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingRSSI(void)
|
||||
{
|
||||
// not available, only bool RPD
|
||||
return INVALID_RSSI;
|
||||
}
|
||||
|
||||
int16_t transportGetSendingSNR(void)
|
||||
{
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingSNR(void)
|
||||
{
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerPercent(void)
|
||||
{
|
||||
return static_cast<int16_t>(RF24_getTxPowerPercent());
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerLevel(void)
|
||||
{
|
||||
return static_cast<int16_t>(RF24_getTxPowerLevel());
|
||||
}
|
||||
|
||||
bool transportSetTxPowerPercent(const uint8_t powerPercent)
|
||||
{
|
||||
return RF24_setTxPowerPercent(powerPercent);
|
||||
}
|
||||
577
lib/MySensors/hal/transport/RF24/driver/RF24.cpp
Normal file
577
lib/MySensors/hal/transport/RF24/driver/RF24.cpp
Normal file
@@ -0,0 +1,577 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2019 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* 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) 2017 Olivier Mauti <olivier@mysensors.org>
|
||||
*/
|
||||
|
||||
#include "RF24.h"
|
||||
|
||||
// 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;
|
||||
#endif
|
||||
|
||||
#if defined(__linux__)
|
||||
uint8_t RF24_spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes)
|
||||
uint8_t RF24_spi_txbuff[32+1]
|
||||
; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)
|
||||
#endif
|
||||
|
||||
LOCAL void RF24_csn(const bool level)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
(void)level;
|
||||
#else
|
||||
hwDigitalWrite(MY_RF24_CS_PIN, level);
|
||||
#endif
|
||||
}
|
||||
|
||||
LOCAL void RF24_ce(const bool level)
|
||||
{
|
||||
hwDigitalWrite(MY_RF24_CE_PIN, level);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t *buf, uint8_t len,
|
||||
const bool readMode)
|
||||
{
|
||||
uint8_t status;
|
||||
uint8_t *current = buf;
|
||||
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
|
||||
RF24_SPI.beginTransaction(SPISettings(MY_RF24_SPI_SPEED, RF24_SPI_DATA_ORDER,
|
||||
RF24_SPI_DATA_MODE));
|
||||
#endif
|
||||
|
||||
RF24_csn(LOW);
|
||||
// timing
|
||||
delayMicroseconds(10);
|
||||
#ifdef __linux__
|
||||
uint8_t *prx = RF24_spi_rxbuff;
|
||||
uint8_t *ptx = RF24_spi_txbuff;
|
||||
uint8_t size = len + 1; // Add register value to transmit buffer
|
||||
|
||||
*ptx++ = cmd;
|
||||
while ( len-- ) {
|
||||
if (readMode) {
|
||||
*ptx++ = RF24_CMD_NOP;
|
||||
} else {
|
||||
*ptx++ = *current++;
|
||||
}
|
||||
}
|
||||
RF24_SPI.transfernb( (char *) RF24_spi_txbuff, (char *) RF24_spi_rxbuff, size);
|
||||
if (readMode) {
|
||||
if (size == 2) {
|
||||
status = *++prx; // result is 2nd byte of receive buffer
|
||||
} else {
|
||||
status = *prx++; // status is 1st byte of receive buffer
|
||||
// decrement before to skip status byte
|
||||
while (--size && (buf != NULL)) {
|
||||
*buf++ = *prx++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
status = *prx; // status is 1st byte of receive buffer
|
||||
}
|
||||
#else
|
||||
status = RF24_SPI.transfer(cmd);
|
||||
while ( len-- ) {
|
||||
if (readMode) {
|
||||
status = RF24_SPI.transfer(RF24_CMD_NOP);
|
||||
if (buf != NULL) {
|
||||
*current++ = status;
|
||||
}
|
||||
} else {
|
||||
(void)RF24_SPI.transfer(*current++);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
RF24_csn(HIGH);
|
||||
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
|
||||
RF24_SPI.endTransaction();
|
||||
#endif
|
||||
// timing
|
||||
delayMicroseconds(10);
|
||||
return status;
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_spiByteTransfer(const uint8_t cmd)
|
||||
{
|
||||
return RF24_spiMultiByteTransfer(cmd, NULL, 0, false);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_RAW_readByteRegister(const uint8_t cmd)
|
||||
{
|
||||
const uint8_t value = RF24_spiMultiByteTransfer(cmd, NULL, 1, true);
|
||||
RF24_DEBUG(PSTR("RF24:RBR:REG=%" PRIu8 ",VAL=%" PRIu8 "\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:WBR:REG=%" PRIu8 ",VAL=%" PRIu8 "\n"), cmd & RF24_REGISTER_MASK, value);
|
||||
return RF24_spiMultiByteTransfer( cmd, &value, 1, false);
|
||||
}
|
||||
|
||||
LOCAL void RF24_flushRX(void)
|
||||
{
|
||||
RF24_DEBUG(PSTR("RF24:FRX\n"));
|
||||
RF24_spiByteTransfer(RF24_CMD_FLUSH_RX);
|
||||
}
|
||||
|
||||
LOCAL void RF24_flushTX(void)
|
||||
{
|
||||
RF24_DEBUG(PSTR("RF24:FTX\n"));
|
||||
RF24_spiByteTransfer(RF24_CMD_FLUSH_TX);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getStatus(void)
|
||||
{
|
||||
return RF24_spiByteTransfer(RF24_CMD_NOP);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getFIFOStatus(void)
|
||||
{
|
||||
return RF24_readByteRegister(RF24_REG_FIFO_STATUS);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setChannel(const uint8_t channel)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_RF_CH,channel);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setRetries(const uint8_t retransmitDelay, const uint8_t retransmitCount)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_SETUP_RETR,
|
||||
retransmitDelay << RF24_ARD | retransmitCount << RF24_ARC);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setAddressWidth(const uint8_t addressWidth)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_SETUP_AW, addressWidth - 2);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setRFSetup(const uint8_t RFsetup)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_RF_SETUP, RFsetup);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setFeature(const uint8_t feature)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_FEATURE, feature);
|
||||
|
||||
if (RF24_getFeature() != feature) {
|
||||
// toggle features (necessary on some clones and non-P versions)
|
||||
RF24_enableFeatures();
|
||||
RF24_writeByteRegister(RF24_REG_FEATURE, feature);
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getFeature(void)
|
||||
{
|
||||
return RF24_readByteRegister(RF24_REG_FEATURE);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setPipe(const uint8_t pipe)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_EN_RXADDR, pipe);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setAutoACK(const uint8_t pipe)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_EN_AA, pipe);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setDynamicPayload(const uint8_t pipe)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_DYNPD, pipe);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setRFConfiguration(const uint8_t configuration)
|
||||
{
|
||||
RF24_writeByteRegister(RF24_REG_NRF_CONFIG, configuration);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t *address, const uint8_t addressWidth)
|
||||
{
|
||||
RF24_writeMultiByteRegister(pipe, address, addressWidth);
|
||||
}
|
||||
|
||||
LOCAL void RF24_setPipeLSB(const uint8_t pipe, const uint8_t LSB)
|
||||
{
|
||||
RF24_writeByteRegister(pipe, LSB);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getObserveTX(void)
|
||||
{
|
||||
return RF24_readByteRegister(RF24_REG_OBSERVE_TX);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_setStatus(const uint8_t status)
|
||||
{
|
||||
return RF24_writeByteRegister(RF24_REG_STATUS, status);
|
||||
}
|
||||
|
||||
LOCAL void RF24_enableFeatures(void)
|
||||
{
|
||||
RF24_RAW_writeByteRegister(RF24_CMD_ACTIVATE, 0x73);
|
||||
}
|
||||
|
||||
LOCAL void RF24_openWritingPipe(const uint8_t recipient)
|
||||
{
|
||||
RF24_DEBUG(PSTR("RF24:OWP:RCPT=%" PRIu8 "\n"), recipient); // open writing pipe
|
||||
// only write LSB of RX0 and TX pipe
|
||||
RF24_setPipeLSB(RF24_REG_RX_ADDR_P0, recipient);
|
||||
RF24_setPipeLSB(RF24_REG_TX_ADDR, recipient);
|
||||
}
|
||||
|
||||
LOCAL void RF24_startListening(void)
|
||||
{
|
||||
RF24_DEBUG(PSTR("RF24:STL\n")); // start listening
|
||||
// toggle PRX
|
||||
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);
|
||||
}
|
||||
|
||||
LOCAL void RF24_stopListening(void)
|
||||
{
|
||||
RF24_DEBUG(PSTR("RF24:SPL\n")); // stop listening
|
||||
RF24_ce(LOW);
|
||||
// timing
|
||||
delayMicroseconds(130);
|
||||
RF24_setRFConfiguration(RF24_CONFIGURATION | _BV(RF24_PWR_UP) );
|
||||
// timing
|
||||
delayMicroseconds(100);
|
||||
}
|
||||
|
||||
LOCAL void RF24_powerDown(void)
|
||||
{
|
||||
#if defined(MY_RF24_POWER_PIN)
|
||||
hwDigitalWrite(MY_RF24_POWER_PIN, LOW);
|
||||
#endif
|
||||
}
|
||||
|
||||
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));
|
||||
// There must be a delay of up to 4.5ms after the nRF24L01+ leaves power down mode before the CE is set high.
|
||||
delayMicroseconds(4500);
|
||||
}
|
||||
|
||||
|
||||
LOCAL bool RF24_sendMessage(const uint8_t recipient, const void *buf, const uint8_t len,
|
||||
const bool noACK)
|
||||
{
|
||||
RF24_stopListening();
|
||||
RF24_openWritingPipe(recipient);
|
||||
RF24_DEBUG(PSTR("RF24:TXM:TO=%" PRIu8 ",LEN=%" PRIu8 "\n"), recipient, len); // send message
|
||||
// flush TX FIFO
|
||||
RF24_flushTX();
|
||||
if (noACK) {
|
||||
// noACK messages are only sent once
|
||||
RF24_setRetries(RF24_SET_ARD, 0);
|
||||
}
|
||||
// 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
|
||||
(void)RF24_spiMultiByteTransfer(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;
|
||||
while (!(RF24_getStatus() & (_BV(RF24_MAX_RT) | _BV(RF24_TX_DS))) && timeout--) {
|
||||
doYield();
|
||||
}
|
||||
// timeout value after successful TX on 16Mhz AVR ~ 65500, i.e. msg is transmitted after ~36 loop cycles
|
||||
RF24_ce(LOW);
|
||||
// reset interrupts
|
||||
const uint8_t RF24_status = RF24_setStatus(_BV(RF24_RX_DR) | _BV(RF24_TX_DS) | _BV(RF24_MAX_RT));
|
||||
// Max retries exceeded
|
||||
if (RF24_status & _BV(RF24_MAX_RT)) {
|
||||
// flush packet
|
||||
RF24_DEBUG(PSTR("?RF24:TXM:MAX_RT\n")); // max retries (normal messages) and noACK messages
|
||||
RF24_flushTX();
|
||||
}
|
||||
if (noACK) {
|
||||
RF24_setRetries(RF24_SET_ARD, RF24_SET_ARC);
|
||||
}
|
||||
RF24_startListening();
|
||||
// true if message sent
|
||||
return (RF24_status & _BV(RF24_TX_DS) || noACK);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getDynamicPayloadSize(void)
|
||||
{
|
||||
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:PYL INV\n")); // payload len invalid
|
||||
RF24_flushRX();
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
LOCAL bool RF24_isDataAvailable(void)
|
||||
{
|
||||
// prevent debug message flooding
|
||||
#if defined(MY_DEBUG_VERBOSE_RF24)
|
||||
const uint8_t value = RF24_spiMultiByteTransfer(RF24_CMD_READ_REGISTER | (RF24_REGISTER_MASK &
|
||||
(RF24_REG_FIFO_STATUS)), NULL, 1, true);
|
||||
return (bool)(!(value & _BV(RF24_RX_EMPTY)));
|
||||
#else
|
||||
return (bool)(!(RF24_getFIFOStatus() & _BV(RF24_RX_EMPTY)) );
|
||||
#endif
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_readMessage(void *buf)
|
||||
{
|
||||
const uint8_t len = RF24_getDynamicPayloadSize();
|
||||
RF24_DEBUG(PSTR("RF24:RXM:LEN=%" PRIu8 "\n"), len); // read message
|
||||
RF24_spiMultiByteTransfer(RF24_CMD_READ_RX_PAYLOAD, (uint8_t *)buf, len, true);
|
||||
// clear RX interrupt
|
||||
(void)RF24_setStatus(_BV(RF24_RX_DR));
|
||||
return len;
|
||||
}
|
||||
|
||||
LOCAL void RF24_setNodeAddress(const uint8_t 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
|
||||
RF24_setAutoACK(_BV(RF24_ENAA_P0));
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL uint8_t RF24_getNodeID(void)
|
||||
{
|
||||
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_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=%" PRIu8 "\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)));
|
||||
}
|
||||
|
||||
LOCAL void RF24_enableConstantCarrierWave(void)
|
||||
{
|
||||
RF24_standBy();
|
||||
RF24_setRFSetup(RF24_RF_SETUP | _BV(RF24_CONT_WAVE) | _BV(RF24_PLL_LOCK) );
|
||||
RF24_ce(HIGH);
|
||||
}
|
||||
|
||||
LOCAL void RF24_disableConstantCarrierWave(void)
|
||||
{
|
||||
RF24_ce(LOW);
|
||||
RF24_setRFSetup(RF24_RF_SETUP);
|
||||
}
|
||||
|
||||
LOCAL bool RF24_getReceivedPowerDetector(void)
|
||||
{
|
||||
// nRF24L01+ only. nRF24L01 contains a carrier detect function (same register & bit) which works
|
||||
// slightly different and takes at least 128us to become active.
|
||||
return (RF24_readByteRegister(RF24_REG_RPD) & _BV(RF24_RPD)) != 0;
|
||||
}
|
||||
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
LOCAL void IRQ_HANDLER_ATTR RF24_irqHandler(void)
|
||||
{
|
||||
if (RF24_receiveCallback) {
|
||||
#if defined(MY_GATEWAY_SERIAL) && !defined(__linux__)
|
||||
// Will stay for a while (several 100us) in this interrupt handler. Any interrupts from serial
|
||||
// rx coming in during our stay will not be handled and will cause characters to be lost.
|
||||
// As a workaround we re-enable interrupts to allow nested processing of other interrupts.
|
||||
// Our own handler is disconnected to prevent recursive calling of this handler.
|
||||
detachInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN));
|
||||
interrupts();
|
||||
#endif
|
||||
// Read FIFO until empty.
|
||||
// Procedure acc. to datasheet (pg. 63):
|
||||
// 1.Read payload, 2.Clear RX_DR IRQ, 3.Read FIFO_status, 4.Repeat when more data available.
|
||||
// Datasheet (ch. 8.5) states, that the nRF de-asserts IRQ after reading STATUS.
|
||||
|
||||
#if defined(__linux__)
|
||||
// Start checking if RX-FIFO is not empty, as we might end up here from an interrupt
|
||||
// for a message we've already read.
|
||||
if (RF24_isDataAvailable()) {
|
||||
do {
|
||||
RF24_receiveCallback(); // Must call RF24_readMessage(), which will clear RX_DR IRQ !
|
||||
} while (RF24_isDataAvailable());
|
||||
} else {
|
||||
// Occasionally interrupt is triggered but no data is available - clear RX interrupt only
|
||||
RF24_setStatus(_BV(RF24_RX_DR));
|
||||
logNotice("RF24: Recovered from a bad interrupt trigger.\n");
|
||||
}
|
||||
#else
|
||||
// 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_isDataAvailable()) {
|
||||
RF24_receiveCallback(); // Must call RF24_readMessage(), which will clear RX_DR IRQ !
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MY_GATEWAY_SERIAL) && !defined(__linux__)
|
||||
// Restore our interrupt handler.
|
||||
noInterrupts();
|
||||
attachInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN), RF24_irqHandler, FALLING);
|
||||
#endif
|
||||
} else {
|
||||
// clear RX interrupt
|
||||
RF24_setStatus(_BV(RF24_RX_DR));
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL void RF24_registerReceiveCallback(RF24_receiveCallbackType cb)
|
||||
{
|
||||
MY_CRITICAL_SECTION {
|
||||
RF24_receiveCallback = cb;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
LOCAL bool RF24_initialize(void)
|
||||
{
|
||||
RF24_DEBUG(PSTR("RF24:INIT:PIN,CE=%" PRIu8 ",CS=%" PRIu8 "\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
|
||||
hwPinMode(MY_RF24_CE_PIN, OUTPUT);
|
||||
#if !defined(__linux__)
|
||||
hwPinMode(MY_RF24_CS_PIN, OUTPUT);
|
||||
#endif
|
||||
RF24_ce(LOW);
|
||||
RF24_csn(HIGH);
|
||||
|
||||
// Initialize SPI
|
||||
RF24_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,
|
||||
// therefore it is unsafe to use MY_RF24_IRQ_PIN with ESP8266/SoftSPI!
|
||||
RF24_SPI.usingInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN));
|
||||
// attach interrupt
|
||||
attachInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN), RF24_irqHandler, FALLING);
|
||||
#endif
|
||||
// power up and standby
|
||||
RF24_standBy();
|
||||
// set address width
|
||||
RF24_setAddressWidth(MY_RF24_ADDR_WIDTH);
|
||||
// auto retransmit delay 1500us, auto retransmit count 15
|
||||
RF24_setRetries(RF24_SET_ARD, RF24_SET_ARC);
|
||||
// set channel
|
||||
RF24_setChannel(MY_RF24_CHANNEL);
|
||||
// set data rate and pa level
|
||||
RF24_setRFSetup(RF24_RF_SETUP);
|
||||
// enable ACK payload and dynamic payload
|
||||
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
|
||||
return false;
|
||||
}
|
||||
// enable broadcasting pipe
|
||||
RF24_setPipe(_BV(RF24_ERX_P0 + RF24_BROADCAST_PIPE));
|
||||
// disable AA on all pipes, activate when node pipe set
|
||||
RF24_setAutoACK(0x00);
|
||||
// enable dynamic payloads on used pipes
|
||||
RF24_setDynamicPayload(_BV(RF24_DPL_P0 + RF24_BROADCAST_PIPE) | _BV(RF24_DPL_P0));
|
||||
// listen to broadcast pipe
|
||||
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_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();
|
||||
// reset interrupts
|
||||
RF24_setStatus(_BV(RF24_TX_DS) | _BV(RF24_MAX_RT) | _BV(RF24_RX_DR));
|
||||
return true;
|
||||
}
|
||||
407
lib/MySensors/hal/transport/RF24/driver/RF24.h
Normal file
407
lib/MySensors/hal/transport/RF24/driver/RF24.h
Normal file
@@ -0,0 +1,407 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2019 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* 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) 2017 Olivier Mauti <olivier@mysensors.org>
|
||||
*
|
||||
* Definitions for Nordic nRF24L01+ radios:
|
||||
* https://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24L01P
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file RF24.h
|
||||
*
|
||||
* @defgroup RF24grp RF24
|
||||
* @ingroup internals
|
||||
* @{
|
||||
*
|
||||
* RF24 driver-related log messages, format: [!]SYSTEM:[SUB SYSTEM:]MESSAGE
|
||||
* - [!] Exclamation mark is prepended in case of error
|
||||
*
|
||||
* |E| SYS | SUB | Message | Comment
|
||||
* |-|------|------|----------------------|---------------------------------------------------------------------
|
||||
* | | RF24 | INIT | PIN,CE=%%d,CS=%%d | Initialise RF24 radio, pin configuration: chip enable (CE), chip select (CS)
|
||||
* |!| RF24 | INIT | SANCHK FAIL | Sanity check failed, check wiring or replace module
|
||||
* | | RF24 | SPP | PCT=%%d,TX LEVEL=%%d | Set TX level, input TX percent (PCT)
|
||||
* | | RF24 | RBR | REG=%%d,VAL=%%d | Read register (REG), value=(VAL)
|
||||
* | | RF24 | WBR | REG=%%d,VAL=%%d | Write register (REG), value=(VAL)
|
||||
* | | RF24 | FRX | | Flush RX buffer
|
||||
* | | RF24 | FTX | | Flush TX buffer
|
||||
* | | RF24 | OWP | RCPT=%%d | Open writing pipe, recipient=(RCPT)
|
||||
* | | RF24 | STL | | Start listening
|
||||
* | | RF24 | SPL | | Stop listening
|
||||
* | | RF24 | SLP | | Set radio to sleep
|
||||
* | | RF24 | SBY | | Set radio to standby
|
||||
* | | RF24 | TXM | TO=%%d,LEN=%%d | Transmit message to=(TO), length=(LEN)
|
||||
* |!| RF24 | TXM | MAX_RT | Max TX retries, no ACK received
|
||||
* |!| RF24 | GDP | PYL INV | Invalid payload size
|
||||
* | | RF24 | RXM | LEN=%%d | Read message, length=(LEN)
|
||||
* | | RF24 | STX | LEVEL=%%d | Set TX level, level=(LEVEL)
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __RF24_H__
|
||||
#define __RF24_H__
|
||||
|
||||
#include "RF24registers.h"
|
||||
|
||||
#if !defined(RF24_SPI)
|
||||
#define RF24_SPI hwSPI //!< default SPI
|
||||
#endif
|
||||
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
#define DEFAULT_RF24_CE_PIN (9) //!< DEFAULT_RF24_CE_PIN
|
||||
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||
#define DEFAULT_RF24_CE_PIN (4) //!< DEFAULT_RF24_CE_PIN
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#define DEFAULT_RF24_CE_PIN (17) //!< DEFAULT_RF24_CE_PIN
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#define DEFAULT_RF24_CE_PIN (27) //!< DEFAULT_RF24_CE_PIN
|
||||
#elif defined(LINUX_ARCH_RASPBERRYPI)
|
||||
#define DEFAULT_RF24_CE_PIN (22) //!< DEFAULT_RF24_CE_PIN
|
||||
//#define DEFAULT_RF24_CS_PIN (24) //!< DEFAULT_RF24_CS_PIN
|
||||
#elif defined(ARDUINO_ARCH_STM32F1)
|
||||
#define DEFAULT_RF24_CE_PIN (PB0) //!< DEFAULT_RF24_CE_PIN
|
||||
#elif defined(TEENSYDUINO)
|
||||
#define DEFAULT_RF24_CE_PIN (9) //!< DEFAULT_RF24_CE_PIN
|
||||
#else
|
||||
#define DEFAULT_RF24_CE_PIN (9) //!< DEFAULT_RF24_CE_PIN
|
||||
#endif
|
||||
|
||||
#define DEFAULT_RF24_CS_PIN (SS) //!< DEFAULT_RF24_CS_PIN
|
||||
|
||||
|
||||
#define LOCAL static //!< static
|
||||
|
||||
// SPI settings
|
||||
#define RF24_SPI_DATA_ORDER MSBFIRST //!< RF24_SPI_DATA_ORDER
|
||||
#define RF24_SPI_DATA_MODE SPI_MODE0 //!< RF24_SPI_DATA_MODE
|
||||
|
||||
#define RF24_BROADCAST_ADDRESS (255u) //!< RF24_BROADCAST_ADDRESS
|
||||
|
||||
// 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
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
#define RF24_CONFIGURATION (uint8_t) ((RF24_CRC_16 << 2) | (1 << RF24_MASK_TX_DS) | (1 << RF24_MASK_MAX_RT)) //!< MY_RF24_CONFIGURATION
|
||||
#else
|
||||
#define RF24_CONFIGURATION (uint8_t) (RF24_CRC_16 << 2) //!< RF24_CONFIGURATION
|
||||
#endif
|
||||
#define RF24_FEATURE (uint8_t)( _BV(RF24_EN_DPL)) //!< RF24_FEATURE
|
||||
#define RF24_RF_SETUP (uint8_t)(( ((MY_RF24_DATARATE & 0b10 ) << 4) | ((MY_RF24_DATARATE & 0b01 ) << 3) | (MY_RF24_PA_LEVEL << 1) ) + 1) //!< RF24_RF_SETUP, +1 for Si24R1 and LNA
|
||||
|
||||
// powerup delay
|
||||
#define RF24_POWERUP_DELAY_MS (100u) //!< Power up delay, allow VCC to settle, transport to become fully operational
|
||||
|
||||
// pipes
|
||||
#define RF24_BROADCAST_PIPE (1u) //!< RF24_BROADCAST_PIPE
|
||||
#define RF24_NODE_PIPE (0u) //!< RF24_NODE_PIPE
|
||||
|
||||
// functions
|
||||
/**
|
||||
* @brief RF24_csn
|
||||
* @param level
|
||||
*/
|
||||
LOCAL void RF24_csn(const bool level);
|
||||
/**
|
||||
* @brief RF24_ce
|
||||
* @param level
|
||||
*/
|
||||
LOCAL void RF24_ce(const bool level);
|
||||
/**
|
||||
* @brief RF24_spiMultiByteTransfer
|
||||
* @param cmd
|
||||
* @param buf
|
||||
* @param len
|
||||
* @param readMode
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t *buf, const uint8_t len,
|
||||
const bool readMode);
|
||||
/**
|
||||
* @brief RF24_spiByteTransfer
|
||||
* @param cmd
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_spiByteTransfer(const uint8_t cmd);
|
||||
/**
|
||||
* @brief RF24_RAW_readByteRegister
|
||||
* @param cmd
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_RAW_readByteRegister(const uint8_t cmd);
|
||||
/**
|
||||
* @brief RF24_RAW_writeByteRegister
|
||||
* @param cmd
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_RAW_writeByteRegister(const uint8_t cmd, const uint8_t value);
|
||||
|
||||
// helper macros
|
||||
#define RF24_readByteRegister(__reg) RF24_RAW_readByteRegister(RF24_CMD_READ_REGISTER | (RF24_REGISTER_MASK & (__reg))) //!< RF24_readByteRegister
|
||||
#define RF24_writeByteRegister(__reg,__value) RF24_RAW_writeByteRegister(RF24_CMD_WRITE_REGISTER | (RF24_REGISTER_MASK & (__reg)), __value) //!< RF24_writeByteRegister
|
||||
#define RF24_writeMultiByteRegister(__reg,__buf,__len) RF24_spiMultiByteTransfer(RF24_CMD_WRITE_REGISTER | (RF24_REGISTER_MASK & (__reg)),(uint8_t *)__buf, __len,false) //!< RF24_writeMultiByteRegister
|
||||
|
||||
/**
|
||||
* @brief RF24_flushRX
|
||||
*/
|
||||
LOCAL void RF24_flushRX(void);
|
||||
/**
|
||||
* @brief RF24_flushTX
|
||||
*/
|
||||
LOCAL void RF24_flushTX(void);
|
||||
/**
|
||||
* @brief RF24_getStatus
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_getStatus(void);
|
||||
/**
|
||||
* @brief RF24_getFIFOStatus
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_getFIFOStatus(void) __attribute__((unused));
|
||||
/**
|
||||
* @brief RF24_openWritingPipe
|
||||
* @param recipient
|
||||
*/
|
||||
LOCAL void RF24_openWritingPipe(const uint8_t recipient);
|
||||
/**
|
||||
* @brief RF24_startListening
|
||||
*/
|
||||
LOCAL void RF24_startListening(void);
|
||||
/**
|
||||
* @brief RF24_stopListening
|
||||
*/
|
||||
LOCAL void RF24_stopListening(void);
|
||||
/**
|
||||
* @brief RF24_sleep
|
||||
*/
|
||||
LOCAL void RF24_sleep(void);
|
||||
/**
|
||||
* @brief RF24_standBy
|
||||
*/
|
||||
LOCAL void RF24_standBy(void);
|
||||
/**
|
||||
* @brief RF24_powerDown
|
||||
*/
|
||||
LOCAL void RF24_powerDown(void);
|
||||
/**
|
||||
* @brief RF24_powerUp
|
||||
*/
|
||||
LOCAL void RF24_powerUp(void);
|
||||
/**
|
||||
* @brief RF24_sendMessage
|
||||
* @param recipient
|
||||
* @param buf
|
||||
* @param len
|
||||
* @param noACK set True if no ACK is required
|
||||
* @return
|
||||
*/
|
||||
LOCAL bool RF24_sendMessage(const uint8_t recipient, const void *buf, const uint8_t len,
|
||||
const bool noACK = false);
|
||||
/**
|
||||
* @brief RF24_getDynamicPayloadSize
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_getDynamicPayloadSize(void);
|
||||
/**
|
||||
* @brief RF24_isDataAvailable
|
||||
* @return
|
||||
*/
|
||||
LOCAL bool RF24_isDataAvailable(void);
|
||||
/**
|
||||
* @brief RF24_readMessage
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_readMessage(void *buf);
|
||||
/**
|
||||
* @brief RF24_setNodeAddress
|
||||
* @param address
|
||||
*/
|
||||
LOCAL void RF24_setNodeAddress(const uint8_t address);
|
||||
/**
|
||||
* @brief RF24_getNodeID
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_getNodeID(void);
|
||||
/**
|
||||
* @brief RF24_sanityCheck
|
||||
* @return
|
||||
*/
|
||||
LOCAL bool RF24_sanityCheck(void);
|
||||
/**
|
||||
* @brief RF24_initialize
|
||||
* @return
|
||||
*/
|
||||
LOCAL bool RF24_initialize(void);
|
||||
/**
|
||||
* @brief RF24_setChannel
|
||||
* @param channel
|
||||
*/
|
||||
LOCAL void RF24_setChannel(const uint8_t channel);
|
||||
/**
|
||||
* @brief RF24_setRetries
|
||||
* @param retransmitDelay
|
||||
* @param retransmitCount
|
||||
*/
|
||||
LOCAL void RF24_setRetries(const uint8_t retransmitDelay, const uint8_t retransmitCount);
|
||||
/**
|
||||
* @brief RF24_setAddressWidth
|
||||
* @param addressWidth
|
||||
*/
|
||||
LOCAL void RF24_setAddressWidth(const uint8_t addressWidth);
|
||||
/**
|
||||
* @brief RF24_setRFSetup
|
||||
* @param RFsetup
|
||||
*/
|
||||
LOCAL void RF24_setRFSetup(const uint8_t RFsetup);
|
||||
/**
|
||||
* @brief RF24_setFeature
|
||||
* @param feature
|
||||
*/
|
||||
LOCAL void RF24_setFeature(const uint8_t feature);
|
||||
/**
|
||||
* @brief RF24_getFeature
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_getFeature(void);
|
||||
/**
|
||||
* @brief RF24_setPipe
|
||||
* @param pipe
|
||||
*/
|
||||
LOCAL void RF24_setPipe(const uint8_t pipe);
|
||||
/**
|
||||
* @brief RF24_setAutoACK
|
||||
* @param pipe
|
||||
*/
|
||||
LOCAL void RF24_setAutoACK(const uint8_t pipe);
|
||||
/**
|
||||
* @brief RF24_setDynamicPayload
|
||||
* @param pipe
|
||||
*/
|
||||
LOCAL void RF24_setDynamicPayload(const uint8_t pipe);
|
||||
/**
|
||||
* @brief RF24_setRFConfiguration
|
||||
* @param configuration
|
||||
*/
|
||||
LOCAL void RF24_setRFConfiguration(const uint8_t configuration);
|
||||
/**
|
||||
* @brief RF24_setPipeAddress
|
||||
* @param pipe
|
||||
* @param address
|
||||
* @param addressWidth
|
||||
*/
|
||||
LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t *address, const uint8_t addressWidth);
|
||||
/**
|
||||
* @brief RF24_setPipeLSB
|
||||
* @param pipe
|
||||
* @param LSB
|
||||
*/
|
||||
LOCAL void RF24_setPipeLSB(const uint8_t pipe, const uint8_t LSB);
|
||||
/**
|
||||
* @brief RF24_getObserveTX
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_getObserveTX(void);
|
||||
/**
|
||||
* @brief RF24_setStatus
|
||||
* @param status
|
||||
* @return status byte before setting new status
|
||||
*/
|
||||
LOCAL uint8_t RF24_setStatus(const uint8_t status);
|
||||
/**
|
||||
* @brief RF24_enableFeatures
|
||||
*/
|
||||
LOCAL void RF24_enableFeatures(void);
|
||||
/**
|
||||
* @brief RF24_getTxPowerPercent
|
||||
* @return
|
||||
*/
|
||||
LOCAL uint8_t RF24_getTxPowerPercent(void);
|
||||
/**
|
||||
* @brief RF24_getTxPowerLevel
|
||||
* @return
|
||||
*/
|
||||
LOCAL int16_t RF24_getTxPowerLevel(void);
|
||||
/**
|
||||
* @brief RF24_setTxPowerPercent
|
||||
* @param newPowerPercent
|
||||
* @return
|
||||
*/
|
||||
LOCAL bool RF24_setTxPowerPercent(const uint8_t newPowerPercent);
|
||||
/**
|
||||
* @brief RF24_getSendingRSSI
|
||||
* @return Pseudo-RSSI based on ARC register
|
||||
*/
|
||||
LOCAL int16_t RF24_getSendingRSSI(void);
|
||||
/**
|
||||
* @brief Generate a constant carrier wave at active channel & transmit power (for testing only).
|
||||
*/
|
||||
LOCAL void RF24_enableConstantCarrierWave(void) __attribute__((unused));
|
||||
/**
|
||||
* @brief Stop generating a constant carrier wave (for testing only).
|
||||
*/
|
||||
LOCAL void RF24_disableConstantCarrierWave(void) __attribute__((unused));
|
||||
/**
|
||||
* @brief Retrieve latched RPD power level, in receive mode (for testing, nRF24L01+ only).
|
||||
* @return True when power level >-64dBm for more than 40us.
|
||||
*/
|
||||
LOCAL bool RF24_getReceivedPowerDetector(void) __attribute__((unused));
|
||||
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
/**
|
||||
* @brief Callback type
|
||||
*/
|
||||
typedef void (*RF24_receiveCallbackType)(void);
|
||||
/**
|
||||
* @brief RF24_registerReceiveCallback
|
||||
* Register a callback, which will be called (from interrupt context) for every message received.
|
||||
* @note When a callback is registered, it _must_ retrieve the message from the nRF24
|
||||
* by calling RF24_readMessage(). Otherwise the interrupt will not get deasserted
|
||||
* and message reception will stop.
|
||||
* @param cb
|
||||
*/
|
||||
LOCAL void RF24_registerReceiveCallback(RF24_receiveCallbackType cb);
|
||||
#endif
|
||||
|
||||
#endif // __RF24_H__
|
||||
|
||||
/** @}*/
|
||||
159
lib/MySensors/hal/transport/RF24/driver/RF24registers.h
Normal file
159
lib/MySensors/hal/transport/RF24/driver/RF24registers.h
Normal file
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2019 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* 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>
|
||||
*/
|
||||
|
||||
// PA levels
|
||||
#define RF24_PA_MIN (0)
|
||||
#define RF24_PA_LOW (1)
|
||||
#define RF24_PA_HIGH (2)
|
||||
#define RF24_PA_MAX (3)
|
||||
|
||||
// power level limits
|
||||
#define RF24_MIN_POWER_LEVEL (0)
|
||||
#define RF24_MAX_POWER_LEVEL (3)
|
||||
|
||||
// data rate
|
||||
#define RF24_1MBPS (0)
|
||||
#define RF24_2MBPS (1)
|
||||
#define RF24_250KBPS (2)
|
||||
|
||||
// CRC
|
||||
#define RF24_CRC_DISABLED (0)
|
||||
#define RF24_CRC_8 (2)
|
||||
#define RF24_CRC_16 (3)
|
||||
|
||||
// ARD, auto retry delay
|
||||
#define RF24_SET_ARD (5) //=1500us
|
||||
|
||||
// ARD, auto retry count
|
||||
#define RF24_SET_ARC (15)
|
||||
|
||||
// nRF24L01(+) register definitions
|
||||
#define RF24_REG_NRF_CONFIG (0x00)
|
||||
#define RF24_REG_EN_AA (0x01)
|
||||
#define RF24_REG_EN_RXADDR (0x02)
|
||||
#define RF24_REG_SETUP_AW (0x03)
|
||||
#define RF24_REG_SETUP_RETR (0x04)
|
||||
#define RF24_REG_RF_CH (0x05)
|
||||
#define RF24_REG_RF_SETUP (0x06)
|
||||
#define RF24_REG_STATUS (0x07)
|
||||
#define RF24_REG_OBSERVE_TX (0x08)
|
||||
#define RF24_REG_RPD (0x09) // nRF24L01+
|
||||
#define RF24_REG_CD (RF24_REG_RPD) // nRF24L01
|
||||
#define RF24_REG_RX_ADDR_P0 (0x0A)
|
||||
#define RF24_REG_RX_ADDR_P1 (0x0B)
|
||||
#define RF24_REG_RX_ADDR_P2 (0x0C)
|
||||
#define RF24_REG_RX_ADDR_P3 (0x0D)
|
||||
#define RF24_REG_RX_ADDR_P4 (0x0E)
|
||||
#define RF24_REG_RX_ADDR_P5 (0x0F)
|
||||
#define RF24_REG_TX_ADDR (0x10)
|
||||
#define RF24_REG_RX_PW_P0 (0x11)
|
||||
#define RF24_REG_RX_PW_P1 (0x12)
|
||||
#define RF24_REG_RX_PW_P2 (0x13)
|
||||
#define RF24_REG_RX_PW_P3 (0x14)
|
||||
#define RF24_REG_RX_PW_P4 (0x15)
|
||||
#define RF24_REG_RX_PW_P5 (0x16)
|
||||
#define RF24_REG_FIFO_STATUS (0x17)
|
||||
#define RF24_REG_DYNPD (0x1C)
|
||||
#define RF24_REG_FEATURE (0x1D)
|
||||
|
||||
// mask
|
||||
#define RF24_REGISTER_MASK (0x1F)
|
||||
|
||||
// instructions
|
||||
#define RF24_CMD_READ_REGISTER (0x00)
|
||||
#define RF24_CMD_WRITE_REGISTER (0x20)
|
||||
#define RF24_CMD_ACTIVATE (0x50)
|
||||
#define RF24_CMD_READ_RX_PL_WID (0x60)
|
||||
#define RF24_CMD_READ_RX_PAYLOAD (0x61)
|
||||
#define RF24_CMD_WRITE_TX_PAYLOAD (0xA0)
|
||||
#define RF24_CMD_WRITE_ACK_PAYLOAD (0xA8)
|
||||
#define RF24_CMD_WRITE_TX_PAYLOAD_NO_ACK (0xB0)
|
||||
#define RF24_CMD_FLUSH_TX (0xE1)
|
||||
#define RF24_CMD_FLUSH_RX (0xE2)
|
||||
#define RF24_CMD_REUSE_TX_PL (0xE3)
|
||||
#define RF24_CMD_NOP (0xFF)
|
||||
|
||||
// bit mnemonics
|
||||
#define RF24_MASK_RX_DR (6)
|
||||
#define RF24_MASK_TX_DS (5)
|
||||
#define RF24_MASK_MAX_RT (4)
|
||||
#define RF24_EN_CRC (3)
|
||||
#define RF24_CRCO (2)
|
||||
#define RF24_PWR_UP (1)
|
||||
#define RF24_PRIM_RX (0)
|
||||
|
||||
// auto ACK
|
||||
#define RF24_ENAA_P5 (5)
|
||||
#define RF24_ENAA_P4 (4)
|
||||
#define RF24_ENAA_P3 (3)
|
||||
#define RF24_ENAA_P2 (2)
|
||||
#define RF24_ENAA_P1 (1)
|
||||
#define RF24_ENAA_P0 (0)
|
||||
|
||||
// rx pipe
|
||||
#define RF24_ERX_P5 (5)
|
||||
#define RF24_ERX_P4 (4)
|
||||
#define RF24_ERX_P3 (3)
|
||||
#define RF24_ERX_P2 (2)
|
||||
#define RF24_ERX_P1 (1)
|
||||
#define RF24_ERX_P0 (0)
|
||||
|
||||
// dynamic payload
|
||||
#define RF24_DPL_P5 (5)
|
||||
#define RF24_DPL_P4 (4)
|
||||
#define RF24_DPL_P3 (3)
|
||||
#define RF24_DPL_P2 (2)
|
||||
#define RF24_DPL_P1 (1)
|
||||
#define RF24_DPL_P0 (0)
|
||||
|
||||
#define RF24_AW (0)
|
||||
#define RF24_ARD (4)
|
||||
#define RF24_ARC (0)
|
||||
#define RF24_PLL_LOCK (4)
|
||||
#define RF24_CONT_WAVE (7)
|
||||
#define RF24_RF_DR (3)
|
||||
#define RF24_RF_PWR (6)
|
||||
#define RF24_RX_DR (6)
|
||||
#define RF24_TX_DS (5)
|
||||
#define RF24_MAX_RT (4)
|
||||
#define RF24_RX_P_NO (1)
|
||||
#define RF24_TX_FULL (0)
|
||||
#define RF24_PLOS_CNT (4)
|
||||
#define RF24_ARC_CNT (0)
|
||||
#define RF24_TX_REUSE (6)
|
||||
#define RF24_FIFO_FULL (5)
|
||||
#define RF24_TX_EMPTY (4)
|
||||
#define RF24_RX_FULL (1)
|
||||
#define RF24_RX_EMPTY (0)
|
||||
#define RF24_RPD (0) // nRF24L01+
|
||||
#define RF24_CD (RF24_RPD) // nRF24L01
|
||||
|
||||
// features
|
||||
#define RF24_EN_DPL (2)
|
||||
#define RF24_EN_ACK_PAY (1)
|
||||
#define RF24_EN_DYN_ACK (0)
|
||||
|
||||
#define RF24_LNA_HCURR (0)
|
||||
#define RF24_RF_DR_LOW (5)
|
||||
#define RF24_RF_DR_HIGH (3)
|
||||
#define RF24_RF_PWR_LOW (1)
|
||||
#define RF24_RF_PWR_HIGH (2)
|
||||
Reference in New Issue
Block a user