Skip to content

Commit 2bfde05

Browse files
authored
Reverse Engineering tricks (#804)
* new toggleAllPipes() function * add toggleAllPipes() to python wrapper * trigger linux CI on changes to python wrapper * add new function setRadiation() * use uint8_t level like setPALevel() does * fix typo in docs reference * add new functions to sphinx docs * ran a hardware test; looks good
1 parent 3befead commit 2bfde05

File tree

5 files changed

+111
-38
lines changed

5 files changed

+111
-38
lines changed

.github/workflows/build_linux.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ on:
2020
- "!examples_linux/*.py"
2121
- "!examples_linux/*.md"
2222
- "pyRF24/setup.py"
23+
- "pyRF24/pyRF24.cpp"
2324
- ".github/workflows/build_linux.yml"
2425
push:
2526
paths:
@@ -39,6 +40,7 @@ on:
3940
- "!examples_linux/*.py"
4041
- "!examples_linux/*.md"
4142
- "pyRF24/setup.py"
43+
- "pyRF24/pyRF24.cpp"
4244
- ".github/workflows/build_linux.yml"
4345
release:
4446
types: [published, edited]

RF24.cpp

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,9 @@ bool RF24::_init_radio()
924924
setRetries(5, 15);
925925

926926
// Then set the data rate to the slowest (and most reliable) speed supported by all
927-
// hardware.
928-
setDataRate(RF24_1MBPS);
927+
// hardware. Since this value occupies the same register as the PA level value, set
928+
// the PA level to MAX
929+
setRadiation(RF24_PA_MAX, RF24_1MBPS); // LNA enabled by default
929930

930931
// detect if is a plus variant & use old toggle features command accordingly
931932
uint8_t before_toggle = read_register(FEATURE);
@@ -1670,16 +1671,9 @@ bool RF24::testRPD(void)
16701671

16711672
void RF24::setPALevel(uint8_t level, bool lnaEnable)
16721673
{
1673-
1674-
uint8_t setup = read_register(RF_SETUP) & 0xF8;
1675-
1676-
if (level > 3) { // If invalid level, go to max PA
1677-
level = static_cast<uint8_t>((RF24_PA_MAX << 1) + lnaEnable); // +1 to support the SI24R1 chip extra bit
1678-
} else {
1679-
level = static_cast<uint8_t>((level << 1) + lnaEnable); // Else set level as requested
1680-
}
1681-
1682-
write_register(RF_SETUP, setup |= level); // Write it to the chip
1674+
uint8_t setup = read_register(RF_SETUP) & static_cast<uint8_t>(0xF8);
1675+
setup |= _pa_level_reg_value(level, lnaEnable);
1676+
write_register(RF_SETUP, setup);
16831677
}
16841678

16851679
/****************************************************************************/
@@ -1707,33 +1701,8 @@ bool RF24::setDataRate(rf24_datarate_e speed)
17071701

17081702
// HIGH and LOW '00' is 1Mbs - our default
17091703
setup = static_cast<uint8_t>(setup & ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)));
1704+
setup |= _data_rate_reg_value(speed);
17101705

1711-
#if !defined(F_CPU) || F_CPU > 20000000
1712-
txDelay = 280;
1713-
#else //16Mhz Arduino
1714-
txDelay=85;
1715-
#endif
1716-
if (speed == RF24_250KBPS) {
1717-
// Must set the RF_DR_LOW to 1; RF_DR_HIGH (used to be RF_DR) is already 0
1718-
// Making it '10'.
1719-
setup |= _BV(RF_DR_LOW);
1720-
#if !defined(F_CPU) || F_CPU > 20000000
1721-
txDelay = 505;
1722-
#else //16Mhz Arduino
1723-
txDelay = 155;
1724-
#endif
1725-
} else {
1726-
// Set 2Mbs, RF_DR (RF_DR_HIGH) is set 1
1727-
// Making it '01'
1728-
if (speed == RF24_2MBPS) {
1729-
setup |= _BV(RF_DR_HIGH);
1730-
#if !defined(F_CPU) || F_CPU > 20000000
1731-
txDelay = 240;
1732-
#else // 16Mhz Arduino
1733-
txDelay = 65;
1734-
#endif
1735-
}
1736-
}
17371706
write_register(RF_SETUP, setup);
17381707

17391708
// Verify our result
@@ -1851,6 +1820,7 @@ void RF24::startConstCarrier(rf24_pa_dbm_e level, uint8_t channel)
18511820
}
18521821

