Skip to content

Commit

Permalink
convert _SPI calls to pointers where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Apr 11, 2021
1 parent e971e5b commit 5b0b0f9
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 70 deletions.
163 changes: 107 additions & 56 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,29 @@ void RF24::csn(bool mode)
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz

#if !defined(SOFTSPI)
_SPI.setBitOrder(MSBFIRST);
_SPI.setDataMode(SPI_MODE0);
#if !defined(F_CPU) || F_CPU < 20000000
_SPI.setClockDivider(SPI_CLOCK_DIV2);
#elif F_CPU < 40000000
_SPI.setClockDivider(SPI_CLOCK_DIV4);
#elif F_CPU < 80000000
_SPI.setClockDivider(SPI_CLOCK_DIV8);
#elif F_CPU < 160000000
_SPI.setClockDivider(SPI_CLOCK_DIV16);
#elif F_CPU < 320000000
_SPI.setClockDivider(SPI_CLOCK_DIV32);
#elif F_CPU < 640000000
_SPI.setClockDivider(SPI_CLOCK_DIV64);
#elif F_CPU < 1280000000
_SPI.setClockDivider(SPI_CLOCK_DIV128);
#else
#error "Unsupported CPU frequency. Please set correct SPI divider."
#endif

#endif
// applies to SPI_UART and inherent hardware SPI
_spi->setBitOrder(MSBFIRST);
_spi->setDataMode(SPI_MODE0);

#if !defined(F_CPU) || F_CPU < 20000000
_spi->setClockDivider(SPI_CLOCK_DIV2);
#elif F_CPU < 40000000
_spi->setClockDivider(SPI_CLOCK_DIV4);
#elif F_CPU < 80000000
_spi->setClockDivider(SPI_CLOCK_DIV8);
#elif F_CPU < 160000000
_spi->setClockDivider(SPI_CLOCK_DIV16);
#elif F_CPU < 320000000
_spi->setClockDivider(SPI_CLOCK_DIV32);
#elif F_CPU < 640000000
_spi->setClockDivider(SPI_CLOCK_DIV64);
#elif F_CPU < 1280000000
_spi->setClockDivider(SPI_CLOCK_DIV128);
#else // F_CPU >= 1280000000
#error "Unsupported CPU frequency. Please set correct SPI divider."
#endif // F_CPU to SPI_CLOCK_DIV translation

#endif // !defined(SOFTSPI)
#elif defined(RF24_RPi)
if(!mode)
_SPI.chipSelect(csn_pin);
Expand All @@ -85,9 +87,13 @@ void RF24::ce(bool level)

inline void RF24::beginTransaction()
{
#if defined(RF24_SPI_TRANSACTIONS)
#if defined (RF24_SPI_TRANSACTIONS)
#if defined (RF24_RPi)
_SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
#endif // defined(RF24_SPI_TRANSACTIONS)
#else // !defined(RF24_RPi)
_spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
#endif // !defined(RF24_RPi)
#endif // defined (RF24_SPI_TRANSACTIONS)
csn(LOW);
}

Expand All @@ -97,8 +103,12 @@ inline void RF24::endTransaction()
{
csn(HIGH);
#if defined(RF24_SPI_TRANSACTIONS)
_SPI.endTransaction();
#endif // defined(RF24_SPI_TRANSACTIONS)
#if defined(RF24_RPi)
_SPI.endTransacrtion();
#else // !defined(RF24_RPi)
_spi->endTransaction();
#endif // !defined(RF24_RPi)
#endif // defined (RF24_SPI_TRANSACTIONS)
}

/****************************************************************************/
Expand All @@ -120,15 +130,20 @@ void RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
status = *prx++; // status is 1st byte of receive buffer

// decrement before to skip status byte
while ( --size ){ *buf++ = *prx++; }
while (--size) { *buf++ = *prx++; }
endTransaction(); //unlocks mutex and setting csn high
#else // !defined(RF24_LINUX)

beginTransaction();
#if defined (XMEGA_D3)
status = _SPI.transfer(R_REGISTER | reg);
while (len--) {
*buf++ = _SPI.transfer(0xFF);
}
while (len--) { *buf++ = _SPI.transfer(0xFF); }

#else // !defined(XMEGA_D3)
status = _spi->transfer(R_REGISTER | reg);
while (len--) { *buf++ = _spi->transfer(0xFF); }

#endif // !defined(XMEGA_D3)
endTransaction();
#endif // !defined(RF24_LINUX)
}
Expand All @@ -155,10 +170,14 @@ uint8_t RF24::read_register(uint8_t reg)
#else // !defined(RF24_LINUX)

beginTransaction();
#if defined (XMEGA_D3)
status = _SPI.transfer(R_REGISTER | reg);
result = _SPI.transfer(0xff);
#else // !defined(XMEGA_D3)
status = _spi->transfer(R_REGISTER | reg);
result = _spi->transfer(0xff);
#endif // !defined(XMEGA_D3)
endTransaction();

#endif // !defined(RF24_LINUX)

