mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 20:09:14 +03:00
gatewayTransportSend
This commit is contained in:
241
lib/MySensors/hal/transport/MyTransportHAL.cpp
Normal file
241
lib/MySensors/hal/transport/MyTransportHAL.cpp
Normal file
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
* 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/Arduino/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 "MyTransportHAL.h"
|
||||
|
||||
#if defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL)
|
||||
#define TRANSPORT_HAL_DEBUG(x,...) DEBUG_OUTPUT(x, ##__VA_ARGS__) //!< debug
|
||||
#else
|
||||
#define TRANSPORT_HAL_DEBUG(x,...) //!< debug NULL
|
||||
#endif
|
||||
|
||||
bool transportHALInit(void)
|
||||
{
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:INIT\n"));
|
||||
#if defined(MY_TRANSPORT_ENCRYPTION)
|
||||
uint8_t transportPSK[16];
|
||||
#if defined(MY_ENCRYPTION_SIMPLE_PASSWD)
|
||||
(void)memset((void *)transportPSK, 0, sizeof(transportPSK));
|
||||
(void)memcpy((void *)transportPSK, MY_ENCRYPTION_SIMPLE_PASSWD,
|
||||
strnlen(MY_ENCRYPTION_SIMPLE_PASSWD, sizeof(transportPSK)));
|
||||
#else
|
||||
hwReadConfigBlock((void *)transportPSK, (void *)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS,
|
||||
sizeof(transportPSK));
|
||||
#endif
|
||||
#if defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL)
|
||||
hwDebugBuf2Str((const uint8_t *)transportPSK, sizeof(transportPSK));
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:INIT:PSK=%s\n"),hwDebugPrintStr);
|
||||
#endif
|
||||
#endif
|
||||
bool result = transportInit();
|
||||
|
||||
#if defined(MY_TRANSPORT_ENCRYPTION)
|
||||
#if defined(MY_RADIO_RFM69)
|
||||
transportEncrypt((const char *)transportPSK);
|
||||
#else
|
||||
//set up AES-key
|
||||
AES128CBCInit(transportPSK);
|
||||
#endif
|
||||
// Make sure it is purged from memory when set
|
||||
(void)memset((void *)transportPSK, 0,
|
||||
sizeof(transportPSK));
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
void transportHALSetAddress(const uint8_t address)
|
||||
{
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:SAD:ADDR=%" PRIu8 "\n"), address);
|
||||
transportSetAddress(address);
|
||||
}
|
||||
|
||||
uint8_t transportHALGetAddress(void)
|
||||
{
|
||||
uint8_t result = transportGetAddress();
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:GAD:ADDR=%" PRIu8 "\n"), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool transportHALDataAvailable(void)
|
||||
{
|
||||
bool result = transportDataAvailable();
|
||||
#if defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL)
|
||||
if (result) {
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:DATA:AVAIL\n"));
|
||||
}
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
bool transportHALSanityCheck(void)
|
||||
{
|
||||
bool result = transportSanityCheck();
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:SAN:RES=%" PRIu8 "\n"), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool transportHALReceive(MyMessage *inMsg, uint8_t *msgLength)
|
||||
{
|
||||
// set pointer to first byte of data structure
|
||||
uint8_t *rx_data = &inMsg->last;
|
||||
uint8_t payloadLength = transportReceive((void *)rx_data);
|
||||
#if defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL)
|
||||
hwDebugBuf2Str((const uint8_t *)rx_data, payloadLength);
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:RCV:MSG=%s\n"), hwDebugPrintStr);
|
||||
#endif
|
||||
#if defined(MY_TRANSPORT_ENCRYPTION) && !defined(MY_RADIO_RFM69)
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:RCV:DECRYPT\n"));
|
||||
// has to be adjusted, WIP!
|
||||
uint8_t IV[16] = { 0 };
|
||||
// decrypt data
|
||||
AES128CBCDecrypt(IV, (uint8_t *)rx_data, payloadLength);
|
||||
#if defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL)
|
||||
hwDebugBuf2Str((const uint8_t *)rx_data, payloadLength);
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:RCV:PLAIN=%s\n"), hwDebugPrintStr);
|
||||
#endif
|
||||
#endif
|
||||
// Reject messages with incorrect protocol version
|
||||
MyMessage tmp = *inMsg;
|
||||
if (!tmp.isProtocolVersionValid()) {
|
||||
setIndication(INDICATION_ERR_VERSION);
|
||||
TRANSPORT_HAL_DEBUG(PSTR("!THA:RCV:PVER=%" PRIu8 "\n"),
|
||||
tmp.getVersion()); // protocol version mismatch
|
||||
return false;
|
||||
}
|
||||
*msgLength = tmp.getLength();
|
||||
#if defined(MY_TRANSPORT_ENCRYPTION) && !defined(MY_RADIO_RFM69)
|
||||
// payload length = a multiple of blocksize length for decrypted messages, i.e. cannot be used for payload length check
|
||||
#else
|
||||
// Reject payloads with incorrect length
|
||||
const uint8_t expectedMessageLength = tmp.getExpectedMessageSize();
|
||||
if (payloadLength != expectedMessageLength) {
|
||||
setIndication(INDICATION_ERR_LENGTH);
|
||||
TRANSPORT_HAL_DEBUG(PSTR("!THA:RCV:LEN=%" PRIu8 ",EXP=%" PRIu8 "\n"), payloadLength,
|
||||
expectedMessageLength); // invalid payload length
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:RCV:MSG LEN=%" PRIu8 "\n"), payloadLength);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool transportHALSend(const uint8_t nextRecipient, const MyMessage *outMsg, const uint8_t len,
|
||||
const bool noACK)
|
||||
{
|
||||
if (outMsg == NULL) {
|
||||
|
||||
// nothing to send
|
||||
return false;
|
||||
}
|
||||
#if defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL)
|
||||
hwDebugBuf2Str((const uint8_t *)&outMsg->last, len);
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:SND:MSG=%s\n"), hwDebugPrintStr);
|
||||
#endif
|
||||
|
||||
#if defined(MY_TRANSPORT_ENCRYPTION) && !defined(MY_RADIO_RFM69)
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:SND:ENCRYPT\n"));
|
||||
uint8_t *tx_data[MAX_MESSAGE_SIZE];
|
||||
// copy input data because it is read-only
|
||||
(void)memcpy((void *)tx_data, (void *)&outMsg->last, len);
|
||||
// We us IV vector filled with zeros but randomize unused bytes in encryption block
|
||||
uint8_t IV[16] = { 0 };
|
||||
const uint8_t finalLength = len > 16 ? 32 : 16;
|
||||
// fill block with random data
|
||||
for (uint8_t i = len; i < finalLength; i++) {
|
||||
*((uint8_t *)tx_data + i) = random(256);
|
||||
}
|
||||
//encrypt data
|
||||
AES128CBCEncrypt(IV, (uint8_t *)tx_data, finalLength);
|
||||
#if defined(MY_DEBUG_VERBOSE_TRANSPORT_HAL)
|
||||
hwDebugBuf2Str((const uint8_t *)tx_data, finalLength);
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:SND:CIP=%s\n"), hwDebugPrintStr);
|
||||
#endif
|
||||
|
||||
#else
|
||||
const uint8_t *tx_data = &outMsg->last;
|
||||
const uint8_t finalLength = len;
|
||||
#endif
|
||||
|
||||
bool result = transportSend(nextRecipient, (void *)tx_data, finalLength, noACK);
|
||||
TRANSPORT_HAL_DEBUG(PSTR("THA:SND:MSG LEN=%" PRIu8 ",RES=%" PRIu8 "\n"), finalLength, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void transportHALPowerDown(void)
|
||||
{
|
||||
transportPowerDown();
|
||||
}
|
||||
|
||||
void transportHALPowerUp(void)
|
||||
{
|
||||
transportPowerUp();
|
||||
}
|
||||
|
||||
void transportHALSleep(void)
|
||||
{
|
||||
transportSleep();
|
||||
}
|
||||
|
||||
void transportHALStandBy(void)
|
||||
{
|
||||
transportStandBy();
|
||||
}
|
||||
|
||||
int16_t transportHALGetSendingRSSI(void)
|
||||
{
|
||||
int16_t result = transportGetSendingRSSI();
|
||||
return result;
|
||||
}
|
||||
|
||||
int16_t transportHALGetReceivingRSSI(void)
|
||||
{
|
||||
int16_t result = transportGetReceivingRSSI();
|
||||
return result;
|
||||
}
|
||||
|
||||
int16_t transportHALGetSendingSNR(void)
|
||||
{
|
||||
int16_t result = transportGetSendingSNR();
|
||||
return result;
|
||||
}
|
||||
|
||||
int16_t transportHALGetReceivingSNR(void)
|
||||
{
|
||||
int16_t result = transportGetReceivingSNR();
|
||||
return result;
|
||||
}
|
||||
|
||||
int16_t transportHALGetTxPowerPercent(void)
|
||||
{
|
||||
int16_t result = transportGetTxPowerPercent();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool transportHALSetTxPowerPercent(const uint8_t powerPercent)
|
||||
{
|
||||
bool result = transportSetTxPowerPercent(powerPercent);
|
||||
return result;
|
||||
}
|
||||
|
||||
int16_t transportHALGetTxPowerLevel(void)
|
||||
{
|
||||
int16_t result = transportGetTxPowerLevel();
|
||||
return result;
|
||||
}
|
||||
176
lib/MySensors/hal/transport/MyTransportHAL.h
Normal file
176
lib/MySensors/hal/transport/MyTransportHAL.h
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* TransportHAL debug log messages:
|
||||
*
|
||||
* |E| SYS | SUB | Message | Comment
|
||||
* |-|-----|-------|----------------------------|---------------------------------------------------------------------
|
||||
* | | THA | INIT | | Initialize transportHAL
|
||||
* | | THA | INIT | PSK=%%s | Load PSK for transport encryption (%AES)
|
||||
* | | THA | SAD | ADDR=%%d | Set transport address (ADDR)
|
||||
* | | THA | GAD | ADDR=%%d | Get trnasport address (ADDR)
|
||||
* | | THA | DATA | AVAIL | Message available
|
||||
* | | THA | SAN | RES=%%d | Transport sanity check, result (RES)
|
||||
* | | THA | RCV | MSG=%%s | Receive message (MSG)
|
||||
* | | THA | RCV | DECRYPT | Decrypt received message
|
||||
* | | THA | RCV | PLAIN=%%s | Decrypted message (PLAIN)
|
||||
* |!| THA | RCV | PVER=%%d | Message protocol version (PVER) mismatch
|
||||
* |!| THA | RCV | LEN=%%d,EXP=%%d | Invalid message length (LEN), exptected length (EXP)
|
||||
* | | THA | RCV | MSG LEN=%%d | Length of received message (LEN)
|
||||
* | | THA | SND | MSG=%%s | Send message (MSG)
|
||||
* | | THA | SND | ENCRYPT | Encrypt message to send (%AES)
|
||||
* | | THA | SND | CIP=%%s | Ciphertext of encypted message (CIP)
|
||||
* | | THA | SND | MSG LEN=%%d,RES=%%d | Sending message with length (LEN), result (RES)
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef MyTransportHAL_h
|
||||
#define MyTransportHAL_h
|
||||
|
||||
#define INVALID_SNR ((int16_t)-256) //!< INVALID_SNR
|
||||
#define INVALID_RSSI ((int16_t)-256) //!< INVALID_RSSI
|
||||
#define INVALID_PERCENT ((int16_t)-100) //!< INVALID_PERCENT
|
||||
#define INVALID_LEVEL ((int16_t)-256) //!< INVALID_LEVEL
|
||||
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
#if defined(MY_RADIO_NRF5_ESB)
|
||||
#error Receive message buffering not supported for NRF5 radio! Please define MY_NRF5_RX_BUFFER_SIZE
|
||||
#endif
|
||||
#if defined(MY_RADIO_RFM69)
|
||||
#error Receive message buffering not supported for RFM69!
|
||||
#endif
|
||||
#if defined(MY_RADIO_RFM95)
|
||||
#error Receive message buffering not supported for RFM95!
|
||||
#endif
|
||||
#if defined(MY_RS485)
|
||||
#error Receive message buffering not supported for RS485!
|
||||
#endif
|
||||
#elif defined(MY_RX_MESSAGE_BUFFER_SIZE)
|
||||
#error Receive message buffering requires message buffering feature enabled!
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Signal report selector
|
||||
*/
|
||||
typedef enum {
|
||||
SR_RX_RSSI, //!< SR_RX_RSSI
|
||||
SR_TX_RSSI, //!< SR_TX_RSSI
|
||||
SR_RX_SNR, //!< SR_RX_SNR
|
||||
SR_TX_SNR, //!< SR_TX_SNR
|
||||
SR_TX_POWER_LEVEL, //!< SR_TX_POWER_LEVEL
|
||||
SR_TX_POWER_PERCENT, //!< SR_TX_POWER_PERCENT
|
||||
SR_UPLINK_QUALITY, //!< SR_UPLINK_QUALITY
|
||||
SR_NOT_DEFINED //!< SR_NOT_DEFINED
|
||||
} signalReport_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Initialize transport HW
|
||||
* @return true if initialization successful
|
||||
*/
|
||||
bool transportHALInit(void);
|
||||
/**
|
||||
* @brief Set node address
|
||||
*/
|
||||
void transportHALSetAddress(const uint8_t address);
|
||||
/**
|
||||
* @brief Retrieve node address
|
||||
*/
|
||||
uint8_t transportHALGetAddress(void);
|
||||
/**
|
||||
* @brief Send message
|
||||
* @param to recipient
|
||||
* @param data message to be sent
|
||||
* @param len length of message (header + payload)
|
||||
* @param noACK do not wait for ACK
|
||||
* @return true if message sent successfully
|
||||
*/
|
||||
bool transportHALSend(const uint8_t nextRecipient, const MyMessage *outMsg, const uint8_t len,
|
||||
const bool noACK);
|
||||
/**
|
||||
* @brief Verify if RX FIFO has pending messages
|
||||
* @return true if message available in RX FIFO
|
||||
*/
|
||||
bool transportHALDataAvailable(void);
|
||||
/**
|
||||
* @brief Sanity check for transport: is transport HW still responsive?
|
||||
* @return true if transport HW is ok
|
||||
*/
|
||||
bool transportHALSanityCheck(void);
|
||||
/**
|
||||
* @brief Receive message from FIFO
|
||||
* @param inMsg
|
||||
* @param msgLength length of received message (header + payload)
|
||||
* @return True if valid message received
|
||||
*/
|
||||
bool transportHALReceive(MyMessage *inMsg, uint8_t *msgLength);
|
||||
/**
|
||||
* @brief Power down transport HW (if corresponding MY_XYZ_POWER_PIN defined)
|
||||
*/
|
||||
void transportHALPowerDown(void);
|
||||
/**
|
||||
* @brief Power up transport HW (if corresponding MY_XYZ_POWER_PIN defined)
|
||||
*/
|
||||
void transportHALPowerUp(void);
|
||||
/**
|
||||
* @brief Set transport HW to sleep (no power down)
|
||||
*/
|
||||
void transportHALSleep(void);
|
||||
/**
|
||||
* @brief Set transport HW to standby
|
||||
*/
|
||||
void transportHALStandBy(void);
|
||||
/**
|
||||
* @brief transportGetSendingRSSI
|
||||
* @return RSSI of outgoing message (via ACK packet)
|
||||
*/
|
||||
int16_t transportHALGetSendingRSSI(void);
|
||||
/**
|
||||
* @brief transportGetReceivingRSSI
|
||||
* @return RSSI of incoming message
|
||||
*/
|
||||
int16_t transportHALGetReceivingRSSI(void);
|
||||
/**
|
||||
* @brief transportGetSendingSNR
|
||||
* @return SNR of outgoing message (via ACK packet)
|
||||
*/
|
||||
int16_t transportHALGetSendingSNR(void);
|
||||
/**
|
||||
* @brief transportGetReceivingSNR
|
||||
* @return SNR of incoming message
|
||||
*/
|
||||
int16_t transportHALGetReceivingSNR(void);
|
||||
/**
|
||||
* @brief transportGetTxPowerPercent
|
||||
* @return TX power level in percent
|
||||
*/
|
||||
int16_t transportHALGetTxPowerPercent(void);
|
||||
/**
|
||||
* @brief transportSetTxPowerPercent
|
||||
* @param powerPercent power level in percent
|
||||
* @return True if power level set
|
||||
*/
|
||||
bool transportHALSetTxPowerPercent(const uint8_t powerPercent);
|
||||
/**
|
||||
* @brief transportGetTxPowerLevel
|
||||
* @return TX power in dBm
|
||||
*/
|
||||
int16_t transportHALGetTxPowerLevel(void);
|
||||
|
||||
#endif // MyTransportHAL_h
|
||||
118
lib/MySensors/hal/transport/NRF5_ESB/MyTransportNRF5_ESB.cpp
Normal file
118
lib/MySensors/hal/transport/NRF5_ESB/MyTransportNRF5_ESB.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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
|
||||
* Copyright (C) 2017 Frank Holtz
|
||||
* 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/NRF5_ESB/driver/Radio.h"
|
||||
#include "hal/transport/NRF5_ESB/driver/Radio_ESB.h"
|
||||
|
||||
#include "drivers/CircularBuffer/CircularBuffer.h"
|
||||
|
||||
bool transportInit(void)
|
||||
{
|
||||
return NRF5_ESB_initialize();
|
||||
}
|
||||
|
||||
void transportSetAddress(const uint8_t address)
|
||||
{
|
||||
NRF5_ESB_setNodeAddress(address);
|
||||
NRF5_ESB_startListening();
|
||||
}
|
||||
|
||||
uint8_t transportGetAddress(void)
|
||||
{
|
||||
return NRF5_ESB_getNodeID();
|
||||
}
|
||||
|
||||
bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
|
||||
{
|
||||
return NRF5_ESB_sendMessage(to, data, len, noACK);
|
||||
}
|
||||
|
||||
bool transportDataAvailable(void)
|
||||
{
|
||||
return NRF5_ESB_isDataAvailable();
|
||||
}
|
||||
|
||||
bool transportSanityCheck(void)
|
||||
{
|
||||
return NRF5_ESB_sanityCheck();
|
||||
}
|
||||
|
||||
uint8_t transportReceive(void *data)
|
||||
{
|
||||
uint8_t len = 0;
|
||||
len = NRF5_ESB_readMessage(data);
|
||||
return len;
|
||||
}
|
||||
|
||||
void transportPowerDown(void)
|
||||
{
|
||||
NRF5_ESB_powerDown();
|
||||
}
|
||||
|
||||
void transportPowerUp(void)
|
||||
{
|
||||
NRF5_ESB_powerUp();
|
||||
}
|
||||
|
||||
void transportSleep(void)
|
||||
{
|
||||
NRF5_ESB_sleep();
|
||||
}
|
||||
|
||||
void transportStandBy(void)
|
||||
{
|
||||
NRF5_ESB_standBy();
|
||||
}
|
||||
|
||||
int16_t transportGetSendingRSSI(void)
|
||||
{
|
||||
return NRF5_ESB_getSendingRSSI();
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingRSSI(void)
|
||||
{
|
||||
return NRF5_ESB_getReceivingRSSI();
|
||||
}
|
||||
|
||||
int16_t transportGetSendingSNR(void)
|
||||
{
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingSNR(void)
|
||||
{
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerPercent(void)
|
||||
{
|
||||
return NRF5_getTxPowerPercent();
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerLevel(void)
|
||||
{
|
||||
return static_cast<int16_t>(NRF5_getTxPowerLevel());
|
||||
}
|
||||
|
||||
bool transportSetTxPowerPercent(const uint8_t powerPercent)
|
||||
{
|
||||
return NRF5_setTxPowerPercent(powerPercent);
|
||||
}
|
||||
90
lib/MySensors/hal/transport/NRF5_ESB/driver/Radio.cpp
Normal file
90
lib/MySensors/hal/transport/NRF5_ESB/driver/Radio.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "Radio.h"
|
||||
|
||||
int16_t NRF5_getTxPowerPercent(void)
|
||||
{
|
||||
// NRF5_PA_MAX = 100% NRF5_PA_MIN=0%
|
||||
int16_t dbm = NRF5_getTxPowerLevel();
|
||||
int16_t dbm_diff = ((int8_t)NRF5_PA_MAX-(int8_t)NRF5_PA_MIN);
|
||||
int16_t dbm_min = (int8_t)NRF5_PA_MIN;
|
||||
return ((dbm-dbm_min)*100)/dbm_diff;
|
||||
}
|
||||
|
||||
int16_t NRF5_getTxPowerLevel(void)
|
||||
{
|
||||
return (int8_t)NRF_RADIO->TXPOWER;
|
||||
}
|
||||
|
||||
bool NRF5_setTxPowerPercent(const uint8_t powerPercent)
|
||||
{
|
||||
/* Current mapping:
|
||||
* NRF51/NRF52822:
|
||||
* 0.. 2% -> -40dBm (0%)
|
||||
* 3..56% -> -16dBm (54%)
|
||||
* 57..65% -> -12dBm (63%)
|
||||
* 66..72% -> -8dBm (72%)
|
||||
* 75..84% -> -4dBm (81%)
|
||||
* 85..95% -> 0dBm (90%)
|
||||
* NRF51 96..100%-> 4dBm (100%)
|
||||
* NRF52 96..99% -> 3dBm (97%)
|
||||
* NRF52 100% -> 4dBm (100%)
|
||||
*/
|
||||
|
||||
// Calculate dbm level
|
||||
int16_t dbm_diff = ((int8_t)NRF5_PA_MAX-(int8_t)NRF5_PA_MIN);
|
||||
int16_t dbm_min = (int8_t)NRF5_PA_MIN;
|
||||
int8_t dbm = ((dbm_diff * powerPercent)/100)+dbm_min;
|
||||
if (dbm >= (int8_t)NRF5_PA_MAX) {
|
||||
NRF_RADIO->TXPOWER = NRF5_PA_MAX;
|
||||
return true;
|
||||
}
|
||||
if (dbm > 1) {
|
||||
#ifdef RADIO_TXPOWER_TXPOWER_Pos2dBm
|
||||
// NRF52840
|
||||
NRF_RADIO->TXPOWER = dbm;
|
||||
#elif defined(NRF51)
|
||||
// nRF51x22
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos4dBm;
|
||||
#else
|
||||
// NRF52822
|
||||
if (dbm > 3) {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos4dBm;
|
||||
} else {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos3dBm;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
/* duplicate condition
|
||||
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg4dBm) {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_0dBm;
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg4dBm) {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_0dBm;
|
||||
return true;
|
||||
}
|
||||
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg8dBm) {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg4dBm;
|
||||
return true;
|
||||
}
|
||||
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg12dBm) {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg8dBm;
|
||||
return true;
|
||||
}
|
||||
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg16dBm) {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg12dBm;
|
||||
return true;
|
||||
}
|
||||
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg20dBm) {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg16dBm;
|
||||
return true;
|
||||
}
|
||||
if (dbm > (int8_t)NRF5_PA_MIN) {
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg16dBm;
|
||||
return true;
|
||||
}
|
||||
|
||||
NRF_RADIO->TXPOWER = NRF5_PA_MIN;
|
||||
return true;
|
||||
}
|
||||
77
lib/MySensors/hal/transport/NRF5_ESB/driver/Radio.h
Normal file
77
lib/MySensors/hal/transport/NRF5_ESB/driver/Radio.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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 Frank Holtz
|
||||
* Copyright (C) 2017 Frank Holtz
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef __NRF_RADIO_H__
|
||||
#define __NRF_RADIO_H__
|
||||
|
||||
#if !defined(ARDUINO_ARCH_NRF5)
|
||||
#error "NRF5 Radio is not supported for this platform."
|
||||
#endif
|
||||
|
||||
#include <nrf.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Timer to use
|
||||
#define NRF5_RADIO_TIMER NRF_TIMER0
|
||||
#define NRF5_RADIO_TIMER_IRQ_HANDLER TIMER0_IRQHandler
|
||||
#define NRF5_RADIO_TIMER_IRQN TIMER0_IRQn
|
||||
|
||||
// debug
|
||||
#if defined(MY_DEBUG_VERBOSE_NRF5_ESB)
|
||||
#define NRF5_RADIO_DEBUG(x, ...) DEBUG_OUTPUT(x, ##__VA_ARGS__) //!< DEBUG
|
||||
#else
|
||||
#define NRF5_RADIO_DEBUG(x, ...) //!< DEBUG null
|
||||
#endif
|
||||
|
||||
// tx power
|
||||
typedef enum {
|
||||
#ifdef NRF51
|
||||
NRF5_PA_MIN = RADIO_TXPOWER_TXPOWER_Neg30dBm, // Deprecated
|
||||
#else
|
||||
NRF5_PA_MIN = RADIO_TXPOWER_TXPOWER_Neg40dBm,
|
||||
#endif
|
||||
NRF5_PA_LOW = RADIO_TXPOWER_TXPOWER_Neg16dBm,
|
||||
NRF5_PA_HIGH = RADIO_TXPOWER_TXPOWER_0dBm,
|
||||
#ifdef RADIO_TXPOWER_TXPOWER_Pos9dBm
|
||||
// nRF52840
|
||||
NRF5_PA_MAX = RADIO_TXPOWER_TXPOWER_Pos9dBm,
|
||||
#else
|
||||
// nRF51x22/nRF52822
|
||||
NRF5_PA_MAX = RADIO_TXPOWER_TXPOWER_Pos4dBm,
|
||||
#endif
|
||||
} nrf5_txpower_e;
|
||||
|
||||
// Radio mode (Data rate)
|
||||
typedef enum {
|
||||
NRF5_1MBPS = RADIO_MODE_MODE_Nrf_1Mbit,
|
||||
NRF5_2MBPS = RADIO_MODE_MODE_Nrf_2Mbit,
|
||||
NRF5_250KBPS = RADIO_MODE_MODE_Nrf_250Kbit, // Deprecated!!!
|
||||
NRF5_BLE_1MBPS = RADIO_MODE_MODE_Ble_1Mbit,
|
||||
} nrf5_mode_e;
|
||||
|
||||
int16_t NRF5_getTxPowerPercent(void);
|
||||
int16_t NRF5_getTxPowerLevel(void);
|
||||
bool NRF5_setTxPowerPercent(const uint8_t powerPercent);
|
||||
|
||||
|
||||
#endif // __NRF_RADIO_H__
|
||||
710
lib/MySensors/hal/transport/NRF5_ESB/driver/Radio_ESB.cpp
Normal file
710
lib/MySensors/hal/transport/NRF5_ESB/driver/Radio_ESB.cpp
Normal file
@@ -0,0 +1,710 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors formrs 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 Frank Holtz
|
||||
* Copyright (C) 2017 Frank Holtz
|
||||
* 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 "Radio.h"
|
||||
#include "Radio_ESB.h"
|
||||
#include "hal/architecture/NRF5/MyHwNRF5.h"
|
||||
#include "drivers/CircularBuffer/CircularBuffer.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// internal functions
|
||||
static uint8_t reverse_byte(uint8_t address);
|
||||
inline void _stopTimer();
|
||||
inline void _stopACK();
|
||||
|
||||
// RX Buffer
|
||||
static NRF5_ESB_Packet rx_circular_buffer_buffer[MY_NRF5_ESB_RX_BUFFER_SIZE];
|
||||
// Poiter to rx circular buffer
|
||||
static NRF5_ESB_Packet rx_buffer;
|
||||
// Circular buffer
|
||||
static CircularBuffer<NRF5_ESB_Packet>
|
||||
rx_circular_buffer(rx_circular_buffer_buffer, MY_NRF5_ESB_RX_BUFFER_SIZE);
|
||||
// Dedect duplicate packages for every pipe available
|
||||
static volatile uint32_t package_ids[8];
|
||||
|
||||
// TX Buffer
|
||||
static NRF5_ESB_Packet tx_buffer;
|
||||
// remaining TX retries
|
||||
static volatile int8_t tx_retries;
|
||||
// PID number for ACK
|
||||
static volatile int8_t ack_pid;
|
||||
// Flag for ack received
|
||||
static volatile bool ack_received;
|
||||
// Flag for end TX event
|
||||
static volatile bool events_end_tx;
|
||||
// Last RSSI sample provided by NRF5_ESB_readMessage
|
||||
static volatile int16_t rssi_rx;
|
||||
// Last RSSI sample by last package
|
||||
static volatile int16_t rssi_tx;
|
||||
// Buffer node address
|
||||
static uint8_t node_address = 0;
|
||||
// TX power level
|
||||
static int8_t tx_power_level = (MY_NRF5_ESB_PA_LEVEL << RADIO_TXPOWER_TXPOWER_Pos);
|
||||
|
||||
// Initialize radio unit
|
||||
static bool NRF5_ESB_initialize()
|
||||
{
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:INIT:ESB\n"));
|
||||
|
||||
#if defined(SOFTDEVICE_PRESENT)
|
||||
// Disable the SoftDevice; requires NRF5 SDK available
|
||||
sd_softdevice_disable();
|
||||
#endif
|
||||
|
||||
// Power on radio unit
|
||||
NRF_RADIO->POWER = 1;
|
||||
|
||||
// Disable shorts
|
||||
NRF_RADIO->SHORTS = 0;
|
||||
|
||||
// Disable radio
|
||||
NRF_RADIO->TASKS_DISABLE = 1;
|
||||
|
||||
// Enable radio interrupt
|
||||
NVIC_SetPriority(RADIO_IRQn, 1);
|
||||
NVIC_ClearPendingIRQ(RADIO_IRQn);
|
||||
NVIC_EnableIRQ(RADIO_IRQn);
|
||||
|
||||
// Enable timer interrupt
|
||||
NVIC_SetPriority(NRF5_RADIO_TIMER_IRQN, 2);
|
||||
NVIC_ClearPendingIRQ(NRF5_RADIO_TIMER_IRQN);
|
||||
NVIC_EnableIRQ(NRF5_RADIO_TIMER_IRQN);
|
||||
|
||||
// Clear all events
|
||||
NRF_RADIO->EVENTS_ADDRESS = 0;
|
||||
NRF_RADIO->EVENTS_BCMATCH = 0;
|
||||
NRF_RADIO->EVENTS_DEVMATCH = 0;
|
||||
NRF_RADIO->EVENTS_DEVMISS = 0;
|
||||
NRF_RADIO->EVENTS_DISABLED = 0;
|
||||
NRF_RADIO->EVENTS_END = 0;
|
||||
NRF_RADIO->EVENTS_PAYLOAD = 0;
|
||||
NRF_RADIO->EVENTS_READY = 0;
|
||||
NRF_RADIO->EVENTS_RSSIEND = 0;
|
||||
|
||||
// Disable all interrupts
|
||||
NRF_RADIO->INTENCLR = (uint32_t)~0;
|
||||
|
||||
// Select interrupt events
|
||||
NRF_RADIO->INTENSET = RADIO_INTENSET_END_Msk | RADIO_INTENSET_BCMATCH_Msk;
|
||||
|
||||
// Configure radio parameters: tx power
|
||||
NRF_RADIO->TXPOWER = tx_power_level;
|
||||
|
||||
// Configure radio parameters: radio channel
|
||||
NRF_RADIO->FREQUENCY = MY_NRF5_ESB_CHANNEL;
|
||||
|
||||
// Configure radio parameters: data rate
|
||||
NRF_RADIO->MODE = MY_NRF5_ESB_MODE;
|
||||
|
||||
#ifdef NRF52
|
||||
// Configure nRF52 specific mode register
|
||||
NRF_RADIO->MODECNF0 = (RADIO_MODECNF0_RU_Default << RADIO_MODECNF0_RU_Pos) |
|
||||
(RADIO_MODECNF0_DTX_Center << RADIO_MODECNF0_DTX_Pos);
|
||||
#endif
|
||||
|
||||
// Configure radio parameters: CRC16
|
||||
NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos);
|
||||
NRF_RADIO->CRCINIT = 0xFFFFUL;
|
||||
NRF_RADIO->CRCPOLY = 0x11021UL;
|
||||
|
||||
// Radio address config
|
||||
uint8_t address[MY_NRF5_ESB_ADDR_WIDTH] = {MY_NRF5_ESB_BASE_RADIO_ID};
|
||||
|
||||
// Configure addresses
|
||||
NRF_RADIO->PREFIX0 = (NRF5_ESB_NODE_ADDR_MSK | reverse_byte(node_address) <<
|
||||
(NRF5_ESB_NODE_ADDR << 5));
|
||||
NRF_RADIO->BASE0 = reverse_byte(address[1]) << 24 |
|
||||
reverse_byte(address[2]) << 16 |
|
||||
reverse_byte(address[3]) << 8 | reverse_byte(address[4]);
|
||||
NRF_RADIO->BASE1 = reverse_byte(address[1]) << 24 |
|
||||
reverse_byte(address[2]) << 16 |
|
||||
reverse_byte(address[3]) << 8 | reverse_byte(address[4]);
|
||||
NRF_RADIO->PREFIX1 = NRF5_ESB_TX_ADDR_MSK; // Broadcast and send address
|
||||
|
||||
// Enable listening on Node and BC address
|
||||
NRF_RADIO->RXADDRESSES = (1 << NRF5_ESB_NODE_ADDR) | (1 << NRF5_ESB_BC_ADDR);
|
||||
|
||||
// Packet configuration for nRF24 compatibility
|
||||
NRF_RADIO->PCNF0 = (6 << RADIO_PCNF0_LFLEN_Pos) | // 6 Bits length field
|
||||
(0 << RADIO_PCNF0_S0LEN_Pos) | // No S0
|
||||
#ifdef RADIO_PCNF0_S1INCL_Pos
|
||||
(1 << RADIO_PCNF0_S1INCL_Pos) | // Force include S1 in RAM
|
||||
#endif
|
||||
(3 << RADIO_PCNF0_S1LEN_Pos); // 3 Bits S1 (NOACK and PID)
|
||||
|
||||
// Packet configuration
|
||||
NRF_RADIO->PCNF1 =
|
||||
(MAX_MESSAGE_SIZE << RADIO_PCNF1_MAXLEN_Pos) | // maximum length
|
||||
(0 << RADIO_PCNF1_STATLEN_Pos) | // minimum message length
|
||||
((MY_NRF5_ESB_ADDR_WIDTH - 1) << RADIO_PCNF1_BALEN_Pos) | // Set base address length
|
||||
(RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) | // Big endian
|
||||
(RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos); // Disable whitening
|
||||
|
||||
// HINT: Fast ramp up can enabled here. Needs more code on other lines
|
||||
// Fast ramp up isn't supported by nRF24 and NRF51 series.
|
||||
|
||||
// Set bitcounter to trigger interrupt after ACK bit
|
||||
NRF_RADIO->BCC = NRF5_ESB_BITCOUNTER;
|
||||
|
||||
#ifdef NRF51
|
||||
// Enable timer
|
||||
NRF5_RADIO_TIMER->POWER = 1;
|
||||
#endif
|
||||
// Stop timer, if running
|
||||
_stopTimer();
|
||||
// Prepare timer running at 1 MHz/1us
|
||||
NRF5_RADIO_TIMER->PRESCALER = 4;
|
||||
// Timer mode
|
||||
NRF5_RADIO_TIMER->MODE = TIMER_MODE_MODE_Timer;
|
||||
// in 16 Bit mode
|
||||
NRF5_RADIO_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos;
|
||||
// Stop timer when CC0 reached
|
||||
NRF5_RADIO_TIMER->SHORTS =
|
||||
TIMER_SHORTS_COMPARE3_CLEAR_Msk | TIMER_SHORTS_COMPARE3_STOP_Msk;
|
||||
// Reset timer
|
||||
NRF5_RADIO_TIMER->TASKS_CLEAR = 1;
|
||||
|
||||
// Reset compare events
|
||||
#ifdef NRF51
|
||||
for (uint8_t i=0; i<4; i++) {
|
||||
#else
|
||||
for (uint8_t i=0; i<6; i++) {
|
||||
#endif
|
||||
NRF5_RADIO_TIMER->EVENTS_COMPARE[i] = 0;
|
||||
}
|
||||
|
||||
// Enable interrupt
|
||||
NRF5_RADIO_TIMER->INTENSET = TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos;
|
||||
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
intcntr_bcmatch=0;
|
||||
intcntr_ready=0;
|
||||
intcntr_end=0;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void NRF5_ESB_powerDown()
|
||||
{
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:PD\n"));
|
||||
|
||||
// Disable inerrupt
|
||||
NVIC_DisableIRQ(RADIO_IRQn);
|
||||
NVIC_DisableIRQ(NRF5_RADIO_TIMER_IRQN);
|
||||
|
||||
// Clear PPI
|
||||
NRF_PPI->CHENCLR = NRF5_ESB_PPI_BITS;
|
||||
|
||||
// Save power level
|
||||
tx_power_level = NRF_RADIO->TXPOWER;
|
||||
|
||||
// Power off readio unit
|
||||
NRF_RADIO->POWER = 0;
|
||||
|
||||
// Shutdown timer
|
||||
NRF5_RADIO_TIMER->TASKS_SHUTDOWN = 1;
|
||||
#ifdef NRF51
|
||||
// Power off timer
|
||||
NRF5_RADIO_TIMER->POWER = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void NRF5_ESB_powerUp()
|
||||
{
|
||||
NRF5_ESB_initialize();
|
||||
}
|
||||
|
||||
static void NRF5_ESB_sleep()
|
||||
{
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:SLP\n"));
|
||||
|
||||
// Disable shorts
|
||||
NRF_RADIO->SHORTS = 0;
|
||||
|
||||
// Disable radio
|
||||
NRF_RADIO->TASKS_DISABLE = 1;
|
||||
}
|
||||
|
||||
static void NRF5_ESB_standBy()
|
||||
{
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:SBY\n"));
|
||||
NRF5_ESB_startListening();
|
||||
}
|
||||
|
||||
static bool NRF5_ESB_sanityCheck()
|
||||
{
|
||||
// always true
|
||||
return true;
|
||||
}
|
||||
|
||||
static void NRF5_ESB_setNodeAddress(const uint8_t address)
|
||||
{
|
||||
node_address = address;
|
||||
NRF_RADIO->PREFIX0 = (NRF_RADIO->PREFIX0 & NRF5_ESB_NODE_ADDR_MSK) |
|
||||
reverse_byte(node_address) << (NRF5_ESB_NODE_ADDR << 5);
|
||||
}
|
||||
|
||||
static uint8_t NRF5_ESB_getNodeID()
|
||||
{
|
||||
return reverse_byte((NRF_RADIO->PREFIX0 & NRF5_ESB_NODE_ADDR_MSK) >> (NRF5_ESB_NODE_ADDR << 5));
|
||||
}
|
||||
|
||||
static void NRF5_ESB_startListening()
|
||||
{
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:STL\n"));
|
||||
|
||||
// Check if radio is initialized
|
||||
if (NRF_RADIO->POWER == 0) {
|
||||
NRF5_ESB_initialize();
|
||||
}
|
||||
|
||||
#ifdef NRF52
|
||||
// Fix PAN#102 and PAN#106
|
||||
*((volatile uint32_t *)0x40001774) = (*((volatile uint32_t *)0x40001774) & 0xFFFFFFFE) | 0x01000000;
|
||||
#endif
|
||||
|
||||
// Enable Ready interrupt
|
||||
NRF_RADIO->INTENSET = RADIO_INTENSET_READY_Msk;
|
||||
|
||||
// Enable RX when ready, Enable RX after disabling task
|
||||
NRF_RADIO->SHORTS = NRF5_ESB_SHORTS_RX;
|
||||
|
||||
// Switch to RX
|
||||
if (NRF_RADIO->STATE == RADIO_STATE_STATE_Disabled) {
|
||||
NRF_RADIO->TASKS_RXEN = 1;
|
||||
} else {
|
||||
NRF_RADIO->TASKS_DISABLE = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static bool NRF5_ESB_isDataAvailable()
|
||||
{
|
||||
return rx_circular_buffer.available() > 0;
|
||||
}
|
||||
|
||||
static uint8_t NRF5_ESB_readMessage(void *data)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
|
||||
// get content from rx buffer
|
||||
NRF5_ESB_Packet *buffer = rx_circular_buffer.getBack();
|
||||
// Nothing to read?
|
||||
if (buffer != NULL) {
|
||||
// copy content
|
||||
memcpy(data, buffer->data, buffer->len);
|
||||
ret = buffer->len;
|
||||
rssi_rx = 0-buffer->rssi;
|
||||
|
||||
// Debug message
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:RX:LEN=%" PRIu8 ",NOACK=%" PRIu8 ",PID=%" PRIu8 ",RSSI=%" PRIi16 ",RX=%"
|
||||
PRIu32 "\n"),
|
||||
buffer->len, buffer->noack, buffer->pid, rssi_rx, buffer->rxmatch);
|
||||
#endif
|
||||
|
||||
// release buffer
|
||||
rx_circular_buffer.popBack();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void NRF5_ESB_endtx();
|
||||
void NRF5_ESB_starttx()
|
||||
{
|
||||
if (tx_retries > 0) {
|
||||
// Prevent radio to write into TX memory while receiving
|
||||
if (NRF_RADIO->PACKETPTR != (uint32_t)&tx_buffer) {
|
||||
// Disable shorts
|
||||
NRF_RADIO->SHORTS = 0;
|
||||
// Disable radio
|
||||
NRF_RADIO->TASKS_DISABLE = 1;
|
||||
}
|
||||
|
||||
// Mark TX as unfinised
|
||||
events_end_tx = false;
|
||||
|
||||
// Configure TX address to address at index NRF5_ESB_TX_ADDR
|
||||
NRF_RADIO->TXADDRESS = NRF5_ESB_TX_ADDR;
|
||||
|
||||
// Enable TX when ready, Enable TX after disabling task
|
||||
NRF_RADIO->SHORTS = NRF5_ESB_SHORTS_TX;
|
||||
|
||||
// reset timer
|
||||
NRF_RESET_EVENT(NRF5_RADIO_TIMER->EVENTS_COMPARE[3]);
|
||||
_stopTimer();
|
||||
NRF5_RADIO_TIMER->TASKS_CLEAR = 1;
|
||||
// Set retransmit time
|
||||
NRF5_RADIO_TIMER->CC[3] = NRF5_ESB_ARD - NRF5_ESB_RAMP_UP_TIME;
|
||||
// Set radio disable time to ACK_WAIT time
|
||||
NRF5_RADIO_TIMER->CC[1] = NRF5_ESB_ACK_WAIT;
|
||||
|
||||
/** Configure PPI (Programmable peripheral interconnect) */
|
||||
// Start timer on END event
|
||||
NRF_PPI->CH[NRF5_ESB_PPI_TIMER_START].EEP = (uint32_t)&NRF_RADIO->EVENTS_END;
|
||||
NRF_PPI->CH[NRF5_ESB_PPI_TIMER_START].TEP = (uint32_t)&NRF5_RADIO_TIMER->TASKS_START;
|
||||
#ifdef NRF52
|
||||
NRF_PPI->FORK[NRF5_ESB_PPI_TIMER_START].TEP = 0;
|
||||
#endif
|
||||
|
||||
#ifndef NRF5_ESB_USE_PREDEFINED_PPI
|
||||
// Disable Radio after CC[1]
|
||||
NRF_PPI->CH[NRF5_ESB_PPI_TIMER_RADIO_DISABLE].EEP = (uint32_t)&NRF5_RADIO_TIMER->EVENTS_COMPARE[1];
|
||||
NRF_PPI->CH[NRF5_ESB_PPI_TIMER_RADIO_DISABLE].TEP = (uint32_t)&NRF_RADIO->TASKS_DISABLE;
|
||||
#ifdef NRF52
|
||||
NRF_PPI->CH[NRF5_ESB_PPI_TIMER_RADIO_DISABLE].TEP = 0;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Set PPI
|
||||
NRF_PPI->CHENSET = NRF5_ESB_PPI_BITS;
|
||||
|
||||
// Disable Ready interrupt
|
||||
NRF_RADIO->INTENCLR = RADIO_INTENSET_READY_Msk;
|
||||
|
||||
// Set buffer
|
||||
NRF_RADIO->PACKETPTR = (uint32_t)&tx_buffer;
|
||||
|
||||
// Switch to TX
|
||||
if (NRF_RADIO->STATE == RADIO_STATE_STATE_Disabled) {
|
||||
NRF_RADIO->TASKS_TXEN = 1;
|
||||
} else {
|
||||
NRF_RADIO->TASKS_DISABLE = 1;
|
||||
}
|
||||
} else {
|
||||
// finised TX
|
||||
NRF5_ESB_endtx();
|
||||
}
|
||||
tx_retries--;
|
||||
}
|
||||
|
||||
void NRF5_ESB_endtx()
|
||||
{
|
||||
// Clear PPI
|
||||
NRF_PPI->CHENCLR = NRF5_ESB_PPI_BITS;
|
||||
// Enable Ready interrupt
|
||||
NRF_RADIO->INTENSET = RADIO_INTENSET_READY_Msk;
|
||||
// Stop Timer
|
||||
_stopTimer();
|
||||
// Mark TX as end
|
||||
events_end_tx = true;
|
||||
// Debug output
|
||||
#ifdef NRF5_ESB_DEBUG_INT_TX_END
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:INT:ENDTX\n"));
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool NRF5_ESB_sendMessage(uint8_t recipient, const void *buf, uint8_t len, const bool noACK)
|
||||
{
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:SND:TO=%" PRIu8 ",LEN=%" PRIu8 ",PID=%" PRIu8 ",NOACK=%" PRIu8 "\n"),
|
||||
recipient, len, tx_buffer.pid,
|
||||
tx_buffer.noack); // send message
|
||||
// Check if radio is initialized
|
||||
if (NRF_RADIO->POWER == 0) {
|
||||
NRF5_ESB_initialize();
|
||||
}
|
||||
|
||||
// check length and truncate data
|
||||
if (len > MAX_MESSAGE_SIZE) {
|
||||
len = MAX_MESSAGE_SIZE;
|
||||
}
|
||||
|
||||
// copy data to tx_buffer
|
||||
memcpy(&tx_buffer.data[0], buf, len);
|
||||
|
||||
// build metadata
|
||||
tx_buffer.len = len;
|
||||
#ifndef MY_NRF5_ESB_REVERSE_ACK_TX
|
||||
tx_buffer.noack = noACK || recipient==BROADCAST_ADDRESS;
|
||||
#else
|
||||
// reverse the noack bit
|
||||
tx_buffer.noack = !(noACK || recipient==BROADCAST_ADDRESS);
|
||||
#endif
|
||||
tx_buffer.pid++;
|
||||
|
||||
// Calculate number of retries
|
||||
if (recipient == BROADCAST_ADDRESS) {
|
||||
tx_retries = NRF5_ESB_BC_ARC;
|
||||
} else {
|
||||
tx_retries = ((noACK == false)?(NRF5_ESB_ARC_ACK):(NRF5_ESB_ARC_NOACK));
|
||||
}
|
||||
int8_t tx_retries_start = tx_retries;
|
||||
ack_received = false;
|
||||
|
||||
// configure TX address
|
||||
NRF_RADIO->PREFIX1 = (NRF_RADIO->PREFIX1 & NRF5_ESB_TX_ADDR_MSK) |
|
||||
(reverse_byte(recipient) << (NRF5_ESB_TX_ADDR - 4));
|
||||
|
||||
// Enable listening on Node, BC and TX address
|
||||
NRF_RADIO->RXADDRESSES = (1 << NRF5_ESB_NODE_ADDR) | (1 << NRF5_ESB_BC_ADDR) |
|
||||
(1 << NRF5_ESB_TX_ADDR);
|
||||
|
||||
// Set RSSI to invalid
|
||||
rssi_tx = INVALID_RSSI;
|
||||
|
||||
NRF5_ESB_starttx();
|
||||
|
||||
// Wait for end of transmission
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
uint32_t wakeups = 0;
|
||||
#endif
|
||||
while (events_end_tx == false) {
|
||||
// Power off CPU until next interrupt
|
||||
hwSleep();
|
||||
// hwWaitForInterrupt();
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
wakeups++;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Calculate RSSI
|
||||
if (rssi_tx == INVALID_RSSI) {
|
||||
// calculate pseudo-RSSI based on retransmission counter (ARC)
|
||||
// min -104dBm at 250kBps
|
||||
// Arbitrary definition: ARC 0 == -29, ARC 15 = -104
|
||||
rssi_tx = (-29 - (8 * (tx_retries_start - tx_retries)));
|
||||
}
|
||||
|
||||
// Enable listening on Node and BC address
|
||||
NRF_RADIO->RXADDRESSES = (1 << NRF5_ESB_NODE_ADDR) | (1 << NRF5_ESB_BC_ADDR);
|
||||
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
NRF5_RADIO_DEBUG(PSTR("NRF5:SND:END=%" PRIu8 ",ACK=%" PRIu8 ",RTRY=%" PRIi8 ",RSSI=%" PRIi16
|
||||
",WAKE=%" PRIu32 "\n"),
|
||||
events_end_tx, ack_received, tx_retries_start - tx_retries, rssi_tx, wakeups);
|
||||
#endif
|
||||
|
||||
|
||||
return ack_received;
|
||||
};
|
||||
|
||||
static int16_t NRF5_ESB_getSendingRSSI()
|
||||
{
|
||||
return rssi_tx;
|
||||
}
|
||||
|
||||
static int16_t NRF5_ESB_getReceivingRSSI()
|
||||
{
|
||||
return rssi_rx;
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal helper functions
|
||||
*/
|
||||
|
||||
// Reverse a byte for address
|
||||
static uint8_t reverse_byte(uint8_t address)
|
||||
{
|
||||
#if __CORTEX_M >= (0x01U)
|
||||
return __REV(__RBIT(address));
|
||||
#else
|
||||
address = ((address * 0x0802LU & 0x22110LU) | (address * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
|
||||
#endif
|
||||
return address;
|
||||
}
|
||||
|
||||
inline void _stopTimer()
|
||||
{
|
||||
// Stop timer
|
||||
NRF5_RADIO_TIMER->TASKS_STOP = 1;
|
||||
// NRF52 PAN#78
|
||||
NRF5_RADIO_TIMER->TASKS_SHUTDOWN = 1;
|
||||
}
|
||||
|
||||
inline void _stopACK()
|
||||
{
|
||||
// Enable RX when ready, Enable RX after disabling task
|
||||
NRF_RADIO->SHORTS = NRF5_ESB_SHORTS_RX;
|
||||
|
||||
// Start disabling radio -> switch to rx by shorts
|
||||
NRF_RADIO->TASKS_DISABLE = 1;
|
||||
}
|
||||
|
||||
// Calculate time to transmit an byte in µs as bit shift -> 2^X
|
||||
static inline uint8_t NRF5_ESB_byte_time()
|
||||
{
|
||||
if ((MY_NRF5_ESB_MODE == NRF5_1MBPS) or
|
||||
(MY_NRF5_ESB_MODE == NRF5_BLE_1MBPS)) {
|
||||
return (3);
|
||||
} else if (MY_NRF5_ESB_MODE == NRF5_2MBPS) {
|
||||
return (2);
|
||||
} else if (MY_NRF5_ESB_MODE == NRF5_250KBPS) {
|
||||
return (5);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
/** Radio Interrupt handler */
|
||||
void RADIO_IRQHandler()
|
||||
{
|
||||
/** Bitcounter event is used to switch between RX/TX
|
||||
* In RX mode, when an ACK required packet is received, switch to TX,
|
||||
* elsewhere start RX again.
|
||||
* In TX mode switch always to RX.
|
||||
*/
|
||||
if (NRF_RADIO->EVENTS_BCMATCH == 1) {
|
||||
NRF_RESET_EVENT(NRF_RADIO->EVENTS_BCMATCH);
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
intcntr_bcmatch++;
|
||||
#endif
|
||||
// Disable bitcounter
|
||||
NRF_RADIO->TASKS_BCSTOP = 1;
|
||||
|
||||
// In RX mode -> prepare ACK or RX
|
||||
if (NRF_RADIO->STATE == RADIO_STATE_STATE_Rx) {
|
||||
// Send ACK only for node address, don't care about the ACK bit to handle bad nRF24 clones
|
||||
if (NRF_RADIO->RXMATCH == NRF5_ESB_NODE_ADDR) {
|
||||
// Send ACK after END, an empty packet is provided in READY event
|
||||
NRF_RADIO->SHORTS = NRF5_ESB_SHORTS_RX_TX;
|
||||
} else {
|
||||
// No ACK -> Start RX after END
|
||||
NRF_RADIO->SHORTS = NRF5_ESB_SHORTS_RX;
|
||||
}
|
||||
|
||||
// Handle incoming ACK packet
|
||||
if (NRF_RADIO->RXMATCH == NRF5_ESB_TX_ADDR) {
|
||||
/** Calculate time to switch radio off
|
||||
* This is an ACK packet, the radio is disabled by Timer
|
||||
* event after CC[1], calculate the time switching of the
|
||||
* radio.
|
||||
*/
|
||||
// Read current timer value
|
||||
NRF5_RADIO_TIMER->TASKS_CAPTURE[1] = 1;
|
||||
|
||||
// Set Timer compare register 0 to end of packet (len+CRC)
|
||||
NRF5_RADIO_TIMER->CC[1] += ((rx_buffer.len + 3) << NRF5_ESB_byte_time());
|
||||
}
|
||||
} else {
|
||||
// Current mode is TX:
|
||||
// After TX the Radio has to be always in RX mode to
|
||||
// receive ACK or start implicit listen mode after send.
|
||||
NRF_RADIO->SHORTS = NRF5_ESB_SHORTS_TX_RX;
|
||||
// HINT: Fast ramp up can enabled here. Needs more code on other lines
|
||||
}
|
||||
}
|
||||
|
||||
/** Ready event is generated before RX starts
|
||||
* An free rx buffer is allocated or radio is disabled on failures
|
||||
*/
|
||||
if (NRF_RADIO->EVENTS_READY == 1) {
|
||||
NRF_RESET_EVENT(NRF_RADIO->EVENTS_READY);
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
intcntr_ready++;
|
||||
#endif
|
||||
// Configure DMA target address
|
||||
NRF_RADIO->PACKETPTR = (uint32_t)&rx_buffer;
|
||||
|
||||
/* Don't care about if next packet RX or ACK,
|
||||
* prepare current rx_buffer to send an ACK */
|
||||
|
||||
// Set outgoing address to node address for ACK packages
|
||||
NRF_RADIO->TXADDRESS = NRF5_ESB_NODE_ADDR;
|
||||
}
|
||||
|
||||
/** This event is generated after TX or RX finised
|
||||
*/
|
||||
if (NRF_RADIO->EVENTS_END == 1) {
|
||||
NRF_RESET_EVENT(NRF_RADIO->EVENTS_END);
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
intcntr_end++;
|
||||
#endif
|
||||
|
||||
// Enable ACK bitcounter for next packet
|
||||
NRF_RADIO->BCC = NRF5_ESB_BITCOUNTER;
|
||||
|
||||
// End of RX packet
|
||||
if ((NRF_RADIO->STATE == RADIO_STATE_STATE_Rx) or
|
||||
(NRF_RADIO->STATE == RADIO_STATE_STATE_RxIdle) or
|
||||
(NRF_RADIO->STATE == RADIO_STATE_STATE_RxDisable) or
|
||||
(NRF_RADIO->STATE == RADIO_STATE_STATE_TxRu)) {
|
||||
if (NRF_RADIO->CRCSTATUS) {
|
||||
// Ensure no ACK package is received
|
||||
if (NRF_RADIO->RXMATCH != NRF5_ESB_TX_ADDR) {
|
||||
// calculate a package id
|
||||
uint32_t pkgid = rx_buffer.pid << 16 | NRF_RADIO->RXCRC;
|
||||
if (pkgid != package_ids[NRF_RADIO->RXMATCH]) {
|
||||
// correct package -> store id to dedect duplicates
|
||||
package_ids[NRF_RADIO->RXMATCH] = pkgid;
|
||||
rx_buffer.rssi = NRF_RADIO->RSSISAMPLE;
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
// Store debug data
|
||||
rx_buffer.rxmatch = NRF_RADIO->RXMATCH;
|
||||
#endif
|
||||
// Push data to buffer
|
||||
if (rx_circular_buffer.pushFront(&rx_buffer)) {
|
||||
// Prepare ACK package
|
||||
rx_buffer.data[0]=rx_buffer.rssi;
|
||||
rx_buffer.len=1; // data[0] is set some lines before
|
||||
#ifndef MY_NRF5_ESB_REVERSE_ACK_TX
|
||||
rx_buffer.noack = 1;
|
||||
#else
|
||||
rx_buffer.noack = 0;
|
||||
#endif
|
||||
} else {
|
||||
// Buffer is full
|
||||
// Stop ACK
|
||||
_stopACK();
|
||||
// Increment pkgid allowing receive the package again
|
||||
package_ids[NRF_RADIO->RXMATCH]++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// ACK package received, ducplicates are accepted
|
||||
|
||||
// rssi value in ACK included?
|
||||
if (rx_buffer.len == 1) {
|
||||
rssi_tx = 0-rx_buffer.data[0];
|
||||
}
|
||||
// notify TX process
|
||||
ack_received = true;
|
||||
// End TX
|
||||
NRF5_ESB_endtx();
|
||||
}
|
||||
} else {
|
||||
/** Invalid CRC -> Switch back to RX, Stop sending ACK */
|
||||
_stopACK();
|
||||
}
|
||||
} else {
|
||||
// TX end
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Timer Interrupt Handler
|
||||
* This timer is used to handle TX retransmit timing
|
||||
*
|
||||
*/
|
||||
void NRF5_RADIO_TIMER_IRQ_HANDLER()
|
||||
{
|
||||
if (NRF5_RADIO_TIMER->EVENTS_COMPARE[3] == 1) {
|
||||
_stopTimer();
|
||||
NRF_RESET_EVENT(NRF5_RADIO_TIMER->EVENTS_COMPARE[1]);
|
||||
if (ack_received == false) {
|
||||
// missing ACK, start TX again
|
||||
NRF5_ESB_starttx();
|
||||
} else {
|
||||
// finised TX
|
||||
NRF5_ESB_endtx();
|
||||
}
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
174
lib/MySensors/hal/transport/NRF5_ESB/driver/Radio_ESB.h
Normal file
174
lib/MySensors/hal/transport/NRF5_ESB/driver/Radio_ESB.h
Normal file
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
* 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 Frank Holtz
|
||||
* Copyright (C) 2017 Frank Holtz
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef __NRF5_ESB_H__
|
||||
#define __NRF5_ESB_H__
|
||||
|
||||
#include "Radio.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
// Check maximum message length
|
||||
#if MAX_MESSAGE_SIZE > (32)
|
||||
#error "Unsupported message size. (MAX_MESSAGE_SIZE)"
|
||||
#endif
|
||||
|
||||
// check rx buffer size
|
||||
#if MY_NRF5_ESB_RX_BUFFER_SIZE < (4)
|
||||
#error "MY_NRF5_ESB_RX_BUFFER_SIZE must be greater than 3."
|
||||
#endif
|
||||
|
||||
/** Wait for start of an ACK packet in us
|
||||
* Calculation: ramp up time + packet header (57 Bit): round to 9 Byte
|
||||
* If you don't receive ACK packages, you have to increase this value.
|
||||
* My measured (Arduino Uno + nRF24L01P) minimal timeouts:
|
||||
* 250kbit 411us -> 182 us to ACK start
|
||||
* 1MBit 205us -> 147 us
|
||||
* 2MBit 173us -> 143 us
|
||||
*/
|
||||
#define NRF5_ESB_ACK_WAIT \
|
||||
((NRF5_ESB_RAMP_UP_TIME << 1) + (9 << NRF5_ESB_byte_time()))
|
||||
|
||||
// auto retry delay in us, don't set this value < 1500us@250kbit
|
||||
#define NRF5_ESB_ARD (1500)
|
||||
|
||||
// auto retry count with noACK is false
|
||||
#define NRF5_ESB_ARC_ACK (15)
|
||||
|
||||
// auto retry count with noACK is true
|
||||
#define NRF5_ESB_ARC_NOACK (3)
|
||||
|
||||
// How often broadcast messages are send
|
||||
#define NRF5_ESB_BC_ARC (3)
|
||||
|
||||
// Node address index
|
||||
#define NRF5_ESB_NODE_ADDR (0)
|
||||
#define NRF5_ESB_NODE_ADDR_MSK (0xffffff00UL)
|
||||
|
||||
// TX address index
|
||||
#define NRF5_ESB_TX_ADDR (4)
|
||||
#define NRF5_ESB_TX_ADDR_MSK (0xffffff00UL)
|
||||
|
||||
// BC address index
|
||||
#define NRF5_ESB_BC_ADDR (7)
|
||||
#define NRF5_ESB_BC_ADDR_MSK (0xffffffffUL)
|
||||
|
||||
// Shorts for RX mode
|
||||
#define NRF5_ESB_SHORTS_RX \
|
||||
(RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_START_Msk | \
|
||||
RADIO_SHORTS_DISABLED_RXEN_Msk | RADIO_SHORTS_ADDRESS_BCSTART_Msk | \
|
||||
RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk)
|
||||
|
||||
// Shorts for TX mode
|
||||
#define NRF5_ESB_SHORTS_TX \
|
||||
(RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_START_Msk | \
|
||||
RADIO_SHORTS_DISABLED_TXEN_Msk | RADIO_SHORTS_ADDRESS_BCSTART_Msk)
|
||||
|
||||
// Shorts to switch from RX to TX
|
||||
#define NRF5_ESB_SHORTS_RX_TX \
|
||||
(RADIO_SHORTS_END_DISABLE_Msk | RADIO_SHORTS_DISABLED_TXEN_Msk | \
|
||||
RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_ADDRESS_BCSTART_Msk)
|
||||
|
||||
// Shorts to switch from TX to RX
|
||||
#define NRF5_ESB_SHORTS_TX_RX \
|
||||
(RADIO_SHORTS_END_DISABLE_Msk | RADIO_SHORTS_DISABLED_RXEN_Msk | \
|
||||
RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_ADDRESS_BCSTART_Msk | \
|
||||
RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk)
|
||||
|
||||
// PPI Channels for TX
|
||||
#if (NRF5_RADIO_TIMER_IRQN != TIMER0_IRQn)
|
||||
// Use two regular PPI channels
|
||||
#define NRF5_ESB_PPI_TIMER_START 14
|
||||
#define NRF5_ESB_PPI_TIMER_RADIO_DISABLE 15
|
||||
#else
|
||||
// Use one regular PPI channel and one predefined PPI channel
|
||||
#define NRF5_ESB_USE_PREDEFINED_PPI
|
||||
#define NRF5_ESB_PPI_TIMER_START 15
|
||||
#define NRF5_ESB_PPI_TIMER_RADIO_DISABLE 22
|
||||
#endif
|
||||
#define NRF5_ESB_PPI_BITS \
|
||||
((1 << NRF5_ESB_PPI_TIMER_START) | \
|
||||
(1 << NRF5_ESB_PPI_TIMER_RADIO_DISABLE))
|
||||
|
||||
/** Bitcounter for Packet Control Field length
|
||||
* 6 Bits address length + 3 Bits S1 (NOACK + PID)
|
||||
*/
|
||||
#define NRF5_ESB_BITCOUNTER (9)
|
||||
|
||||
/** ramp up time
|
||||
* Time to activate radio TX or RX mode
|
||||
*/
|
||||
#define NRF5_ESB_RAMP_UP_TIME (140)
|
||||
|
||||
|
||||
static bool NRF5_ESB_initialize();
|
||||
static void NRF5_ESB_powerDown();
|
||||
static void NRF5_ESB_powerUp();
|
||||
static void NRF5_ESB_sleep();
|
||||
static void NRF5_ESB_standBy();
|
||||
static bool NRF5_ESB_sanityCheck();
|
||||
|
||||
static void NRF5_ESB_setNodeAddress(const uint8_t address);
|
||||
static uint8_t NRF5_ESB_getNodeID();
|
||||
|
||||
static void NRF5_ESB_startListening();
|
||||
static bool NRF5_ESB_isDataAvailable();
|
||||
static uint8_t NRF5_ESB_readMessage(void *data);
|
||||
|
||||
static bool NRF5_ESB_sendMessage(uint8_t recipient, const void *buf, uint8_t len, const bool noACK);
|
||||
|
||||
static int16_t NRF5_ESB_getSendingRSSI();
|
||||
static int16_t NRF5_ESB_getReceivingRSSI();
|
||||
|
||||
// Calculate time to transmit an byte in us as bit shift -> 2^X
|
||||
static inline uint8_t NRF5_ESB_byte_time();
|
||||
|
||||
/** Structure of radio rackets
|
||||
*/
|
||||
typedef struct nrf5_radio_packet_s {
|
||||
// structure written by radio unit
|
||||
struct {
|
||||
uint8_t len;
|
||||
union {
|
||||
uint8_t s1;
|
||||
struct {
|
||||
uint8_t noack : 1;
|
||||
uint8_t pid : 2;
|
||||
};
|
||||
};
|
||||
uint8_t data[MAX_MESSAGE_SIZE];
|
||||
int8_t rssi;
|
||||
/** Debug data structure */
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
uint32_t rxmatch;
|
||||
#endif
|
||||
}
|
||||
#ifndef DOXYGEN
|
||||
__attribute__((packed));
|
||||
#endif
|
||||
} NRF5_ESB_Packet;
|
||||
|
||||
#ifdef MY_DEBUG_VERBOSE_NRF5_ESB
|
||||
static uint32_t intcntr_bcmatch;
|
||||
static uint32_t intcntr_ready;
|
||||
static uint32_t intcntr_end;
|
||||
#endif
|
||||
|
||||
#endif // __NRF5_H__
|
||||
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)
|
||||
301
lib/MySensors/hal/transport/RFM69/MyTransportRFM69.cpp
Normal file
301
lib/MySensors/hal/transport/RFM69/MyTransportRFM69.cpp
Normal file
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if defined(MY_RFM69_NEW_DRIVER)
|
||||
|
||||
#include "hal/transport/RFM69/driver/new/RFM69_new.h"
|
||||
|
||||
bool transportInit(void)
|
||||
{
|
||||
const bool result = RFM69_initialise(MY_RFM69_FREQUENCY);
|
||||
#if defined(MY_GATEWAY_FEATURE) || defined(MY_RFM69_ATC_MODE_DISABLED)
|
||||
// ATC mode function not used
|
||||
(void)RFM69_ATCmode;
|
||||
#else
|
||||
RFM69_ATCmode(true, MY_RFM69_ATC_TARGET_RSSI_DBM);
|
||||
#endif
|
||||
|
||||
#ifdef MY_RFM69_ENABLE_ENCRYPTION
|
||||
uint8_t RFM69_psk[16];
|
||||
#ifdef MY_ENCRYPTION_SIMPLE_PASSWD
|
||||
(void)memset(RFM69_psk, 0, 16);
|
||||
(void)memcpy(RFM69_psk, MY_ENCRYPTION_SIMPLE_PASSWD, strnlen(MY_ENCRYPTION_SIMPLE_PASSWD, 16));
|
||||
#else
|
||||
hwReadConfigBlock((void *)RFM69_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
|
||||
#endif
|
||||
RFM69_encrypt((const char *)RFM69_psk);
|
||||
(void)memset(RFM69_psk, 0, 16); // Make sure it is purged from memory when set
|
||||
#else
|
||||
(void)RFM69_encrypt;
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
void transportSetAddress(const uint8_t address)
|
||||
{
|
||||
RFM69_setAddress(address);
|
||||
}
|
||||
|
||||
uint8_t transportGetAddress(void)
|
||||
{
|
||||
return RFM69_getAddress();
|
||||
}
|
||||
|
||||
bool transportSend(const uint8_t to, const void *data, uint8_t len, const bool noACK)
|
||||
{
|
||||
return RFM69_sendWithRetry(to, data, len, noACK);
|
||||
}
|
||||
|
||||
bool transportDataAvailable(void)
|
||||
{
|
||||
RFM69_handler();
|
||||
return RFM69_available();
|
||||
}
|
||||
|
||||
bool transportSanityCheck(void)
|
||||
{
|
||||
return RFM69_sanityCheck();
|
||||
}
|
||||
|
||||
uint8_t transportReceive(void *data)
|
||||
{
|
||||
return RFM69_receive((uint8_t *)data, MAX_MESSAGE_SIZE);
|
||||
}
|
||||
|
||||
void transportEncrypt(const char *key)
|
||||
{
|
||||
RFM69_encrypt(key);
|
||||
}
|
||||
|
||||
void transportSleep(void)
|
||||
{
|
||||
(void)RFM69_sleep();
|
||||
}
|
||||
|
||||
void transportStandBy(void)
|
||||
{
|
||||
(void)RFM69_standBy();
|
||||
}
|
||||
|
||||
void transportPowerDown(void)
|
||||
{
|
||||
(void)RFM69_powerDown();
|
||||
}
|
||||
|
||||
void transportPowerUp(void)
|
||||
{
|
||||
(void)RFM69_powerUp();
|
||||
}
|
||||
|
||||
bool transportSetTxPowerLevel(const uint8_t powerLevel)
|
||||
{
|
||||
// range 0..23
|
||||
return RFM69_setTxPowerLevel(powerLevel);
|
||||
}
|
||||
|
||||
void transportSetTargetRSSI(const int16_t targetSignalStrength)
|
||||
{
|
||||
#if !defined(MY_GATEWAY_FEATURE) && !defined(MY_RFM69_ATC_MODE_DISABLED)
|
||||
RFM69_ATCmode(true, targetSignalStrength);
|
||||
#else
|
||||
(void)targetSignalStrength;
|
||||
#endif
|
||||
}
|
||||
|
||||
int16_t transportGetSendingRSSI(void)
|
||||
{
|
||||
return RFM69_getSendingRSSI();
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingRSSI(void)
|
||||
{
|
||||
return RFM69_getReceivingRSSI();
|
||||
}
|
||||
|
||||
int16_t transportGetSendingSNR(void)
|
||||
{
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingSNR(void)
|
||||
{
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerPercent(void)
|
||||
{
|
||||
return RFM69_getTxPowerPercent();
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerLevel(void)
|
||||
{
|
||||
return RFM69_getTxPowerLevel();
|
||||
}
|
||||
|
||||
bool transportSetTxPowerPercent(const uint8_t powerPercent)
|
||||
{
|
||||
return RFM69_setTxPowerPercent(powerPercent);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "hal/transport/RFM69/driver/old/RFM69_old.h"
|
||||
|
||||
RFM69 _radio(MY_RFM69_CS_PIN, MY_RFM69_IRQ_PIN, MY_RFM69HW, MY_RFM69_IRQ_NUM);
|
||||
uint8_t _address;
|
||||
|
||||
bool transportInit(void)
|
||||
{
|
||||
#if defined(MY_RFM69_POWER_PIN)
|
||||
//hwPinMode(MY_RFM69_POWER_PIN, OUTPUT);
|
||||
#endif
|
||||
#ifdef MY_RF69_DIO5
|
||||
//hwPinMode(MY_RF69_DIO5, INPUT);
|
||||
#endif
|
||||
// Start up the radio library (_address will be set later by the MySensors library)
|
||||
if (_radio.initialize(MY_RFM69_FREQUENCY, _address, MY_RFM69_NETWORKID)) {
|
||||
#ifdef MY_RFM69_ENABLE_ENCRYPTION
|
||||
uint8_t RFM69_psk[16];
|
||||
#ifdef MY_ENCRYPTION_SIMPLE_PASSWD
|
||||
(void)memset(RFM69_psk, 0, 16);
|
||||
(void)memcpy(RFM69_psk, MY_ENCRYPTION_SIMPLE_PASSWD, strnlen(MY_ENCRYPTION_SIMPLE_PASSWD, 16));
|
||||
#else
|
||||
hwReadConfigBlock((void *)RFM69_psk, (void *)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
|
||||
#endif
|
||||
_radio.encrypt((const char *)RFM69_psk);
|
||||
(void)memset(RFM69_psk, 0, 16); // Make sure it is purged from memory when set
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void transportEncrypt(const char *key)
|
||||
{
|
||||
_radio.encrypt(key);
|
||||
}
|
||||
|
||||
void transportSetAddress(const uint8_t address)
|
||||
{
|
||||
_address = address;
|
||||
_radio.setAddress(address);
|
||||
}
|
||||
|
||||
uint8_t transportGetAddress(void)
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
|
||||
bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
|
||||
{
|
||||
if (noACK) {
|
||||
(void)_radio.sendWithRetry(to, data, len, 0, 0);
|
||||
return true;
|
||||
}
|
||||
return _radio.sendWithRetry(to, data, len);
|
||||
}
|
||||
|
||||
bool transportDataAvailable(void)
|
||||
{
|
||||
return _radio.receiveDone();
|
||||
}
|
||||
|
||||
bool transportSanityCheck(void)
|
||||
{
|
||||
return _radio.sanityCheck();
|
||||
}
|
||||
|
||||
uint8_t transportReceive(void *data)
|
||||
{
|
||||
// save payload length
|
||||
const uint8_t dataLen = _radio.DATALEN < MAX_MESSAGE_SIZE ? _radio.DATALEN : MAX_MESSAGE_SIZE;
|
||||
(void)memcpy((void *)data, (void *)_radio.DATA, dataLen);
|
||||
// Send ack back if this message wasn't a broadcast
|
||||
if (_radio.ACKRequested()) {
|
||||
_radio.sendACK();
|
||||
}
|
||||
return dataLen;
|
||||
}
|
||||
|
||||
void transportSleep(void)
|
||||
{
|
||||
_radio.sleep();
|
||||
}
|
||||
|
||||
void transportStandBy(void)
|
||||
{
|
||||
_radio.standBy();
|
||||
}
|
||||
|
||||
void transportPowerDown(void)
|
||||
{
|
||||
_radio.powerDown();
|
||||
}
|
||||
|
||||
void transportPowerUp(void)
|
||||
{
|
||||
_radio.powerUp();
|
||||
}
|
||||
|
||||
int16_t transportGetSendingRSSI(void)
|
||||
{
|
||||
return INVALID_RSSI;
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingRSSI(void)
|
||||
{
|
||||
return _radio.RSSI;
|
||||
}
|
||||
|
||||
int16_t transportGetSendingSNR(void)
|
||||
{
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingSNR(void)
|
||||
{
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerPercent(void)
|
||||
{
|
||||
return INVALID_PERCENT;
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerLevel(void)
|
||||
{
|
||||
return INVALID_LEVEL;
|
||||
}
|
||||
|
||||
bool transportSetTxPowerLevel(const uint8_t powerLevel)
|
||||
{
|
||||
// not implemented
|
||||
(void)powerLevel;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool transportSetTxPowerPercent(const uint8_t powerPercent)
|
||||
{
|
||||
// not implemented
|
||||
(void)powerPercent;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
1037
lib/MySensors/hal/transport/RFM69/driver/new/RFM69_new.cpp
Normal file
1037
lib/MySensors/hal/transport/RFM69/driver/new/RFM69_new.cpp
Normal file
File diff suppressed because it is too large
Load Diff
561
lib/MySensors/hal/transport/RFM69/driver/new/RFM69_new.h
Normal file
561
lib/MySensors/hal/transport/RFM69/driver/new/RFM69_new.h
Normal file
@@ -0,0 +1,561 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* RFM69 driver refactored for MySensors
|
||||
*
|
||||
* Based on :
|
||||
* - LowPowerLab RFM69 Lib Copyright Felix Rusu (2014), felix@lowpowerlab.com
|
||||
* - Automatic Transmit Power Control class derived from RFM69 library.
|
||||
* Discussion and details in this forum post: https://lowpowerlab.com/forum/index.php/topic,688.0.html
|
||||
* Copyright Thomas Studwell (2014,2015)
|
||||
* - MySensors generic radio driver implementation Copyright (C) 2017, 2018 Olivier Mauti <olivier@mysensors.org>
|
||||
*
|
||||
* Changes by : @tekka, @scalz, @marceloagno
|
||||
*
|
||||
* Definitions for Semtech SX1231/H radios:
|
||||
* https://www.semtech.com/uploads/documents/sx1231.pdf
|
||||
* https://www.semtech.com/uploads/documents/sx1231h.pdf
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @file RFM69_new.h
|
||||
*
|
||||
* @defgroup RFM69Newgrp RFM69New
|
||||
* @ingroup internals
|
||||
* @{
|
||||
*
|
||||
* RFM69 driver-related log messages, format: [!]SYSTEM:[SUB SYSTEM:]MESSAGE
|
||||
* - [!] Exclamation mark is prepended in case of error
|
||||
*
|
||||
* |E| SYS | SUB | Message | Comment
|
||||
* |-|-------|------|--------------------------------------|-----------------------------------------------------------------------------------
|
||||
* | | RFM69 | INIT | | Initialise RFM69 radio
|
||||
* | | RFM69 | INIT | PIN,CS=%%d,IQP=%%d,IQN=%%d[,RST=%%d] | Pin configuration: chip select (CS), IRQ pin (IQP), IRQ number (IQN), Reset (RST)
|
||||
* | | RFM69 | INIT | HWV=%%d | HW version, see datasheet chapter 9
|
||||
* |!| RFM69 | INIT | SANCHK FAIL | Sanity check failed, check wiring or replace module
|
||||
* | | RFM69 | PTX | NO ADJ | TX power level, no adjustment
|
||||
* | | RFM69 | PTX | LEVEL=%%d dbM | TX power level, set to (LEVEL) dBm
|
||||
* | | RFM69 | SAC | SEND ACK,TO=%%d,RSSI=%%d | ACK sent to (TO), RSSI of incoming message (RSSI)
|
||||
* | | RFM69 | ATC | ADJ TXL,cR=%%d,tR=%%d..%%d,TXL=%%d | Adjust TX level, current RSSI (cR), target RSSI range (tR), TX level (TXL)
|
||||
* | | RFM69 | SWR | SEND,TO=%%d,SEQ=%%d,RETRY=%%d | Send to (TO), sequence number (SWQ), retry if no ACK received (RETRY)
|
||||
* | | RFM69 | SWR | ACK,FROM=%%d,SEQ=%%d,RSSI=%%d | ACK received from (FROM), sequence nr (SEQ), ACK RSSI (RSSI)
|
||||
* |!| RFM69 | SWR | NACK | Message sent, no ACK received
|
||||
* | | RFM69 | SPP | PCT=%%d,TX LEVEL=%%d | Set TX level, input TX percent (PCT)
|
||||
* | | RFM69 | RSL | | Radio in sleep mode
|
||||
* | | RFM69 | RSB | | Radio in standby mode
|
||||
* | | RFM69 | PWD | | Power down radio
|
||||
* | | RFM69 | PWU | | Power up radio
|
||||
*
|
||||
* @brief API declaration for RFM69
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _RFM69_h
|
||||
#define _RFM69_h
|
||||
|
||||
#include "RFM69registers_new.h"
|
||||
|
||||
#if !defined(RFM69_SPI)
|
||||
#define RFM69_SPI hwSPI //!< default SPI
|
||||
#endif
|
||||
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
#if defined(__AVR_ATmega32U4__)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (3) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#else
|
||||
#define DEFAULT_RFM69_IRQ_PIN (2) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#endif
|
||||
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (5) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (16) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(DEFAULT_RFM69_IRQ_PIN) //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (2) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#elif defined(LINUX_ARCH_RASPBERRYPI)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (22) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#elif defined(ARDUINO_ARCH_STM32F1)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (PA3) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#elif defined(TEENSYDUINO)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (8) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#else
|
||||
#define DEFAULT_RFM69_IRQ_PIN (2) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#endif
|
||||
|
||||
#define DEFAULT_RFM69_CS_PIN (SS) //!< DEFAULT_RFM69_CS_PIN
|
||||
|
||||
// SPI settings
|
||||
#define RFM69_SPI_DATA_ORDER MSBFIRST //!< SPI data order
|
||||
#define RFM69_SPI_DATA_MODE SPI_MODE0 //!< SPI mode
|
||||
|
||||
// Additional radio settings
|
||||
#define RFM69_SYNCVALUE1 (0x2D) //!< Make this compatible with sync1 byte of RFM12B lib
|
||||
|
||||
#if (MY_RFM69HW==true)
|
||||
// RFM69H(C)W
|
||||
#define RFM69_VERSION_HW //!< HW version
|
||||
#define RFM69_MIN_POWER_LEVEL_DBM ((rfm69_powerlevel_t)-2) //!< min. power level, -2dBm
|
||||
#if defined(MY_RFM69_MAX_POWER_LEVEL_DBM)
|
||||
#define RFM69_MAX_POWER_LEVEL_DBM MY_RFM69_MAX_POWER_LEVEL_DBM //!< MY_RFM69_MAX_POWER_LEVEL_DBM
|
||||
#else
|
||||
#define RFM69_MAX_POWER_LEVEL_DBM ((rfm69_powerlevel_t)20) //!< max. power level, +20dBm
|
||||
#endif
|
||||
#else
|
||||
// RFM69(C)W
|
||||
#define RFM69_MIN_POWER_LEVEL_DBM ((rfm69_powerlevel_t)-18) //!< min. power level, -18dBm
|
||||
#if defined(MY_RFM69_MAX_POWER_LEVEL_DBM)
|
||||
#define RFM69_MAX_POWER_LEVEL_DBM MY_RFM69_MAX_POWER_LEVEL_DBM //!< MY_RFM69_MAX_POWER_LEVEL_DBM
|
||||
#else
|
||||
#define RFM69_MAX_POWER_LEVEL_DBM ((rfm69_powerlevel_t)13) //!< max. power level, +13dBm
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define RFM69_FIFO_SIZE (0xFFu) //!< Max number of bytes the Rx/Tx FIFO can hold
|
||||
#define RFM69_MAX_PACKET_LEN (0x40u) //!< This is the maximum number of bytes that can be carried
|
||||
#define RFM69_ATC_TARGET_RANGE_DBM (2u) //!< ATC target range +/- dBm
|
||||
#define RFM69_PACKET_HEADER_VERSION (1u) //!< RFM69 packet header version
|
||||
#define RFM69_MIN_PACKET_HEADER_VERSION (1u) //!< Minimal RFM69 packet header version
|
||||
|
||||
#define RFM69_RETRIES (5u) //!< Retries in case of failed transmission
|
||||
#define RFM69_RETRY_TIMEOUT_MS (200ul) //!< Timeout for ACK, adjustments needed if modem configuration changed (air time different)
|
||||
#define RFM69_MODE_READY_TIMEOUT_MS (50ul) //!< Timeout for mode ready
|
||||
|
||||
#define RFM69_ACK_REQUESTED (7u) //!< RFM69 header, controlFlag, bit 7
|
||||
#define RFM69_ACK_RECEIVED (6u) //!< RFM69 header, controlFlag, bit 6
|
||||
#define RFM69_ACK_RSSI_REPORT (5u) //!< RFM69 header, controlFlag, bit 5
|
||||
|
||||
#define RFM69_BROADCAST_ADDRESS (255u) //!< Broadcasting address
|
||||
#define RFM69_TARGET_RSSI_DBM (-75) //!< RSSI target
|
||||
#define RFM69_HIGH_POWER_DBM (18u) //!< High power threshold, dBm
|
||||
|
||||
#if !defined(MY_RFM69_TX_TIMEOUT_MS)
|
||||
#define MY_RFM69_TX_TIMEOUT_MS (2*1000ul) //!< Timeout for packet sent
|
||||
#endif
|
||||
|
||||
// CSMA settings
|
||||
#if !defined(MY_RFM69_CSMA_LIMIT_DBM)
|
||||
#define MY_RFM69_CSMA_LIMIT_DBM (-95) //!< upper RX signal sensitivity threshold in dBm for carrier sense access
|
||||
#endif
|
||||
#if !defined(MY_RFM69_CSMA_TIMEOUT_MS)
|
||||
#define MY_RFM69_CSMA_TIMEOUT_MS (500ul) //!< CSMA timeout
|
||||
#endif
|
||||
// powerup delay
|
||||
#define RFM69_POWERUP_DELAY_MS (100ul) //!< Power up delay, allow VCC to settle, transport to become fully operational
|
||||
|
||||
// available frequency bands, non trivial values to avoid misconfiguration
|
||||
#define RFM69_315MHZ (315000000ul) //!< RFM69_315MHZ
|
||||
#define RFM69_433MHZ (433920000ul) //!< RFM69_433MHZ, center frequency 433.92 MHz
|
||||
#define RFM69_865MHZ (865500000ul) //!< RFM69_865MHZ, center frequency 865.5 MHz
|
||||
#define RFM69_868MHZ (868000000ul) //!< RFM69_868MHZ
|
||||
#define RFM69_915MHZ (915000000ul) //!< RFM69_915MHZ
|
||||
|
||||
#define RFM69_COURSE_TEMP_COEF (-90) //!< puts the temperature reading in the ballpark, user can fine tune the returned value
|
||||
#define RFM69_FXOSC (32*1000000ul) //!< OSC freq, 32MHz
|
||||
#define RFM69_FSTEP (RFM69_FXOSC / 524288.0f) //!< FXOSC / 2^19 = 32MHz / 2^19 (p13 in datasheet)
|
||||
|
||||
// helper macros
|
||||
#define RFM69_getACKRequested(__value) ((bool)bitRead(__value,RFM69_ACK_REQUESTED)) //!< getACKRequested
|
||||
#define RFM69_setACKRequested(__value, __flag) bitWrite(__value,RFM69_ACK_REQUESTED,__flag) //!< setACKRequested
|
||||
#define RFM69_getACKReceived(__value) ((bool)bitRead(__value,RFM69_ACK_RECEIVED)) //!< getACKReceived
|
||||
#define RFM69_setACKReceived(__value, __flag) bitWrite(__value,RFM69_ACK_RECEIVED,__flag) //!< setACKReceived
|
||||
#define RFM69_setACKRSSIReport(__value, __flag) bitWrite(__value,RFM69_ACK_RSSI_REPORT,__flag)//!< setACKRSSIReport
|
||||
#define RFM69_getACKRSSIReport(__value) ((bool)bitRead(__value,RFM69_ACK_RSSI_REPORT)) //!< getACKRSSIReport
|
||||
|
||||
// Register access
|
||||
#define RFM69_READ_REGISTER (0x7Fu) //!< reading register
|
||||
#define RFM69_WRITE_REGISTER (0x80u) //!< writing register
|
||||
|
||||
// Modem configuration section
|
||||
#define RFM69_CONFIG_FSK (RFM69_DATAMODUL_DATAMODE_PACKET | RFM69_DATAMODUL_MODULATIONTYPE_FSK | RFM69_DATAMODUL_MODULATIONSHAPING_00) //!< RFM69_CONFIG_FSK
|
||||
#define RFM69_CONFIG_GFSK (RFM69_DATAMODUL_DATAMODE_PACKET | RFM69_DATAMODUL_MODULATIONTYPE_FSK | RFM69_DATAMODUL_MODULATIONSHAPING_10) //!< RFM69_CONFIG_GFSK
|
||||
#define RFM69_CONFIG_OOK (RFM69_DATAMODUL_DATAMODE_PACKET | RFM69_DATAMODUL_MODULATIONTYPE_OOK | RFM69_DATAMODUL_MODULATIONSHAPING_00) //!< RFM69_CONFIG_OOK
|
||||
|
||||
#define RFM69_CONFIG_NOWHITE (RFM69_PACKET1_FORMAT_VARIABLE | RFM69_PACKET1_DCFREE_OFF | RFM69_PACKET1_CRC_ON | RFM69_PACKET1_CRCAUTOCLEAR_ON | RFM69_PACKET1_ADRSFILTERING_NODEBROADCAST) //!< RFM69_CONFIG_NOWHITE
|
||||
#define RFM69_CONFIG_WHITE (RFM69_PACKET1_FORMAT_VARIABLE | RFM69_PACKET1_DCFREE_WHITENING | RFM69_PACKET1_CRC_ON | RFM69_PACKET1_CRCAUTOCLEAR_ON | RFM69_PACKET1_ADRSFILTERING_NODEBROADCAST) //!< RFM69_CONFIG_WHITE
|
||||
#define RFM69_CONFIG_MANCHESTER (RFM69_PACKET1_FORMAT_VARIABLE | RFM69_PACKET1_DCFREE_MANCHESTER | RFM69_PACKET1_CRC_ON | RFM69_PACKET1_CRCAUTOCLEAR_ON | RFM69_PACKET1_ADRSFILTERING_NODEBROADCAST) //!< RFM69_CONFIG_MANCHESTER
|
||||
|
||||
#define RFM69_RXBW_111_24_4 (RFM69_RXBW_DCCFREQ_111 | RFM69_RXBW_MANT_24 | RFM69_RXBW_EXP_4) //!< RFM69_RXBW_111_24_4
|
||||
#define RFM69_RXBW_111_24_3 (RFM69_RXBW_DCCFREQ_111 | RFM69_RXBW_MANT_24 | RFM69_RXBW_EXP_3) //!< RFM69_RXBW_111_24_3
|
||||
#define RFM69_RXBW_111_24_2 (RFM69_RXBW_DCCFREQ_111 | RFM69_RXBW_MANT_24 | RFM69_RXBW_EXP_2) //!< RFM69_RXBW_111_24_2
|
||||
#define RFM69_RXBW_111_16_2 (RFM69_RXBW_DCCFREQ_111 | RFM69_RXBW_MANT_16 | RFM69_RXBW_EXP_2) //!< RFM69_RXBW_111_16_2
|
||||
#define RFM69_RXBW_111_16_1 (RFM69_RXBW_DCCFREQ_111 | RFM69_RXBW_MANT_16 | RFM69_RXBW_EXP_1) //!< RFM69_RXBW_111_16_1
|
||||
#define RFM69_RXBW_111_16_0 (RFM69_RXBW_DCCFREQ_111 | RFM69_RXBW_MANT_16 | RFM69_RXBW_EXP_0) //!< RFM69_RXBW_111_16_0
|
||||
#define RFM69_RXBW_010_16_2 (RFM69_RXBW_DCCFREQ_010 | RFM69_RXBW_MANT_16 | RFM69_RXBW_EXP_2) //!< RFM69_RXBW_010_16_2
|
||||
|
||||
#define RFM69_FSK_BR2_FD5 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_2000, RFM69_BITRATELSB_2000, RFM69_FDEVMSB_5000, RFM69_FDEVLSB_5000, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR2_FD5
|
||||
#define RFM69_FSK_BR2_4_FD4_8 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_2400, RFM69_BITRATELSB_2400, RFM69_FDEVMSB_4800, RFM69_FDEVLSB_4800, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR2_4_FD4_8
|
||||
#define RFM69_FSK_BR4_8_FD9_6 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_4800, RFM69_BITRATELSB_4800, RFM69_FDEVMSB_9600, RFM69_FDEVLSB_9600, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR4_8_FD9_6
|
||||
#define RFM69_FSK_BR9_6_FD19_2 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_9600, RFM69_BITRATELSB_9600, RFM69_FDEVMSB_19200, RFM69_FDEVLSB_19200, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR9_6_FD19_2
|
||||
#define RFM69_FSK_BR19_2_FD38_4 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_19200, RFM69_BITRATELSB_19200, RFM69_FDEVMSB_38400, RFM69_FDEVLSB_38400, RFM69_RXBW_111_24_3, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR19_2_FD38_4
|
||||
#define RFM69_FSK_BR38_4_FD76_8 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_38400, RFM69_BITRATELSB_38400, RFM69_FDEVMSB_76800, RFM69_FDEVLSB_76800, RFM69_RXBW_111_24_2, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR38_4_FD76_8
|
||||
#define RFM69_FSK_BR55_5_FD50 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_55555, RFM69_BITRATELSB_55555, RFM69_FDEVMSB_50000, RFM69_FDEVLSB_50000, RFM69_RXBW_111_16_2, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR55_5_FD50
|
||||
#define RFM69_FSK_BR57_6_FD120 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_57600, RFM69_BITRATELSB_57600, RFM69_FDEVMSB_120000, RFM69_FDEVLSB_120000, RFM69_RXBW_111_16_1, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR57_6_FD120
|
||||
#define RFM69_FSK_BR125_FD125 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_125000, RFM69_BITRATELSB_125000, RFM69_FDEVMSB_125000, RFM69_FDEVLSB_125000, RFM69_RXBW_010_16_2, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR125_FD125
|
||||
#define RFM69_FSK_BR250_FD250 RFM69_CONFIG_FSK, RFM69_BITRATEMSB_250000, RFM69_BITRATELSB_250000, RFM69_FDEVMSB_250000, RFM69_FDEVLSB_250000, RFM69_RXBW_111_16_0, RFM69_CONFIG_WHITE //!< RFM69_FSK_BR250_FD250
|
||||
|
||||
#define RFM69_GFSK_BR2_FD5 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_2000, RFM69_BITRATELSB_2000, RFM69_FDEVMSB_5000, RFM69_FDEVLSB_5000, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR2_FD5
|
||||
#define RFM69_GFSK_BR2_4_FD4_8 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_2400, RFM69_BITRATELSB_2400, RFM69_FDEVMSB_4800, RFM69_FDEVLSB_4800, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR2_4_FD4_8
|
||||
#define RFM69_GFSK_BR4_8_FD9_6 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_4800, RFM69_BITRATELSB_4800, RFM69_FDEVMSB_9600, RFM69_FDEVLSB_9600, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR4_8_FD9_6
|
||||
#define RFM69_GFSK_BR9_6_FD19_2 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_9600, RFM69_BITRATELSB_9600, RFM69_FDEVMSB_19200, RFM69_FDEVLSB_19200, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR9_6_FD19_2
|
||||
#define RFM69_GFSK_BR19_2_FD38_4 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_19200, RFM69_BITRATELSB_19200, RFM69_FDEVMSB_38400, RFM69_FDEVLSB_38400, RFM69_RXBW_111_24_3, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR19_2_FD38_4
|
||||
#define RFM69_GFSK_BR38_4_FD76_8 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_38400, RFM69_BITRATELSB_38400, RFM69_FDEVMSB_76800, RFM69_FDEVLSB_76800, RFM69_RXBW_111_24_2, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR38_4_FD76_8
|
||||
#define RFM69_GFSK_BR55_5_FD50 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_55555, RFM69_BITRATELSB_55555, RFM69_FDEVMSB_50000, RFM69_FDEVLSB_50000, RFM69_RXBW_111_16_2, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR55_5_FD50
|
||||
#define RFM69_GFSK_BR57_6_FD120 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_57600, RFM69_BITRATELSB_57600, RFM69_FDEVMSB_120000, RFM69_FDEVLSB_120000, RFM69_RXBW_111_16_1, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR57_6_FD120
|
||||
#define RFM69_GFSK_BR125_FD125 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_125000, RFM69_BITRATELSB_125000, RFM69_FDEVMSB_125000, RFM69_FDEVLSB_125000, RFM69_RXBW_010_16_2, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR125_FD125
|
||||
#define RFM69_GFSK_BR250_FD250 RFM69_CONFIG_GFSK, RFM69_BITRATEMSB_250000, RFM69_BITRATELSB_250000, RFM69_FDEVMSB_250000, RFM69_FDEVLSB_250000, RFM69_RXBW_111_16_0, RFM69_CONFIG_WHITE //!< RFM69_GFSK_BR250_FD250
|
||||
|
||||
#define RFM69_OOK_BR2_FD5 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_2000, RFM69_BITRATELSB_2000, RFM69_FDEVMSB_5000, RFM69_FDEVLSB_5000, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR2_FD5
|
||||
#define RFM69_OOK_BR2_4_FD4_8 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_2400, RFM69_BITRATELSB_2400, RFM69_FDEVMSB_4800, RFM69_FDEVLSB_4800, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR2_4_FD4_8
|
||||
#define RFM69_OOK_BR4_8_FD9_6 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_4800, RFM69_BITRATELSB_4800, RFM69_FDEVMSB_9600, RFM69_FDEVLSB_9600, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR4_8_FD9_6
|
||||
#define RFM69_OOK_BR9_6_FD19_2 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_9600, RFM69_BITRATELSB_9600, RFM69_FDEVMSB_19200, RFM69_FDEVLSB_19200, RFM69_RXBW_111_24_4, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR9_6_FD19_2
|
||||
#define RFM69_OOK_BR19_2_FD38_4 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_19200, RFM69_BITRATELSB_19200, RFM69_FDEVMSB_38400, RFM69_FDEVLSB_38400, RFM69_RXBW_111_24_3, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR19_2_FD38_4
|
||||
#define RFM69_OOK_BR38_4_FD76_8 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_38400, RFM69_BITRATELSB_38400, RFM69_FDEVMSB_76800, RFM69_FDEVLSB_76800, RFM69_RXBW_111_24_2, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR38_4_FD76_8
|
||||
#define RFM69_OOK_BR55_5_FD50 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_55555, RFM69_BITRATELSB_55555, RFM69_FDEVMSB_50000, RFM69_FDEVLSB_50000, RFM69_RXBW_111_16_2, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR55_5_FD50
|
||||
#define RFM69_OOK_BR57_6_FD120 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_57600, RFM69_BITRATELSB_57600, RFM69_FDEVMSB_120000, RFM69_FDEVLSB_120000, RFM69_RXBW_111_16_1, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR57_6_FD120
|
||||
#define RFM69_OOK_BR125_FD125 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_125000, RFM69_BITRATELSB_125000, RFM69_FDEVMSB_125000, RFM69_FDEVLSB_125000, RFM69_RXBW_010_16_2, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR125_FD125
|
||||
#define RFM69_OOK_BR250_FD250 RFM69_CONFIG_OOK, RFM69_BITRATEMSB_250000, RFM69_BITRATELSB_250000, RFM69_FDEVMSB_250000, RFM69_FDEVLSB_250000, RFM69_RXBW_111_16_0, RFM69_CONFIG_WHITE //!< RFM69_OOK_BR250_FD250
|
||||
|
||||
#if !defined(MY_RFM69_MODEM_CONFIGURATION)
|
||||
#define MY_RFM69_MODEM_CONFIGURATION RFM69_FSK_BR55_5_FD50 //!< default setting, RFM69_FSK_BR55_5_FD50
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Radio modes
|
||||
*/
|
||||
typedef enum {
|
||||
RFM69_RADIO_MODE_RX = 0, //!< RX mode
|
||||
RFM69_RADIO_MODE_TX = 1, //!< TX mode
|
||||
RFM69_RADIO_MODE_CAD = 2, //!< CAD mode
|
||||
RFM69_RADIO_MODE_SLEEP = 3, //!< SLEEP mode
|
||||
RFM69_RADIO_MODE_STDBY = 4, //!< STDBY mode
|
||||
RFM69_RADIO_MODE_SYNTH = 5, //!< SYNTH mode
|
||||
RFM69_RADIO_MODE_LISTEN = 6 //!< LISTEN mode
|
||||
} rfm69_radio_mode_t;
|
||||
|
||||
/**
|
||||
* @brief Sequence number data type
|
||||
*/
|
||||
typedef uint8_t rfm69_sequenceNumber_t;
|
||||
/**
|
||||
* @brief RSSI data type
|
||||
*/
|
||||
typedef uint8_t rfm69_RSSI_t;
|
||||
/**
|
||||
* @brief SNR data type
|
||||
*/
|
||||
typedef int8_t rfm69_SNR_t;
|
||||
/**
|
||||
* @brief Control flag data type
|
||||
*/
|
||||
typedef uint8_t rfm69_controlFlags_t;
|
||||
/**
|
||||
* @brief Power level in dBm
|
||||
*/
|
||||
typedef int8_t rfm69_powerlevel_t;
|
||||
|
||||
/**
|
||||
* @brief RFM69 header
|
||||
* IMPORTANT: Do not change order (see datasheet for packet structure)
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t packetLen; //!< packet length
|
||||
uint8_t recipient; //!< payload recipient
|
||||
uint8_t version; //!< header version (20180128tk: >=3.0.0 fused with controlFlags)
|
||||
uint8_t sender; //!< payload sender
|
||||
rfm69_controlFlags_t controlFlags; //!< control flags, used for ACK
|
||||
rfm69_sequenceNumber_t sequenceNumber; //!< packet sequence number, used for ACK
|
||||
} __attribute__((packed)) rfm69_header_t;
|
||||
|
||||
/**
|
||||
* @brief RFM69 ACK packet structure
|
||||
*/
|
||||
typedef struct {
|
||||
rfm69_sequenceNumber_t sequenceNumber; //!< sequence number
|
||||
rfm69_RSSI_t RSSI; //!< RSSI
|
||||
} __attribute__((packed)) rfm69_ack_t;
|
||||
|
||||
#define RFM69_HEADER_LEN sizeof(rfm69_header_t) //!< Size header inside payload
|
||||
#define RFM69_MAX_PAYLOAD_LEN (RFM69_MAX_PACKET_LEN - RFM69_HEADER_LEN) //!< Max payload length
|
||||
|
||||
/**
|
||||
* @brief Packet structure
|
||||
* IMPORTANT: Do not change order
|
||||
*/
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
rfm69_header_t header; //!< Packet header
|
||||
union {
|
||||
uint8_t payload[RFM69_MAX_PAYLOAD_LEN]; //!< Union: Data Payload, i.e. MySensors message
|
||||
rfm69_ack_t ACK; //!< Union: ACK payload (internal)
|
||||
};
|
||||
};
|
||||
uint8_t data[RFM69_MAX_PACKET_LEN]; //!< RAW data access
|
||||
};
|
||||
uint8_t payloadLen; //!< Length of payload (excluding header)
|
||||
rfm69_RSSI_t RSSI; //!< RSSI of current packet, RSSI = value - 137
|
||||
} __attribute__((packed)) rfm69_packet_t;
|
||||
|
||||
/**
|
||||
* @brief RFM69 internal variables
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t address; //!< Node address
|
||||
rfm69_packet_t currentPacket; //!< Buffer for current packet
|
||||
rfm69_sequenceNumber_t txSequenceNumber; //!< RFM69_txSequenceNumber
|
||||
rfm69_powerlevel_t powerLevel; //!< TX power level dBm
|
||||
uint8_t ATCtargetRSSI; //!< ATC: target RSSI
|
||||
// 8 bit
|
||||
rfm69_radio_mode_t radioMode : 3; //!< current transceiver state
|
||||
bool dataReceived : 1; //!< data received
|
||||
bool ackReceived : 1; //!< ACK received
|
||||
bool ATCenabled : 1; //!< ATC enabled
|
||||
uint8_t reserved : 2; //!< Reserved
|
||||
} rfm69_internal_t;
|
||||
|
||||
#define LOCAL static //!< static
|
||||
|
||||
/**
|
||||
* @brief RFM69_handler
|
||||
*/
|
||||
LOCAL void RFM69_handler(void);
|
||||
|
||||
/**
|
||||
* @brief Clear flags and FIFO
|
||||
*/
|
||||
LOCAL void RFM69_clearFIFO(void);
|
||||
|
||||
/**
|
||||
* @brief Check for channel activity
|
||||
* @return True if channel activity under RFM69_CSMA_LIMIT_DBM
|
||||
*/
|
||||
LOCAL bool RFM69_channelFree(void);
|
||||
|
||||
/**
|
||||
* @brief RFM69_interruptHandling
|
||||
*/
|
||||
LOCAL void RFM69_interruptHandling(void);
|
||||
|
||||
/**
|
||||
* @brief Initialise the driver transport hardware and software
|
||||
* @param frequencyHz Frequency in Hz
|
||||
* @return True if initialisation succeeded
|
||||
*/
|
||||
LOCAL bool RFM69_initialise(const uint32_t frequencyHz);
|
||||
|
||||
/**
|
||||
* @brief Set the driver/node address
|
||||
* @param addr
|
||||
*/
|
||||
LOCAL void RFM69_setAddress(const uint8_t addr);
|
||||
|
||||
/**
|
||||
* @brief Get driver/node address
|
||||
* @return Node address
|
||||
*/
|
||||
LOCAL uint8_t RFM69_getAddress(void);
|
||||
|
||||
/**
|
||||
* @brief Tests whether a new message is available
|
||||
* @return True if a new, complete, error-free uncollected message is available to be retreived by @ref RFM69_receive()
|
||||
*/
|
||||
LOCAL bool RFM69_available(void);
|
||||
|
||||
/**
|
||||
* @brief If a valid message is received, copy it to buf and return length. 0 byte messages are permitted.
|
||||
* @param buf Location to copy the received message
|
||||
* @param maxBufSize Max buffer size
|
||||
* @return Number of bytes
|
||||
*/
|
||||
LOCAL uint8_t RFM69_receive(uint8_t *buf, const uint8_t maxBufSize);
|
||||
|
||||
/**
|
||||
* @brief RFM69_sendFrame
|
||||
* @param packet
|
||||
* @param increaseSequenceCounter
|
||||
* @return True if packet sent
|
||||
*/
|
||||
LOCAL bool RFM69_sendFrame(rfm69_packet_t *packet, const bool increaseSequenceCounter = true);
|
||||
|
||||
/**
|
||||
* @brief RFM69_send
|
||||
* @param recipient
|
||||
* @param data
|
||||
* @param len
|
||||
* @param flags
|
||||
* @param increaseSequenceCounter
|
||||
* @return True if frame sent
|
||||
*/
|
||||
LOCAL bool RFM69_send(const uint8_t recipient, uint8_t *data, const uint8_t len,
|
||||
const rfm69_controlFlags_t flags, const bool increaseSequenceCounter = true);
|
||||
|
||||
/**
|
||||
* @brief Sets the transmitter and receiver center frequency
|
||||
* @param frequencyHz Frequency in Hz
|
||||
*/
|
||||
LOCAL void RFM69_setFrequency(const uint32_t frequencyHz);
|
||||
|
||||
/**
|
||||
* @brief Sets the transmitter power output level, and configures the transmitter pin
|
||||
* @param newPowerLevel Transmitter power level in dBm (-18 to +20dBm)
|
||||
* @return True power level adjusted
|
||||
*/
|
||||
LOCAL bool RFM69_setTxPowerLevel(rfm69_powerlevel_t newPowerLevel);
|
||||
|
||||
/**
|
||||
* @brief Reports the transmitter power output level in dBm
|
||||
* @return power level
|
||||
*/
|
||||
LOCAL rfm69_powerlevel_t RFM69_getTxPowerLevel(void);
|
||||
|
||||
/**
|
||||
* @brief Reports the transmitter power output level in percents
|
||||
* @return power level
|
||||
*/
|
||||
LOCAL uint8_t RFM69_getTxPowerPercent(void);
|
||||
|
||||
/**
|
||||
* @brief Sets the radio into low-power sleep mode
|
||||
* @return true if sleep mode was successfully entered
|
||||
*/
|
||||
LOCAL bool RFM69_sleep(void);
|
||||
|
||||
/**
|
||||
* @brief Sets the radio to standby mode
|
||||
* @return true if standby mode was successfully entered
|
||||
*/
|
||||
LOCAL bool RFM69_standBy(void);
|
||||
|
||||
/**
|
||||
* @brief Power down radio (HW)
|
||||
*/
|
||||
LOCAL void RFM69_powerDown(void);
|
||||
|
||||
/**
|
||||
* @brief Power up radio (HW)
|
||||
*/
|
||||
LOCAL void RFM69_powerUp(void);
|
||||
|
||||
/**
|
||||
* @brief RFM69_sendACK
|
||||
* @param recipient
|
||||
* @param sequenceNumber
|
||||
* @param RSSI (rfm95_RSSI_t)
|
||||
*/
|
||||
LOCAL void RFM69_sendACK(const uint8_t recipient, const rfm69_sequenceNumber_t sequenceNumber,
|
||||
const rfm69_RSSI_t RSSI);
|
||||
|
||||
/**
|
||||
* @brief RFM69_sendWithRetry
|
||||
* @param recipient
|
||||
* @param buffer
|
||||
* @param bufferSize
|
||||
* @param noACK
|
||||
* @return True if packet successfully sent
|
||||
*/
|
||||
LOCAL bool RFM69_sendWithRetry(const uint8_t recipient, const void *buffer,
|
||||
const uint8_t bufferSize,
|
||||
const bool noACK);
|
||||
|
||||
/**
|
||||
* @brief RFM69_setRadioMode
|
||||
* @param newRadioMode
|
||||
* @return True if mode changed
|
||||
*/
|
||||
LOCAL bool RFM69_setRadioMode(const rfm69_radio_mode_t newRadioMode);
|
||||
|
||||
/**
|
||||
* @brief Low level interrupt handler
|
||||
*/
|
||||
LOCAL void RFM69_interruptHandler(void);
|
||||
|
||||
/**
|
||||
* @brief RFM69_getSendingRSSI
|
||||
* @return RSSI Own RSSI as measured at the receiving node of last sent packet (if ACK & ATC enabled)
|
||||
*/
|
||||
LOCAL int16_t RFM69_getSendingRSSI(void);
|
||||
|
||||
/**
|
||||
* @brief RFM69_getReceivingRSSI
|
||||
* @return RSSI Signal strength of last received packet
|
||||
*/
|
||||
LOCAL int16_t RFM69_getReceivingRSSI(void);
|
||||
|
||||
/**
|
||||
* @brief RFM69_executeATC
|
||||
* @param currentRSSI
|
||||
* @param targetRSSI
|
||||
* @return True if power level adjusted
|
||||
*/
|
||||
LOCAL bool RFM69_executeATC(const rfm69_RSSI_t currentRSSI, const rfm69_RSSI_t targetRSSI);
|
||||
|
||||
// TEMP ADDED
|
||||
/**
|
||||
* @brief RFM69_setConfiguration Set general radio register configuration TODO temp use setmodemregisters
|
||||
*/
|
||||
LOCAL void RFM69_setConfiguration(void);
|
||||
|
||||
/**
|
||||
* @brief RFM69_isModeReady
|
||||
* @return True if Mode Ready is ok, false is timeout
|
||||
*/
|
||||
LOCAL bool RFM69_isModeReady(void);
|
||||
|
||||
/**
|
||||
* @brief RFM69_sanityCheck detect HW defect, configuration errors or interrupted SPI line
|
||||
* @return True if radio sanity check passed
|
||||
*/
|
||||
LOCAL bool RFM69_sanityCheck(void);
|
||||
|
||||
/**
|
||||
* @brief RFM69_encrypt Set encryption mode
|
||||
* @param key if key is null, encryption is disabled. Key has to be 16 bytes!
|
||||
*/
|
||||
LOCAL void RFM69_encrypt(const char *key);
|
||||
|
||||
/**
|
||||
* @brief RFM69_setHighPowerRegs
|
||||
* @param onOff
|
||||
*/
|
||||
LOCAL void RFM69_setHighPowerRegs(const bool onOff);
|
||||
|
||||
/**
|
||||
* @brief RFM69_readRSSI
|
||||
* @param forceTrigger
|
||||
* @return RSSI (internal format)
|
||||
*/
|
||||
LOCAL rfm69_RSSI_t RFM69_readRSSI(const bool forceTrigger = false);
|
||||
|
||||
/**
|
||||
* @brief RFM69_ATCmode
|
||||
* @param targetRSSI Target RSSI for transmitter (default -60)
|
||||
* @param onOff True to enable ATC
|
||||
*/
|
||||
LOCAL void RFM69_ATCmode(const bool onOff, const int16_t targetRSSI = RFM69_TARGET_RSSI_DBM);
|
||||
|
||||
/**
|
||||
* @brief RFM69_readAllRegs
|
||||
* Read and display all RFM69 register contents.
|
||||
* @note define RFM69_REGISTER_DETAIL for register content decoding.
|
||||
*/
|
||||
LOCAL void RFM69_readAllRegs(void);
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
@@ -0,0 +1,984 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* RFM69 driver refactored for MySensors
|
||||
*
|
||||
* Based on :
|
||||
* - LowPowerLab RFM69 Lib Copyright Felix Rusu (2014), felix@lowpowerlab.com
|
||||
* - Automatic Transmit Power Control class derived from RFM69 library.
|
||||
* Discussion and details in this forum post: https://lowpowerlab.com/forum/index.php/topic,688.0.html
|
||||
* Copyright Thomas Studwell (2014,2015)
|
||||
* - MySensors generic radio driver implementation Copyright (C) 2017, 2018 Olivier Mauti <olivier@mysensors.org>
|
||||
*
|
||||
* Changes by : @tekka, @scalz, @marceloagno
|
||||
*
|
||||
* Definitions for Semtech SX1231/H radios:
|
||||
* https://www.semtech.com/uploads/documents/sx1231.pdf
|
||||
* https://www.semtech.com/uploads/documents/sx1231h.pdf
|
||||
*/
|
||||
|
||||
#define RFM69_REG_FIFO 0x00
|
||||
#define RFM69_REG_OPMODE 0x01
|
||||
#define RFM69_REG_DATAMODUL 0x02
|
||||
#define RFM69_REG_BITRATEMSB 0x03
|
||||
#define RFM69_REG_BITRATELSB 0x04
|
||||
#define RFM69_REG_FDEVMSB 0x05
|
||||
#define RFM69_REG_FDEVLSB 0x06
|
||||
#define RFM69_REG_FRFMSB 0x07
|
||||
#define RFM69_REG_FRFMID 0x08
|
||||
#define RFM69_REG_FRFLSB 0x09
|
||||
#define RFM69_REG_OSC1 0x0A
|
||||
#define RFM69_REG_AFCCTRL 0x0B
|
||||
#define RFM69_REG_LOWBAT 0x0C
|
||||
#define RFM69_REG_LISTEN1 0x0D
|
||||
#define RFM69_REG_LISTEN2 0x0E
|
||||
#define RFM69_REG_LISTEN3 0x0F
|
||||
#define RFM69_REG_VERSION 0x10
|
||||
#define RFM69_REG_PALEVEL 0x11
|
||||
#define RFM69_REG_PARAMP 0x12
|
||||
#define RFM69_REG_OCP 0x13
|
||||
#define RFM69_REG_AGCREF 0x14 // not present on RFM69/SX1231
|
||||
#define RFM69_REG_AGCTHRESH1 0x15 // not present on RFM69/SX1231
|
||||
#define RFM69_REG_AGCTHRESH2 0x16 // not present on RFM69/SX1231
|
||||
#define RFM69_REG_AGCTHRESH3 0x17 // not present on RFM69/SX1231
|
||||
#define RFM69_REG_LNA 0x18
|
||||
#define RFM69_REG_RXBW 0x19
|
||||
#define RFM69_REG_AFCBW 0x1A
|
||||
#define RFM69_REG_OOKPEAK 0x1B
|
||||
#define RFM69_REG_OOKAVG 0x1C
|
||||
#define RFM69_REG_OOKFIX 0x1D
|
||||
#define RFM69_REG_AFCFEI 0x1E
|
||||
#define RFM69_REG_AFCMSB 0x1F
|
||||
#define RFM69_REG_AFCLSB 0x20
|
||||
#define RFM69_REG_FEIMSB 0x21
|
||||
#define RFM69_REG_FEILSB 0x22
|
||||
#define RFM69_REG_RSSICONFIG 0x23
|
||||
#define RFM69_REG_RSSIVALUE 0x24
|
||||
#define RFM69_REG_DIOMAPPING1 0x25
|
||||
#define RFM69_REG_DIOMAPPING2 0x26
|
||||
#define RFM69_REG_IRQFLAGS1 0x27
|
||||
#define RFM69_REG_IRQFLAGS2 0x28
|
||||
#define RFM69_REG_RSSITHRESH 0x29
|
||||
#define RFM69_REG_RXTIMEOUT1 0x2A
|
||||
#define RFM69_REG_RXTIMEOUT2 0x2B
|
||||
#define RFM69_REG_PREAMBLEMSB 0x2C
|
||||
#define RFM69_REG_PREAMBLELSB 0x2D
|
||||
#define RFM69_REG_SYNCCONFIG 0x2E
|
||||
#define RFM69_REG_SYNCVALUE1 0x2F
|
||||
#define RFM69_REG_SYNCVALUE2 0x30
|
||||
#define RFM69_REG_SYNCVALUE3 0x31
|
||||
#define RFM69_REG_SYNCVALUE4 0x32
|
||||
#define RFM69_REG_SYNCVALUE5 0x33
|
||||
#define RFM69_REG_SYNCVALUE6 0x34
|
||||
#define RFM69_REG_SYNCVALUE7 0x35
|
||||
#define RFM69_REG_SYNCVALUE8 0x36
|
||||
#define RFM69_REG_PACKETCONFIG1 0x37
|
||||
#define RFM69_REG_PAYLOADLENGTH 0x38
|
||||
#define RFM69_REG_NODEADRS 0x39
|
||||
#define RFM69_REG_BROADCASTADRS 0x3A
|
||||
#define RFM69_REG_AUTOMODES 0x3B
|
||||
#define RFM69_REG_FIFOTHRESH 0x3C
|
||||
#define RFM69_REG_PACKETCONFIG2 0x3D
|
||||
#define RFM69_REG_AESKEY1 0x3E
|
||||
#define RFM69_REG_AESKEY2 0x3F
|
||||
#define RFM69_REG_AESKEY3 0x40
|
||||
#define RFM69_REG_AESKEY4 0x41
|
||||
#define RFM69_REG_AESKEY5 0x42
|
||||
#define RFM69_REG_AESKEY6 0x43
|
||||
#define RFM69_REG_AESKEY7 0x44
|
||||
#define RFM69_REG_AESKEY8 0x45
|
||||
#define RFM69_REG_AESKEY9 0x46
|
||||
#define RFM69_REG_AESKEY10 0x47
|
||||
#define RFM69_REG_AESKEY11 0x48
|
||||
#define RFM69_REG_AESKEY12 0x49
|
||||
#define RFM69_REG_AESKEY13 0x4A
|
||||
#define RFM69_REG_AESKEY14 0x4B
|
||||
#define RFM69_REG_AESKEY15 0x4C
|
||||
#define RFM69_REG_AESKEY16 0x4D
|
||||
#define RFM69_REG_TEMP1 0x4E
|
||||
#define RFM69_REG_TEMP2 0x4F
|
||||
#define RFM69_REG_TESTLNA 0x58
|
||||
#define RFM69_REG_TESTPA1 0x5A // only present on RFM69HW/SX1231H
|
||||
#define RFM69_REG_TESTPA2 0x5C // only present on RFM69HW/SX1231H
|
||||
#define RFM69_REG_TESTDAGC 0x6F
|
||||
|
||||
//******************************************************
|
||||
// RF69/SX1231 bit control definition
|
||||
//******************************************************
|
||||
|
||||
// RegOpMode
|
||||
#define RFM69_OPMODE_SEQUENCER_OFF 0x80
|
||||
#define RFM69_OPMODE_SEQUENCER_ON 0x00 // Default
|
||||
|
||||
#define RFM69_OPMODE_LISTEN_ON 0x40
|
||||
#define RFM69_OPMODE_LISTEN_OFF 0x00 // Default
|
||||
|
||||
#define RFM69_OPMODE_LISTENABORT 0x20
|
||||
|
||||
#define RFM69_OPMODE_SLEEP 0x00
|
||||
#define RFM69_OPMODE_STANDBY 0x04 // Default
|
||||
#define RFM69_OPMODE_SYNTHESIZER 0x08
|
||||
#define RFM69_OPMODE_TRANSMITTER 0x0C
|
||||
#define RFM69_OPMODE_RECEIVER 0x10
|
||||
|
||||
|
||||
// RegDataModul
|
||||
#define RFM69_DATAMODUL_DATAMODE_PACKET 0x00 // Default
|
||||
#define RFM69_DATAMODUL_DATAMODE_CONTINUOUS 0x40
|
||||
#define RFM69_DATAMODUL_DATAMODE_CONTINUOUSNOBSYNC 0x60
|
||||
|
||||
#define RFM69_DATAMODUL_MODULATIONTYPE_FSK 0x00 // Default
|
||||
#define RFM69_DATAMODUL_MODULATIONTYPE_OOK 0x08
|
||||
|
||||
#define RFM69_DATAMODUL_MODULATIONSHAPING_00 0x00 // Default
|
||||
#define RFM69_DATAMODUL_MODULATIONSHAPING_01 0x01
|
||||
#define RFM69_DATAMODUL_MODULATIONSHAPING_10 0x02
|
||||
#define RFM69_DATAMODUL_MODULATIONSHAPING_11 0x03
|
||||
|
||||
// RegBitRate (bits/sec) example bit rates
|
||||
#define RFM69_BITRATEMSB_1200 0x68
|
||||
#define RFM69_BITRATELSB_1200 0x2B
|
||||
#define RFM69_BITRATEMSB_2000 0x3e
|
||||
#define RFM69_BITRATELSB_2000 0x80
|
||||
#define RFM69_BITRATEMSB_2400 0x34
|
||||
#define RFM69_BITRATELSB_2400 0x15
|
||||
#define RFM69_BITRATEMSB_4800 0x1A // Default
|
||||
#define RFM69_BITRATELSB_4800 0x0B // Default
|
||||
#define RFM69_BITRATEMSB_9600 0x0D
|
||||
#define RFM69_BITRATELSB_9600 0x05
|
||||
#define RFM69_BITRATEMSB_12500 0x0A
|
||||
#define RFM69_BITRATELSB_12500 0x00
|
||||
#define RFM69_BITRATEMSB_19200 0x06
|
||||
#define RFM69_BITRATELSB_19200 0x83
|
||||
#define RFM69_BITRATEMSB_25000 0x05
|
||||
#define RFM69_BITRATELSB_25000 0x00
|
||||
#define RFM69_BITRATEMSB_32768 0x03
|
||||
#define RFM69_BITRATELSB_32768 0xD1
|
||||
#define RFM69_BITRATEMSB_38400 0x03
|
||||
#define RFM69_BITRATELSB_38400 0x41
|
||||
#define RFM69_BITRATEMSB_50000 0x02
|
||||
#define RFM69_BITRATELSB_50000 0x80
|
||||
#define RFM69_BITRATEMSB_55555 0x02
|
||||
#define RFM69_BITRATELSB_55555 0x40
|
||||
#define RFM69_BITRATEMSB_57600 0x02
|
||||
#define RFM69_BITRATELSB_57600 0x2C
|
||||
#define RFM69_BITRATEMSB_76800 0x01
|
||||
#define RFM69_BITRATELSB_76800 0xA1
|
||||
#define RFM69_BITRATEMSB_100000 0x01
|
||||
#define RFM69_BITRATELSB_100000 0x40
|
||||
#define RFM69_BITRATEMSB_115200 0x01
|
||||
#define RFM69_BITRATELSB_115200 0x16
|
||||
#define RFM69_BITRATEMSB_125000 0x01
|
||||
#define RFM69_BITRATELSB_125000 0x00
|
||||
#define RFM69_BITRATEMSB_150000 0x00
|
||||
#define RFM69_BITRATELSB_150000 0xD5
|
||||
#define RFM69_BITRATEMSB_153600 0x00
|
||||
#define RFM69_BITRATELSB_153600 0xD0
|
||||
#define RFM69_BITRATEMSB_200000 0x00
|
||||
#define RFM69_BITRATELSB_200000 0xA0
|
||||
#define RFM69_BITRATEMSB_250000 0x00
|
||||
#define RFM69_BITRATELSB_250000 0x80
|
||||
#define RFM69_BITRATEMSB_300000 0x00
|
||||
#define RFM69_BITRATELSB_300000 0x6B
|
||||
|
||||
// RegFdev - frequency deviation (Hz)
|
||||
#define RFM69_FDEVMSB_2000 0x00
|
||||
#define RFM69_FDEVLSB_2000 0x21
|
||||
#define RFM69_FDEVMSB_4800 0x00
|
||||
#define RFM69_FDEVLSB_4800 0x4f
|
||||
#define RFM69_FDEVMSB_5000 0x00 // Default
|
||||
#define RFM69_FDEVLSB_5000 0x52 // Default
|
||||
#define RFM69_FDEVMSB_7500 0x00
|
||||
#define RFM69_FDEVLSB_7500 0x7B
|
||||
#define RFM69_FDEVMSB_9600 0x00
|
||||
#define RFM69_FDEVLSB_9600 0x9d
|
||||
#define RFM69_FDEVMSB_10000 0x00
|
||||
#define RFM69_FDEVLSB_10000 0xA4
|
||||
#define RFM69_FDEVMSB_15000 0x00
|
||||
#define RFM69_FDEVLSB_15000 0xF6
|
||||
#define RFM69_FDEVMSB_19200 0x01
|
||||
#define RFM69_FDEVLSB_19200 0x3b
|
||||
#define RFM69_FDEVMSB_20000 0x01
|
||||
#define RFM69_FDEVLSB_20000 0x48
|
||||
#define RFM69_FDEVMSB_25000 0x01
|
||||
#define RFM69_FDEVLSB_25000 0x9A
|
||||
#define RFM69_FDEVMSB_30000 0x01
|
||||
#define RFM69_FDEVLSB_30000 0xEC
|
||||
#define RFM69_FDEVMSB_35000 0x02
|
||||
#define RFM69_FDEVLSB_35000 0x3D
|
||||
#define RFM69_FDEVMSB_38400 0x02
|
||||
#define RFM69_FDEVLSB_38400 0x75
|
||||
#define RFM69_FDEVMSB_40000 0x02
|
||||
#define RFM69_FDEVLSB_40000 0x8F
|
||||
#define RFM69_FDEVMSB_45000 0x02
|
||||
#define RFM69_FDEVLSB_45000 0xE1
|
||||
#define RFM69_FDEVMSB_50000 0x03
|
||||
#define RFM69_FDEVLSB_50000 0x33
|
||||
#define RFM69_FDEVMSB_55000 0x03
|
||||
#define RFM69_FDEVLSB_55000 0x85
|
||||
#define RFM69_FDEVMSB_60000 0x03
|
||||
#define RFM69_FDEVLSB_60000 0xD7
|
||||
#define RFM69_FDEVMSB_65000 0x04
|
||||
#define RFM69_FDEVLSB_65000 0x29
|
||||
#define RFM69_FDEVMSB_70000 0x04
|
||||
#define RFM69_FDEVLSB_70000 0x7B
|
||||
#define RFM69_FDEVMSB_75000 0x04
|
||||
#define RFM69_FDEVLSB_75000 0xCD
|
||||
#define RFM69_FDEVMSB_76800 0x04
|
||||
#define RFM69_FDEVLSB_76800 0xea
|
||||
#define RFM69_FDEVMSB_80000 0x05
|
||||
#define RFM69_FDEVLSB_80000 0x1F
|
||||
#define RFM69_FDEVMSB_85000 0x05
|
||||
#define RFM69_FDEVLSB_85000 0x71
|
||||
#define RFM69_FDEVMSB_90000 0x05
|
||||
#define RFM69_FDEVLSB_90000 0xC3
|
||||
#define RFM69_FDEVMSB_95000 0x06
|
||||
#define RFM69_FDEVLSB_95000 0x14
|
||||
#define RFM69_FDEVMSB_100000 0x06
|
||||
#define RFM69_FDEVLSB_100000 0x66
|
||||
#define RFM69_FDEVMSB_110000 0x07
|
||||
#define RFM69_FDEVLSB_110000 0x0A
|
||||
#define RFM69_FDEVMSB_120000 0x07
|
||||
#define RFM69_FDEVLSB_120000 0xAE
|
||||
#define RFM69_FDEVMSB_125000 0x08
|
||||
#define RFM69_FDEVLSB_125000 0x00
|
||||
#define RFM69_FDEVMSB_130000 0x08
|
||||
#define RFM69_FDEVLSB_130000 0x52
|
||||
#define RFM69_FDEVMSB_140000 0x08
|
||||
#define RFM69_FDEVLSB_140000 0xF6
|
||||
#define RFM69_FDEVMSB_150000 0x09
|
||||
#define RFM69_FDEVLSB_150000 0x9A
|
||||
#define RFM69_FDEVMSB_160000 0x0A
|
||||
#define RFM69_FDEVLSB_160000 0x3D
|
||||
#define RFM69_FDEVMSB_170000 0x0A
|
||||
#define RFM69_FDEVLSB_170000 0xE1
|
||||
#define RFM69_FDEVMSB_180000 0x0B
|
||||
#define RFM69_FDEVLSB_180000 0x85
|
||||
#define RFM69_FDEVMSB_190000 0x0C
|
||||
#define RFM69_FDEVLSB_190000 0x29
|
||||
#define RFM69_FDEVMSB_200000 0x0C
|
||||
#define RFM69_FDEVLSB_200000 0xCD
|
||||
#define RFM69_FDEVMSB_210000 0x0D
|
||||
#define RFM69_FDEVLSB_210000 0x71
|
||||
#define RFM69_FDEVMSB_220000 0x0E
|
||||
#define RFM69_FDEVLSB_220000 0x14
|
||||
#define RFM69_FDEVMSB_230000 0x0E
|
||||
#define RFM69_FDEVLSB_230000 0xB8
|
||||
#define RFM69_FDEVMSB_240000 0x0F
|
||||
#define RFM69_FDEVLSB_240000 0x5C
|
||||
#define RFM69_FDEVMSB_250000 0x10
|
||||
#define RFM69_FDEVLSB_250000 0x00
|
||||
#define RFM69_FDEVMSB_260000 0x10
|
||||
#define RFM69_FDEVLSB_260000 0xA4
|
||||
#define RFM69_FDEVMSB_270000 0x11
|
||||
#define RFM69_FDEVLSB_270000 0x48
|
||||
#define RFM69_FDEVMSB_280000 0x11
|
||||
#define RFM69_FDEVLSB_280000 0xEC
|
||||
#define RFM69_FDEVMSB_290000 0x12
|
||||
#define RFM69_FDEVLSB_290000 0x8F
|
||||
#define RFM69_FDEVMSB_300000 0x13
|
||||
#define RFM69_FDEVLSB_300000 0x33
|
||||
|
||||
#define RFM69_NOP 0x00
|
||||
|
||||
// RegOsc1
|
||||
#define RFM69_OSC1_RCCAL_START 0x80
|
||||
#define RFM69_OSC1_RCCAL_DONE 0x40
|
||||
|
||||
|
||||
// RegAfcCtrl
|
||||
#define RFM69_AFCCTRL_LOWBETA_OFF 0x00 // Default
|
||||
#define RFM69_AFCCTRL_LOWBETA_ON 0x20
|
||||
|
||||
|
||||
// RegLowBat
|
||||
#define RFM69_LOWBAT_MONITOR 0x10
|
||||
#define RFM69_LOWBAT_ON 0x08
|
||||
#define RFM69_LOWBAT_OFF 0x00 // Default
|
||||
|
||||
#define RFM69_LOWBAT_TRIM_1695 0x00
|
||||
#define RFM69_LOWBAT_TRIM_1764 0x01
|
||||
#define RFM69_LOWBAT_TRIM_1835 0x02 // Default
|
||||
#define RFM69_LOWBAT_TRIM_1905 0x03
|
||||
#define RFM69_LOWBAT_TRIM_1976 0x04
|
||||
#define RFM69_LOWBAT_TRIM_2045 0x05
|
||||
#define RFM69_LOWBAT_TRIM_2116 0x06
|
||||
#define RFM69_LOWBAT_TRIM_2185 0x07
|
||||
|
||||
|
||||
// RegListen1
|
||||
#define RFM69_LISTEN1_RESOL_64 0x50
|
||||
#define RFM69_LISTEN1_RESOL_4100 0xA0 // Default
|
||||
#define RFM69_LISTEN1_RESOL_262000 0xF0
|
||||
|
||||
#define RFM69_LISTEN1_RESOL_IDLE_64 0x40
|
||||
#define RFM69_LISTEN1_RESOL_IDLE_4100 0x80 // Default
|
||||
#define RFM69_LISTEN1_RESOL_IDLE_262000 0xC0
|
||||
|
||||
#define RFM69_LISTEN1_RESOL_RX_64 0x10
|
||||
#define RFM69_LISTEN1_RESOL_RX_4100 0x20 // Default
|
||||
#define RFM69_LISTEN1_RESOL_RX_262000 0x30
|
||||
|
||||
#define RFM69_LISTEN1_CRITERIA_RSSI 0x00 // Default
|
||||
#define RFM69_LISTEN1_CRITERIA_RSSIANDSYNC 0x08
|
||||
|
||||
#define RFM69_LISTEN1_END_00 0x00
|
||||
#define RFM69_LISTEN1_END_01 0x02 // Default
|
||||
#define RFM69_LISTEN1_END_10 0x04
|
||||
|
||||
|
||||
// RegListen2
|
||||
#define RFM69_LISTEN2_COEFIDLE_VALUE 0xF5 // Default
|
||||
|
||||
|
||||
// RegListen3
|
||||
#define RFM69_LISTEN3_COEFRX_VALUE 0x20 // Default
|
||||
|
||||
|
||||
// RegVersion
|
||||
#define RFM69_VERSION_VER 0x24 // Default
|
||||
|
||||
|
||||
// RegPaLevel
|
||||
#define RFM69_PALEVEL_PA0_ON 0x80 // Default
|
||||
#define RFM69_PALEVEL_PA0_OFF 0x00
|
||||
#define RFM69_PALEVEL_PA1_ON 0x40
|
||||
#define RFM69_PALEVEL_PA1_OFF 0x00 // Default
|
||||
#define RFM69_PALEVEL_PA2_ON 0x20
|
||||
#define RFM69_PALEVEL_PA2_OFF 0x00 // Default
|
||||
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_00000 0x00
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_00001 0x01
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_00010 0x02
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_00011 0x03
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_00100 0x04
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_00101 0x05
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_00110 0x06
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_00111 0x07
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_01000 0x08
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_01001 0x09
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_01010 0x0A
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_01011 0x0B
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_01100 0x0C
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_01101 0x0D
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_01110 0x0E
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_01111 0x0F
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_10000 0x10
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_10001 0x11
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_10010 0x12
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_10011 0x13
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_10100 0x14
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_10101 0x15
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_10110 0x16
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_10111 0x17
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_11000 0x18
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_11001 0x19
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_11010 0x1A
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_11011 0x1B
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_11100 0x1C
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_11101 0x1D
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_11110 0x1E
|
||||
#define RFM69_PALEVEL_OUTPUTPOWER_11111 0x1F // Default
|
||||
|
||||
|
||||
// RegPaRamp
|
||||
#define RFM69_PARAMP_3400 0x00
|
||||
#define RFM69_PARAMP_2000 0x01
|
||||
#define RFM69_PARAMP_1000 0x02
|
||||
#define RFM69_PARAMP_500 0x03
|
||||
#define RFM69_PARAMP_250 0x04
|
||||
#define RFM69_PARAMP_125 0x05
|
||||
#define RFM69_PARAMP_100 0x06
|
||||
#define RFM69_PARAMP_62 0x07
|
||||
#define RFM69_PARAMP_50 0x08
|
||||
#define RFM69_PARAMP_40 0x09 // Default
|
||||
#define RFM69_PARAMP_31 0x0A
|
||||
#define RFM69_PARAMP_25 0x0B
|
||||
#define RFM69_PARAMP_20 0x0C
|
||||
#define RFM69_PARAMP_15 0x0D
|
||||
#define RFM69_PARAMP_12 0x0E
|
||||
#define RFM69_PARAMP_10 0x0F
|
||||
|
||||
|
||||
// RegOcp
|
||||
#define RFM69_OCP_OFF 0x0F
|
||||
#define RFM69_OCP_ON 0x1A // Default
|
||||
|
||||
#define RFM69_OCP_TRIM_45 0x00
|
||||
#define RFM69_OCP_TRIM_50 0x01
|
||||
#define RFM69_OCP_TRIM_55 0x02
|
||||
#define RFM69_OCP_TRIM_60 0x03
|
||||
#define RFM69_OCP_TRIM_65 0x04
|
||||
#define RFM69_OCP_TRIM_70 0x05
|
||||
#define RFM69_OCP_TRIM_75 0x06
|
||||
#define RFM69_OCP_TRIM_80 0x07
|
||||
#define RFM69_OCP_TRIM_85 0x08
|
||||
#define RFM69_OCP_TRIM_90 0x09
|
||||
#define RFM69_OCP_TRIM_95 0x0A // Default
|
||||
#define RFM69_OCP_TRIM_100 0x0B
|
||||
#define RFM69_OCP_TRIM_105 0x0C
|
||||
#define RFM69_OCP_TRIM_110 0x0D
|
||||
#define RFM69_OCP_TRIM_115 0x0E
|
||||
#define RFM69_OCP_TRIM_120 0x0F
|
||||
|
||||
|
||||
// RegAgcRef - not present on RFM69/SX1231
|
||||
#define RFM69_AGCREF_AUTO_ON 0x40 // Default
|
||||
#define RFM69_AGCREF_AUTO_OFF 0x00
|
||||
|
||||
#define RFM69_AGCREF_LEVEL_MINUS80 0x00 // Default
|
||||
#define RFM69_AGCREF_LEVEL_MINUS81 0x01
|
||||
#define RFM69_AGCREF_LEVEL_MINUS82 0x02
|
||||
#define RFM69_AGCREF_LEVEL_MINUS83 0x03
|
||||
#define RFM69_AGCREF_LEVEL_MINUS84 0x04
|
||||
#define RFM69_AGCREF_LEVEL_MINUS85 0x05
|
||||
#define RFM69_AGCREF_LEVEL_MINUS86 0x06
|
||||
#define RFM69_AGCREF_LEVEL_MINUS87 0x07
|
||||
#define RFM69_AGCREF_LEVEL_MINUS88 0x08
|
||||
#define RFM69_AGCREF_LEVEL_MINUS89 0x09
|
||||
#define RFM69_AGCREF_LEVEL_MINUS90 0x0A
|
||||
#define RFM69_AGCREF_LEVEL_MINUS91 0x0B
|
||||
#define RFM69_AGCREF_LEVEL_MINUS92 0x0C
|
||||
#define RFM69_AGCREF_LEVEL_MINUS93 0x0D
|
||||
#define RFM69_AGCREF_LEVEL_MINUS94 0x0E
|
||||
#define RFM69_AGCREF_LEVEL_MINUS95 0x0F
|
||||
#define RFM69_AGCREF_LEVEL_MINUS96 0x10
|
||||
#define RFM69_AGCREF_LEVEL_MINUS97 0x11
|
||||
#define RFM69_AGCREF_LEVEL_MINUS98 0x12
|
||||
#define RFM69_AGCREF_LEVEL_MINUS99 0x13
|
||||
#define RFM69_AGCREF_LEVEL_MINUS100 0x14
|
||||
#define RFM69_AGCREF_LEVEL_MINUS101 0x15
|
||||
#define RFM69_AGCREF_LEVEL_MINUS102 0x16
|
||||
#define RFM69_AGCREF_LEVEL_MINUS103 0x17
|
||||
#define RFM69_AGCREF_LEVEL_MINUS104 0x18
|
||||
#define RFM69_AGCREF_LEVEL_MINUS105 0x19
|
||||
#define RFM69_AGCREF_LEVEL_MINUS106 0x1A
|
||||
#define RFM69_AGCREF_LEVEL_MINUS107 0x1B
|
||||
#define RFM69_AGCREF_LEVEL_MINUS108 0x1C
|
||||
#define RFM69_AGCREF_LEVEL_MINUS109 0x1D
|
||||
#define RFM69_AGCREF_LEVEL_MINUS110 0x1E
|
||||
#define RFM69_AGCREF_LEVEL_MINUS111 0x1F
|
||||
#define RFM69_AGCREF_LEVEL_MINUS112 0x20
|
||||
#define RFM69_AGCREF_LEVEL_MINUS113 0x21
|
||||
#define RFM69_AGCREF_LEVEL_MINUS114 0x22
|
||||
#define RFM69_AGCREF_LEVEL_MINUS115 0x23
|
||||
#define RFM69_AGCREF_LEVEL_MINUS116 0x24
|
||||
#define RFM69_AGCREF_LEVEL_MINUS117 0x25
|
||||
#define RFM69_AGCREF_LEVEL_MINUS118 0x26
|
||||
#define RFM69_AGCREF_LEVEL_MINUS119 0x27
|
||||
#define RFM69_AGCREF_LEVEL_MINUS120 0x28
|
||||
#define RFM69_AGCREF_LEVEL_MINUS121 0x29
|
||||
#define RFM69_AGCREF_LEVEL_MINUS122 0x2A
|
||||
#define RFM69_AGCREF_LEVEL_MINUS123 0x2B
|
||||
#define RFM69_AGCREF_LEVEL_MINUS124 0x2C
|
||||
#define RFM69_AGCREF_LEVEL_MINUS125 0x2D
|
||||
#define RFM69_AGCREF_LEVEL_MINUS126 0x2E
|
||||
#define RFM69_AGCREF_LEVEL_MINUS127 0x2F
|
||||
#define RFM69_AGCREF_LEVEL_MINUS128 0x30
|
||||
#define RFM69_AGCREF_LEVEL_MINUS129 0x31
|
||||
#define RFM69_AGCREF_LEVEL_MINUS130 0x32
|
||||
#define RFM69_AGCREF_LEVEL_MINUS131 0x33
|
||||
#define RFM69_AGCREF_LEVEL_MINUS132 0x34
|
||||
#define RFM69_AGCREF_LEVEL_MINUS133 0x35
|
||||
#define RFM69_AGCREF_LEVEL_MINUS134 0x36
|
||||
#define RFM69_AGCREF_LEVEL_MINUS135 0x37
|
||||
#define RFM69_AGCREF_LEVEL_MINUS136 0x38
|
||||
#define RFM69_AGCREF_LEVEL_MINUS137 0x39
|
||||
#define RFM69_AGCREF_LEVEL_MINUS138 0x3A
|
||||
#define RFM69_AGCREF_LEVEL_MINUS139 0x3B
|
||||
#define RFM69_AGCREF_LEVEL_MINUS140 0x3C
|
||||
#define RFM69_AGCREF_LEVEL_MINUS141 0x3D
|
||||
#define RFM69_AGCREF_LEVEL_MINUS142 0x3E
|
||||
#define RFM69_AGCREF_LEVEL_MINUS143 0x3F
|
||||
|
||||
|
||||
// RegAgcThresh1 - not present on RFM69/SX1231
|
||||
#define RFM69_AGCTHRESH1_SNRMARGIN_000 0x00
|
||||
#define RFM69_AGCTHRESH1_SNRMARGIN_001 0x20
|
||||
#define RFM69_AGCTHRESH1_SNRMARGIN_010 0x40
|
||||
#define RFM69_AGCTHRESH1_SNRMARGIN_011 0x60
|
||||
#define RFM69_AGCTHRESH1_SNRMARGIN_100 0x80
|
||||
#define RFM69_AGCTHRESH1_SNRMARGIN_101 0xA0 // Default
|
||||
#define RFM69_AGCTHRESH1_SNRMARGIN_110 0xC0
|
||||
#define RFM69_AGCTHRESH1_SNRMARGIN_111 0xE0
|
||||
|
||||
#define RFM69_AGCTHRESH1_STEP1_0 0x00
|
||||
#define RFM69_AGCTHRESH1_STEP1_1 0x01
|
||||
#define RFM69_AGCTHRESH1_STEP1_2 0x02
|
||||
#define RFM69_AGCTHRESH1_STEP1_3 0x03
|
||||
#define RFM69_AGCTHRESH1_STEP1_4 0x04
|
||||
#define RFM69_AGCTHRESH1_STEP1_5 0x05
|
||||
#define RFM69_AGCTHRESH1_STEP1_6 0x06
|
||||
#define RFM69_AGCTHRESH1_STEP1_7 0x07
|
||||
#define RFM69_AGCTHRESH1_STEP1_8 0x08
|
||||
#define RFM69_AGCTHRESH1_STEP1_9 0x09
|
||||
#define RFM69_AGCTHRESH1_STEP1_10 0x0A
|
||||
#define RFM69_AGCTHRESH1_STEP1_11 0x0B
|
||||
#define RFM69_AGCTHRESH1_STEP1_12 0x0C
|
||||
#define RFM69_AGCTHRESH1_STEP1_13 0x0D
|
||||
#define RFM69_AGCTHRESH1_STEP1_14 0x0E
|
||||
#define RFM69_AGCTHRESH1_STEP1_15 0x0F
|
||||
#define RFM69_AGCTHRESH1_STEP1_16 0x10 // Default
|
||||
#define RFM69_AGCTHRESH1_STEP1_17 0x11
|
||||
#define RFM69_AGCTHRESH1_STEP1_18 0x12
|
||||
#define RFM69_AGCTHRESH1_STEP1_19 0x13
|
||||
#define RFM69_AGCTHRESH1_STEP1_20 0x14
|
||||
#define RFM69_AGCTHRESH1_STEP1_21 0x15
|
||||
#define RFM69_AGCTHRESH1_STEP1_22 0x16
|
||||
#define RFM69_AGCTHRESH1_STEP1_23 0x17
|
||||
#define RFM69_AGCTHRESH1_STEP1_24 0x18
|
||||
#define RFM69_AGCTHRESH1_STEP1_25 0x19
|
||||
#define RFM69_AGCTHRESH1_STEP1_26 0x1A
|
||||
#define RFM69_AGCTHRESH1_STEP1_27 0x1B
|
||||
#define RFM69_AGCTHRESH1_STEP1_28 0x1C
|
||||
#define RFM69_AGCTHRESH1_STEP1_29 0x1D
|
||||
#define RFM69_AGCTHRESH1_STEP1_30 0x1E
|
||||
#define RFM69_AGCTHRESH1_STEP1_31 0x1F
|
||||
|
||||
|
||||
// RegAgcThresh2 - not present on RFM69/SX1231
|
||||
#define RFM69_AGCTHRESH2_STEP2_0 0x00
|
||||
#define RFM69_AGCTHRESH2_STEP2_1 0x10
|
||||
#define RFM69_AGCTHRESH2_STEP2_2 0x20
|
||||
#define RFM69_AGCTHRESH2_STEP2_3 0x30 // XXX wrong -- Default
|
||||
#define RFM69_AGCTHRESH2_STEP2_4 0x40
|
||||
#define RFM69_AGCTHRESH2_STEP2_5 0x50
|
||||
#define RFM69_AGCTHRESH2_STEP2_6 0x60
|
||||
#define RFM69_AGCTHRESH2_STEP2_7 0x70 // default
|
||||
#define RFM69_AGCTHRESH2_STEP2_8 0x80
|
||||
#define RFM69_AGCTHRESH2_STEP2_9 0x90
|
||||
#define RFM69_AGCTHRESH2_STEP2_10 0xA0
|
||||
#define RFM69_AGCTHRESH2_STEP2_11 0xB0
|
||||
#define RFM69_AGCTHRESH2_STEP2_12 0xC0
|
||||
#define RFM69_AGCTHRESH2_STEP2_13 0xD0
|
||||
#define RFM69_AGCTHRESH2_STEP2_14 0xE0
|
||||
#define RFM69_AGCTHRESH2_STEP2_15 0xF0
|
||||
|
||||
#define RFM69_AGCTHRESH2_STEP3_0 0x00
|
||||
#define RFM69_AGCTHRESH2_STEP3_1 0x01
|
||||
#define RFM69_AGCTHRESH2_STEP3_2 0x02
|
||||
#define RFM69_AGCTHRESH2_STEP3_3 0x03
|
||||
#define RFM69_AGCTHRESH2_STEP3_4 0x04
|
||||
#define RFM69_AGCTHRESH2_STEP3_5 0x05
|
||||
#define RFM69_AGCTHRESH2_STEP3_6 0x06
|
||||
#define RFM69_AGCTHRESH2_STEP3_7 0x07
|
||||
#define RFM69_AGCTHRESH2_STEP3_8 0x08
|
||||
#define RFM69_AGCTHRESH2_STEP3_9 0x09
|
||||
#define RFM69_AGCTHRESH2_STEP3_10 0x0A
|
||||
#define RFM69_AGCTHRESH2_STEP3_11 0x0B // Default
|
||||
#define RFM69_AGCTHRESH2_STEP3_12 0x0C
|
||||
#define RFM69_AGCTHRESH2_STEP3_13 0x0D
|
||||
#define RFM69_AGCTHRESH2_STEP3_14 0x0E
|
||||
#define RFM69_AGCTHRESH2_STEP3_15 0x0F
|
||||
|
||||
|
||||
// RegAgcThresh3 - not present on RFM69/SX1231
|
||||
#define RFM69_AGCTHRESH3_STEP4_0 0x00
|
||||
#define RFM69_AGCTHRESH3_STEP4_1 0x10
|
||||
#define RFM69_AGCTHRESH3_STEP4_2 0x20
|
||||
#define RFM69_AGCTHRESH3_STEP4_3 0x30
|
||||
#define RFM69_AGCTHRESH3_STEP4_4 0x40
|
||||
#define RFM69_AGCTHRESH3_STEP4_5 0x50
|
||||
#define RFM69_AGCTHRESH3_STEP4_6 0x60
|
||||
#define RFM69_AGCTHRESH3_STEP4_7 0x70
|
||||
#define RFM69_AGCTHRESH3_STEP4_8 0x80
|
||||
#define RFM69_AGCTHRESH3_STEP4_9 0x90 // Default
|
||||
#define RFM69_AGCTHRESH3_STEP4_10 0xA0
|
||||
#define RFM69_AGCTHRESH3_STEP4_11 0xB0
|
||||
#define RFM69_AGCTHRESH3_STEP4_12 0xC0
|
||||
#define RFM69_AGCTHRESH3_STEP4_13 0xD0
|
||||
#define RFM69_AGCTHRESH3_STEP4_14 0xE0
|
||||
#define RFM69_AGCTHRESH3_STEP4_15 0xF0
|
||||
|
||||
#define RFM69_AGCTHRESH3_STEP5_0 0x00
|
||||
#define RFM69_AGCTHRESH3_STEP5_1 0x01
|
||||
#define RFM69_AGCTHRESH3_STEP5_2 0x02
|
||||
#define RFM69_AGCTHRESH3_STEP5_3 0x03
|
||||
#define RFM69_AGCTHRESH3_STEP5_4 0x04
|
||||
#define RFM69_AGCTHRESH3_STEP5_5 0x05
|
||||
#define RFM69_AGCTHRESH3_STEP5_6 0x06
|
||||
#define RFM69_AGCTHRESH3_STEP5_7 0x07
|
||||
#define RFM69_AGCTHRES33_STEP5_8 0x08
|
||||
#define RFM69_AGCTHRESH3_STEP5_9 0x09
|
||||
#define RFM69_AGCTHRESH3_STEP5_10 0x0A
|
||||
#define RFM69_AGCTHRESH3_STEP5_11 0x0B // Default
|
||||
#define RFM69_AGCTHRESH3_STEP5_12 0x0C
|
||||
#define RFM69_AGCTHRESH3_STEP5_13 0x0D
|
||||
#define RFM69_AGCTHRESH3_STEP5_14 0x0E
|
||||
#define RFM69_AGCTHRESH3_STEP5_15 0x0F
|
||||
|
||||
|
||||
// RegLna
|
||||
#define RFM69_LNA_ZIN_50 0x00 // Reset value
|
||||
#define RFM69_LNA_ZIN_200 0x80 // Recommended default
|
||||
|
||||
#define RFM69_LNA_LOWPOWER_OFF 0x00 // Default
|
||||
#define RFM69_LNA_LOWPOWER_ON 0x40
|
||||
|
||||
#define RFM69_LNA_CURRENTGAIN 0x08
|
||||
|
||||
#define RFM69_LNA_GAINSELECT_AUTO 0x00 // Default
|
||||
#define RFM69_LNA_GAINSELECT_MAX 0x01
|
||||
#define RFM69_LNA_GAINSELECT_MAXMINUS6 0x02
|
||||
#define RFM69_LNA_GAINSELECT_MAXMINUS12 0x03
|
||||
#define RFM69_LNA_GAINSELECT_MAXMINUS24 0x04
|
||||
#define RFM69_LNA_GAINSELECT_MAXMINUS36 0x05
|
||||
#define RFM69_LNA_GAINSELECT_MAXMINUS48 0x06
|
||||
|
||||
|
||||
// RegRxBw
|
||||
#define RFM69_RXBW_DCCFREQ_000 0x00
|
||||
#define RFM69_RXBW_DCCFREQ_001 0x20
|
||||
#define RFM69_RXBW_DCCFREQ_010 0x40 // Recommended default
|
||||
#define RFM69_RXBW_DCCFREQ_011 0x60
|
||||
#define RFM69_RXBW_DCCFREQ_100 0x80 // Reset value
|
||||
#define RFM69_RXBW_DCCFREQ_101 0xA0
|
||||
#define RFM69_RXBW_DCCFREQ_110 0xC0
|
||||
#define RFM69_RXBW_DCCFREQ_111 0xE0
|
||||
|
||||
#define RFM69_RXBW_MANT_16 0x00 // Reset value
|
||||
#define RFM69_RXBW_MANT_20 0x08
|
||||
#define RFM69_RXBW_MANT_24 0x10 // Recommended default
|
||||
|
||||
#define RFM69_RXBW_EXP_0 0x00
|
||||
#define RFM69_RXBW_EXP_1 0x01
|
||||
#define RFM69_RXBW_EXP_2 0x02
|
||||
#define RFM69_RXBW_EXP_3 0x03
|
||||
#define RFM69_RXBW_EXP_4 0x04
|
||||
#define RFM69_RXBW_EXP_5 0x05 // Recommended default
|
||||
#define RFM69_RXBW_EXP_6 0x06 // Reset value
|
||||
#define RFM69_RXBW_EXP_7 0x07
|
||||
|
||||
|
||||
// RegAfcBw
|
||||
#define RFM69_AFCBW_DCCFREQAFC_000 0x00
|
||||
#define RFM69_AFCBW_DCCFREQAFC_001 0x20
|
||||
#define RFM69_AFCBW_DCCFREQAFC_010 0x40
|
||||
#define RFM69_AFCBW_DCCFREQAFC_011 0x60
|
||||
#define RFM69_AFCBW_DCCFREQAFC_100 0x80 // Default
|
||||
#define RFM69_AFCBW_DCCFREQAFC_101 0xA0
|
||||
#define RFM69_AFCBW_DCCFREQAFC_110 0xC0
|
||||
#define RFM69_AFCBW_DCCFREQAFC_111 0xE0
|
||||
|
||||
#define RFM69_AFCBW_MANTAFC_16 0x00
|
||||
#define RFM69_AFCBW_MANTAFC_20 0x08 // Default
|
||||
#define RFM69_AFCBW_MANTAFC_24 0x10
|
||||
|
||||
#define RFM69_AFCBW_EXPAFC_0 0x00
|
||||
#define RFM69_AFCBW_EXPAFC_1 0x01
|
||||
#define RFM69_AFCBW_EXPAFC_2 0x02 // Reset value
|
||||
#define RFM69_AFCBW_EXPAFC_3 0x03 // Recommended default
|
||||
#define RFM69_AFCBW_EXPAFC_4 0x04
|
||||
#define RFM69_AFCBW_EXPAFC_5 0x05
|
||||
#define RFM69_AFCBW_EXPAFC_6 0x06
|
||||
#define RFM69_AFCBW_EXPAFC_7 0x07
|
||||
|
||||
|
||||
// RegOokPeak
|
||||
#define RFM69_OOKPEAK_THRESHTYPE_FIXED 0x00
|
||||
#define RFM69_OOKPEAK_THRESHTYPE_PEAK 0x40 // Default
|
||||
#define RFM69_OOKPEAK_THRESHTYPE_AVERAGE 0x80
|
||||
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHSTEP_000 0x00 // Default
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHSTEP_001 0x08
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHSTEP_010 0x10
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHSTEP_011 0x18
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHSTEP_100 0x20
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHSTEP_101 0x28
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHSTEP_110 0x30
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHSTEP_111 0x38
|
||||
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHDEC_000 0x00 // Default
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHDEC_001 0x01
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHDEC_010 0x02
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHDEC_011 0x03
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHDEC_100 0x04
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHDEC_101 0x05
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHDEC_110 0x06
|
||||
#define RFM69_OOKPEAK_PEAKTHRESHDEC_111 0x07
|
||||
|
||||
|
||||
// RegOokAvg
|
||||
#define RFM69_OOKAVG_AVERAGETHRESHFILT_00 0x00
|
||||
#define RFM69_OOKAVG_AVERAGETHRESHFILT_01 0x40
|
||||
#define RFM69_OOKAVG_AVERAGETHRESHFILT_10 0x80 // Default
|
||||
#define RFM69_OOKAVG_AVERAGETHRESHFILT_11 0xC0
|
||||
|
||||
|
||||
// RegOokFix
|
||||
#define RFM69_OOKFIX_FIXEDTHRESH_VALUE 0x06 // Default
|
||||
|
||||
|
||||
// RegAfcFei
|
||||
#define RFM69_AFCFEI_FEI_DONE 0x40
|
||||
#define RFM69_AFCFEI_FEI_START 0x20
|
||||
#define RFM69_AFCFEI_AFC_DONE 0x10
|
||||
#define RFM69_AFCFEI_AFCAUTOCLEAR_ON 0x08
|
||||
#define RFM69_AFCFEI_AFCAUTOCLEAR_OFF 0x00 // Default
|
||||
|
||||
#define RFM69_AFCFEI_AFCAUTO_ON 0x04
|
||||
#define RFM69_AFCFEI_AFCAUTO_OFF 0x00 // Default
|
||||
|
||||
#define RFM69_AFCFEI_AFC_CLEAR 0x02
|
||||
#define RFM69_AFCFEI_AFC_START 0x01
|
||||
|
||||
|
||||
// RegRssiConfig
|
||||
#define RFM69_RSSI_FASTRX_ON 0x08 // not present on RFM69/SX1231
|
||||
#define RFM69_RSSI_FASTRX_OFF 0x00 // Default
|
||||
|
||||
#define RFM69_RSSI_DONE 0x02
|
||||
#define RFM69_RSSI_START 0x01
|
||||
|
||||
|
||||
// RegDioMapping1
|
||||
#define RFM69_DIOMAPPING1_DIO0_00 0x00 // Default
|
||||
#define RFM69_DIOMAPPING1_DIO0_01 0x40
|
||||
#define RFM69_DIOMAPPING1_DIO0_10 0x80
|
||||
#define RFM69_DIOMAPPING1_DIO0_11 0xC0
|
||||
|
||||
#define RFM69_DIOMAPPING1_DIO1_00 0x00 // Default
|
||||
#define RFM69_DIOMAPPING1_DIO1_01 0x10
|
||||
#define RFM69_DIOMAPPING1_DIO1_10 0x20
|
||||
#define RFM69_DIOMAPPING1_DIO1_11 0x30
|
||||
|
||||
#define RFM69_DIOMAPPING1_DIO2_00 0x00 // Default
|
||||
#define RFM69_DIOMAPPING1_DIO2_01 0x04
|
||||
#define RFM69_DIOMAPPING1_DIO2_10 0x08
|
||||
#define RFM69_DIOMAPPING1_DIO2_11 0x0C
|
||||
|
||||
#define RFM69_DIOMAPPING1_DIO3_00 0x00 // Default
|
||||
#define RFM69_DIOMAPPING1_DIO3_01 0x01
|
||||
#define RFM69_DIOMAPPING1_DIO3_10 0x02
|
||||
#define RFM69_DIOMAPPING1_DIO3_11 0x03
|
||||
|
||||
|
||||
// RegDioMapping2
|
||||
#define RFM69_DIOMAPPING2_DIO4_00 0x00 // Default
|
||||
#define RFM69_DIOMAPPING2_DIO4_01 0x40
|
||||
#define RFM69_DIOMAPPING2_DIO4_10 0x80
|
||||
#define RFM69_DIOMAPPING2_DIO4_11 0xC0
|
||||
|
||||
#define RFM69_DIOMAPPING2_DIO5_00 0x00 // Default
|
||||
#define RFM69_DIOMAPPING2_DIO5_01 0x10
|
||||
#define RFM69_DIOMAPPING2_DIO5_10 0x20
|
||||
#define RFM69_DIOMAPPING2_DIO5_11 0x30
|
||||
|
||||
#define RFM69_DIOMAPPING2_CLKOUT_32 0x00
|
||||
#define RFM69_DIOMAPPING2_CLKOUT_16 0x01
|
||||
#define RFM69_DIOMAPPING2_CLKOUT_8 0x02
|
||||
#define RFM69_DIOMAPPING2_CLKOUT_4 0x03
|
||||
#define RFM69_DIOMAPPING2_CLKOUT_2 0x04
|
||||
#define RFM69_DIOMAPPING2_CLKOUT_1 0x05 // Reset value
|
||||
#define RFM69_DIOMAPPING2_CLKOUT_RC 0x06
|
||||
#define RFM69_DIOMAPPING2_CLKOUT_OFF 0x07 // Recommended default
|
||||
|
||||
|
||||
// RegIrqFlags1
|
||||
#define RFM69_IRQFLAGS1_MODEREADY 0x80
|
||||
#define RFM69_IRQFLAGS1_RXREADY 0x40
|
||||
#define RFM69_IRQFLAGS1_TXREADY 0x20
|
||||
#define RFM69_IRQFLAGS1_PLLLOCK 0x10
|
||||
#define RFM69_IRQFLAGS1_RSSI 0x08
|
||||
#define RFM69_IRQFLAGS1_TIMEOUT 0x04
|
||||
#define RFM69_IRQFLAGS1_AUTOMODE 0x02
|
||||
#define RFM69_IRQFLAGS1_SYNCADDRESSMATCH 0x01
|
||||
|
||||
|
||||
// RegIrqFlags2
|
||||
#define RFM69_IRQFLAGS2_FIFOFULL 0x80
|
||||
#define RFM69_IRQFLAGS2_FIFONOTEMPTY 0x40
|
||||
#define RFM69_IRQFLAGS2_FIFOLEVEL 0x20
|
||||
#define RFM69_IRQFLAGS2_FIFOOVERRUN 0x10
|
||||
#define RFM69_IRQFLAGS2_PACKETSENT 0x08
|
||||
#define RFM69_IRQFLAGS2_PAYLOADREADY 0x04
|
||||
#define RFM69_IRQFLAGS2_CRCOK 0x02
|
||||
#define RFM69_IRQFLAGS2_LOWBAT 0x01 // not present on RFM69/SX1231
|
||||
|
||||
|
||||
// RegRssiThresh
|
||||
#define RFM69_RSSITHRESH_VALUE 0xE4 // Default
|
||||
|
||||
|
||||
// RegRxTimeout1
|
||||
#define RFM69_RXTIMEOUT1_RXSTART_VALUE 0x00 // Default
|
||||
|
||||
|
||||
// RegRxTimeout2
|
||||
#define RFM69_RXTIMEOUT2_RSSITHRESH_VALUE 0x00 // Default
|
||||
|
||||
|
||||
// RegPreamble
|
||||
#define RFM69_PREAMBLESIZE_MSB_VALUE 0x00 // Default
|
||||
#define RFM69_PREAMBLESIZE_LSB_VALUE 0x03 // Default
|
||||
|
||||
|
||||
// RegSyncConfig
|
||||
#define RFM69_SYNC_ON 0x80 // Default
|
||||
#define RFM69_SYNC_OFF 0x00
|
||||
|
||||
#define RFM69_SYNC_FIFOFILL_AUTO 0x00 // Default -- when sync interrupt occurs
|
||||
#define RFM69_SYNC_FIFOFILL_MANUAL 0x40
|
||||
|
||||
#define RFM69_SYNC_SIZE_1 0x00
|
||||
#define RFM69_SYNC_SIZE_2 0x08
|
||||
#define RFM69_SYNC_SIZE_3 0x10
|
||||
#define RFM69_SYNC_SIZE_4 0x18 // Default
|
||||
#define RFM69_SYNC_SIZE_5 0x20
|
||||
#define RFM69_SYNC_SIZE_6 0x28
|
||||
#define RFM69_SYNC_SIZE_7 0x30
|
||||
#define RFM69_SYNC_SIZE_8 0x38
|
||||
|
||||
#define RFM69_SYNC_TOL_0 0x00 // Default
|
||||
#define RFM69_SYNC_TOL_1 0x01
|
||||
#define RFM69_SYNC_TOL_2 0x02
|
||||
#define RFM69_SYNC_TOL_3 0x03
|
||||
#define RFM69_SYNC_TOL_4 0x04
|
||||
#define RFM69_SYNC_TOL_5 0x05
|
||||
#define RFM69_SYNC_TOL_6 0x06
|
||||
#define RFM69_SYNC_TOL_7 0x07
|
||||
|
||||
|
||||
// RegSyncValue1-8
|
||||
#define RFM69_SYNC_BYTE1_VALUE 0x00 // Default
|
||||
#define RFM69_SYNC_BYTE2_VALUE 0x00 // Default
|
||||
#define RFM69_SYNC_BYTE3_VALUE 0x00 // Default
|
||||
#define RFM69_SYNC_BYTE4_VALUE 0x00 // Default
|
||||
#define RFM69_SYNC_BYTE5_VALUE 0x00 // Default
|
||||
#define RFM69_SYNC_BYTE6_VALUE 0x00 // Default
|
||||
#define RFM69_SYNC_BYTE7_VALUE 0x00 // Default
|
||||
#define RFM69_SYNC_BYTE8_VALUE 0x00 // Default
|
||||
|
||||
|
||||
// RegPacketConfig1
|
||||
#define RFM69_PACKET1_FORMAT_FIXED 0x00 // Default
|
||||
#define RFM69_PACKET1_FORMAT_VARIABLE 0x80
|
||||
|
||||
#define RFM69_PACKET1_DCFREE_OFF 0x00 // Default
|
||||
#define RFM69_PACKET1_DCFREE_MANCHESTER 0x20
|
||||
#define RFM69_PACKET1_DCFREE_WHITENING 0x40
|
||||
|
||||
#define RFM69_PACKET1_CRC_ON 0x10 // Default
|
||||
#define RFM69_PACKET1_CRC_OFF 0x00
|
||||
|
||||
#define RFM69_PACKET1_CRCAUTOCLEAR_ON 0x00 // Default
|
||||
#define RFM69_PACKET1_CRCAUTOCLEAR_OFF 0x08
|
||||
|
||||
#define RFM69_PACKET1_ADRSFILTERING_OFF 0x00 // Default
|
||||
#define RFM69_PACKET1_ADRSFILTERING_NODE 0x02
|
||||
#define RFM69_PACKET1_ADRSFILTERING_NODEBROADCAST 0x04
|
||||
|
||||
|
||||
// RegPayloadLength
|
||||
#define RFM69_PAYLOADLENGTH_VALUE 0x40 // Default
|
||||
|
||||
|
||||
// RegBroadcastAdrs
|
||||
#define RFM69_BROADCASTADDRESS_VALUE 0x00
|
||||
|
||||
|
||||
// RegAutoModes
|
||||
#define RFM69_AUTOMODES_ENTER_OFF 0x00 // Default
|
||||
#define RFM69_AUTOMODES_ENTER_FIFONOTEMPTY 0x20
|
||||
#define RFM69_AUTOMODES_ENTER_FIFOLEVEL 0x40
|
||||
#define RFM69_AUTOMODES_ENTER_CRCOK 0x60
|
||||
#define RFM69_AUTOMODES_ENTER_PAYLOADREADY 0x80
|
||||
#define RFM69_AUTOMODES_ENTER_SYNCADRSMATCH 0xA0
|
||||
#define RFM69_AUTOMODES_ENTER_PACKETSENT 0xC0
|
||||
#define RFM69_AUTOMODES_ENTER_FIFOEMPTY 0xE0
|
||||
|
||||
#define RFM69_AUTOMODES_EXIT_OFF 0x00 // Default
|
||||
#define RFM69_AUTOMODES_EXIT_FIFOEMPTY 0x04
|
||||
#define RFM69_AUTOMODES_EXIT_FIFOLEVEL 0x08
|
||||
#define RFM69_AUTOMODES_EXIT_CRCOK 0x0C
|
||||
#define RFM69_AUTOMODES_EXIT_PAYLOADREADY 0x10
|
||||
#define RFM69_AUTOMODES_EXIT_SYNCADRSMATCH 0x14
|
||||
#define RFM69_AUTOMODES_EXIT_PACKETSENT 0x18
|
||||
#define RFM69_AUTOMODES_EXIT_RXTIMEOUT 0x1C
|
||||
|
||||
#define RFM69_AUTOMODES_INTERMEDIATE_SLEEP 0x00 // Default
|
||||
#define RFM69_AUTOMODES_INTERMEDIATE_STANDBY 0x01
|
||||
#define RFM69_AUTOMODES_INTERMEDIATE_RECEIVER 0x02
|
||||
#define RFM69_AUTOMODES_INTERMEDIATE_TRANSMITTER 0x03
|
||||
|
||||
|
||||
// RegFifoThresh
|
||||
#define RFM69_FIFOTHRESH_TXSTART_FIFOTHRESH 0x00 // Reset value
|
||||
#define RFM69_FIFOTHRESH_TXSTART_FIFONOTEMPTY 0x80 // Recommended default
|
||||
#define RFM69_FIFOTHRESH_VALUE 0x0F // Default
|
||||
|
||||
|
||||
// RegPacketConfig2
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_1BIT 0x00 // Default
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_2BITS 0x10
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_4BITS 0x20
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_8BITS 0x30
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_16BITS 0x40
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_32BITS 0x50
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_64BITS 0x60
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_128BITS 0x70
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_256BITS 0x80
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_512BITS 0x90
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_1024BITS 0xA0
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_2048BITS 0xB0
|
||||
#define RFM69_PACKET2_RXRESTARTDELAY_NONE 0xC0
|
||||
#define RFM69_PACKET2_RXRESTART 0x04
|
||||
|
||||
#define RFM69_PACKET2_AUTORXRESTART_ON 0x02 // Default
|
||||
#define RFM69_PACKET2_AUTORXRESTART_OFF 0x00
|
||||
|
||||
#define RFM69_PACKET2_AES_ON 0x01
|
||||
#define RFM69_PACKET2_AES_OFF 0x00 // Default
|
||||
|
||||
|
||||
// RegAesKey1-16
|
||||
#define RFM69_AESKEY1_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY2_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY3_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY4_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY5_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY6_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY7_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY8_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY9_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY10_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY11_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY12_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY13_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY14_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY15_VALUE 0x00 // Default
|
||||
#define RFM69_AESKEY16_VALUE 0x00 // Default
|
||||
|
||||
|
||||
// RegTemp1
|
||||
#define RFM69_TEMP1_MEAS_START 0x08
|
||||
#define RFM69_TEMP1_MEAS_RUNNING 0x04
|
||||
// not present on RFM69/SX1231
|
||||
#define RFM69_TEMP1_ADCLOWPOWER_ON 0x01 // Default
|
||||
#define RFM69_TEMP1_ADCLOWPOWER_OFF 0x00
|
||||
|
||||
|
||||
// RegTestLna
|
||||
#define RFM69_TESTLNA_NORMAL 0x1B
|
||||
#define RFM69_TESTLNA_HIGH_SENSITIVITY 0x2D
|
||||
|
||||
|
||||
// RegTestDagc
|
||||
#define RFM69_DAGC_NORMAL 0x00 // Reset value
|
||||
#define RFM69_DAGC_IMPROVED_LOWBETA1 0x20
|
||||
#define RFM69_DAGC_IMPROVED_LOWBETA0 0x30 // Recommended default
|
||||
913
lib/MySensors/hal/transport/RFM69/driver/old/RFM69_old.cpp
Normal file
913
lib/MySensors/hal/transport/RFM69/driver/old/RFM69_old.cpp
Normal file
@@ -0,0 +1,913 @@
|
||||
// **********************************************************************************
|
||||
// Driver definition for HopeRF RFM69W/RFM69HW/RFM69CW/RFM69HCW, Semtech SX1231/1231H
|
||||
// **********************************************************************************
|
||||
// Copyright Felix Rusu (2014), felix@lowpowerlab.com
|
||||
// http://lowpowerlab.com/
|
||||
// **********************************************************************************
|
||||
// License
|
||||
// **********************************************************************************
|
||||
// This program is free software; you can redistribute it
|
||||
// and/or modify it under the terms of the GNU General
|
||||
// Public License as published by the Free Software
|
||||
// Foundation; either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will
|
||||
// be useful, but WITHOUT ANY WARRANTY; without even the
|
||||
// implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
// PARTICULAR PURPOSE. See the GNU General Public
|
||||
// License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General
|
||||
// Public License along with this program.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// Licence can be viewed at
|
||||
// http://www.gnu.org/licenses/gpl-3.0.txt
|
||||
//
|
||||
// Please maintain this license information along with authorship
|
||||
// and copyright notices in any redistribution of this code
|
||||
// **********************************************************************************
|
||||
#include "RFM69_old.h"
|
||||
#include "RFM69registers_old.h"
|
||||
|
||||
volatile uint8_t RFM69::DATA[RFM69_MAX_DATA_LEN];
|
||||
volatile uint8_t RFM69::_mode; // current transceiver state
|
||||
volatile uint8_t RFM69::DATALEN;
|
||||
volatile uint8_t RFM69::SENDERID;
|
||||
volatile uint8_t RFM69::TARGETID; // should match _address
|
||||
volatile uint8_t RFM69::PAYLOADLEN;
|
||||
volatile uint8_t RFM69::ACK_REQUESTED;
|
||||
volatile uint8_t
|
||||
RFM69::ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request
|
||||
volatile int16_t
|
||||
RFM69::RSSI; // most accurate RSSI during reception (closest to the reception)
|
||||
RFM69* RFM69::selfPointer;
|
||||
|
||||
bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID)
|
||||
{
|
||||
//powerUp();
|
||||
//reset();
|
||||
const uint8_t CONFIG[][2] = {
|
||||
/* 0x01 */ { REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY },
|
||||
/* 0x02 */ { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 }, // no shaping
|
||||
/* 0x03 */ { REG_BITRATEMSB, RF_BITRATEMSB_55555}, // default: 4.8 KBPS
|
||||
/* 0x04 */ { REG_BITRATELSB, RF_BITRATELSB_55555},
|
||||
/* 0x05 */ { REG_FDEVMSB, RF_FDEVMSB_50000}, // default: 5KHz, (FDEV + BitRate / 2 <= 500KHz)
|
||||
/* 0x06 */ { REG_FDEVLSB, RF_FDEVLSB_50000},
|
||||
|
||||
/* 0x07 */ { REG_FRFMSB, (uint8_t) (freqBand==RFM69_315MHZ ? RF_FRFMSB_315 : (freqBand==RFM69_433MHZ ? RF_FRFMSB_433 : (freqBand==RFM69_868MHZ ? RF_FRFMSB_868 : RF_FRFMSB_915))) },
|
||||
/* 0x08 */ { REG_FRFMID, (uint8_t) (freqBand==RFM69_315MHZ ? RF_FRFMID_315 : (freqBand==RFM69_433MHZ ? RF_FRFMID_433 : (freqBand==RFM69_868MHZ ? RF_FRFMID_868 : RF_FRFMID_915))) },
|
||||
/* 0x09 */ { REG_FRFLSB, (uint8_t) (freqBand==RFM69_315MHZ ? RF_FRFLSB_315 : (freqBand==RFM69_433MHZ ? RF_FRFLSB_433 : (freqBand==RFM69_868MHZ ? RF_FRFLSB_868 : RF_FRFLSB_915))) },
|
||||
|
||||
// looks like PA1 and PA2 are not implemented on RFM69W, hence the max output power is 13dBm
|
||||
// +17dBm and +20dBm are possible on RFM69HW
|
||||
// +13dBm formula: Pout = -18 + OutputPower (with PA0 or PA1**)
|
||||
// +17dBm formula: Pout = -14 + OutputPower (with PA1 and PA2)**
|
||||
// +20dBm formula: Pout = -11 + OutputPower (with PA1 and PA2)** and high power PA settings (section 3.3.7 in datasheet)
|
||||
///* 0x11 */ { REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | RF_PALEVEL_OUTPUTPOWER_11111},
|
||||
///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, // over current protection (default is 95mA)
|
||||
|
||||
// RXBW defaults are { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4KHz)
|
||||
/* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2 }, // (BitRate < 2 * RxBw)
|
||||
//for BR-19200: /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_3 },
|
||||
/* 0x25 */ { REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01 }, // DIO0 is the only IRQ we're using
|
||||
/* 0x26 */ { REG_DIOMAPPING2, RF_DIOMAPPING2_CLKOUT_OFF }, // DIO5 ClkOut disable for power saving
|
||||
/* 0x28 */ { REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN }, // writing to this bit ensures that the FIFO & status flags are reset
|
||||
/* 0x29 */ { REG_RSSITHRESH, 220 }, // must be set to dBm = (-Sensitivity / 2), default is 0xE4 = 228 so -114dBm
|
||||
///* 0x2D */ { REG_PREAMBLELSB, RF_PREAMBLESIZE_LSB_VALUE } // default 3 preamble bytes 0xAAAAAA
|
||||
/* 0x2E */ { REG_SYNCCONFIG, RF_SYNC_ON | RF_SYNC_FIFOFILL_AUTO | RF_SYNC_SIZE_2 | RF_SYNC_TOL_0 },
|
||||
/* 0x2F */ { REG_SYNCVALUE1, 0x2D }, // attempt to make this compatible with sync1 byte of RFM12B lib
|
||||
/* 0x30 */ { REG_SYNCVALUE2, networkID }, // NETWORK ID
|
||||
/* 0x37 */ { REG_PACKETCONFIG1, RF_PACKET1_FORMAT_VARIABLE | RF_PACKET1_DCFREE_OFF | RF_PACKET1_CRC_ON | RF_PACKET1_CRCAUTOCLEAR_ON | RF_PACKET1_ADRSFILTERING_OFF },
|
||||
/* 0x38 */ { REG_PAYLOADLENGTH, 66 }, // in variable length mode: the max frame size, not used in TX
|
||||
///* 0x39 */ { REG_NODEADRS, nodeID }, // turned off because we're not using address filtering
|
||||
/* 0x3C */ { REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY | RF_FIFOTHRESH_VALUE }, // TX on FIFO not empty
|
||||
/* 0x3D */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_2BITS | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, // RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
|
||||
//for BR-19200: /* 0x3D */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_NONE | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, // RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
|
||||
/* 0x6F */ { REG_TESTDAGC, RF_DAGC_IMPROVED_LOWBETA0 }, // run DAGC continuously in RX mode for Fading Margin Improvement, recommended default for AfcLowBetaOn=0
|
||||
{255, 0}
|
||||
};
|
||||
|
||||
hwDigitalWrite(_slaveSelectPin, HIGH);
|
||||
hwPinMode(_slaveSelectPin, OUTPUT);
|
||||
hwPinMode(_interruptPin, INPUT);
|
||||
|
||||
RFM69_SPI.begin();
|
||||
unsigned long start = hwMillis();
|
||||
const uint8_t timeout = 50;
|
||||
do {
|
||||
writeReg(REG_SYNCVALUE1, 0xAA);
|
||||
doYield();
|
||||
} while (readReg(REG_SYNCVALUE1) != 0xAA && hwMillis()-start < timeout);
|
||||
if (hwMillis() - start >= timeout) {
|
||||
// timeout: checking wiring or replace module
|
||||
return false;
|
||||
}
|
||||
start = hwMillis();
|
||||
do {
|
||||
writeReg(REG_SYNCVALUE1, 0x55);
|
||||
doYield();
|
||||
} while (readReg(REG_SYNCVALUE1) != 0x55 && hwMillis()-start < timeout);
|
||||
if (hwMillis() - start >= timeout) {
|
||||
// timeout: checking wiring or replace module
|
||||
return false;
|
||||
}
|
||||
for (uint8_t i = 0; CONFIG[i][0] != 255; i++) {
|
||||
writeReg(CONFIG[i][0], CONFIG[i][1]);
|
||||
}
|
||||
|
||||
// Encryption is persistent between resets and can trip you up during debugging.
|
||||
// Disable it during initialization so we always start from a known state.
|
||||
encrypt(0);
|
||||
|
||||
setHighPower(_isRFM69HW); // called regardless if it's a RFM69W or RFM69HW
|
||||
setMode(RFM69_MODE_STANDBY);
|
||||
start = hwMillis();
|
||||
while (((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) && hwMillis()-start < timeout) {
|
||||
doYield();
|
||||
} // wait for ModeReady
|
||||
if (hwMillis()-start >= timeout) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//RFM69_SPI.usingInterrupt(_interruptNum);
|
||||
attachInterrupt(_interruptNum, RFM69::isr0, RISING);
|
||||
|
||||
selfPointer = this;
|
||||
_address = nodeID;
|
||||
return true;
|
||||
}
|
||||
|
||||
// return the frequency (in Hz)
|
||||
uint32_t RFM69::getFrequency()
|
||||
{
|
||||
return RFM69_FSTEP * (((uint32_t) readReg(REG_FRFMSB) << 16) + ((uint16_t) readReg(
|
||||
REG_FRFMID) << 8) + readReg(REG_FRFLSB));
|
||||
}
|
||||
|
||||
// set the frequency (in Hz)
|
||||
void RFM69::setFrequency(uint32_t freqHz)
|
||||
{
|
||||
uint8_t oldMode = _mode;
|
||||
if (oldMode == RFM69_MODE_TX) {
|
||||
setMode(RFM69_MODE_RX);
|
||||
}
|
||||
freqHz /= RFM69_FSTEP; // divide down by FSTEP to get FRF
|
||||
writeReg(REG_FRFMSB, freqHz >> 16);
|
||||
writeReg(REG_FRFMID, freqHz >> 8);
|
||||
writeReg(REG_FRFLSB, freqHz);
|
||||
if (oldMode == RFM69_MODE_RX) {
|
||||
setMode(RFM69_MODE_SYNTH);
|
||||
}
|
||||
setMode(oldMode);
|
||||
}
|
||||
|
||||
void RFM69::setMode(uint8_t newMode)
|
||||
{
|
||||
if (newMode == _mode) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t currentOPMODE = readReg(REG_OPMODE) & 0xE3;
|
||||
|
||||
switch (newMode) {
|
||||
case RFM69_MODE_TX:
|
||||
writeReg(REG_OPMODE, currentOPMODE | RF_OPMODE_TRANSMITTER);
|
||||
if (_isRFM69HW) {
|
||||
setHighPowerRegs(true);
|
||||
}
|
||||
break;
|
||||
case RFM69_MODE_RX:
|
||||
writeReg(REG_OPMODE, currentOPMODE | RF_OPMODE_RECEIVER);
|
||||
if (_isRFM69HW) {
|
||||
setHighPowerRegs(false);
|
||||
}
|
||||
break;
|
||||
case RFM69_MODE_SYNTH:
|
||||
writeReg(REG_OPMODE, currentOPMODE | RF_OPMODE_SYNTHESIZER);
|
||||
break;
|
||||
case RFM69_MODE_STANDBY:
|
||||
writeReg(REG_OPMODE, currentOPMODE | RF_OPMODE_STANDBY);
|
||||
break;
|
||||
case RFM69_MODE_SLEEP:
|
||||
writeReg(REG_OPMODE, currentOPMODE | RF_OPMODE_SLEEP);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
// we are using packet mode, so this check is not really needed
|
||||
// but waiting for mode ready is necessary when going from sleep because the FIFO may not be immediately available from previous mode
|
||||
while (_mode == RFM69_MODE_SLEEP &&
|
||||
(readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
|
||||
|
||||
_mode = newMode;
|
||||
}
|
||||
|
||||
//put transceiver in sleep mode to save battery - to wake or resume receiving just call receiveDone()
|
||||
void RFM69::sleep(void)
|
||||
{
|
||||
setMode(RFM69_MODE_SLEEP);
|
||||
}
|
||||
|
||||
void RFM69::standBy(void)
|
||||
{
|
||||
setMode(RFM69_MODE_STANDBY);
|
||||
}
|
||||
void RFM69::powerDown(void)
|
||||
{
|
||||
#if defined(MY_RFM69_POWER_PIN)
|
||||
hwDigitalWrite(MY_RFM69_POWER_PIN, LOW);
|
||||
#endif
|
||||
}
|
||||
void RFM69::powerUp(void)
|
||||
{
|
||||
#if defined(MY_RFM69_POWER_PIN)
|
||||
hwDigitalWrite(MY_RFM69_POWER_PIN, HIGH);
|
||||
delay(RFM69_POWERUP_DELAY_MS);
|
||||
#endif
|
||||
}
|
||||
void RFM69::reset(void)
|
||||
{
|
||||
// reset radio if RESET pin is defined
|
||||
#ifdef MY_RFM69_RST_PIN
|
||||
hwPinMode(MY_RFM69_RST_PIN, OUTPUT);
|
||||
hwDigitalWrite(MY_RFM69_RST_PIN, HIGH);
|
||||
// 100uS
|
||||
delayMicroseconds(100);
|
||||
hwDigitalWrite(MY_RFM69_RST_PIN, LOW);
|
||||
// wait until chip ready
|
||||
delay(5);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool RFM69::sanityCheck(void)
|
||||
{
|
||||
bool result = true;
|
||||
// check Bitrate
|
||||
result &= readReg(REG_BITRATEMSB) == RF_BITRATEMSB_55555;
|
||||
result &= readReg(REG_BITRATELSB) == RF_BITRATELSB_55555;
|
||||
// default: 5KHz, (FDEV + BitRate / 2 <= 500KHz)
|
||||
result &= readReg(REG_FDEVMSB) == RF_FDEVMSB_50000;
|
||||
result &= readReg(REG_FDEVLSB) == RF_FDEVLSB_50000;
|
||||
/*
|
||||
// Check radio frequency band
|
||||
result &= readReg(REG_FRFMSB) == (uint8_t)(MY_RFM69_FREQUENCY == RFM69_315MHZ ? RF_FRFMSB_315 : (MY_RFM69_FREQUENCY == RFM69_433MHZ ? RF_FRFMSB_433 : (MY_RFM69_FREQUENCY == RFM69_868MHZ ? RF_FRFMSB_868 : RF_FRFMSB_915)));
|
||||
result &= readReg(REG_FRFMID) == (uint8_t)(MY_RFM69_FREQUENCY == RFM69_315MHZ ? RF_FRFMID_315 : (MY_RFM69_FREQUENCY == RFM69_433MHZ ? RF_FRFMID_433 : (MY_RFM69_FREQUENCY == RFM69_868MHZ ? RF_FRFMID_868 : RF_FRFMID_915)));
|
||||
result &= readReg(REG_FRFLSB) == (uint8_t)(MY_RFM69_FREQUENCY == RFM69_315MHZ ? RF_FRFLSB_315 : (MY_RFM69_FREQUENCY == RFM69_433MHZ ? RF_FRFLSB_433 : (MY_RFM69_FREQUENCY == RFM69_868MHZ ? RF_FRFLSB_868 : RF_FRFLSB_915)));
|
||||
*/
|
||||
return result;
|
||||
}
|
||||
|
||||
//set this node's address
|
||||
void RFM69::setAddress(uint8_t addr)
|
||||
{
|
||||
_address = addr;
|
||||
writeReg(REG_NODEADRS, _address);
|
||||
}
|
||||
|
||||
//set this node's network id
|
||||
void RFM69::setNetwork(uint8_t networkID)
|
||||
{
|
||||
writeReg(REG_SYNCVALUE2, networkID);
|
||||
}
|
||||
|
||||
// set *transmit/TX* output power: 0=min, 31=max
|
||||
// this results in a "weaker" transmitted signal, and directly results in a lower RSSI at the receiver
|
||||
// the power configurations are explained in the SX1231H datasheet (Table 10 on p21; RegPaLevel p66): http://www.semtech.com/images/datasheet/sx1231h.pdf
|
||||
// valid powerLevel parameter values are 0-31 and result in a directly proportional effect on the output/transmission power
|
||||
// this function implements 2 modes as follows:
|
||||
// - for RFM69W the range is from 0-31 [-18dBm to 13dBm] (PA0 only on RFIO pin)
|
||||
// - for RFM69HW the range is from 0-31 [5dBm to 20dBm] (PA1 & PA2 on PA_BOOST pin & high Power PA settings - see section 3.3.7 in datasheet, p22)
|
||||
void RFM69::setPowerLevel(uint8_t powerLevel)
|
||||
{
|
||||
_powerLevel = (powerLevel > 31 ? 31 : powerLevel);
|
||||
if (_isRFM69HW) {
|
||||
_powerLevel /= 2;
|
||||
}
|
||||
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0xE0) | _powerLevel);
|
||||
}
|
||||
|
||||
bool RFM69::canSend()
|
||||
{
|
||||
if (_mode == RFM69_MODE_RX && PAYLOADLEN == 0 &&
|
||||
readRSSI() < CSMA_LIMIT) { // if signal stronger than -100dBm is detected assume channel activity
|
||||
setMode(RFM69_MODE_STANDBY);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void RFM69::send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK)
|
||||
{
|
||||
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) |
|
||||
RF_PACKET2_RXRESTART); // avoid RX deadlocks
|
||||
uint32_t now = hwMillis();
|
||||
while (!canSend() && hwMillis() - now < RFM69_CSMA_LIMIT_MS) {
|
||||
receiveDone();
|
||||
}
|
||||
sendFrame(toAddress, buffer, bufferSize, requestACK, false);
|
||||
}
|
||||
|
||||
// to increase the chance of getting a packet across, call this function instead of send
|
||||
// and it handles all the ACK requesting/retrying for you :)
|
||||
// The only twist is that you have to manually listen to ACK requests on the other side and send back the ACKs
|
||||
// The reason for the semi-automaton is that the lib is interrupt driven and
|
||||
// requires user action to read the received data and decide what to do with it
|
||||
// replies usually take only 5..8ms at 50kbps@915MHz
|
||||
bool RFM69::sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
|
||||
uint8_t retries, uint8_t retryWaitTime)
|
||||
{
|
||||
for (uint8_t i = 0; i <= retries; i++) {
|
||||
send(toAddress, buffer, bufferSize, true);
|
||||
uint32_t sentTime = hwMillis();
|
||||
while (hwMillis() - sentTime < retryWaitTime) {
|
||||
if (ACKReceived(toAddress)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//Serial.print(" RETRY#"); Serial.println(i + 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// should be polled immediately after sending a packet with ACK request
|
||||
bool RFM69::ACKReceived(uint8_t fromNodeID)
|
||||
{
|
||||
if (receiveDone()) {
|
||||
return (SENDERID == fromNodeID || fromNodeID == RFM69_BROADCAST_ADDR) && ACK_RECEIVED;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// check whether an ACK was requested in the last received packet (non-broadcasted packet)
|
||||
bool RFM69::ACKRequested()
|
||||
{
|
||||
return ACK_REQUESTED && (TARGETID != RFM69_BROADCAST_ADDR);
|
||||
}
|
||||
|
||||
// should be called immediately after reception in case sender wants ACK
|
||||
void RFM69::sendACK(const void* buffer, uint8_t bufferSize)
|
||||
{
|
||||
ACK_REQUESTED =
|
||||
0; // TWS added to make sure we don't end up in a timing race and infinite loop sending Acks
|
||||
uint8_t sender = SENDERID;
|
||||
int16_t _RSSI = RSSI; // save payload received RSSI value
|
||||
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) |
|
||||
RF_PACKET2_RXRESTART); // avoid RX deadlocks
|
||||
uint32_t now = hwMillis();
|
||||
while (!canSend() && hwMillis() - now < RFM69_CSMA_LIMIT_MS) {
|
||||
receiveDone();
|
||||
doYield();
|
||||
}
|
||||
SENDERID = sender; // TWS: Restore SenderID after it gets wiped out by receiveDone()
|
||||
sendFrame(sender, buffer, bufferSize, false, true);
|
||||
RSSI = _RSSI; // restore payload RSSI
|
||||
}
|
||||
|
||||
void RFM69::interruptHook(uint8_t CTLbyte)
|
||||
{
|
||||
(void)CTLbyte;
|
||||
};
|
||||
|
||||
// internal function
|
||||
void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK,
|
||||
bool sendACK)
|
||||
{
|
||||
setMode(RFM69_MODE_STANDBY); // turn off receiver to prevent reception while filling fifo
|
||||
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) {} // wait for ModeReady
|
||||
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent"
|
||||
if (bufferSize > RFM69_MAX_DATA_LEN) {
|
||||
bufferSize = RFM69_MAX_DATA_LEN;
|
||||
}
|
||||
|
||||
// control byte
|
||||
uint8_t CTLbyte = 0x00;
|
||||
if (sendACK) {
|
||||
CTLbyte = RFM69_CTL_SENDACK;
|
||||
} else if (requestACK) {
|
||||
CTLbyte = RFM69_CTL_REQACK;
|
||||
}
|
||||
|
||||
// write to FIFO
|
||||
select();
|
||||
RFM69_SPI.transfer(REG_FIFO | 0x80);
|
||||
RFM69_SPI.transfer(bufferSize + 3);
|
||||
RFM69_SPI.transfer(toAddress);
|
||||
RFM69_SPI.transfer(_address);
|
||||
RFM69_SPI.transfer(CTLbyte);
|
||||
|
||||
for (uint8_t i = 0; i < bufferSize; i++) {
|
||||
RFM69_SPI.transfer(((uint8_t *)buffer)[i]);
|
||||
}
|
||||
unselect();
|
||||
|
||||
// no need to wait for transmit mode to be ready since its handled by the radio
|
||||
setMode(RFM69_MODE_TX);
|
||||
uint32_t txStart = hwMillis();
|
||||
while (hwDigitalRead(_interruptPin) == 0 &&
|
||||
hwMillis() - txStart <
|
||||
RFM69_TX_LIMIT_MS) {} // wait for DIO0 to turn HIGH signalling transmission finish
|
||||
//while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // wait for ModeReady
|
||||
setMode(RFM69_MODE_STANDBY);
|
||||
}
|
||||
|
||||
// internal function - interrupt gets called when a packet is received
|
||||
void IRQ_HANDLER_ATTR RFM69::interruptHandler()
|
||||
{
|
||||
//hwPinMode(4, OUTPUT);
|
||||
//hwDigitalWrite(4, 1);
|
||||
if (_mode == RFM69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)) {
|
||||
//RSSI = readRSSI();
|
||||
setMode(RFM69_MODE_STANDBY);
|
||||
select();
|
||||
RFM69_SPI.transfer(REG_FIFO & 0x7F);
|
||||
PAYLOADLEN = RFM69_SPI.transfer(0);
|
||||
PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution
|
||||
TARGETID = RFM69_SPI.transfer(0);
|
||||
if(!(_promiscuousMode || TARGETID == _address ||
|
||||
TARGETID ==
|
||||
RFM69_BROADCAST_ADDR) // match this node's address, or broadcast address or anything in promiscuous mode
|
||||
|| PAYLOADLEN <
|
||||
3) { // address situation could receive packets that are malformed and don't fit this libraries extra fields
|
||||
PAYLOADLEN = 0;
|
||||
unselect();
|
||||
receiveBegin();
|
||||
//hwDigitalWrite(4, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
DATALEN = PAYLOADLEN - 3;
|
||||
SENDERID = RFM69_SPI.transfer(0);
|
||||
uint8_t CTLbyte = RFM69_SPI.transfer(0);
|
||||
|
||||
ACK_RECEIVED = CTLbyte & RFM69_CTL_SENDACK; // extract ACK-received flag
|
||||
ACK_REQUESTED = CTLbyte & RFM69_CTL_REQACK; // extract ACK-requested flag
|
||||
|
||||
interruptHook(CTLbyte); // TWS: hook to derived class interrupt function
|
||||
|
||||
for (uint8_t i = 0; i < DATALEN; i++) {
|
||||
DATA[i] = RFM69_SPI.transfer(0);
|
||||
}
|
||||
if (DATALEN < RFM69_MAX_DATA_LEN) {
|
||||
DATA[DATALEN] = 0; // add null at end of string
|
||||
}
|
||||
unselect();
|
||||
setMode(RFM69_MODE_RX);
|
||||
}
|
||||
RSSI = readRSSI();
|
||||
//hwDigitalWrite(4, 0);
|
||||
}
|
||||
|
||||
// internal function
|
||||
void IRQ_HANDLER_ATTR RFM69::isr0()
|
||||
{
|
||||
selfPointer->interruptHandler();
|
||||
}
|
||||
|
||||
// internal function
|
||||
void RFM69::receiveBegin()
|
||||
{
|
||||
DATALEN = 0;
|
||||
SENDERID = 0;
|
||||
TARGETID = 0;
|
||||
PAYLOADLEN = 0;
|
||||
ACK_REQUESTED = 0;
|
||||
ACK_RECEIVED = 0;
|
||||
RSSI = 0;
|
||||
if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY) {
|
||||
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) |
|
||||
RF_PACKET2_RXRESTART); // avoid RX deadlocks
|
||||
}
|
||||
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); // set DIO0 to "PAYLOADREADY" in receive mode
|
||||
setMode(RFM69_MODE_RX);
|
||||
}
|
||||
|
||||
// checks if a packet was received and/or puts transceiver in receive (ie RX or listen) mode
|
||||
bool RFM69::receiveDone()
|
||||
{
|
||||
//ATOMIC_BLOCK(ATOMIC_FORCEON)
|
||||
//{
|
||||
noInterrupts(); // re-enabled in unselect() via setMode() or via receiveBegin()
|
||||
if (_mode == RFM69_MODE_RX && PAYLOADLEN > 0) {
|
||||
setMode(RFM69_MODE_STANDBY); // enables interrupts
|
||||
return true;
|
||||
} else if (_mode == RFM69_MODE_RX) { // already in RX no payload yet
|
||||
interrupts(); // explicitly re-enable interrupts
|
||||
return false;
|
||||
}
|
||||
receiveBegin();
|
||||
return false;
|
||||
//}
|
||||
}
|
||||
|
||||
// To enable encryption: radio.encrypt("ABCDEFGHIJKLMNOP");
|
||||
// To disable encryption: radio.encrypt(null) or radio.encrypt(0)
|
||||
// KEY HAS TO BE 16 bytes !!!
|
||||
void RFM69::encrypt(const char* key)
|
||||
{
|
||||
setMode(RFM69_MODE_STANDBY);
|
||||
if (key != 0) {
|
||||
select();
|
||||
RFM69_SPI.transfer(REG_AESKEY1 | 0x80);
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
RFM69_SPI.transfer(key[i]);
|
||||
}
|
||||
unselect();
|
||||
}
|
||||
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFE) | (key ? 1 : 0));
|
||||
}
|
||||
|
||||
// get the received signal strength indicator (RSSI)
|
||||
int16_t RFM69::readRSSI(bool forceTrigger)
|
||||
{
|
||||
int16_t rssi = 0;
|
||||
if (forceTrigger) {
|
||||
// RSSI trigger not needed if DAGC is in continuous mode
|
||||
writeReg(REG_RSSICONFIG, RF_RSSI_START);
|
||||
while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00) {} // wait for RSSI_Ready
|
||||
}
|
||||
rssi = -readReg(REG_RSSIVALUE);
|
||||
rssi >>= 1;
|
||||
return rssi;
|
||||
}
|
||||
|
||||
uint8_t RFM69::readReg(uint8_t addr)
|
||||
{
|
||||
select();
|
||||
RFM69_SPI.transfer(addr & 0x7F);
|
||||
uint8_t regval = RFM69_SPI.transfer(0);
|
||||
unselect();
|
||||
return regval;
|
||||
}
|
||||
|
||||
void RFM69::writeReg(uint8_t addr, uint8_t value)
|
||||
{
|
||||
select();
|
||||
RFM69_SPI.transfer(addr | 0x80);
|
||||
RFM69_SPI.transfer(value);
|
||||
unselect();
|
||||
}
|
||||
|
||||
// select the RFM69 transceiver (save SPI settings, set CS low)
|
||||
void RFM69::select()
|
||||
{
|
||||
noInterrupts();
|
||||
#if defined (SPCR) && defined (SPSR)
|
||||
// save current SPI settings
|
||||
_SPCR = SPCR;
|
||||
_SPSR = SPSR;
|
||||
#endif
|
||||
// set RFM69 SPI settings
|
||||
RFM69_SPI.setDataMode(SPI_MODE0);
|
||||
RFM69_SPI.setBitOrder(MSBFIRST);
|
||||
RFM69_SPI.setClockDivider(RFM69_CLOCK_DIV);
|
||||
hwDigitalWrite(_slaveSelectPin, LOW);
|
||||
}
|
||||
|
||||
// unselect the RFM69 transceiver (set CS high, restore SPI settings)
|
||||
void RFM69::unselect()
|
||||
{
|
||||
hwDigitalWrite(_slaveSelectPin, HIGH);
|
||||
// restore SPI settings to what they were before talking to RFM69
|
||||
#if defined (SPCR) && defined (SPSR)
|
||||
SPCR = _SPCR;
|
||||
SPSR = _SPSR;
|
||||
#endif
|
||||
interrupts();
|
||||
}
|
||||
|
||||
// true = disable filtering to capture all frames on network
|
||||
// false = enable node/broadcast filtering to capture only frames sent to this/broadcast address
|
||||
void RFM69::promiscuous(bool onOff)
|
||||
{
|
||||
_promiscuousMode = onOff;
|
||||
//writeReg(REG_PACKETCONFIG1, (readReg(REG_PACKETCONFIG1) & 0xF9) | (onOff ? RF_PACKET1_ADRSFILTERING_OFF : RF_PACKET1_ADRSFILTERING_NODEBROADCAST));
|
||||
}
|
||||
|
||||
// for RFM69HW only: you must call setHighPower(true) after initialize() or else transmission won't work
|
||||
void RFM69::setHighPower(bool onOff)
|
||||
{
|
||||
_isRFM69HW = onOff;
|
||||
writeReg(REG_OCP, _isRFM69HW ? RF_OCP_OFF : RF_OCP_ON);
|
||||
if (_isRFM69HW) { // turning ON
|
||||
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0x1F) | RF_PALEVEL_PA1_ON |
|
||||
RF_PALEVEL_PA2_ON); // enable P1 & P2 amplifier stages
|
||||
} else {
|
||||
writeReg(REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF |
|
||||
_powerLevel); // enable P0 only
|
||||
}
|
||||
}
|
||||
|
||||
// internal function
|
||||
void RFM69::setHighPowerRegs(bool onOff)
|
||||
{
|
||||
writeReg(REG_TESTPA1, onOff ? 0x5D : 0x55);
|
||||
writeReg(REG_TESTPA2, onOff ? 0x7C : 0x70);
|
||||
}
|
||||
|
||||
// set the slave select (CS) pin
|
||||
void RFM69::setCS(uint8_t newSPISlaveSelect)
|
||||
{
|
||||
_slaveSelectPin = newSPISlaveSelect;
|
||||
hwDigitalWrite(_slaveSelectPin, HIGH);
|
||||
hwPinMode(_slaveSelectPin, OUTPUT);
|
||||
}
|
||||
|
||||
//for debugging
|
||||
#define REGISTER_DETAIL 0
|
||||
#if REGISTER_DETAIL
|
||||
// SERIAL PRINT
|
||||
// replace Serial.print("string") with SerialPrint("string")
|
||||
#define SerialPrint(x) SerialPrint_P(PSTR(x))
|
||||
void SerialWrite ( uint8_t c )
|
||||
{
|
||||
Serial.write ( c );
|
||||
}
|
||||
|
||||
void SerialPrint_P(PGM_P str, void (*f)(uint8_t) = SerialWrite )
|
||||
{
|
||||
for (uint8_t c; (c = pgm_read_byte(str)); str++) {
|
||||
(*f)(c);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void RFM69::readAllRegs()
|
||||
{
|
||||
#if REGISTER_DETAIL
|
||||
int capVal;
|
||||
|
||||
//... State Variables for intelligent decoding
|
||||
uint8_t modeFSK = 0;
|
||||
int bitRate = 0;
|
||||
int freqDev = 0;
|
||||
long freqCenter = 0;
|
||||
#endif
|
||||
|
||||
Serial.println("Address - HEX - BIN");
|
||||
for (uint8_t regAddr = 1; regAddr <= 0x4F; regAddr++) {
|
||||
select();
|
||||
RFM69_SPI.transfer(regAddr & 0x7F); // send address + r/w bit
|
||||
uint8_t regVal = RFM69_SPI.transfer(0);
|
||||
unselect();
|
||||
|
||||
Serial.print(regAddr, HEX);
|
||||
Serial.print(" - ");
|
||||
Serial.print(regVal,HEX);
|
||||
Serial.print(" - ");
|
||||
Serial.println(regVal,BIN);
|
||||
|
||||
#if REGISTER_DETAIL
|
||||
switch ( regAddr ) {
|
||||
case 0x1 : {
|
||||
SerialPrint ( "Controls the automatic Sequencer ( see section 4.2 )\nSequencerOff : " );
|
||||
if ( 0x80 & regVal ) {
|
||||
SerialPrint ( "1 -> Mode is forced by the user\n" );
|
||||
} else {
|
||||
SerialPrint ( "0 -> Operating mode as selected with Mode bits in RegOpMode is automatically reached with the Sequencer\n" );
|
||||
}
|
||||
|
||||
SerialPrint( "\nEnables Listen mode, should be enabled whilst in Standby mode:\nListenOn : " );
|
||||
if ( 0x40 & regVal ) {
|
||||
SerialPrint ( "1 -> On\n" );
|
||||
} else {
|
||||
SerialPrint ( "0 -> Off ( see section 4.3)\n" );
|
||||
}
|
||||
|
||||
SerialPrint( "\nAborts Listen mode when set together with ListenOn=0 See section 4.3.4 for details (Always reads 0.)\n" );
|
||||
if ( 0x20 & regVal ) {
|
||||
SerialPrint ( "ERROR - ListenAbort should NEVER return 1 this is a write only register\n" );
|
||||
}
|
||||
|
||||
SerialPrint("\nTransceiver's operating modes:\nMode : ");
|
||||
capVal = (regVal >> 2) & 0x7;
|
||||
if ( capVal == 0b000 ) {
|
||||
SerialPrint ( "000 -> Sleep mode (SLEEP)\n" );
|
||||
} else if ( capVal = 0b001 ) {
|
||||
SerialPrint ( "001 -> Standby mode (STDBY)\n" );
|
||||
} else if ( capVal = 0b010 ) {
|
||||
SerialPrint ( "010 -> Frequency Synthesizer mode (FS)\n" );
|
||||
} else if ( capVal = 0b011 ) {
|
||||
SerialPrint ( "011 -> Transmitter mode (TX)\n" );
|
||||
} else if ( capVal = 0b100 ) {
|
||||
SerialPrint ( "100 -> Receiver Mode (RX)\n" );
|
||||
} else {
|
||||
Serial.print( capVal, BIN );
|
||||
SerialPrint ( " -> RESERVED\n" );
|
||||
}
|
||||
SerialPrint ( "\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x2 : {
|
||||
|
||||
SerialPrint("Data Processing mode:\nDataMode : ");
|
||||
capVal = (regVal >> 5) & 0x3;
|
||||
if ( capVal == 0b00 ) {
|
||||
SerialPrint ( "00 -> Packet mode\n" );
|
||||
} else if ( capVal == 0b01 ) {
|
||||
SerialPrint ( "01 -> reserved\n" );
|
||||
} else if ( capVal == 0b10 ) {
|
||||
SerialPrint ( "10 -> Continuous mode with bit synchronizer\n" );
|
||||
} else if ( capVal == 0b11 ) {
|
||||
SerialPrint ( "11 -> Continuous mode without bit synchronizer\n" );
|
||||
}
|
||||
|
||||
SerialPrint("\nModulation scheme:\nModulation Type : ");
|
||||
capVal = (regVal >> 3) & 0x3;
|
||||
if ( capVal == 0b00 ) {
|
||||
SerialPrint ( "00 -> FSK\n" );
|
||||
modeFSK = 1;
|
||||
} else if ( capVal == 0b01 ) {
|
||||
SerialPrint ( "01 -> OOK\n" );
|
||||
} else if ( capVal == 0b10 ) {
|
||||
SerialPrint ( "10 -> reserved\n" );
|
||||
} else if ( capVal == 0b11 ) {
|
||||
SerialPrint ( "11 -> reserved\n" );
|
||||
}
|
||||
|
||||
SerialPrint("\nData shaping: ");
|
||||
if ( modeFSK ) {
|
||||
SerialPrint( "in FSK:\n" );
|
||||
} else {
|
||||
SerialPrint( "in OOK:\n" );
|
||||
}
|
||||
SerialPrint ("ModulationShaping : ");
|
||||
capVal = regVal & 0x3;
|
||||
if ( modeFSK ) {
|
||||
if ( capVal == 0b00 ) {
|
||||
SerialPrint ( "00 -> no shaping\n" );
|
||||
} else if ( capVal == 0b01 ) {
|
||||
SerialPrint ( "01 -> Gaussian filter, BT = 1.0\n" );
|
||||
} else if ( capVal == 0b10 ) {
|
||||
SerialPrint ( "10 -> Gaussian filter, BT = 0.5\n" );
|
||||
} else if ( capVal == 0b11 ) {
|
||||
SerialPrint ( "11 -> Gaussian filter, BT = 0.3\n" );
|
||||
}
|
||||
} else {
|
||||
if ( capVal == 0b00 ) {
|
||||
SerialPrint ( "00 -> no shaping\n" );
|
||||
} else if ( capVal == 0b01 ) {
|
||||
SerialPrint ( "01 -> filtering with f(cutoff) = BR\n" );
|
||||
} else if ( capVal == 0b10 ) {
|
||||
SerialPrint ( "10 -> filtering with f(cutoff) = 2*BR\n" );
|
||||
} else if ( capVal == 0b11 ) {
|
||||
SerialPrint ( "ERROR - 11 is reserved\n" );
|
||||
}
|
||||
}
|
||||
|
||||
SerialPrint ( "\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x3 : {
|
||||
bitRate = (regVal << 8);
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x4 : {
|
||||
bitRate |= regVal;
|
||||
SerialPrint ( "Bit Rate (Chip Rate when Manchester encoding is enabled)\nBitRate : ");
|
||||
unsigned long val = 32UL * 1000UL * 1000UL / bitRate;
|
||||
Serial.println( val );
|
||||
SerialPrint( "\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x5 : {
|
||||
freqDev = ( (regVal & 0x3f) << 8 );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x6 : {
|
||||
freqDev |= regVal;
|
||||
SerialPrint( "Frequency deviation\nFdev : " );
|
||||
unsigned long val = 61UL * freqDev;
|
||||
Serial.println( val );
|
||||
SerialPrint ( "\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x7 : {
|
||||
unsigned long tempVal = regVal;
|
||||
freqCenter = ( tempVal << 16 );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x8 : {
|
||||
unsigned long tempVal = regVal;
|
||||
freqCenter = freqCenter | ( tempVal << 8 );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x9 : {
|
||||
freqCenter = freqCenter | regVal;
|
||||
SerialPrint ( "RF Carrier frequency\nFRF : " );
|
||||
unsigned long val = 61UL * freqCenter;
|
||||
Serial.println( val );
|
||||
SerialPrint( "\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0xa : {
|
||||
SerialPrint ( "RC calibration control & status\nRcCalDone : " );
|
||||
if ( 0x40 & regVal ) {
|
||||
SerialPrint ( "1 -> RC calibration is over\n" );
|
||||
} else {
|
||||
SerialPrint ( "0 -> RC calibration is in progress\n" );
|
||||
}
|
||||
|
||||
SerialPrint ( "\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0xb : {
|
||||
SerialPrint ( "Improved AFC routine for signals with modulation index lower than 2. Refer to section 3.4.16 for details\nAfcLowBetaOn : " );
|
||||
if ( 0x20 & regVal ) {
|
||||
SerialPrint ( "1 -> Improved AFC routine\n" );
|
||||
} else {
|
||||
SerialPrint ( "0 -> Standard AFC routine\n" );
|
||||
}
|
||||
SerialPrint ( "\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0xc : {
|
||||
SerialPrint ( "Reserved\n\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
case 0xd : {
|
||||
byte val;
|
||||
SerialPrint ( "Resolution of Listen mode Idle time (calibrated RC osc):\nListenResolIdle : " );
|
||||
val = regVal >> 6;
|
||||
if ( val == 0b00 ) {
|
||||
SerialPrint ( "00 -> reserved\n" );
|
||||
} else if ( val == 0b01 ) {
|
||||
SerialPrint ( "01 -> 64 us\n" );
|
||||
} else if ( val == 0b10 ) {
|
||||
SerialPrint ( "10 -> 4.1 ms\n" );
|
||||
} else if ( val == 0b11 ) {
|
||||
SerialPrint ( "11 -> 262 ms\n" );
|
||||
}
|
||||
|
||||
SerialPrint ( "\nResolution of Listen mode Rx time (calibrated RC osc):\nListenResolRx : " );
|
||||
val = (regVal >> 4) & 0x3;
|
||||
if ( val == 0b00 ) {
|
||||
SerialPrint ( "00 -> reserved\n" );
|
||||
} else if ( val == 0b01 ) {
|
||||
SerialPrint ( "01 -> 64 us\n" );
|
||||
} else if ( val == 0b10 ) {
|
||||
SerialPrint ( "10 -> 4.1 ms\n" );
|
||||
} else if ( val == 0b11 ) {
|
||||
SerialPrint ( "11 -> 262 ms\n" );
|
||||
}
|
||||
|
||||
SerialPrint ( "\nCriteria for packet acceptance in Listen mode:\nListenCriteria : " );
|
||||
if ( 0x8 & regVal ) {
|
||||
SerialPrint ( "1 -> signal strength is above RssiThreshold and SyncAddress matched\n" );
|
||||
} else {
|
||||
SerialPrint ( "0 -> signal strength is above RssiThreshold\n" );
|
||||
}
|
||||
|
||||
SerialPrint ( "\nAction taken after acceptance of a packet in Listen mode:\nListenEnd : " );
|
||||
val = (regVal >> 1 ) & 0x3;
|
||||
if ( val == 0b00 ) {
|
||||
SerialPrint ( "00 -> chip stays in Rx mode. Listen mode stops and must be disabled (see section 4.3)\n" );
|
||||
} else if ( val == 0b01 ) {
|
||||
SerialPrint ( "01 -> chip stays in Rx mode until PayloadReady or Timeout interrupt occurs. It then goes to the mode defined by Mode. Listen mode stops and must be disabled (see section 4.3)\n" );
|
||||
} else if ( val == 0b10 ) {
|
||||
SerialPrint ( "10 -> chip stays in Rx mode until PayloadReady or Timeout occurs. Listen mode then resumes in Idle state. FIFO content is lost at next Rx wakeup.\n" );
|
||||
} else if ( val == 0b11 ) {
|
||||
SerialPrint ( "11 -> Reserved\n" );
|
||||
}
|
||||
|
||||
|
||||
SerialPrint ( "\n" );
|
||||
break;
|
||||
}
|
||||
|
||||
default : {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
unselect();
|
||||
}
|
||||
|
||||
uint8_t RFM69::readTemperature(uint8_t calFactor) // returns centigrade
|
||||
{
|
||||
setMode(RFM69_MODE_STANDBY);
|
||||
writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);
|
||||
while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING)) {}
|
||||
return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF +
|
||||
calFactor; // 'complement' corrects the slope, rising temp = rising val
|
||||
} // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction
|
||||
|
||||
void RFM69::rcCalibration()
|
||||
{
|
||||
writeReg(REG_OSC1, RF_OSC1_RCCAL_START);
|
||||
while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00) {}
|
||||
}
|
||||
232
lib/MySensors/hal/transport/RFM69/driver/old/RFM69_old.h
Normal file
232
lib/MySensors/hal/transport/RFM69/driver/old/RFM69_old.h
Normal file
@@ -0,0 +1,232 @@
|
||||
// **********************************************************************************
|
||||
// Driver definition for HopeRF RFM69W/RFM69HW/RFM69CW/RFM69HCW, Semtech SX1231/1231H
|
||||
// **********************************************************************************
|
||||
// Copyright Felix Rusu (2014), felix@lowpowerlab.com
|
||||
// http://lowpowerlab.com/
|
||||
// **********************************************************************************
|
||||
// License
|
||||
// **********************************************************************************
|
||||
// This program is free software; you can redistribute it
|
||||
// and/or modify it under the terms of the GNU General
|
||||
// Public License as published by the Free Software
|
||||
// Foundation; either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will
|
||||
// be useful, but WITHOUT ANY WARRANTY; without even the
|
||||
// implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
// PARTICULAR PURPOSE. See the GNU General Public
|
||||
// License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General
|
||||
// Public License along with this program.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// Licence can be viewed at
|
||||
// http://www.gnu.org/licenses/gpl-3.0.txt
|
||||
//
|
||||
// Please maintain this license information along with authorship
|
||||
// and copyright notices in any redistribution of this code
|
||||
// **********************************************************************************
|
||||
#ifndef RFM69_h
|
||||
#define RFM69_h
|
||||
|
||||
#if !defined(RFM69_SPI)
|
||||
#define RFM69_SPI hwSPI //!< default SPI
|
||||
#endif
|
||||
|
||||
#define RFM69_MAX_DATA_LEN (61u) // to take advantage of the built in AES/CRC we want to limit the frame size to the internal FIFO size (66 bytes - 3 bytes overhead - 2 bytes crc)
|
||||
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
#if defined(__AVR_ATmega32U4__)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (3) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#else
|
||||
#define DEFAULT_RFM69_IRQ_PIN (2) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#endif
|
||||
#define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(MY_RFM69_IRQ_PIN) //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (5) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(MY_RFM69_IRQ_PIN) //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (16) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(DEFAULT_RFM69_IRQ_PIN) //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (2) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(MY_RFM69_IRQ_PIN) //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#elif defined(LINUX_ARCH_RASPBERRYPI)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (22) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#define DEFAULT_RFM69_IRQ_NUM DEFAULT_RFM69_IRQ_PIN //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_STM32F1)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (PA3) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#define DEFAULT_RFM69_IRQ_NUM DEFAULT_RFM69_IRQ_PIN //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#elif defined(TEENSYDUINO)
|
||||
#define DEFAULT_RFM69_IRQ_PIN (8) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(MY_RFM69_IRQ_PIN) //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#else
|
||||
#define DEFAULT_RFM69_IRQ_PIN (2) //!< DEFAULT_RFM69_IRQ_PIN
|
||||
#define DEFAULT_RFM69_IRQ_NUM (2) //!< DEFAULT_RFM69_IRQ_NUM
|
||||
#endif
|
||||
|
||||
#define DEFAULT_RFM69_CS_PIN (SS) //!< DEFAULT_RFM69_CS_PIN
|
||||
|
||||
// SPI clock divier for non-transaction implementations
|
||||
#if (MY_RFM69_SPI_SPEED >= F_CPU / 2)
|
||||
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV2 //!< SPI clock divider 2
|
||||
#elif (MY_RFM69_SPI_SPEED >= F_CPU / 4)
|
||||
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV4 //!< SPI clock divider 4
|
||||
#elif (MY_RFM69_SPI_SPEED >= F_CPU / 8)
|
||||
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV8 //!< SPI clock divider 8
|
||||
#elif (MY_RFM69_SPI_SPEED >= F_CPU / 16)
|
||||
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV16 //!< SPI clock divider 16
|
||||
#elif (MY_RFM69_SPI_SPEED >= F_CPU / 32)
|
||||
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV32 //!< SPI clock divider 32
|
||||
#elif (MY_RFM69_SPI_SPEED >= F_CPU / 64)
|
||||
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV64 //!< SPI clock divider 64
|
||||
#elif (MY_RFM69_SPI_SPEED >= F_CPU / 128)
|
||||
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV128 //!< SPI clock divider 128
|
||||
#else
|
||||
#define RFM69_CLOCK_DIV SPI_CLOCK_DIV256 //!< SPI clock divider 256
|
||||
#endif
|
||||
|
||||
// powerup delay
|
||||
#define RFM69_POWERUP_DELAY_MS (100u) //!< Power up delay, allow VCC to settle, transport to become fully operational
|
||||
|
||||
#define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access
|
||||
#define RFM69_MODE_SLEEP 0 // XTAL OFF
|
||||
#define RFM69_MODE_STANDBY 1 // XTAL ON
|
||||
#define RFM69_MODE_SYNTH 2 // PLL ON
|
||||
#define RFM69_MODE_RX 3 // RX MODE
|
||||
#define RFM69_MODE_TX 4 // TX MODE
|
||||
|
||||
// available frequency bands
|
||||
#define RFM69_315MHZ 31 // non trivial values to avoid misconfiguration
|
||||
#define RFM69_433MHZ 43
|
||||
#define RFM69_868MHZ 86
|
||||
#define RFM69_915MHZ 91
|
||||
|
||||
#define null 0
|
||||
#define COURSE_TEMP_COEF -90 // puts the temperature reading in the ballpark, user can fine tune the returned value
|
||||
#define RFM69_BROADCAST_ADDR 255
|
||||
#define RFM69_CSMA_LIMIT_MS 1000
|
||||
#define RFM69_TX_LIMIT_MS 1000
|
||||
|
||||
#define RFM69_FXOSC (32*1000000ul) //!< The crystal oscillator frequency of the module, 32MHz
|
||||
#define RFM69_FSTEP (RFM69_FXOSC / 524288ul) //!< The Frequency Synthesizer step
|
||||
|
||||
// TWS: define CTLbyte bits
|
||||
#define RFM69_CTL_SENDACK 0x80
|
||||
#define RFM69_CTL_REQACK 0x40
|
||||
|
||||
/** RFM69 class */
|
||||
class RFM69
|
||||
{
|
||||
public:
|
||||
static volatile uint8_t DATA[RFM69_MAX_DATA_LEN]; //!< recv/xmit buf, including hdr & crc bytes
|
||||
static volatile uint8_t DATALEN; //!< DATALEN
|
||||
static volatile uint8_t SENDERID; //!< SENDERID
|
||||
static volatile uint8_t TARGETID; //!< should match _address
|
||||
static volatile uint8_t PAYLOADLEN; //!< PAYLOADLEN
|
||||
static volatile uint8_t ACK_REQUESTED; //!< ACK_REQUESTED
|
||||
static volatile uint8_t
|
||||
ACK_RECEIVED; //!< Should be polled immediately after sending a packet with ACK requestwith ACK request
|
||||
static volatile int16_t RSSI; //!< most accurate RSSI during reception (closest to the reception)
|
||||
static volatile uint8_t _mode; //!< should be protected?
|
||||
|
||||
/**
|
||||
* @brief Constructor
|
||||
*
|
||||
* @param slaveSelectPin ChipSelect pin.
|
||||
* @param interruptPin Interrupt pin.
|
||||
* @param isRFM69HW Set to @c true to indicate RFM69HW variant.
|
||||
* @param interruptNum Interrupt number.
|
||||
*/
|
||||
// cppcheck-suppress uninitMemberVar
|
||||
RFM69(uint8_t slaveSelectPin=MY_RFM69_CS_PIN, uint8_t interruptPin=MY_RFM69_IRQ_PIN,
|
||||
bool isRFM69HW=false,
|
||||
uint8_t interruptNum=digitalPinToInterrupt(MY_RFM69_IRQ_PIN))
|
||||
{
|
||||
_slaveSelectPin = slaveSelectPin;
|
||||
_interruptPin = interruptPin;
|
||||
_interruptNum = interruptNum;
|
||||
_mode = RFM69_MODE_STANDBY;
|
||||
_promiscuousMode = false;
|
||||
_powerLevel = 31;
|
||||
_isRFM69HW = isRFM69HW;
|
||||
_address = RFM69_BROADCAST_ADDR;
|
||||
#if !defined(SPI_HAS_TRANSACTION)
|
||||
#if defined (SPCR) && defined (SPSR)
|
||||
_SPCR = 0;
|
||||
_SPSR = 0;
|
||||
#endif
|
||||
#if defined (SREG)
|
||||
_SREG = 0;
|
||||
#endif
|
||||
#endif // SPI_HAS_TRANSACTION
|
||||
}
|
||||
|
||||
bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1); //!< initialize
|
||||
void setAddress(uint8_t addr); //!< setAddress
|
||||
void setNetwork(uint8_t networkID); //!< setNetwork
|
||||
bool canSend(); //!< canSend
|
||||
virtual void send(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
|
||||
bool requestACK=false); //!< send
|
||||
virtual bool sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
|
||||
uint8_t retries=5, uint8_t retryWaitTime=
|
||||
200); //!< sendWithRetry (40ms roundtrip req for 61byte packets, adjusted)
|
||||
virtual bool receiveDone(); //!< receiveDone
|
||||
bool ACKReceived(uint8_t fromNodeID); //!< ACKReceived
|
||||
bool ACKRequested(); //!< ACKRequested
|
||||
virtual void sendACK(const void* buffer = "", uint8_t bufferSize=0); //!< sendACK
|
||||
uint32_t getFrequency(); //!< getFrequency
|
||||
void setFrequency(uint32_t freqHz); //!< setFrequency
|
||||
void encrypt(const char* key); //!< encrypt
|
||||
void setCS(uint8_t newSPISlaveSelect); //!< setCS
|
||||
int16_t readRSSI(bool forceTrigger=false); //!< readRSSI
|
||||
void promiscuous(bool onOff=true); //!< promiscuous
|
||||
virtual void setHighPower(bool onOFF=
|
||||
true); //!< setHighPower (have to call it after initialize for RFM69HW)
|
||||
virtual void setPowerLevel(uint8_t level); //!< setPowerLevel (reduce/increase transmit power level)
|
||||
void sleep(void); //!< sleep
|
||||
void standBy(void); //!< standBy
|
||||
void powerDown(void); //!< powerDown
|
||||
void powerUp(void); //!< powerUp
|
||||
void reset(void); //!< reset
|
||||
bool sanityCheck(void); //!< sanityCheck
|
||||
uint8_t readTemperature(uint8_t calFactor=0); //!< readTemperature (get CMOS temperature (8bit))
|
||||
void rcCalibration(); //!< rcCalibration (calibrate the internal RC oscillator for use in wide temperature variations - see datasheet section [4.3.5. RC Timer Accuracy])
|
||||
|
||||
// allow hacking registers by making these public
|
||||
uint8_t readReg(uint8_t addr); //!< readReg
|
||||
void writeReg(uint8_t addr, uint8_t val); //!< writeReg
|
||||
void readAllRegs(); //!< readAllRegs
|
||||
protected:
|
||||
static void isr0(); //!< isr0
|
||||
void virtual interruptHandler(); //!< interruptHandler
|
||||
virtual void interruptHook(uint8_t CTLbyte); //!< interruptHook
|
||||
virtual void sendFrame(uint8_t toAddress, const void* buffer, uint8_t size, bool requestACK=false,
|
||||
bool sendACK=false); //!< sendFrame
|
||||
|
||||
static RFM69* selfPointer; //!< selfPointer
|
||||
uint8_t _slaveSelectPin; //!< _slaveSelectPin
|
||||
uint8_t _interruptPin; //!< _interruptPin
|
||||
uint8_t _interruptNum; //!< _interruptNum
|
||||
uint8_t _address; //!< _address
|
||||
bool _promiscuousMode; //!< _promiscuousMode
|
||||
uint8_t _powerLevel; //!< _powerLevel
|
||||
bool _isRFM69HW; //!< _isRFM69HW
|
||||
#if defined (SPCR) && defined (SPSR)
|
||||
uint8_t _SPCR; //!< _SPCR
|
||||
uint8_t _SPSR; //!< _SPSR
|
||||
#endif
|
||||
#if defined (SREG)
|
||||
uint8_t _SREG; //!< _SREG
|
||||
#endif
|
||||
|
||||
virtual void receiveBegin(); //!< receiveBegin
|
||||
virtual void setMode(uint8_t mode); //!< setMode
|
||||
virtual void setHighPowerRegs(bool onOff); //!< setHighPowerRegs
|
||||
virtual void select(); //!< select
|
||||
virtual void unselect(); //!< unselect
|
||||
};
|
||||
|
||||
#endif
|
||||
1109
lib/MySensors/hal/transport/RFM69/driver/old/RFM69registers_old.h
Normal file
1109
lib/MySensors/hal/transport/RFM69/driver/old/RFM69registers_old.h
Normal file
File diff suppressed because it is too large
Load Diff
126
lib/MySensors/hal/transport/RFM95/MyTransportRFM95.cpp
Normal file
126
lib/MySensors/hal/transport/RFM95/MyTransportRFM95.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* 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/RFM95/driver/RFM95.h"
|
||||
|
||||
bool transportInit(void)
|
||||
{
|
||||
const bool result = RFM95_initialise(MY_RFM95_FREQUENCY);
|
||||
#if defined(MY_RFM95_TCXO)
|
||||
RFM95_enableTCXO();
|
||||
#endif
|
||||
#if !defined(MY_GATEWAY_FEATURE) && !defined(MY_RFM95_ATC_MODE_DISABLED)
|
||||
// only enable ATC mode in nodes
|
||||
RFM95_ATCmode(true, MY_RFM95_ATC_TARGET_RSSI);
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
void transportSetAddress(const uint8_t address)
|
||||
{
|
||||
RFM95_setAddress(address);
|
||||
}
|
||||
|
||||
uint8_t transportGetAddress(void)
|
||||
{
|
||||
return RFM95_getAddress();
|
||||
}
|
||||
|
||||
bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
|
||||
{
|
||||
return RFM95_sendWithRetry(to, data, len, noACK);
|
||||
}
|
||||
|
||||
bool transportDataAvailable(void)
|
||||
{
|
||||
RFM95_handler();
|
||||
return RFM95_available();
|
||||
}
|
||||
|
||||
bool transportSanityCheck(void)
|
||||
{
|
||||
return RFM95_sanityCheck();
|
||||
}
|
||||
|
||||
uint8_t transportReceive(void *data)
|
||||
{
|
||||
uint8_t len = RFM95_receive((uint8_t *)data, MAX_MESSAGE_SIZE);
|
||||
return len;
|
||||
}
|
||||
|
||||
void transportSleep(void)
|
||||
{
|
||||
(void)RFM95_sleep();
|
||||
}
|
||||
|
||||
void transportStandBy(void)
|
||||
{
|
||||
(void)RFM95_standBy();
|
||||
}
|
||||
|
||||
void transportPowerDown(void)
|
||||
{
|
||||
RFM95_powerDown();
|
||||
}
|
||||
|
||||
void transportPowerUp(void)
|
||||
{
|
||||
RFM95_powerUp();
|
||||
}
|
||||
|
||||
void transportToggleATCmode(const bool OnOff, const int16_t targetRSSI)
|
||||
{
|
||||
RFM95_ATCmode(OnOff, targetRSSI);
|
||||
}
|
||||
|
||||
int16_t transportGetSendingRSSI(void)
|
||||
{
|
||||
return RFM95_getSendingRSSI();
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingRSSI(void)
|
||||
{
|
||||
return RFM95_getReceivingRSSI();
|
||||
}
|
||||
|
||||
int16_t transportGetSendingSNR(void)
|
||||
{
|
||||
return RFM95_getSendingSNR();
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingSNR(void)
|
||||
{
|
||||
return RFM95_getReceivingSNR();
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerPercent(void)
|
||||
{
|
||||
return RFM95_getTxPowerPercent();
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerLevel(void)
|
||||
{
|
||||
return RFM95_getTxPowerLevel();
|
||||
}
|
||||
|
||||
bool transportSetTxPowerPercent(const uint8_t powerPercent)
|
||||
{
|
||||
return RFM95_setTxPowerPercent(powerPercent);
|
||||
}
|
||||
|
||||
685
lib/MySensors/hal/transport/RFM95/driver/RFM95.cpp
Normal file
685
lib/MySensors/hal/transport/RFM95/driver/RFM95.cpp
Normal file
@@ -0,0 +1,685 @@
|
||||
/*
|
||||
* 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 Mike McCauley's RFM95 library, Copyright (C) 2014 Mike McCauley <mikem@airspayce.com>
|
||||
* Radiohead http://www.airspayce.com/mikem/arduino/RadioHead/index.html
|
||||
*
|
||||
* RFM95 driver refactored and optimized for MySensors, Copyright (C) 2017-2018 Olivier Mauti <olivier@mysensors.org>
|
||||
*
|
||||
* Definitions for HopeRF LoRa radios:
|
||||
* http://www.hoperf.com/upload/rf/RFM95_96_97_98W.pdf
|
||||
*
|
||||
*/
|
||||
|
||||
#include "RFM95.h"
|
||||
|
||||
// debug
|
||||
#if defined(MY_DEBUG_VERBOSE_RFM95)
|
||||
#define RFM95_DEBUG(x,...) DEBUG_OUTPUT(x, ##__VA_ARGS__) //!< Debug print
|
||||
#else
|
||||
#define RFM95_DEBUG(x,...) //!< DEBUG null
|
||||
#endif
|
||||
|
||||
rfm95_internal_t RFM95; //!< internal variables
|
||||
volatile uint8_t RFM95_irq; //<! rfm95 irq flag
|
||||
|
||||
#if defined(__linux__)
|
||||
// SPI RX and TX buffers (max packet len + 1 byte for the command)
|
||||
uint8_t RFM95_spi_rxbuff[RFM95_MAX_PACKET_LEN + 1];
|
||||
uint8_t RFM95_spi_txbuff[RFM95_MAX_PACKET_LEN + 1];
|
||||
#endif
|
||||
|
||||
LOCAL void RFM95_csn(const bool level)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
(void)level;
|
||||
#else
|
||||
hwDigitalWrite(MY_RFM95_CS_PIN, level);
|
||||
#endif
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t *buf, uint8_t len,
|
||||
const bool aReadMode)
|
||||
{
|
||||
uint8_t status;
|
||||
uint8_t *current = buf;
|
||||
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
|
||||
RFM95_SPI.beginTransaction(SPISettings(MY_RFM95_SPI_SPEED, RFM95_SPI_DATA_ORDER,
|
||||
RFM95_SPI_DATA_MODE));
|
||||
#endif
|
||||
|
||||
RFM95_csn(LOW);
|
||||
#if defined(__linux__)
|
||||
uint8_t *prx = RFM95_spi_rxbuff;
|
||||
uint8_t *ptx = RFM95_spi_txbuff;
|
||||
uint8_t size = len + 1; // Add register value to transmit buffer
|
||||
|
||||
*ptx++ = cmd;
|
||||
while (len--) {
|
||||
if (aReadMode) {
|
||||
*ptx++ = (uint8_t)RFM95_NOP;
|
||||
} else {
|
||||
*ptx++ = *current++;
|
||||
}
|
||||
}
|
||||
RFM95_SPI.transfernb((char *)RFM95_spi_txbuff, (char *)RFM95_spi_rxbuff, size);
|
||||
if (aReadMode) {
|
||||
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 = RFM95_SPI.transfer(cmd);
|
||||
while (len--) {
|
||||
if (aReadMode) {
|
||||
status = RFM95_SPI.transfer((uint8_t)RFM95_NOP);
|
||||
if (buf != NULL) {
|
||||
*current++ = status;
|
||||
}
|
||||
} else {
|
||||
status = RFM95_SPI.transfer(*current++);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
RFM95_csn(HIGH);
|
||||
|
||||
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
|
||||
RFM95_SPI.endTransaction();
|
||||
#endif
|
||||
return status;
|
||||
}
|
||||
|
||||
// low level register access
|
||||
LOCAL uint8_t RFM95_RAW_readByteRegister(const uint8_t address)
|
||||
{
|
||||
return RFM95_spiMultiByteTransfer(address, NULL, 1, true);
|
||||
}
|
||||
|
||||
// low level register access
|
||||
LOCAL uint8_t RFM95_RAW_writeByteRegister(const uint8_t address, uint8_t value)
|
||||
{
|
||||
return RFM95_spiMultiByteTransfer(address, &value, 1, false);
|
||||
}
|
||||
|
||||
// helper functions
|
||||
LOCAL inline uint8_t RFM95_readReg(const uint8_t reg)
|
||||
{
|
||||
return RFM95_RAW_readByteRegister(reg & RFM95_READ_REGISTER);
|
||||
}
|
||||
|
||||
LOCAL inline uint8_t RFM95_writeReg(const uint8_t reg, const uint8_t value)
|
||||
{
|
||||
return RFM95_RAW_writeByteRegister(reg | RFM95_WRITE_REGISTER, value);
|
||||
}
|
||||
|
||||
LOCAL inline uint8_t RFM95_burstReadReg(const uint8_t reg, void *buf, uint8_t len)
|
||||
{
|
||||
return RFM95_spiMultiByteTransfer(reg & RFM95_READ_REGISTER, (uint8_t *)buf, len, true);
|
||||
}
|
||||
|
||||
LOCAL inline uint8_t RFM95_burstWriteReg(const uint8_t reg, const void *buf, uint8_t len)
|
||||
{
|
||||
return RFM95_spiMultiByteTransfer(reg | RFM95_WRITE_REGISTER, (uint8_t *)buf, len, false);
|
||||
}
|
||||
|
||||
LOCAL inline rfm95_RSSI_t RFM95_RSSItoInternal(const int16_t externalRSSI)
|
||||
{
|
||||
return static_cast<rfm95_RSSI_t>(externalRSSI + RFM95_RSSI_OFFSET);
|
||||
}
|
||||
|
||||
LOCAL inline int16_t RFM95_internalToRSSI(const rfm95_RSSI_t internalRSSI)
|
||||
{
|
||||
return static_cast<int16_t>(internalRSSI - RFM95_RSSI_OFFSET);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_initialise(const uint32_t frequencyHz)
|
||||
{
|
||||
RFM95_DEBUG(PSTR("RFM95:INIT\n"));
|
||||
// power pin, if defined
|
||||
#if defined(MY_RFM95_POWER_PIN)
|
||||
hwPinMode(MY_RFM95_POWER_PIN, OUTPUT);
|
||||
#endif
|
||||
RFM95_powerUp();
|
||||
// reset radio module if rst pin defined
|
||||
#if defined(MY_RFM95_RST_PIN)
|
||||
hwPinMode(MY_RFM95_RST_PIN, OUTPUT);
|
||||
hwDigitalWrite(MY_RFM95_RST_PIN, LOW);
|
||||
// 100uS
|
||||
delayMicroseconds(RFM95_POWERUP_DELAY_MS);
|
||||
hwDigitalWrite(MY_RFM95_RST_PIN, HIGH);
|
||||
// wait until chip ready
|
||||
delay(5);
|
||||
RFM95_DEBUG(PSTR("RFM95:INIT:PIN,CS=%" PRIu8 ",IQP=%" PRIu8 ",IQN=%" PRIu8 ",RST=%" PRIu8 "\n"),
|
||||
MY_RFM95_CS_PIN, MY_RFM95_IRQ_PIN,
|
||||
MY_RFM95_IRQ_NUM,MY_RFM95_RST_PIN);
|
||||
#else
|
||||
RFM95_DEBUG(PSTR("RFM95:INIT:PIN,CS=%" PRIu8 ",IQP=%" PRIu8 ",IQN=%" PRIu8 "\n"), MY_RFM95_CS_PIN,
|
||||
MY_RFM95_IRQ_PIN,
|
||||
MY_RFM95_IRQ_NUM);
|
||||
#endif
|
||||
|
||||
// set variables
|
||||
RFM95.address = RFM95_BROADCAST_ADDRESS;
|
||||
RFM95.ackReceived = false;
|
||||
RFM95.dataReceived = false;
|
||||
RFM95.txSequenceNumber = 0; // initialise TX sequence counter
|
||||
RFM95.powerLevel = 0;
|
||||
RFM95.ATCenabled = false;
|
||||
RFM95.ATCtargetRSSI = RFM95_RSSItoInternal(RFM95_TARGET_RSSI);
|
||||
|
||||
// SPI init
|
||||
#if !defined(__linux__)
|
||||
hwDigitalWrite(MY_RFM95_CS_PIN, HIGH);
|
||||
hwPinMode(MY_RFM95_CS_PIN, OUTPUT);
|
||||
#endif
|
||||
RFM95_SPI.begin();
|
||||
|
||||
// Set LoRa mode (during sleep mode)
|
||||
(void)RFM95_writeReg(RFM95_REG_01_OP_MODE, RFM95_MODE_SLEEP | RFM95_LONG_RANGE_MODE);
|
||||
delay(10); // Wait for sleep mode to take over
|
||||
|
||||
// TCXO init, if present
|
||||
#if defined(MY_RFM95_TCXO)
|
||||
RFM95_enableTCXO();
|
||||
#else
|
||||
(void)RFM95_enableTCXO;
|
||||
#endif
|
||||
|
||||
// Set up FIFO, 256 bytes: LoRa max message 64 bytes, set half RX half TX (default)
|
||||
(void)RFM95_writeReg(RFM95_REG_0F_FIFO_RX_BASE_ADDR, RFM95_RX_FIFO_ADDR);
|
||||
(void)RFM95_writeReg(RFM95_REG_0E_FIFO_TX_BASE_ADDR, RFM95_TX_FIFO_ADDR);
|
||||
(void)RFM95_writeReg(RFM95_REG_23_MAX_PAYLOAD_LENGTH, RFM95_MAX_PACKET_LEN);
|
||||
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
const rfm95_modemConfig_t configuration = { MY_RFM95_MODEM_CONFIGRUATION };
|
||||
RFM95_setModemRegisters(&configuration);
|
||||
RFM95_setPreambleLength(RFM95_PREAMBLE_LENGTH);
|
||||
RFM95_setFrequency(frequencyHz);
|
||||
(void)RFM95_setTxPowerLevel(MY_RFM95_TX_POWER_DBM);
|
||||
|
||||
if (!RFM95_sanityCheck()) {
|
||||
// sanity check failed, check wiring or replace module
|
||||
RFM95_DEBUG(PSTR("!RFM95:INIT:SANCHK FAIL\n"));
|
||||
return false;
|
||||
}
|
||||
|
||||
// IRQ
|
||||
RFM95_irq = false;
|
||||
hwPinMode(MY_RFM95_IRQ_PIN, INPUT);
|
||||
attachInterrupt(MY_RFM95_IRQ_NUM, RFM95_interruptHandler, RISING);
|
||||
return true;
|
||||
}
|
||||
|
||||
LOCAL void IRQ_HANDLER_ATTR RFM95_interruptHandler(void)
|
||||
{
|
||||
// set flag
|
||||
RFM95_irq = true;
|
||||
}
|
||||
|
||||
// RxDone, TxDone, CADDone is mapped to DI0
|
||||
LOCAL void RFM95_interruptHandling(void)
|
||||
{
|
||||
// read interrupt register
|
||||
const uint8_t irqFlags = RFM95_readReg(RFM95_REG_12_IRQ_FLAGS);
|
||||
if (RFM95.radioMode == RFM95_RADIO_MODE_RX && (irqFlags & RFM95_RX_DONE)) {
|
||||
// RXSingle mode: Radio goes automatically to STDBY after packet received
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
// Check CRC flag
|
||||
if (!(irqFlags & RFM95_PAYLOAD_CRC_ERROR)) {
|
||||
const uint8_t bufLen = min(RFM95_readReg(RFM95_REG_13_RX_NB_BYTES), (uint8_t)RFM95_MAX_PACKET_LEN);
|
||||
if (bufLen >= RFM95_HEADER_LEN) {
|
||||
// Reset the fifo read ptr to the beginning of the packet
|
||||
(void)RFM95_writeReg(RFM95_REG_0D_FIFO_ADDR_PTR, RFM95_readReg(RFM95_REG_10_FIFO_RX_CURRENT_ADDR));
|
||||
(void)RFM95_burstReadReg(RFM95_REG_00_FIFO, RFM95.currentPacket.data, bufLen);
|
||||
RFM95.currentPacket.RSSI = static_cast<rfm95_RSSI_t>(RFM95_readReg(
|
||||
RFM95_REG_1A_PKT_RSSI_VALUE)); // RSSI of latest packet received
|
||||
RFM95.currentPacket.SNR = static_cast<rfm95_SNR_t>(RFM95_readReg(RFM95_REG_19_PKT_SNR_VALUE));
|
||||
RFM95.currentPacket.payloadLen = bufLen - RFM95_HEADER_LEN;
|
||||
if ((RFM95.currentPacket.header.version >= RFM95_MIN_PACKET_HEADER_VERSION) &&
|
||||
(RFM95_PROMISCUOUS || RFM95.currentPacket.header.recipient == RFM95.address ||
|
||||
RFM95.currentPacket.header.recipient == RFM95_BROADCAST_ADDRESS)) {
|
||||
// Message for us
|
||||
RFM95.ackReceived = RFM95_getACKReceived(RFM95.currentPacket.header.controlFlags) &&
|
||||
!RFM95_getACKRequested(RFM95.currentPacket.header.controlFlags);
|
||||
RFM95.dataReceived = !RFM95.ackReceived;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// CRC error
|
||||
RFM95_DEBUG(PSTR("!RFM95:IRH:CRC ERROR\n"));
|
||||
// FIFO is cleared when switch from STDBY to RX or TX
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
}
|
||||
} else if (RFM95.radioMode == RFM95_RADIO_MODE_TX && (irqFlags & RFM95_TX_DONE) ) {
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
} else if (RFM95.radioMode == RFM95_RADIO_MODE_CAD && (irqFlags & RFM95_CAD_DONE) ) {
|
||||
RFM95.channelActive = irqFlags & RFM95_CAD_DETECTED;
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
}
|
||||
// Clear IRQ flags
|
||||
RFM95_writeReg(RFM95_REG_12_IRQ_FLAGS, RFM95_CLEAR_IRQ);
|
||||
}
|
||||
|
||||
LOCAL void RFM95_handler(void)
|
||||
{
|
||||
if (RFM95_irq) {
|
||||
RFM95_irq = false;
|
||||
RFM95_interruptHandling();
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_available(void)
|
||||
{
|
||||
if (RFM95.dataReceived) {
|
||||
// data received - we are still in STDBY from IRQ handler
|
||||
return true;
|
||||
} else if (RFM95.radioMode == RFM95_RADIO_MODE_TX) {
|
||||
return false;
|
||||
} else if (RFM95.radioMode != RFM95_RADIO_MODE_RX) {
|
||||
// we are not in RX, not CAD, and no data received
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_receive(uint8_t *buf, const uint8_t maxBufSize)
|
||||
{
|
||||
const uint8_t payloadLen = min(RFM95.currentPacket.payloadLen, maxBufSize);
|
||||
const uint8_t sender = RFM95.currentPacket.header.sender;
|
||||
const rfm95_sequenceNumber_t sequenceNumber = RFM95.currentPacket.header.sequenceNumber;
|
||||
const rfm95_controlFlags_t controlFlags = RFM95.currentPacket.header.controlFlags;
|
||||
const rfm95_RSSI_t RSSI = RFM95.currentPacket.RSSI;
|
||||
const rfm95_SNR_t SNR = RFM95.currentPacket.SNR;
|
||||
if (buf != NULL) {
|
||||
(void)memcpy((void *)buf, (void *)&RFM95.currentPacket.payload, payloadLen);
|
||||
}
|
||||
// clear data flag
|
||||
RFM95.dataReceived = false;
|
||||
// ACK handling
|
||||
if (RFM95_getACKRequested(controlFlags) && !RFM95_getACKReceived(controlFlags)) {
|
||||
#if defined(MY_GATEWAY_FEATURE) && (F_CPU>16*1000000ul)
|
||||
// delay for fast GW and slow nodes
|
||||
delay(50);
|
||||
#endif
|
||||
RFM95_sendACK(sender, sequenceNumber, RSSI, SNR);
|
||||
}
|
||||
return payloadLen;
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_sendFrame(rfm95_packet_t *packet, const bool increaseSequenceCounter)
|
||||
{
|
||||
// Check channel activity
|
||||
if (!RFM95_waitCAD()) {
|
||||
return false;
|
||||
}
|
||||
// radio is in STDBY
|
||||
if (increaseSequenceCounter) {
|
||||
// increase sequence counter, overflow is ok
|
||||
RFM95.txSequenceNumber++;
|
||||
}
|
||||
packet->header.sequenceNumber = RFM95.txSequenceNumber;
|
||||
// Position at the beginning of the TX FIFO
|
||||
(void)RFM95_writeReg(RFM95_REG_0D_FIFO_ADDR_PTR, RFM95_TX_FIFO_ADDR);
|
||||
// write packet
|
||||
const uint8_t finalLen = packet->payloadLen + RFM95_HEADER_LEN;
|
||||
(void)RFM95_burstWriteReg(RFM95_REG_00_FIFO, packet->data, finalLen);
|
||||
// total payload length
|
||||
(void)RFM95_writeReg(RFM95_REG_22_PAYLOAD_LENGTH, finalLen);
|
||||
// send message, if sent, irq fires and radio returns to standby
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_TX);
|
||||
// wait until IRQ fires or timeout
|
||||
const uint32_t startTX_MS = hwMillis();
|
||||
// todo: make this payload length + bit rate dependend
|
||||
while (!RFM95_irq && (hwMillis() - startTX_MS < MY_RFM95_TX_TIMEOUT_MS) ) {
|
||||
doYield();
|
||||
}
|
||||
return RFM95_irq;
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_send(const uint8_t recipient, uint8_t *data, const uint8_t len,
|
||||
const rfm95_controlFlags_t flags, const bool increaseSequenceCounter)
|
||||
{
|
||||
rfm95_packet_t packet;
|
||||
packet.header.version = RFM95_PACKET_HEADER_VERSION;
|
||||
packet.header.sender = RFM95.address;
|
||||
packet.header.recipient = recipient;
|
||||
packet.payloadLen = min(len, (uint8_t)RFM95_MAX_PAYLOAD_LEN);
|
||||
packet.header.controlFlags = flags;
|
||||
(void)memcpy((void *)&packet.payload, (void *)data, packet.payloadLen);
|
||||
return RFM95_sendFrame(&packet, increaseSequenceCounter);
|
||||
}
|
||||
|
||||
LOCAL void RFM95_setFrequency(const uint32_t frequencyHz)
|
||||
{
|
||||
const uint32_t freqReg = (uint32_t)(frequencyHz / RFM95_FSTEP);
|
||||
(void)RFM95_writeReg(RFM95_REG_06_FRF_MSB, (uint8_t)((freqReg >> 16) & 0xff));
|
||||
(void)RFM95_writeReg(RFM95_REG_07_FRF_MID, (uint8_t)((freqReg >> 8) & 0xff));
|
||||
(void)RFM95_writeReg(RFM95_REG_08_FRF_LSB, (uint8_t)(freqReg & 0xff));
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_setTxPowerLevel(rfm95_powerLevel_t newPowerLevel)
|
||||
{
|
||||
// RFM95/96/97/98 does not have RFO pins connected to anything. Only PA_BOOST
|
||||
newPowerLevel = max((int8_t)RFM95_MIN_POWER_LEVEL_DBM, newPowerLevel);
|
||||
newPowerLevel = min((int8_t)RFM95_MAX_POWER_LEVEL_DBM, newPowerLevel);
|
||||
if (newPowerLevel != RFM95.powerLevel) {
|
||||
RFM95.powerLevel = newPowerLevel;
|
||||
uint8_t val;
|
||||
if (newPowerLevel > 20) {
|
||||
// enable DAC, adds 3dBm
|
||||
// The documentation is pretty confusing on this topic: PaSelect says the max power is 20dBm,
|
||||
// but OutputPower claims it would be 17dBm. Measurements show 20dBm is correct
|
||||
(void)RFM95_writeReg(RFM95_REG_4D_PA_DAC, RFM95_PA_DAC_ENABLE);
|
||||
val = newPowerLevel - 8;
|
||||
} else {
|
||||
(void)RFM95_writeReg(RFM95_REG_4D_PA_DAC, RFM95_PA_DAC_DISABLE);
|
||||
val = newPowerLevel - 5;
|
||||
}
|
||||
(void)RFM95_writeReg(RFM95_REG_09_PA_CONFIG, RFM95_PA_SELECT | val);
|
||||
RFM95_DEBUG(PSTR("RFM95:PTX:LEVEL=%" PRIi8 "\n"), newPowerLevel);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
LOCAL void RFM95_enableTCXO(void)
|
||||
{
|
||||
while ((RFM95_readReg(RFM95_REG_4B_TCXO) & RFM95_TCXO_TCXO_INPUT_ON) != RFM95_TCXO_TCXO_INPUT_ON) {
|
||||
(void)RFM95_writeReg(RFM95_REG_4B_TCXO,
|
||||
(RFM95_readReg(RFM95_REG_4B_TCXO) | RFM95_TCXO_TCXO_INPUT_ON));
|
||||
}
|
||||
}
|
||||
|
||||
// Sets registers from a canned modem configuration structure
|
||||
LOCAL void RFM95_setModemRegisters(const rfm95_modemConfig_t *config)
|
||||
{
|
||||
(void)RFM95_writeReg(RFM95_REG_1D_MODEM_CONFIG1, config->reg_1d);
|
||||
(void)RFM95_writeReg(RFM95_REG_1E_MODEM_CONFIG2, config->reg_1e);
|
||||
(void)RFM95_writeReg(RFM95_REG_26_MODEM_CONFIG3, config->reg_26);
|
||||
}
|
||||
|
||||
LOCAL void RFM95_setPreambleLength(const uint16_t preambleLength)
|
||||
{
|
||||
(void)RFM95_writeReg(RFM95_REG_20_PREAMBLE_MSB, (uint8_t)((preambleLength >> 8) & 0xff));
|
||||
(void)RFM95_writeReg(RFM95_REG_21_PREAMBLE_LSB, (uint8_t)(preambleLength & 0xff));
|
||||
}
|
||||
|
||||
LOCAL void RFM95_setAddress(const uint8_t addr)
|
||||
{
|
||||
RFM95.address = addr;
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_getAddress(void)
|
||||
{
|
||||
return RFM95.address;
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode)
|
||||
{
|
||||
if (RFM95.radioMode == newRadioMode) {
|
||||
return false;
|
||||
}
|
||||
uint8_t regMode;
|
||||
|
||||
if (newRadioMode == RFM95_RADIO_MODE_STDBY) {
|
||||
regMode = RFM95_MODE_STDBY;
|
||||
} else if (newRadioMode == RFM95_RADIO_MODE_SLEEP) {
|
||||
regMode = RFM95_MODE_SLEEP;
|
||||
} else if (newRadioMode == RFM95_RADIO_MODE_CAD) {
|
||||
regMode = RFM95_MODE_CAD;
|
||||
(void)RFM95_writeReg(RFM95_REG_40_DIO_MAPPING1, 0x80); // Interrupt on CadDone, DIO0
|
||||
} else if (newRadioMode == RFM95_RADIO_MODE_RX) {
|
||||
RFM95.dataReceived = false;
|
||||
RFM95.ackReceived = false;
|
||||
regMode = RFM95_MODE_RXCONTINUOUS;
|
||||
(void)RFM95_writeReg(RFM95_REG_40_DIO_MAPPING1, 0x00); // Interrupt on RxDone, DIO0
|
||||
(void)RFM95_writeReg(RFM95_REG_0D_FIFO_ADDR_PTR,
|
||||
RFM95_RX_FIFO_ADDR); // set FIFO ptr to beginning of RX FIFO address
|
||||
} else if (newRadioMode == RFM95_RADIO_MODE_TX) {
|
||||
regMode = RFM95_MODE_TX;
|
||||
(void)RFM95_writeReg(RFM95_REG_40_DIO_MAPPING1, 0x40); // Interrupt on TxDone, DIO0
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
(void)RFM95_writeReg(RFM95_REG_01_OP_MODE, regMode);
|
||||
|
||||
RFM95.radioMode = newRadioMode;
|
||||
return true;
|
||||
}
|
||||
|
||||
LOCAL void RFM95_powerUp(void)
|
||||
{
|
||||
#if defined(MY_RFM95_POWER_PIN)
|
||||
RFM95_DEBUG(PSTR("RFM95:PWU\n")); // power up radio
|
||||
hwDigitalWrite(MY_RFM95_POWER_PIN, HIGH);
|
||||
delay(RFM95_POWERUP_DELAY_MS);
|
||||
#endif
|
||||
}
|
||||
LOCAL void RFM95_powerDown(void)
|
||||
{
|
||||
#if defined(MY_RFM95_POWER_PIN)
|
||||
RFM95_DEBUG(PSTR("RFM95:PWD\n")); // power down radio
|
||||
hwDigitalWrite(MY_RFM95_POWER_PIN, LOW);
|
||||
#endif
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_sleep(void)
|
||||
{
|
||||
RFM95_DEBUG(PSTR("RFM95:RSL\n")); // put radio to sleep
|
||||
return RFM95_setRadioMode(RFM95_RADIO_MODE_SLEEP);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_standBy(void)
|
||||
{
|
||||
RFM95_DEBUG(PSTR("RFM95:RSB\n")); // put radio to standby
|
||||
return RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
}
|
||||
|
||||
|
||||
// should be called immediately after reception in case sender wants ACK
|
||||
LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t sequenceNumber,
|
||||
const rfm95_RSSI_t RSSI, const rfm95_SNR_t SNR)
|
||||
{
|
||||
RFM95_DEBUG(PSTR("RFM95:SAC:SEND ACK,TO=%" PRIu8 ",SEQ=%" PRIu16 ",RSSI=%" PRIi16 ",SNR=%" PRIi8
|
||||
"\n"),recipient,sequenceNumber,
|
||||
RFM95_internalToRSSI(RSSI),RFM95_internalToSNR(SNR));
|
||||
rfm95_ack_t ACK;
|
||||
ACK.sequenceNumber = sequenceNumber;
|
||||
ACK.RSSI = RSSI;
|
||||
ACK.SNR = SNR;
|
||||
rfm95_controlFlags_t flags = 0u;
|
||||
RFM95_setACKReceived(flags, true);
|
||||
RFM95_setACKRSSIReport(flags, true);
|
||||
(void)RFM95_send(recipient, (uint8_t *)&ACK, sizeof(rfm95_ack_t), flags);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_executeATC(const rfm95_RSSI_t currentRSSI, const rfm95_RSSI_t targetRSSI)
|
||||
{
|
||||
rfm95_powerLevel_t newPowerLevel = RFM95.powerLevel;
|
||||
const int16_t ownRSSI = RFM95_internalToRSSI(currentRSSI);
|
||||
const int16_t uRange = RFM95_internalToRSSI(targetRSSI) + RFM95_ATC_TARGET_RANGE_DBM;
|
||||
const int16_t lRange = RFM95_internalToRSSI(targetRSSI) - RFM95_ATC_TARGET_RANGE_DBM;
|
||||
if (ownRSSI < lRange && RFM95.powerLevel < RFM95_MAX_POWER_LEVEL_DBM) {
|
||||
// increase transmitter power
|
||||
newPowerLevel++;
|
||||
} else if (ownRSSI > uRange && RFM95.powerLevel > RFM95_MIN_POWER_LEVEL_DBM) {
|
||||
// decrease transmitter power
|
||||
newPowerLevel--;
|
||||
} else {
|
||||
// nothing to adjust
|
||||
return false;
|
||||
}
|
||||
RFM95_DEBUG(PSTR("RFM95:ATC:ADJ TXL,cR=%" PRIi16 ",tR=%" PRIi16 "..%" PRIi16 ",TXL=%" PRIi8 "\n"),
|
||||
ownRSSI, lRange, uRange, RFM95.powerLevel);
|
||||
return RFM95_setTxPowerLevel(newPowerLevel);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void *buffer,
|
||||
const uint8_t bufferSize, const bool noACK)
|
||||
{
|
||||
for (uint8_t retry = 0; retry < RFM95_RETRIES; retry++) {
|
||||
RFM95_DEBUG(PSTR("RFM95:SWR:SEND,TO=%" PRIu8 ",SEQ=%" PRIu16 ",RETRY=%" PRIu8 "\n"), recipient,
|
||||
RFM95.txSequenceNumber,
|
||||
retry);
|
||||
rfm95_controlFlags_t flags = 0u;
|
||||
RFM95_setACKRequested(flags, !noACK);
|
||||
// send packet
|
||||
if (!RFM95_send(recipient, (uint8_t *)buffer, bufferSize, flags, !retry)) {
|
||||
return false;
|
||||
}
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
if (noACK) {
|
||||
return true;
|
||||
}
|
||||
const uint32_t enterMS = hwMillis();
|
||||
while (hwMillis() - enterMS < RFM95_RETRY_TIMEOUT_MS && !RFM95.dataReceived) {
|
||||
RFM95_handler();
|
||||
if (RFM95.ackReceived) {
|
||||
const uint8_t sender = RFM95.currentPacket.header.sender;
|
||||
const rfm95_sequenceNumber_t ACKsequenceNumber = RFM95.currentPacket.ACK.sequenceNumber;
|
||||
const rfm95_controlFlags_t flag = RFM95.currentPacket.header.controlFlags;
|
||||
const rfm95_RSSI_t RSSI = RFM95.currentPacket.ACK.RSSI;
|
||||
//const rfm95_SNR_t SNR = RFM95.currentPacket.ACK.SNR;
|
||||
RFM95.ackReceived = false;
|
||||
// packet read, back to RX
|
||||
RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
if (sender == recipient &&
|
||||
(ACKsequenceNumber == RFM95.txSequenceNumber)) {
|
||||
RFM95_DEBUG(PSTR("RFM95:SWR:ACK FROM=%" PRIu8 ",SEQ=%" PRIu16 ",RSSI=%" PRIi16 "\n"),sender,
|
||||
ACKsequenceNumber,
|
||||
RFM95_internalToRSSI(RSSI));
|
||||
//RFM95_clearRxBuffer();
|
||||
// ATC
|
||||
if (RFM95.ATCenabled && RFM95_getACKRSSIReport(flag)) {
|
||||
(void)RFM95_executeATC(RSSI, RFM95.ATCtargetRSSI);
|
||||
}
|
||||
return true;
|
||||
} // seq check
|
||||
}
|
||||
doYield();
|
||||
}
|
||||
RFM95_DEBUG(PSTR("!RFM95:SWR:NACK\n"));
|
||||
const uint32_t enterCSMAMS = hwMillis();
|
||||
const uint16_t randDelayCSMA = enterMS % 100;
|
||||
while (hwMillis() - enterCSMAMS < randDelayCSMA) {
|
||||
doYield();
|
||||
}
|
||||
}
|
||||
if (RFM95.ATCenabled) {
|
||||
// No ACK received, maybe out of reach: increase power level
|
||||
(void)RFM95_setTxPowerLevel(RFM95.powerLevel + 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait until no channel activity detected or timeout
|
||||
LOCAL bool RFM95_waitCAD(void)
|
||||
{
|
||||
// receiver needs to be in STDBY before entering CAD mode
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_CAD);
|
||||
const uint32_t enterMS = hwMillis();
|
||||
while (RFM95.radioMode == RFM95_RADIO_MODE_CAD && (hwMillis() - enterMS < RFM95_CAD_TIMEOUT_MS) ) {
|
||||
doYield();
|
||||
RFM95_handler();
|
||||
}
|
||||
return !RFM95.channelActive;
|
||||
}
|
||||
|
||||
LOCAL void RFM95_ATCmode(const bool OnOff, const int16_t targetRSSI)
|
||||
{
|
||||
RFM95.ATCenabled = OnOff;
|
||||
RFM95.ATCtargetRSSI = RFM95_RSSItoInternal(targetRSSI);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_sanityCheck(void)
|
||||
{
|
||||
bool result = true;
|
||||
result &= RFM95_readReg(RFM95_REG_0F_FIFO_RX_BASE_ADDR) == RFM95_RX_FIFO_ADDR;
|
||||
result &= RFM95_readReg(RFM95_REG_0E_FIFO_TX_BASE_ADDR) == RFM95_TX_FIFO_ADDR;
|
||||
result &= RFM95_readReg(RFM95_REG_23_MAX_PAYLOAD_LENGTH) == RFM95_MAX_PACKET_LEN;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
LOCAL int16_t RFM95_getSendingRSSI(void)
|
||||
{
|
||||
// own RSSI, as measured by the recipient - ACK part
|
||||
if (RFM95_getACKRSSIReport(RFM95.currentPacket.header.controlFlags)) {
|
||||
return RFM95_internalToRSSI(RFM95.currentPacket.ACK.RSSI);
|
||||
} else {
|
||||
// not possible
|
||||
return INVALID_RSSI;
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL int16_t RFM95_getSendingSNR(void)
|
||||
{
|
||||
// own SNR, as measured by the recipient - ACK part
|
||||
if (RFM95_getACKRSSIReport(RFM95.currentPacket.header.controlFlags)) {
|
||||
return static_cast<int16_t>(RFM95_internalToSNR(RFM95.currentPacket.ACK.SNR));
|
||||
} else {
|
||||
// not possible
|
||||
return INVALID_SNR;
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL int16_t RFM95_getReceivingRSSI(void)
|
||||
{
|
||||
// RSSI from last received packet
|
||||
return static_cast<int16_t>(RFM95_internalToRSSI(RFM95.currentPacket.RSSI));
|
||||
}
|
||||
|
||||
LOCAL int16_t RFM95_getReceivingSNR(void)
|
||||
{
|
||||
// SNR from last received packet
|
||||
return static_cast<int16_t>(RFM95_internalToSNR(RFM95.currentPacket.SNR));
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_getTxPowerLevel(void)
|
||||
{
|
||||
return RFM95.powerLevel;
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_getTxPowerPercent(void)
|
||||
{
|
||||
// report TX level in %
|
||||
const uint8_t result = static_cast<uint8_t>(100.0f * (RFM95.powerLevel -
|
||||
RFM95_MIN_POWER_LEVEL_DBM) /
|
||||
(RFM95_MAX_POWER_LEVEL_DBM
|
||||
- RFM95_MIN_POWER_LEVEL_DBM));
|
||||
return result;
|
||||
}
|
||||
LOCAL bool RFM95_setTxPowerPercent(const uint8_t newPowerPercent)
|
||||
{
|
||||
const rfm95_powerLevel_t newPowerLevel = static_cast<rfm95_powerLevel_t>
|
||||
(RFM95_MIN_POWER_LEVEL_DBM + (RFM95_MAX_POWER_LEVEL_DBM
|
||||
- RFM95_MIN_POWER_LEVEL_DBM) * (newPowerPercent / 100.0f));
|
||||
RFM95_DEBUG(PSTR("RFM95:SPP:PCT=%" PRIu8 ",TX LEVEL=%" PRIi8 "\n"), newPowerPercent,newPowerLevel);
|
||||
return RFM95_setTxPowerLevel(newPowerLevel);
|
||||
}
|
||||
|
||||
487
lib/MySensors/hal/transport/RFM95/driver/RFM95.h
Normal file
487
lib/MySensors/hal/transport/RFM95/driver/RFM95.h
Normal file
@@ -0,0 +1,487 @@
|
||||
/*
|
||||
* 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 Mike McCauley's RFM95 library, Copyright (C) 2014 Mike McCauley <mikem@airspayce.com>
|
||||
* Radiohead http://www.airspayce.com/mikem/arduino/RadioHead/index.html
|
||||
*
|
||||
* RFM95 driver refactored and optimized for MySensors, Copyright (C) 2017-2018 Olivier Mauti <olivier@mysensors.org>
|
||||
*
|
||||
* Changelog:
|
||||
* - ACK with sequenceNumber
|
||||
* - ATC control
|
||||
*
|
||||
* Definitions for HopeRF LoRa radios:
|
||||
* http://www.hoperf.com/upload/rf/RFM95_96_97_98W.pdf
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file RFM95.h
|
||||
*
|
||||
* @defgroup RFM95grp RFM95
|
||||
* @ingroup internals
|
||||
* @{
|
||||
*
|
||||
* RFM95 driver-related log messages, format: [!]SYSTEM:[SUB SYSTEM:]MESSAGE
|
||||
* - [!] Exclamation mark is prepended in case of error
|
||||
*
|
||||
* |E| SYS | SUB | Message | Comment
|
||||
* |-|-------|------|----------------------------------------|-----------------------------------------------------------------------------------
|
||||
* | | RFM95 | INIT | | Initialise RFM95 radio
|
||||
* | | RFM95 | INIT | PIN,CS=%%d,IQP=%%d,IQN=%%d[,RST=%%d] | Pin configuration: chip select (CS), IRQ pin (IQP), IRQ number (IQN), Reset (RST)
|
||||
* |!| RFM95 | INIT | SANCHK FAIL | Sanity check failed, check wiring or replace module
|
||||
* |!| RFM95 | IRH | CRC FAIL | Incoming packet has CRC error, skip
|
||||
* | | RFM95 | RCV | SEND ACK | ACK request received, sending ACK back
|
||||
* | | RFM95 | PTC | LEVEL=%%d | Set TX power level
|
||||
* | | RFM95 | SAC | SEND ACK,TO=%%d,RSSI=%%d,SNR=%%d | Send ACK to node (TO), RSSI of received message (RSSI), SNR of message (SNR)
|
||||
* | | RFM95 | ATC | ADJ TXL,cR=%%d,tR=%%d..%%d,TXL=%%d | Adjust TX level, current RSSI (cR), target RSSI range (tR), TX level (TXL)
|
||||
* | | RFM95 | SWR | SEND,TO=%%d,RETRY=%%d | Send message to (TO), NACK retry counter (RETRY)
|
||||
* | | RFM95 | SWR | ACK FROM=%%d,SEQ=%%d,RSSI=%%d,SNR=%%d | ACK received from node (FROM), seq ID (SEQ), (RSSI), (SNR)
|
||||
* |!| RFM95 | SWR | NACK | No ACK received
|
||||
* | | RFM95 | SPP | PCT=%%d,TX LEVEL=%%d | Set TX level percent (PCT), TX level (LEVEL)
|
||||
* | | RFM95 | PWD | | Power down radio
|
||||
* | | RFM95 | PWU | | Power up radio
|
||||
*
|
||||
* RFM95 modem configuration
|
||||
*
|
||||
* BW = Bandwidth in kHz
|
||||
* CR = Error correction code
|
||||
* SF = Spreading factor, chips / symbol
|
||||
*
|
||||
* | CONFIG | BW | CR | SF | Comment | air-time (15 bytes)
|
||||
* |------------------|-------|-----|------|-----------------------|------------------------
|
||||
* | BW125CR45SF128 | 125 | 4/5 | 128 | Default, medium range | 50ms
|
||||
* | BW500CR45SF128 | 500 | 4/5 | 128 | Fast, short range | 15ms
|
||||
* | BW31_25CR48SF512 | 31.25 | 4/8 | 512 | Slow, long range | 900ms
|
||||
* | BW125CR48SF4096 | 125 | 4/8 | 4096 | Slow, long range | 1500ms
|
||||
*
|
||||
* See here for air-time calculation: https://docs.google.com/spreadsheets/d/1voGAtQAjC1qBmaVuP1ApNKs1ekgUjavHuVQIXyYSvNc
|
||||
*
|
||||
* @brief API declaration for RFM95
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _RFM95_h
|
||||
#define _RFM95_h
|
||||
|
||||
#include "RFM95registers.h"
|
||||
|
||||
#if !defined(RFM95_SPI)
|
||||
#define RFM95_SPI hwSPI //!< default SPI
|
||||
#endif
|
||||
|
||||
// default PIN assignments, can be overridden
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
#if defined(__AVR_ATmega32U4__)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (3) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#else
|
||||
#define DEFAULT_RFM95_IRQ_PIN (2) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#endif
|
||||
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (5) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (16) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#define DEFAULT_RFM95_IRQ_NUM digitalPinToInterrupt(DEFAULT_RFM95_IRQ_PIN) //!< DEFAULT_RFM95_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (2) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#elif defined(LINUX_ARCH_RASPBERRYPI)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (22) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#elif defined(ARDUINO_ARCH_STM32F1)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (PA3) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#elif defined(TEENSYDUINO)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (8) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#else
|
||||
#define DEFAULT_RFM95_IRQ_PIN (2) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#endif
|
||||
|
||||
#define DEFAULT_RFM95_CS_PIN (SS) //!< DEFAULT_RFM95_CS_PIN
|
||||
|
||||
// SPI settings
|
||||
#define RFM95_SPI_DATA_ORDER MSBFIRST //!< SPI data order
|
||||
#define RFM95_SPI_DATA_MODE SPI_MODE0 //!< SPI mode
|
||||
|
||||
// RFM95 radio configurations: reg_1d, reg_1e, reg_26 (see datasheet)
|
||||
#define RFM95_BW125CR45SF128 RFM95_BW_125KHZ | RFM95_CODING_RATE_4_5, RFM95_SPREADING_FACTOR_128CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x72,0x74,0x04
|
||||
#define RFM95_BW500CR45SF128 RFM95_BW_500KHZ | RFM95_CODING_RATE_4_5, RFM95_SPREADING_FACTOR_128CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x92,0x74,0x04
|
||||
#define RFM95_BW31_25CR48SF512 RFM95_BW_31_25KHZ | RFM95_CODING_RATE_4_8, RFM95_SPREADING_FACTOR_512CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x48,0x94,0x04
|
||||
#define RFM95_BW125CR48SF4096 RFM95_BW_125KHZ | RFM95_CODING_RATE_4_8, RFM95_SPREADING_FACTOR_4096CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON | RFM95_LOW_DATA_RATE_OPTIMIZE //!< 0x78,0xc4,0x0C
|
||||
|
||||
#if !defined(RFM95_RETRY_TIMEOUT_MS)
|
||||
// air-time approximation for timeout, 1 hop ~15 bytes payload - adjust if needed
|
||||
// BW125/SF128: 50ms
|
||||
// BW500/SF128: 15ms
|
||||
// BW31.25/SF512: 900ms
|
||||
// BW125/SF4096: 1500ms
|
||||
#define RFM95_RETRY_TIMEOUT_MS (500ul) //!< Timeout for ACK, adjustments needed if modem configuration changed (air time different)
|
||||
#endif
|
||||
|
||||
#if !defined(MY_RFM95_TX_TIMEOUT_MS)
|
||||
#define MY_RFM95_TX_TIMEOUT_MS (5*1000ul) //!< TX timeout
|
||||
#endif
|
||||
|
||||
// Frequency definitions
|
||||
#define RFM95_169MHZ (169000000ul) //!< 169 Mhz
|
||||
#define RFM95_315MHZ (315000000ul) //!< 315 Mhz
|
||||
#define RFM95_434MHZ (433920000ul) //!< 433.92 Mhz
|
||||
#define RFM95_868MHZ (868100000ul) //!< 868.1 Mhz
|
||||
#define RFM95_915MHZ (915000000ul) //!< 915 Mhz
|
||||
|
||||
#define RFM95_RETRIES (5u) //!< Retries in case of failed transmission
|
||||
#define RFM95_FIFO_SIZE (0xFFu) //!< Max number of bytes the LORA Rx/Tx FIFO can hold
|
||||
#define RFM95_RX_FIFO_ADDR (0x00u) //!< RX FIFO addr pointer
|
||||
#define RFM95_TX_FIFO_ADDR (0x80u) //!< TX FIFO addr pointer
|
||||
#define RFM95_MAX_PACKET_LEN (0x40u) //!< This is the maximum number of bytes that can be carried by the LORA
|
||||
#define RFM95_PREAMBLE_LENGTH (8u) //!< Preamble length, default=8
|
||||
#define RFM95_CAD_TIMEOUT_MS (2*1000ul) //!< channel activity detection timeout
|
||||
#define RFM95_POWERUP_DELAY_MS (100u) //!< Power up delay, allow VCC to settle, transport to become fully operational
|
||||
|
||||
#define RFM95_PACKET_HEADER_VERSION (1u) //!< RFM95 packet header version
|
||||
#define RFM95_MIN_PACKET_HEADER_VERSION (1u) //!< Minimal RFM95 packet header version
|
||||
#define RFM95_BIT_ACK_REQUESTED (7u) //!< RFM95 header, controlFlag, bit 7
|
||||
#define RFM95_BIT_ACK_RECEIVED (6u) //!< RFM95 header, controlFlag, bit 6
|
||||
#define RFM95_BIT_ACK_RSSI_REPORT (5u) //!< RFM95 header, controlFlag, bit 5
|
||||
|
||||
#define RFM95_BROADCAST_ADDRESS (255u) //!< Broadcasting address
|
||||
#define RFM95_ATC_TARGET_RANGE_DBM (2u) //!< ATC target range +/- dBm
|
||||
#define RFM95_RSSI_OFFSET (137u) //!< RSSI offset
|
||||
#define RFM95_TARGET_RSSI (-70) //!< RSSI target
|
||||
#define RFM95_PROMISCUOUS (false) //!< RFM95 promiscuous mode
|
||||
|
||||
#define RFM95_FXOSC (32*1000000ul) //!< The crystal oscillator frequency of the module
|
||||
#define RFM95_FSTEP (RFM95_FXOSC / 524288.0f) //!< The Frequency Synthesizer step
|
||||
|
||||
// helper macros
|
||||
#define RFM95_getACKRequested(__value) ((bool)bitRead(__value, RFM95_BIT_ACK_REQUESTED)) //!< getACKRequested
|
||||
#define RFM95_setACKRequested(__value, __flag) bitWrite(__value, RFM95_BIT_ACK_REQUESTED,__flag) //!< setACKRequested
|
||||
#define RFM95_getACKReceived(__value) ((bool)bitRead(__value, RFM95_BIT_ACK_RECEIVED)) //!< getACKReceived
|
||||
#define RFM95_setACKReceived(__value, __flag) bitWrite(__value, RFM95_BIT_ACK_RECEIVED,__flag) //!< setACKReceived
|
||||
#define RFM95_setACKRSSIReport(__value, __flag) bitWrite(__value, RFM95_BIT_ACK_RSSI_REPORT,__flag) //!< setACKRSSIReport
|
||||
#define RFM95_getACKRSSIReport(__value) ((bool)bitRead(__value, RFM95_BIT_ACK_RSSI_REPORT)) //!< getACKRSSIReport
|
||||
#define RFM95_internalToSNR(__value) ((int8_t)(__value / 4)) //!< Convert internal SNR to SNR
|
||||
|
||||
#define RFM95_MIN_POWER_LEVEL_DBM ((rfm95_powerLevel_t)5u) //!< min. power level
|
||||
#if defined(MY_RFM95_MAX_POWER_LEVEL_DBM)
|
||||
#define RFM95_MAX_POWER_LEVEL_DBM MY_RFM95_MAX_POWER_LEVEL_DBM //!< MY_RFM95_MAX_POWER_LEVEL_DBM
|
||||
#else
|
||||
#define RFM95_MAX_POWER_LEVEL_DBM ((rfm95_powerLevel_t)23u) //!< max. power level
|
||||
#endif
|
||||
/**
|
||||
* @brief Radio modes
|
||||
*/
|
||||
typedef enum {
|
||||
RFM95_RADIO_MODE_RX = 0, //!< RX mode
|
||||
RFM95_RADIO_MODE_TX = 1, //!< TX mode
|
||||
RFM95_RADIO_MODE_CAD = 2, //!< CAD mode
|
||||
RFM95_RADIO_MODE_SLEEP = 3, //!< SLEEP mode
|
||||
RFM95_RADIO_MODE_STDBY = 4 //!< STDBY mode
|
||||
} rfm95_radioMode_t;
|
||||
|
||||
/**
|
||||
* @brief RFM95 modem config registers
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t reg_1d; //!< Value for register REG_1D_MODEM_CONFIG1
|
||||
uint8_t reg_1e; //!< Value for register REG_1E_MODEM_CONFIG2
|
||||
uint8_t reg_26; //!< Value for register REG_26_MODEM_CONFIG3
|
||||
} rfm95_modemConfig_t;
|
||||
|
||||
/**
|
||||
* @brief Sequence number data type
|
||||
*/
|
||||
typedef uint16_t rfm95_sequenceNumber_t; // will eventually change to uint8_t in 3.0
|
||||
/**
|
||||
* @brief RSSI data type
|
||||
*/
|
||||
typedef uint8_t rfm95_RSSI_t;
|
||||
/**
|
||||
* @brief SNR data type
|
||||
*/
|
||||
typedef int8_t rfm95_SNR_t;
|
||||
/**
|
||||
* @brief Control flag data type
|
||||
*/
|
||||
typedef uint8_t rfm95_controlFlags_t;
|
||||
/**
|
||||
* @brief Power level in dBm
|
||||
*/
|
||||
typedef int8_t rfm95_powerLevel_t;
|
||||
/**
|
||||
* @brief RFM95 LoRa header
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t version; //!< Header version
|
||||
uint8_t recipient; //!< Payload recipient
|
||||
uint8_t sender; //!< Payload sender
|
||||
rfm95_controlFlags_t controlFlags; //!< Control flags, used for ACK
|
||||
rfm95_sequenceNumber_t sequenceNumber; //!< Packet sequence number, used for ACK
|
||||
} __attribute__((packed)) rfm95_header_t;
|
||||
|
||||
/**
|
||||
* @brief RFM95 LoRa ACK packet structure
|
||||
*/
|
||||
typedef struct {
|
||||
rfm95_sequenceNumber_t sequenceNumber; //!< sequence number
|
||||
rfm95_RSSI_t RSSI; //!< RSSI
|
||||
rfm95_SNR_t SNR; //!< SNR
|
||||
} __attribute__((packed)) rfm95_ack_t;
|
||||
|
||||
|
||||
#define RFM95_HEADER_LEN sizeof(rfm95_header_t) //!< Size header inside LoRa payload
|
||||
#define RFM95_MAX_PAYLOAD_LEN (RFM95_MAX_PACKET_LEN - RFM95_HEADER_LEN) //!< Max payload length
|
||||
|
||||
/**
|
||||
* @brief LoRa packet structure
|
||||
*/
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
rfm95_header_t header; //!< LoRa header
|
||||
union {
|
||||
uint8_t payload[RFM95_MAX_PAYLOAD_LEN]; //!< Payload, i.e. MySensors message
|
||||
rfm95_ack_t ACK; //!< Union: ACK
|
||||
};
|
||||
};
|
||||
uint8_t data[RFM95_MAX_PACKET_LEN]; //!< RAW
|
||||
};
|
||||
uint8_t payloadLen; //!< Length of payload (excluding header)
|
||||
rfm95_RSSI_t RSSI; //!< RSSI of current packet, RSSI = value - 137
|
||||
rfm95_SNR_t SNR; //!< SNR of current packet
|
||||
} __attribute__((packed)) rfm95_packet_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief RFM95 internal variables
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t address; //!< Node address
|
||||
rfm95_packet_t currentPacket; //!< Buffer for current packet
|
||||
rfm95_sequenceNumber_t txSequenceNumber; //!< RFM95_txSequenceNumber
|
||||
rfm95_powerLevel_t powerLevel; //!< TX power level dBm
|
||||
rfm95_RSSI_t ATCtargetRSSI; //!< ATC: target RSSI
|
||||
// 8 bit
|
||||
rfm95_radioMode_t radioMode : 3; //!< current transceiver state
|
||||
bool channelActive : 1; //!< RFM95_cad
|
||||
bool ATCenabled : 1; //!< ATC enabled
|
||||
bool ackReceived : 1; //!< ACK received
|
||||
bool dataReceived : 1; //!< Data received
|
||||
bool reserved : 1; //!< unused
|
||||
} rfm95_internal_t;
|
||||
|
||||
#define LOCAL static //!< static
|
||||
|
||||
/**
|
||||
* @brief Initialise the driver transport hardware and software
|
||||
* @param frequencyHz Transmitter frequency in Hz
|
||||
* @return True if initialisation succeeded
|
||||
*/
|
||||
LOCAL bool RFM95_initialise(const uint32_t frequencyHz);
|
||||
/**
|
||||
* @brief Set the driver/node address
|
||||
* @param addr
|
||||
*/
|
||||
LOCAL void RFM95_setAddress(const uint8_t addr);
|
||||
/**
|
||||
* @brief Get driver/node address
|
||||
* @return Node address
|
||||
*/
|
||||
LOCAL uint8_t RFM95_getAddress(void);
|
||||
/**
|
||||
* @brief Sets all the registers required to configure the data modem in the RF95/96/97/98, including the
|
||||
* bandwidth, spreading factor etc.
|
||||
* @param config See modemConfig_t and references therein
|
||||
*/
|
||||
LOCAL void RFM95_setModemRegisters(const rfm95_modemConfig_t *config);
|
||||
/**
|
||||
* @brief Tests whether a new message is available
|
||||
* @return True if a new, complete, error-free uncollected message is available to be retreived by @ref RFM95_receive()
|
||||
*/
|
||||
LOCAL bool RFM95_available(void);
|
||||
/**
|
||||
* @brief If a valid message is received, copy it to buf and return length. 0 byte messages are permitted.
|
||||
* @param buf Location to copy the received message
|
||||
* @param maxBufSize Max buffer size
|
||||
* @return Number of bytes
|
||||
*/
|
||||
LOCAL uint8_t RFM95_receive(uint8_t *buf, const uint8_t maxBufSize);
|
||||
/**
|
||||
* @brief RFM95_send
|
||||
* @param recipient
|
||||
* @param data
|
||||
* @param len
|
||||
* @param flags
|
||||
* @param increaseSequenceCounter
|
||||
* @return True if packet sent
|
||||
*/
|
||||
LOCAL bool RFM95_send(const uint8_t recipient, uint8_t *data, const uint8_t len,
|
||||
const rfm95_controlFlags_t flags, const bool increaseSequenceCounter = true);
|
||||
/**
|
||||
* @brief RFM95_sendFrame
|
||||
* @param packet
|
||||
* @param increaseSequenceCounter
|
||||
* @return True if frame sent
|
||||
*/
|
||||
LOCAL bool RFM95_sendFrame(rfm95_packet_t *packet, const bool increaseSequenceCounter = true);
|
||||
/**
|
||||
* @brief RFM95_setPreambleLength
|
||||
* @param preambleLength
|
||||
*/
|
||||
LOCAL void RFM95_setPreambleLength(const uint16_t preambleLength);
|
||||
/**
|
||||
* @brief Sets the transmitter and receiver centre frequency
|
||||
* @param frequencyHz Frequency in Hz
|
||||
*/
|
||||
LOCAL void RFM95_setFrequency(const uint32_t frequencyHz);
|
||||
/**
|
||||
* @brief Sets the transmitter power output level, and configures the transmitter pin
|
||||
* @param newPowerLevel Transmitter power level in dBm (+5 to +23)
|
||||
* @return True power level adjusted
|
||||
*/
|
||||
LOCAL bool RFM95_setTxPowerLevel(rfm95_powerLevel_t newPowerLevel);
|
||||
|
||||
/**
|
||||
* @brief Sets the transmitter power output percent.
|
||||
* @param newPowerPercent Transmitter power level in percent
|
||||
* @return True power level adjusted
|
||||
*/
|
||||
LOCAL bool RFM95_setTxPowerPercent(const uint8_t newPowerPercent);
|
||||
|
||||
/**
|
||||
* @brief Enable TCXO mode
|
||||
* Call this immediately after init(), to force your radio to use an external
|
||||
* frequency source, such as a Temperature Compensated Crystal Oscillator (TCXO).
|
||||
* See the comments in the main documentation about the sensitivity of this radio to
|
||||
* clock frequency especially when using narrow bandwidths.
|
||||
* @note Has to be called while radio is in sleep mode.
|
||||
*/
|
||||
LOCAL void RFM95_enableTCXO(void);
|
||||
|
||||
/**
|
||||
* @brief Sets the radio into low-power sleep mode
|
||||
* @return true if sleep mode was successfully entered
|
||||
*/
|
||||
LOCAL bool RFM95_sleep(void);
|
||||
/**
|
||||
* @brief Sets the radio into standby mode
|
||||
* @return true if standby mode was successfully entered
|
||||
*/
|
||||
LOCAL bool RFM95_standBy(void);
|
||||
/**
|
||||
* @brief Powerdown radio, if RFM95_POWER_PIN defined
|
||||
*/
|
||||
LOCAL void RFM95_powerDown(void);
|
||||
/**
|
||||
* @brief Powerup radio, if RFM95_POWER_PIN defined
|
||||
*/
|
||||
LOCAL void RFM95_powerUp(void);
|
||||
/**
|
||||
* @brief RFM95_sendACK
|
||||
* @param recipient
|
||||
* @param sequenceNumber
|
||||
* @param RSSI (rfm95_RSSI_t)
|
||||
* @param SNR (rfm95_SNR_t)
|
||||
*/
|
||||
LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t sequenceNumber,
|
||||
const rfm95_RSSI_t RSSI, const rfm95_SNR_t SNR);
|
||||
/**
|
||||
* @brief RFM95_sendWithRetry
|
||||
* @param recipient
|
||||
* @param buffer
|
||||
* @param bufferSize
|
||||
* @param noACK
|
||||
* @return True if packet successfully sent
|
||||
*/
|
||||
LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void *buffer,
|
||||
const uint8_t bufferSize, const bool noACK);
|
||||
/**
|
||||
* @brief Wait until no channel activity detected
|
||||
* @return True if no channel activity detected, False if timeout occured
|
||||
*/
|
||||
LOCAL bool RFM95_waitCAD(void);
|
||||
|
||||
/**
|
||||
* @brief RFM95_setRadioMode
|
||||
* @param newRadioMode
|
||||
* @return True if mode changed
|
||||
*/
|
||||
LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode);
|
||||
/**
|
||||
* @brief Low level interrupt handler
|
||||
*/
|
||||
LOCAL void RFM95_interruptHandler(void);
|
||||
|
||||
/**
|
||||
* @brief Packet engine
|
||||
*/
|
||||
LOCAL void RFM95_interruptHandling(void);
|
||||
|
||||
/**
|
||||
* @brief RFM95_handler
|
||||
*/
|
||||
LOCAL void RFM95_handler(void);
|
||||
/**
|
||||
* @brief RFM95_getSendingRSSI
|
||||
* @return RSSI Signal strength of last packet received
|
||||
*/
|
||||
LOCAL int16_t RFM95_getReceivingRSSI(void);
|
||||
/**
|
||||
* @brief RFM95_getSendingRSSI
|
||||
* @return RSSI Signal strength of last packet sent (if ACK and ATC enabled)
|
||||
*/
|
||||
LOCAL int16_t RFM95_getSendingRSSI(void);
|
||||
/**
|
||||
* @brief RFM95_getReceivingSNR
|
||||
* @return SNR
|
||||
*/
|
||||
LOCAL int16_t RFM95_getReceivingSNR(void);
|
||||
/**
|
||||
* @brief RFM95_getSendingSNR
|
||||
* @return SNR of last packet sent (if ACK and ATC enabled)
|
||||
*/
|
||||
LOCAL int16_t RFM95_getSendingSNR(void);
|
||||
/**
|
||||
* @brief Get transmitter power level
|
||||
* @return Transmitter power level in percents
|
||||
*/
|
||||
LOCAL uint8_t RFM95_getTxPowerPercent(void);
|
||||
/**
|
||||
* @brief Get transmitter power level
|
||||
* @return Transmitter power level in dBm
|
||||
*/
|
||||
LOCAL uint8_t RFM95_getTxPowerLevel(void);
|
||||
/**
|
||||
* @brief RFM_executeATC
|
||||
* @param currentRSSI
|
||||
* @param targetRSSI
|
||||
* @return True if power level adjusted
|
||||
*/
|
||||
LOCAL bool RFM95_executeATC(const rfm95_RSSI_t currentRSSI, const rfm95_RSSI_t targetRSSI);
|
||||
/**
|
||||
* @brief RFM95_ATCmode
|
||||
* @param targetRSSI Target RSSI for transmitter (default -60)
|
||||
* @param OnOff True to enable ATC
|
||||
*/
|
||||
LOCAL void RFM95_ATCmode(const bool OnOff, const int16_t targetRSSI = RFM95_TARGET_RSSI);
|
||||
/**
|
||||
* @brief RFM95_sanityCheck
|
||||
* @return True if sanity check passed
|
||||
*/
|
||||
LOCAL bool RFM95_sanityCheck(void);
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
205
lib/MySensors/hal/transport/RFM95/driver/RFM95registers.h
Normal file
205
lib/MySensors/hal/transport/RFM95/driver/RFM95registers.h
Normal file
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* 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 Mike McCauley's RFM95 library, Copyright (C) 2014 Mike McCauley <mikem@airspayce.com>
|
||||
* Radiohead http://www.airspayce.com/mikem/arduino/RadioHead/index.html
|
||||
*
|
||||
* RFM95 driver refactored and optimized for MySensors, Copyright (C) 2017-2018 Olivier Mauti <olivier@mysensors.org>
|
||||
*
|
||||
* Definitions for HopeRF LoRa radios:
|
||||
* http://www.hoperf.com/upload/rf/RFM95_96_97_98W.pdf
|
||||
*
|
||||
*/
|
||||
|
||||
// Register access
|
||||
#define RFM95_READ_REGISTER (0x7Fu) //!< reading register
|
||||
#define RFM95_WRITE_REGISTER (0x80u) //!< writing register
|
||||
#define RFM95_NOP (0x00u) //!< NOP
|
||||
|
||||
// Registers, available in LoRa mode
|
||||
#define RFM95_REG_00_FIFO 0x00 //!< REG_00_FIFO
|
||||
#define RFM95_REG_01_OP_MODE 0x01 //!< REG_01_OP_MODE
|
||||
#define RFM95_REG_02_RESERVED 0x02 //!< REG_02_RESERVED
|
||||
#define RFM95_REG_03_RESERVED 0x03 //!< REG_03_RESERVED
|
||||
#define RFM95_REG_04_RESERVED 0x04 //!< REG_04_RESERVED
|
||||
#define RFM95_REG_05_RESERVED 0x05 //!< REG_05_RESERVED
|
||||
#define RFM95_REG_06_FRF_MSB 0x06 //!< REG_06_FRF_MSB
|
||||
#define RFM95_REG_07_FRF_MID 0x07 //!< REG_07_FRF_MID
|
||||
#define RFM95_REG_08_FRF_LSB 0x08 //!< REG_08_FRF_LSB
|
||||
#define RFM95_REG_09_PA_CONFIG 0x09 //!< REG_09_PA_CONFIG
|
||||
#define RFM95_REG_0A_PA_RAMP 0x0a //!< REG_0A_PA_RAMP
|
||||
#define RFM95_REG_0B_OCP 0x0b //!< REG_0B_OCP
|
||||
#define RFM95_REG_0C_LNA 0x0c //!< REG_0C_LNA
|
||||
#define RFM95_REG_0D_FIFO_ADDR_PTR 0x0d //!< REG_0D_FIFO_ADDR_PTR
|
||||
#define RFM95_REG_0E_FIFO_TX_BASE_ADDR 0x0e //!< REG_0E_FIFO_TX_BASE_ADDR
|
||||
#define RFM95_REG_0F_FIFO_RX_BASE_ADDR 0x0f //!< REG_0F_FIFO_RX_BASE_ADDR
|
||||
#define RFM95_REG_10_FIFO_RX_CURRENT_ADDR 0x10 //!< REG_10_FIFO_RX_CURRENT_ADDR
|
||||
#define RFM95_REG_11_IRQ_FLAGS_MASK 0x11 //!< REG_11_IRQ_FLAGS_MASK
|
||||
#define RFM95_REG_12_IRQ_FLAGS 0x12 //!< REG_12_IRQ_FLAGS
|
||||
#define RFM95_REG_13_RX_NB_BYTES 0x13 //!< REG_13_RX_NB_BYTES
|
||||
#define RFM95_REG_14_RX_HEADER_CNT_VALUE_MSB 0x14 //!< REG_14_RX_HEADER_CNT_VALUE_MSB
|
||||
#define RFM95_REG_15_RX_HEADER_CNT_VALUE_LSB 0x15 //!< REG_15_RX_HEADER_CNT_VALUE_LSB
|
||||
#define RFM95_REG_16_RX_PACKET_CNT_VALUE_MSB 0x16 //!< REG_16_RX_PACKET_CNT_VALUE_MSB
|
||||
#define RFM95_REG_17_RX_PACKET_CNT_VALUE_LSB 0x17 //!< REG_17_RX_PACKET_CNT_VALUE_LSB
|
||||
#define RFM95_REG_18_MODEM_STAT 0x18 //!< REG_18_MODEM_STAT
|
||||
#define RFM95_REG_19_PKT_SNR_VALUE 0x19 //!< REG_19_PKT_SNR_VALUE
|
||||
#define RFM95_REG_1A_PKT_RSSI_VALUE 0x1a //!< REG_1A_PKT_RSSI_VALUE
|
||||
#define RFM95_REG_1B_RSSI_VALUE 0x1b //!< REG_1B_RSSI_VALUE
|
||||
#define RFM95_REG_1C_HOP_CHANNEL 0x1c //!< REG_1C_HOP_CHANNEL
|
||||
#define RFM95_REG_1D_MODEM_CONFIG1 0x1d //!< REG_1D_MODEM_CONFIG1
|
||||
#define RFM95_REG_1E_MODEM_CONFIG2 0x1e //!< REG_1E_MODEM_CONFIG2
|
||||
#define RFM95_REG_1F_SYMB_TIMEOUT_LSB 0x1f //!< REG_1F_SYMB_TIMEOUT_LSB
|
||||
#define RFM95_REG_20_PREAMBLE_MSB 0x20 //!< REG_20_PREAMBLE_MSB
|
||||
#define RFM95_REG_21_PREAMBLE_LSB 0x21 //!< REG_21_PREAMBLE_LSB
|
||||
#define RFM95_REG_22_PAYLOAD_LENGTH 0x22 //!< REG_22_PAYLOAD_LENGTH
|
||||
#define RFM95_REG_23_MAX_PAYLOAD_LENGTH 0x23 //!< REG_23_MAX_PAYLOAD_LENGTH
|
||||
#define RFM95_REG_24_HOP_PERIOD 0x24 //!< REG_24_HOP_PERIOD
|
||||
#define RFM95_REG_25_FIFO_RX_BYTE_ADDR 0x25 //!< REG_25_FIFO_RX_BYTE_ADDR
|
||||
#define RFM95_REG_26_MODEM_CONFIG3 0x26 //!< REG_26_MODEM_CONFIG3
|
||||
|
||||
// Reserved when in LoRa mode
|
||||
#define RFM95_REG_40_DIO_MAPPING1 0x40 //!< REG_40_DIO_MAPPING1
|
||||
#define RFM95_REG_41_DIO_MAPPING2 0x41 //!< REG_41_DIO_MAPPING2
|
||||
#define RFM95_REG_42_VERSION 0x42 //!< REG_42_VERSION
|
||||
#define RFM95_REG_4B_TCXO 0x4b //!< REG_4B_TCXO
|
||||
#define RFM95_REG_4D_PA_DAC 0x4d //!< REG_4D_PA_DAC
|
||||
#define RFM95_REG_5B_FORMER_TEMP 0x5b //!< REG_5B_FORMER_TEMP
|
||||
#define RFM95_REG_61_AGC_REF 0x61 //!< REG_61_AGC_REF
|
||||
#define RFM95_REG_62_AGC_THRESH1 0x62 //!< REG_62_AGC_THRESH1
|
||||
#define RFM95_REG_63_AGC_THRESH2 0x63 //!< REG_63_AGC_THRESH2
|
||||
#define RFM95_REG_64_AGC_THRESH3 0x64 //!< REG_64_AGC_THRESH3
|
||||
|
||||
// RFM95_REG_01_OP_MODE 0x01
|
||||
#define RFM95_LONG_RANGE_MODE 0x80 //!< LONG_RANGE_MODE
|
||||
#define RFM95_ACCESS_SHARED_REG 0x40 //!< ACCESS_SHARED_REG
|
||||
|
||||
#define RFM95_MODE_SLEEP 0x00 //!< MODE_SLEEP
|
||||
#define RFM95_MODE_STDBY 0x01 //!< MODE_STDBY
|
||||
#define RFM95_MODE_FSTX 0x02 //!< MODE_FSTX
|
||||
#define RFM95_MODE_TX 0x03 //!< MODE_TX
|
||||
#define RFM95_MODE_FSRX 0x04 //!< MODE_FSRX
|
||||
#define RFM95_MODE_RXCONTINUOUS 0x05 //!< MODE_RXCONTINUOUS
|
||||
#define RFM95_MODE_RXSINGLE 0x06 //!< MODE_RXSINGLE
|
||||
#define RFM95_MODE_CAD 0x07 //!< MODE_CAD
|
||||
|
||||
// RFM95_REG_09_PA_CONFIG 0x09
|
||||
#define RFM95_OUTPUT_POWER 0x0F //!< OUTPUT_POWER
|
||||
#define RFM95_MAX_POWER 0x70 //!< MAX_POWER
|
||||
#define RFM95_PA_SELECT 0x80 //!< PA_SELECT
|
||||
|
||||
// RFM95_REG_0A_PA_RAMP 0x0a
|
||||
#define RFM95_PA_RAMP_3_4MS 0x00 //!< PA_RAMP_3_4MS
|
||||
#define RFM95_PA_RAMP_2MS 0x01 //!< PA_RAMP_2MS
|
||||
#define RFM95_PA_RAMP_1MS 0x02 //!< PA_RAMP_1MS
|
||||
#define RFM95_PA_RAMP_500US 0x03 //!< PA_RAMP_500US
|
||||
#define RFM95_PA_RAMP_250US 0x04 //!< PA_RAMP_250US
|
||||
#define RFM95_PA_RAMP_125US 0x05 //!< PA_RAMP_125US
|
||||
#define RFM95_PA_RAMP_100US 0x06 //!< PA_RAMP_100US
|
||||
#define RFM95_PA_RAMP_62US 0x07 //!< PA_RAMP_62US
|
||||
#define RFM95_PA_RAMP_50US 0x08 //!< PA_RAMP_50US
|
||||
#define RFM95_PA_RAMP_40US 0x09 //!< PA_RAMP_40US
|
||||
#define RFM95_PA_RAMP_31US 0x0A //!< PA_RAMP_31US
|
||||
#define RFM95_PA_RAMP_25US 0x0B //!< PA_RAMP_25US
|
||||
#define RFM95_PA_RAMP_20US 0x0C //!< PA_RAMP_20US
|
||||
#define RFM95_PA_RAMP_15US 0x0D //!< PA_RAMP_15US
|
||||
#define RFM95_PA_RAMP_12US 0x0E //!< PA_RAMP_12US
|
||||
#define RFM95_PA_RAMP_10US 0x0F //!< PA_RAMP_10US
|
||||
#define RFM95_LOW_PN_TX_PLL_OFF 0x10 //!< LOW_PN_TX_PLL_OFF
|
||||
|
||||
// RFM95_REG_0B_OCP 0x0b
|
||||
#define RFM95_OCP_TRIM 0x1f //!< OCP_TRIM
|
||||
#define RFM95_OCP_ON 0x20 //!< OCP_ON
|
||||
|
||||
// RFM95_REG_0C_LNA 0x0c
|
||||
#define RFM95_LNA_BOOST_DEFAULT 0x20 //!< LNA_BOOST_DEFAULT
|
||||
#define RFM95_LNA_BOOST 0x03 //!< LNA_BOOST
|
||||
|
||||
// RFM95_REG_11_IRQ_FLAGS_MASK 0x11
|
||||
#define RFM95_CAD_DETECTED_MASK 0x01 //!< CAD_DETECTED_MASK
|
||||
#define RFM95_FHSS_CHANGE_CHANNEL_MASK 0x02 //!< FHSS_CHANGE_CHANNEL_MASK
|
||||
#define RFM95_CAD_DONE_MASK 0x04 //!< CAD_DONE_MASK
|
||||
#define RFM95_TX_DONE_MASK 0x08 //!< TX_DONE_MASK
|
||||
#define RFM95_VALID_HEADER_MASK 0x10 //!< VALID_HEADER_MASK
|
||||
#define RFM95_PAYLOAD_CRC_ERROR_MASK 0x20 //!< PAYLOAD_CRC_ERROR_MASK
|
||||
#define RFM95_RX_DONE_MASK 0x40 //!< RX_DONE_MASK
|
||||
#define RFM95_RX_TIMEOUT_MASK 0x80 //!< RX_TIMEOUT_MASK
|
||||
|
||||
// RFM95_REG_12_IRQ_FLAGS 0x12
|
||||
#define RFM95_CAD_DETECTED 0x01 //!< CAD_DETECTED
|
||||
#define RFM95_FHSS_CHANGE_CHANNEL 0x02 //!< FHSS_CHANGE_CHANNEL
|
||||
#define RFM95_CAD_DONE 0x04 //!< CAD_DONE
|
||||
#define RFM95_TX_DONE 0x08 //!< TX_DONE
|
||||
#define RFM95_VALID_HEADER 0x10 //!< VALID_HEADER
|
||||
#define RFM95_PAYLOAD_CRC_ERROR 0x20 //!< PAYLOAD_CRC_ERROR
|
||||
#define RFM95_RX_DONE 0x40 //!< RX_DONE
|
||||
#define RFM95_RX_TIMEOUT 0x80 //!< RX_TIMEOUT
|
||||
#define RFM95_CLEAR_IRQ 0xFF //<! Clear IRQ
|
||||
|
||||
// RFM95_REG_18_MODEM_STAT 0x18
|
||||
#define RFM95_MODEM_STATUS_SIGNAL_DETECTED 0x01 //!< MODEM_STATUS_SIGNAL_DETECTED
|
||||
#define RFM95_MODEM_STATUS_SIGNAL_SYNCHRONIZED 0x02 //!< MODEM_STATUS_SIGNAL_SYNCHRONIZED
|
||||
#define RFM95_MODEM_STATUS_RX_ONGOING 0x04 //!< MODEM_STATUS_RX_ONGOING
|
||||
#define RFM95_MODEM_STATUS_HEADER_INFO_VALID 0x08 //!< MODEM_STATUS_HEADER_INFO_VALID
|
||||
#define RFM95_MODEM_STATUS_CLEAR 0x10 //!< MODEM_STATUS_CLEAR
|
||||
|
||||
// RFM95_REG_1C_HOP_CHANNEL 0x1c
|
||||
#define RFM95_RX_PAYLOAD_CRC_IS_ON 0x40 //!< RX_PAYLOAD_CRC_IS_ON
|
||||
#define RFM95_PLL_TIMEOUT 0x80 //!< PLL_TIMEOUT
|
||||
|
||||
// RFM95_REG_1D_MODEM_CONFIG1 0x1d
|
||||
|
||||
#define RFM95_BW_7_8KHZ 0x00 //!< BW_7_8KHZ
|
||||
#define RFM95_BW_10_4KHZ 0x10 //!< BW_10_4KHZ
|
||||
#define RFM95_BW_15_6KHZ 0x20 //!< BW_15_6KHZ
|
||||
#define RFM95_BW_20_8KHZ 0x30 //!< BW_20_8KHZ
|
||||
#define RFM95_BW_31_25KHZ 0x40 //!< BW_31_25KHZ
|
||||
#define RFM95_BW_41_7KHZ 0x50 //!< BW_41_7KHZ
|
||||
#define RFM95_BW_62_5KHZ 0x60 //!< BW_62_5KHZ
|
||||
#define RFM95_BW_125KHZ 0x70 //!< BW_125KHZ
|
||||
#define RFM95_BW_250KHZ 0x80 //!< BW_250KHZ
|
||||
#define RFM95_BW_500KHZ 0x90 //!< BW_500KHZ
|
||||
|
||||
#define RFM95_IMPLICIT_HEADER_MODE_ON 0x01 //!< IMPLICIT_HEADER_MODE_ON
|
||||
#define RFM95_CODING_RATE_4_5 0x02 //!< CODING_RATE_4_5
|
||||
#define RFM95_CODING_RATE_4_6 0x04 //!< CODING_RATE_4_6
|
||||
#define RFM95_CODING_RATE_4_7 0x06 //!< CODING_RATE_4_7
|
||||
#define RFM95_CODING_RATE_4_8 0x08 //!< CODING_RATE_4_8
|
||||
|
||||
// RFM95_REG_1E_MODEM_CONFIG2 0x1e
|
||||
#define RFM95_SPREADING_FACTOR_64CPS 0x60 //!< SPREADING_FACTOR_64CPS, SF6
|
||||
#define RFM95_SPREADING_FACTOR_128CPS 0x70 //!< SPREADING_FACTOR_128CPS, SF7
|
||||
#define RFM95_SPREADING_FACTOR_256CPS 0x80 //!< SPREADING_FACTOR_256CPS, SF8
|
||||
#define RFM95_SPREADING_FACTOR_512CPS 0x90 //!< SPREADING_FACTOR_512CPS, SF9
|
||||
#define RFM95_SPREADING_FACTOR_1024CPS 0xA0 //!< SPREADING_FACTOR_1024CPS, SF10
|
||||
#define RFM95_SPREADING_FACTOR_2048CPS 0xB0 //!< SPREADING_FACTOR_2048CPS, SF11
|
||||
#define RFM95_SPREADING_FACTOR_4096CPS 0xC0 //!< SPREADING_FACTOR_4096CPS, SF12
|
||||
|
||||
#define RFM95_SYM_TIMEOUT_MSB 0x03 //!< SYM_TIMEOUT_MSB
|
||||
#define RFM95_RX_PAYLOAD_CRC_ON 0x04 //!< RX_PAYLOAD_CRC_ON
|
||||
#define RFM95_TX_CONTINUOUS_MOdE 0x08 //!< TX_CONTINUOUS_MODE
|
||||
|
||||
// RFM95_REG_26_MODEM_CONFIG3 0x26
|
||||
#define RFM95_LOW_DATA_RATE_OPTIMIZE 0x08 //!< LOW_DATA_RATE_OPTIMIZE
|
||||
#define RFM95_AGC_AUTO_ON 0x04 //!< AGC_AUTO_ON
|
||||
|
||||
// RFM95_REG_4B_TCXO 0x4b
|
||||
#define RFM95_TCXO_TCXO_INPUT_ON 0x10 //!< TCXO_TCXO_INPUT_ON
|
||||
|
||||
// RFM95_REG_4D_PA_DAC 0x4d
|
||||
#define RFM95_PA_DAC_DISABLE 0x04 //!< PA_DAC_DISABLE
|
||||
#define RFM95_PA_DAC_ENABLE 0x07 //!< PA_DAC_ENABLE
|
||||
443
lib/MySensors/hal/transport/RS485/MyTransportRS485.cpp
Normal file
443
lib/MySensors/hal/transport/RS485/MyTransportRS485.cpp
Normal file
@@ -0,0 +1,443 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* -------------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2013, Majenko Technologies and S.J.Hoeksma
|
||||
* Copyright (c) 2015, LeoDesigner
|
||||
* https://github.com/leodesigner/mysensors-serial-transport
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those
|
||||
* of the authors and should not be interpreted as representing official policies,
|
||||
* either expressed or implied, of Majenko Technologies.
|
||||
********************************************************************************/
|
||||
|
||||
// transport(Serial,0,-1); // serial port, node, dePin (-1 disabled)
|
||||
|
||||
// Serial Transport
|
||||
|
||||
#ifdef __linux__
|
||||
#include "SerialPort.h"
|
||||
#endif
|
||||
|
||||
#if defined(MY_RS485_DE_PIN)
|
||||
#if !defined(MY_RS485_DE_INVERSE)
|
||||
#define assertDE() hwDigitalWrite(MY_RS485_DE_PIN, HIGH); delayMicroseconds(5)
|
||||
#define deassertDE() hwDigitalWrite(MY_RS485_DE_PIN, LOW)
|
||||
#else
|
||||
#define assertDE() hwDigitalWrite(MY_RS485_DE_PIN, LOW); delayMicroseconds(5)
|
||||
#define deassertDE() hwDigitalWrite(MY_RS485_DE_PIN, HIG)
|
||||
#endif
|
||||
#else
|
||||
#define assertDE()
|
||||
#define deassertDE()
|
||||
#endif
|
||||
|
||||
// We only use SYS_PACK in this application
|
||||
#define ICSC_SYS_PACK 0x58
|
||||
|
||||
// Receiving header information
|
||||
char _header[6];
|
||||
|
||||
// Reception state machine control and storage variables
|
||||
unsigned char _recPhase;
|
||||
unsigned char _recPos;
|
||||
unsigned char _recCommand;
|
||||
unsigned char _recLen;
|
||||
unsigned char _recStation;
|
||||
unsigned char _recSender;
|
||||
unsigned char _recCS;
|
||||
unsigned char _recCalcCS;
|
||||
|
||||
|
||||
#if defined(__linux__)
|
||||
SerialPort _dev = SerialPort(MY_RS485_HWSERIAL);
|
||||
#elif defined(MY_RS485_HWSERIAL)
|
||||
HardwareSerial& _dev = MY_RS485_HWSERIAL;
|
||||
#else
|
||||
AltSoftSerial _dev;
|
||||
#endif
|
||||
|
||||
unsigned char _nodeId;
|
||||
char _data[MY_RS485_MAX_MESSAGE_LENGTH];
|
||||
uint8_t _packet_len;
|
||||
unsigned char _packet_from;
|
||||
bool _packet_received;
|
||||
|
||||
// Packet wrapping characters, defined in standard ASCII table
|
||||
#define SOH 1
|
||||
#define STX 2
|
||||
#define ETX 3
|
||||
#define EOT 4
|
||||
|
||||
|
||||
|
||||
//Reset the state machine and release the data pointer
|
||||
void _serialReset()
|
||||
{
|
||||
_recPhase = 0;
|
||||
_recPos = 0;
|
||||
_recLen = 0;
|
||||
_recCommand = 0;
|
||||
_recCS = 0;
|
||||
_recCalcCS = 0;
|
||||
}
|
||||
|
||||
// This is the main reception state machine. Progress through the states
|
||||
// is keyed on either special control characters, or counted number of bytes
|
||||
// received. If all the data is in the right format, and the calculated
|
||||
// checksum matches the received checksum, AND the destination station is
|
||||
// our station ID, then look for a registered command that matches the
|
||||
// command code. If all the above is true, execute the command's
|
||||
// function.
|
||||
bool _serialProcess()
|
||||
{
|
||||
unsigned char i;
|
||||
if (!_dev.available()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while(_dev.available()) {
|
||||
char inch;
|
||||
inch = _dev.read();
|
||||
|
||||
switch(_recPhase) {
|
||||
|
||||
// Case 0 looks for the header. Bytes arrive in the serial interface and get
|
||||
// shifted through a header buffer. When the start and end characters in
|
||||
// the buffer match the SOH/STX pair, and the destination station ID matches
|
||||
// our ID, save the header information and progress to the next state.
|
||||
case 0:
|
||||
memcpy(&_header[0],&_header[1],5);
|
||||
_header[5] = inch;
|
||||
if ((_header[0] == SOH) && (_header[5] == STX) && (_header[1] != _header[2])) {
|
||||
_recCalcCS = 0;
|
||||
_recStation = _header[1];
|
||||
_recSender = _header[2];
|
||||
_recCommand = _header[3];
|
||||
_recLen = _header[4];
|
||||
|
||||
for (i=1; i<=4; i++) {
|
||||
_recCalcCS += _header[i];
|
||||
}
|
||||
_recPhase = 1;
|
||||
_recPos = 0;
|
||||
|
||||
//Avoid _data[] overflow
|
||||
if (_recLen >= MY_RS485_MAX_MESSAGE_LENGTH) {
|
||||
_serialReset();
|
||||
break;
|
||||
}
|
||||
|
||||
//Check if we should process this message
|
||||
//We reject the message if we are the sender
|
||||
//We reject if we are not the receiver and message is not a broadcast
|
||||
if ((_recSender == _nodeId) ||
|
||||
(_recStation != _nodeId &&
|
||||
_recStation != BROADCAST_ADDRESS)) {
|
||||
_serialReset();
|
||||
break;
|
||||
}
|
||||
|
||||
if (_recLen == 0) {
|
||||
_recPhase = 2;
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
// Case 1 receives the data portion of the packet. Read in "_recLen" number
|
||||
// of bytes and store them in the _data array.
|
||||
case 1:
|
||||
_data[_recPos++] = inch;
|
||||
_recCalcCS += inch;
|
||||
if (_recPos == _recLen) {
|
||||
_recPhase = 2;
|
||||
}
|
||||
break;
|
||||
|
||||
// After the data comes a single ETX character. Do we have it? If not,
|
||||
// reset the state machine to default and start looking for a new header.
|
||||
case 2:
|
||||
// Packet properly terminated?
|
||||
if (inch == ETX) {
|
||||
_recPhase = 3;
|
||||
} else {
|
||||
_serialReset();
|
||||
}
|
||||
break;
|
||||
|
||||
// Next comes the checksum. We have already calculated it from the incoming
|
||||
// data, so just store the incoming checksum byte for later.
|
||||
case 3:
|
||||
_recCS = inch;
|
||||
_recPhase = 4;
|
||||
break;
|
||||
|
||||
// The final state - check the last character is EOT and that the checksum matches.
|
||||
// If that test passes, then look for a valid command callback to execute.
|
||||
// Execute it if found.
|
||||
case 4:
|
||||
if (inch == EOT) {
|
||||
if (_recCS == _recCalcCS) {
|
||||
// First, check for system level commands. It is possible
|
||||
// to register your own callback as well for system level
|
||||
// commands which will be called after the system default
|
||||
// hook.
|
||||
|
||||
switch (_recCommand) {
|
||||
case ICSC_SYS_PACK:
|
||||
_packet_from = _recSender;
|
||||
_packet_len = _recLen;
|
||||
_packet_received = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//Clear the data
|
||||
_serialReset();
|
||||
//Return true, we have processed one command
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool transportSend(const uint8_t to, const void* data, const uint8_t len, const bool noACK)
|
||||
{
|
||||
(void)noACK; // not implemented
|
||||
const char *datap = static_cast<char const *>(data);
|
||||
unsigned char i;
|
||||
unsigned char cs = 0;
|
||||
|
||||
// This is how many times to try and transmit before failing.
|
||||
unsigned char timeout = 10;
|
||||
|
||||
// Let's start out by looking for a collision. If there has been anything seen in
|
||||
// the last millisecond, then wait for a random time and check again.
|
||||
|
||||
while (_serialProcess()) {
|
||||
unsigned char del;
|
||||
del = rand() % 20;
|
||||
for (i = 0; i < del; i++) {
|
||||
delay(1);
|
||||
_serialProcess();
|
||||
}
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
// Failed to transmit!!!
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(MY_RS485_DE_PIN)
|
||||
#if !defined(MY_RS485_DE_INVERSE)
|
||||
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
|
||||
#else
|
||||
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
|
||||
#endif
|
||||
delayMicroseconds(5);
|
||||
#endif
|
||||
|
||||
// Start of header by writing multiple SOH
|
||||
for(byte w=0; w<MY_RS485_SOH_COUNT; w++) {
|
||||
_dev.write(SOH);
|
||||
}
|
||||
_dev.write(to); // Destination address
|
||||
cs += to;
|
||||
_dev.write(_nodeId); // Source address
|
||||
cs += _nodeId;
|
||||
_dev.write(ICSC_SYS_PACK); // Command code
|
||||
cs += ICSC_SYS_PACK;
|
||||
_dev.write(len); // Length of text
|
||||
cs += len;
|
||||
_dev.write(STX); // Start of text
|
||||
for(i=0; i<len; i++) {
|
||||
_dev.write(datap[i]); // Text bytes
|
||||
cs += datap[i];
|
||||
}
|
||||
_dev.write(ETX); // End of text
|
||||
_dev.write(cs);
|
||||
_dev.write(EOT);
|
||||
|
||||
#if defined(MY_RS485_DE_PIN)
|
||||
#ifdef __PIC32MX__
|
||||
// MPIDE has nothing yet for this. It uses the hardware buffer, which
|
||||
// could be up to 8 levels deep. For now, let's just delay for 8
|
||||
// characters worth.
|
||||
delayMicroseconds((F_CPU/9600)+1);
|
||||
#else
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#if ARDUINO >= 104
|
||||
// Arduino 1.0.4 and upwards does it right
|
||||
_dev.flush();
|
||||
#else
|
||||
// Between 1.0.0 and 1.0.3 it almost does it - need to compensate
|
||||
// for the hardware buffer. Delay for 2 bytes worth of transmission.
|
||||
_dev.flush();
|
||||
delayMicroseconds((20000000UL/9600)+1);
|
||||
#endif
|
||||
#elif defined(__linux__)
|
||||
_dev.flush();
|
||||
#endif
|
||||
#endif
|
||||
#if !defined(MY_RS485_DE_INVERSE)
|
||||
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
|
||||
#else
|
||||
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
|
||||
#endif
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool transportInit(void)
|
||||
{
|
||||
// Reset the state machine
|
||||
_dev.begin(MY_RS485_BAUD_RATE);
|
||||
_serialReset();
|
||||
#if defined(MY_RS485_DE_PIN)
|
||||
hwPinMode(MY_RS485_DE_PIN, OUTPUT);
|
||||
#if !defined(MY_RS485_DE_INVERSE)
|
||||
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
|
||||
#else
|
||||
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
|
||||
#endif
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
void transportSetAddress(const uint8_t address)
|
||||
{
|
||||
_nodeId = address;
|
||||
}
|
||||
|
||||
uint8_t transportGetAddress(void)
|
||||
{
|
||||
return _nodeId;
|
||||
}
|
||||
|
||||
|
||||
bool transportDataAvailable(void)
|
||||
{
|
||||
_serialProcess();
|
||||
return _packet_received;
|
||||
}
|
||||
|
||||
bool transportSanityCheck(void)
|
||||
{
|
||||
// not implemented yet
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t transportReceive(void* data)
|
||||
{
|
||||
if (_packet_received) {
|
||||
memcpy(data,_data,_packet_len);
|
||||
_packet_received = false;
|
||||
return _packet_len;
|
||||
} else {
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
void transportPowerDown(void)
|
||||
{
|
||||
// Nothing to shut down here
|
||||
}
|
||||
|
||||
void transportPowerUp(void)
|
||||
{
|
||||
// not implemented
|
||||
}
|
||||
|
||||
void transportSleep(void)
|
||||
{
|
||||
// not implemented
|
||||
}
|
||||
|
||||
void transportStandBy(void)
|
||||
{
|
||||
// not implemented
|
||||
}
|
||||
|
||||
int16_t transportGetSendingRSSI(void)
|
||||
{
|
||||
// not implemented
|
||||
return INVALID_RSSI;
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingRSSI(void)
|
||||
{
|
||||
// not implemented
|
||||
return INVALID_RSSI;
|
||||
}
|
||||
|
||||
int16_t transportGetSendingSNR(void)
|
||||
{
|
||||
// not implemented
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetReceivingSNR(void)
|
||||
{
|
||||
// not implemented
|
||||
return INVALID_SNR;
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerPercent(void)
|
||||
{
|
||||
// not implemented
|
||||
return static_cast<int16_t>(100);
|
||||
}
|
||||
|
||||
int16_t transportGetTxPowerLevel(void)
|
||||
{
|
||||
// not implemented
|
||||
return static_cast<int16_t>(100);
|
||||
}
|
||||
|
||||
bool transportSetTxPowerPercent(const uint8_t powerPercent)
|
||||
{
|
||||
// not possible
|
||||
(void)powerPercent;
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user