18531822
/****************************************************************************/
1823+
18541824
void RF24::stopConstCarrier()
18551825
{
18561826
/*
@@ -1863,3 +1833,63 @@ void RF24::stopConstCarrier()
18631833
write_register(RF_SETUP, static_cast<uint8_t>(read_register(RF_SETUP) & ~_BV(CONT_WAVE) & ~_BV(PLL_LOCK)));
18641834
ce(LOW);
18651835
}
1836+
1837+
/****************************************************************************/
1838+
1839+
void RF24::toggleAllPipes(bool isEnabled)
1840+
{
1841+
write_register(EN_RXADDR, static_cast<uint8_t>(isEnabled ? 0x3F : 0));
1842+
}
1843+
1844+
/****************************************************************************/
1845+
1846+
uint8_t RF24::_data_rate_reg_value(rf24_datarate_e speed)
1847+
{
1848+
#if !defined(F_CPU) || F_CPU > 20000000
1849+
txDelay = 280;
1850+
#else //16Mhz Arduino
1851+
txDelay=85;
1852+
#endif
1853+
if (speed == RF24_250KBPS) {
1854+
#if !defined(F_CPU) || F_CPU > 20000000
1855+
txDelay = 505;
1856+
#else //16Mhz Arduino
1857+
txDelay = 155;
1858+
#endif
1859+
// Must set the RF_DR_LOW to 1; RF_DR_HIGH (used to be RF_DR) is already 0
1860+
// Making it '10'.
1861+
return static_cast<uint8_t>(_BV(RF_DR_LOW));
1862+
}
1863+
else if (speed == RF24_2MBPS) {
1864+
#if !defined(F_CPU) || F_CPU > 20000000
1865+
txDelay = 240;
1866+
#else // 16Mhz Arduino
1867+
txDelay = 65;
1868+
#endif
1869+
// Set 2Mbs, RF_DR (RF_DR_HIGH) is set 1
1870+
// Making it '01'
1871+
return static_cast<uint8_t>(_BV(RF_DR_HIGH));
1872+
}
1873+
// HIGH and LOW '00' is 1Mbs - our default
1874+
return static_cast<uint8_t>(0);
1875+
1876+
}
1877+
1878+
/****************************************************************************/
1879+
1880+
uint8_t RF24::_pa_level_reg_value(uint8_t level, bool lnaEnable)
1881+
{
1882+
// If invalid level, go to max PA
1883+
// Else set level as requested
1884+
// + lnaEnable (1 or 0) to support the SI24R1 chip extra bit
1885+
return static_cast<uint8_t>(((level > RF24_PA_MAX ? static_cast<uint8_t>(RF24_PA_MAX) : level) << 1) + lnaEnable);
1886+
}
1887+
1888+
/****************************************************************************/
1889+
1890+
void RF24::setRadiation(uint8_t level, rf24_datarate_e speed, bool lnaEnable)
1891+
{
1892+
uint8_t setup = _data_rate_reg_value(speed);
1893+
setup |= _pa_level_reg_value(level, lnaEnable);
1894+
write_register(RF_SETUP, setup);
1895+
}

RF24.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,26 @@ class RF24 {
16421642
*/
16431643
void stopConstCarrier(void);
16441644

1645+
/**
1646+
* @brief Open or close all data pipes.
1647+
*
1648+
* This function does not alter the addresses assigned to pipes. It is simply a
1649+
* convenience function that allows controling all pipes at once.
1650+
* @param isEnabled `true` opens all pipes; `false` closes all pipes.
1651+
*/
1652+
void toggleAllPipes(bool isEnabled);
1653+
1654+
/**
1655+
* @brief configure the RF_SETUP register in 1 transaction
1656+
* @param level This parameter is the same input as setPALevel()'s `level` parameter.
1657+
* See @ref rf24_pa_dbm_e enum for accepted values.
1658+
* @param speed This parameter is the same input as setDataRate()'s `speed` parameter.
1659+
* See @ref rf24_datarate_e enum for accepted values.
1660+
* @param lnaEnable This optional parameter is the same as setPALevel()'s `lnaEnable`
1661+
* optional parameter. Defaults to `true` (meaning LNA feature is enabled) when not specified.
1662+
*/
1663+
void setRadiation(uint8_t level, rf24_datarate_e speed, bool lnaEnable = true);
1664+
16451665
/**@}*/
16461666
/**
16471667
* @name Deprecated
@@ -1864,6 +1884,23 @@ class RF24 {
18641884

18651885
#endif
18661886

1887+
/**
1888+
* @brief Manipulate the @ref Datarate and txDelay
1889+
*
1890+
* This is a helper function to setRadiation() and setDataRate()
1891+
* @param speed The desired data rate.
1892+
*/
1893+
inline uint8_t _data_rate_reg_value(rf24_datarate_e speed);
1894+
1895+
/**
1896+
* @brief Manipulate the @ref PALevel
1897+
*
1898+
* This is a helper function to setRadiation() and setPALevel()
1899+
* @param level The desired @ref PALevel.
1900+
* @param lnaEnable Toggle the LNA feature.
1901+
*/
1902+
inline uint8_t _pa_level_reg_value(uint8_t level, bool lnaEnable);
1903+
18671904
/**@}*/
18681905

18691906
};

docs/sphinx/classRF24.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ Configuration API
8484
.. doxygenfunction:: RF24::maskIRQ
8585
.. doxygenfunction:: RF24::startConstCarrier
8686
.. doxygenfunction:: RF24::stopConstCarrier
87+
.. doxygenfunction:: RF24::toggleAllPipes
88+
.. doxygenfunction:: RF24::setRadiation
8789

8890
Protected API
8991
==============

pyRF24/pyRF24.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ BOOST_PYTHON_MODULE(RF24)
316316
.def("stopListening", &RF24::stopListening)
317317
.def("testCarrier", &RF24::testCarrier)
318318
.def("testRPD", &RF24::testRPD)
319+
.def("toggleAllPipes", &RF24::toggleAllPipes)
320+
.def("setRadiation", &RF24::setRadiation)
319321
.def("txStandBy", (bool (::RF24::*)(::uint32_t, bool))(&RF24::txStandBy), txStandBy_wrap1(bp::args("timeout", "startTx")))
320322
.def("whatHappened", &whatHappened_wrap)
321323
.def("startConstCarrier", &RF24::startConstCarrier, (bp::arg("level"), bp::arg("channel")))

0 commit comments

Comments
 (0)