return result;
Expand All @@ -174,8 +193,8 @@ void RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
uint8_t * ptx = spi_txbuff;
uint8_t size = len + 1; // Add register value to transmit buffer

*ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) );
while ( len-- )
*ptx++ = (W_REGISTER | (REGISTER_MASK & reg));
while (len--)
*ptx++ = *buf++;

_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
Expand All @@ -184,10 +203,15 @@ void RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
#else // !defined(RF24_LINUX)

beginTransaction();
#if defined (XMEGA_D3)
status = _SPI.transfer(W_REGISTER | reg);
while (len--) {
_SPI.transfer(*buf++);
}
while (len--) { _SPI.transfer(*buf++); }

#else // !defined(XMEGA_D3)
status = _spi->transfer(W_REGISTER | reg);
while (len--) { _spi->transfer(*buf++); }

#endif // !defined(XMEGA_D3)
endTransaction();
#endif // !defined(RF24_LINUX)
}
Expand All @@ -201,7 +225,11 @@ void RF24::write_register(uint8_t reg, uint8_t value, bool is_cmd_only)
IF_SERIAL_DEBUG(printf_P(PSTR("write_register(%02x)\r\n"), reg));
}
beginTransaction();
#if defined (RF24_LINUX) || defined (XMEGA_D3)
status = _SPI.transfer(W_REGISTER | reg);
#else
status = _spi->transfer(W_REGISTER | reg);
#endif // !defined(RF24_LINUX) || !defined(XMEGA_D3)
endTransaction();
}
else {
Expand All @@ -219,8 +247,13 @@ void RF24::write_register(uint8_t reg, uint8_t value, bool is_cmd_only)
#else // !defined(RF24_LINUX)

beginTransaction();
#if defined (XMEGA_D3)
status = _SPI.transfer(W_REGISTER | reg);
_SPI.transfer(value);
#else // !defined(XMEGA_D3)
status = _spi->transfer(W_REGISTER | reg);
_spi->transfer(value);
#endif // !defined(XMEGA_D3)
endTransaction();
#endif // !defined(RF24_LINUX)
}
Expand Down Expand Up @@ -252,10 +285,8 @@ void RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t writeT
size = data_len + blank_len + 1 ; // Add register value to transmit buffer

*ptx++ = writeType;
while ( data_len-- )
*ptx++ = *current++;
while ( blank_len-- )
*ptx++ = 0;
while (data_len--) { *ptx++ = *current++; }
while (blank_len--) { *ptx++ = 0; }

_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer
Expand All @@ -264,15 +295,18 @@ void RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t writeT
#else // !defined(RF24_LINUX)

beginTransaction();
#if defined (XMEGA_D3)
status = _SPI.transfer(writeType);
while (data_len--) {
_SPI.transfer(*current++);
}
while (blank_len--) {
_SPI.transfer(0);
}
endTransaction();
while (data_len--) { _SPI.transfer(*current++); }
while (blank_len--) { _SPI.transfer(0); }

#else // !defined(XMEGA_D3)
status = _spi->transfer(writeType);
while (data_len--) { _spi->transfer(*current++); }
while (blank_len--) { _spi->transfer(0); }

#endif // !defined(XMEGA_D3)
endTransaction();
#endif // !defined(RF24_LINUX)
}

Expand Down Expand Up @@ -303,8 +337,7 @@ void RF24::read_payload(void* buf, uint8_t data_len)
size = data_len + blank_len + 1; // Add register value to transmit buffer

*ptx++ = R_RX_PAYLOAD;
while(--size)
*ptx++ = RF24_NOP;
while(--size) { *ptx++ = RF24_NOP; }

size = data_len + blank_len + 1; // Size has been lost during while, re affect

Expand All @@ -313,7 +346,7 @@ void RF24::read_payload(void* buf, uint8_t data_len)
status = *prx++; // 1st byte is status

if (data_len > 0) {
while ( --data_len ) // Decrement before to skip 1st status byte
while (--data_len) // Decrement before to skip 1st status byte
*current++ = *prx++;

*current = *prx;
Expand All @@ -322,13 +355,17 @@ void RF24::read_payload(void* buf, uint8_t data_len)
#else // !defined(RF24_LINUX)

beginTransaction();
#if defined (XMEGA_D3)
status = _SPI.transfer(R_RX_PAYLOAD);
while (data_len--) {
*current++ = _SPI.transfer(0xFF);
}
while (blank_len--) {
_SPI.transfer(0xff);
}
while (data_len--) { *current++ = _SPI.transfer(0xFF); }
while (blank_len--) { _SPI.transfer(0xff); }

#else // !defined(XMEGA_D3)
status = _spi->transfer(R_RX_PAYLOAD);
while (data_len--) { *current++ = _spi->transfer(0xFF); }
while (blank_len--) { _spi->transfer(0xff); }

#endif // !defined(XMEGA_D3)
endTransaction();

#endif // !defined(RF24_LINUX)
Expand Down Expand Up @@ -424,6 +461,15 @@ RF24::RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spi_speed)
:ce_pin(_cepin), csn_pin(_cspin),spi_speed(_spi_speed), payload_size(32), dynamic_payloads_enabled(true), addr_width(5), _is_p_variant(false),
csDelay(5)
{
// Use a pointer on the Arduino platform
#if defined (SOFTSPI)
_spi = &spi;
#elif defined (SPI_UART)
_spi = &uspi;
#elif !defined(RF24_LINUX) && !defined(XMEGA_D3)
_spi = &SPI;
#endif

pipe0_reading_address[0] = 0;
if(spi_speed <= 35000){ //Handle old BCM2835 speed constants, default to RF24_SPI_SPEED
spi_speed = RF24_SPI_SPEED;
Expand Down Expand Up @@ -712,7 +758,7 @@ bool RF24::begin(void)
pinMode(ce_pin, OUTPUT);
pinMode(csn_pin, OUTPUT);
}
_SPI.begin();
_spi->begin();
ce(LOW);
csn(HIGH);
#if defined(__ARDUINO_X86__)
Expand Down Expand Up @@ -1298,8 +1344,13 @@ void RF24::closeReadingPipe(uint8_t pipe)
void RF24::toggle_features(void)
{
beginTransaction();
#if defined (RF24_LINUX) || defined (XMEGA_D3)
status = _SPI.transfer(ACTIVATE);
_SPI.transfer(0x73);
#else
status = _spi->transfer(ACTIVATE);
_spi->transfer(0x73);
#endif
endTransaction();
}

Expand Down
14 changes: 8 additions & 6 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class RF24 {

#if defined (RF24_LINUX) || defined (XMEGA_D3) /* XMEGA can use SPI class */
SPI spi;
#else // everything else (Arduino)
_SPI* _spi;
#endif
#if defined (MRAA)
GPIO gpio;
Expand Down Expand Up @@ -1935,7 +1937,7 @@ class RF24 {
*
* This is a simple example of using the RF24 class on a Raspberry Pi.
*
* Remember to install the <a href="Python.html">Python wrapper</a>, then
* Remember to install the [Python wrapper](md_docs_python_wrapper.html), then
* navigate to the "RF24/examples_linux" folder.
* <br>To run this example, enter
* @code{.sh}python3 getting_started.py @endcode and follow the prompts.
Expand All @@ -1951,7 +1953,7 @@ class RF24 {
* This is a simple example of using the RF24 class on a Raspberry Pi to
* transmit and retrieve custom automatic acknowledgment payloads.
*
* Remember to install the <a href="Python.html">Python wrapper</a>, then
* Remember to install the [Python wrapper](md_docs_python_wrapper.html), then
* navigate to the "RF24/examples_linux" folder.
* <br>To run this example, enter
* @code{.sh}python3 acknowledgement_payloads.py @endcode and follow the prompts.
Expand All @@ -1970,7 +1972,7 @@ class RF24 {
* payloads because automatic ACK payloads' data will always be outdated by 1
* transmission. Instead, this example uses a call and response paradigm.
*
* Remember to install the <a href="Python.html">Python wrapper</a>, then
* Remember to install the [Python wrapper](md_docs_python_wrapper.html), then
* navigate to the "RF24/examples_linux" folder.
* <br>To run this example, enter
* @code{.sh}python3 manual_acknowledgements.py @endcode and follow the prompts.
Expand All @@ -1986,7 +1988,7 @@ class RF24 {
* This is a simple example of using the RF24 class on a Raspberry Pi for
* streaming multiple payloads.
*
* Remember to install the <a href="Python.html">Python wrapper</a>, then
* Remember to install the [Python wrapper](md_docs_python_wrapper.html), then
* navigate to the "RF24/examples_linux" folder.
* <br>To run this example, enter
* @code{.sh}python3 streaming_data.py @endcode and follow the prompts.
Expand All @@ -2002,7 +2004,7 @@ class RF24 {
* This is a simple example of using the RF24 class on a Raspberry Pi to
* detecting (and verifying) the IRQ (interrupt) pin on the nRF24L01.
*
* Remember to install the <a href="Python.html">Python wrapper</a>, then
* Remember to install the [Python wrapper](md_docs_python_wrapper.html), then
* navigate to the "RF24/examples_linux" folder.
* <br>To run this example, enter
* @code{.sh}python3 interrupt_configure.py @endcode and follow the prompts.
Expand All @@ -2019,7 +2021,7 @@ class RF24 {
* using 1 nRF24L01 to receive data from up to 6 other transceivers. This
* technique is called "multiceiver" in the datasheet.
*
* Remember to install the <a href="Python.html">Python wrapper</a>, then
* Remember to install the [Python wrapper](md_docs_python_wrapper.html), then
* navigate to the "RF24/examples_linux" folder.
* <br>To run this example, enter
* @code{.sh}python3 multiceiver_demo.py @endcode and follow the prompts.
Expand Down
Loading

0 comments on commit 5b0b0f9

Please sign in to comment.