Commit bd5ad6b9 authored by TMRh20's avatar TMRh20

Unify RF24 constructor API for all Linux platforms

- Define SPI config vars for non-Arduino systems:
RF24_BIT_ORDER, RF24_DATA_MODE, RF24_CLOCK_DIVIDER
- Allow use of standard RF24 constructor on RPi with default of 8Mhz SPI
clock speed
- Cleanup #ifdefs in RF24::csn();
- Fix compile on ATTiny
parent b342d97f
...@@ -14,28 +14,8 @@ ...@@ -14,28 +14,8 @@
void RF24::csn(bool mode) void RF24::csn(bool mode)
{ {
// Minimum ideal SPI bus speed is 2x data rate
// If we assume 2Mbs data rate and 16Mhz clock, a
// divider of 4 is the minimum we want.
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
#if defined(ARDUINO)
#if ( !defined(RF24_TINY) && !defined (__arm__) && !defined (SOFTSPI)) || defined (CORE_TEENSY)
_SPI.setBitOrder(MSBFIRST);
_SPI.setDataMode(SPI_MODE0);
_SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif
#endif
#if defined (RF24_RPi)
if(!mode){
_SPI.setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); #if defined (RF24_TINY)
_SPI.setDataMode(BCM2835_SPI_MODE0);
_SPI.setClockDivider(spi_speed);
_SPI.chipSelect(csn_pin);
delayMicroseconds(5);
}
#elif defined (RF24_TINY)
if (ce_pin != csn_pin) { if (ce_pin != csn_pin) {
digitalWrite(csn_pin,mode); digitalWrite(csn_pin,mode);
} }
...@@ -49,7 +29,30 @@ void RF24::csn(bool mode) ...@@ -49,7 +29,30 @@ void RF24::csn(bool mode)
delayMicroseconds(11); // allow csn to settle delayMicroseconds(11); // allow csn to settle
} }
} }
#elif !defined (ARDUINO_SAM_DUE) && !defined (RF24_LINUX) // Return, CSN toggle complete
return;
#elif defined(ARDUINO)
// Minimum ideal SPI bus speed is 2x data rate
// If we assume 2Mbs data rate and 16Mhz clock, a
// divider of 4 is the minimum we want.
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
#if !defined (SOFTSPI)
_SPI.setBitOrder(MSBFIRST);
_SPI.setDataMode(SPI_MODE0);
_SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif
#elif defined (RF24_RPi)
_SPI.setBitOrder(RF24_BIT_ORDER);
_SPI.setDataMode(RF24_DATA_MODE);
_SPI.setClockDivider(spi_speed ? spi_speed : RF24_CLOCK_DIVIDER);
_SPI.chipSelect(csn_pin);
delayMicroseconds(5);
#endif
#if !defined (RF24_LINUX)
digitalWrite(csn_pin,mode); digitalWrite(csn_pin,mode);
delayMicroseconds(5); delayMicroseconds(5);
#endif #endif
...@@ -71,7 +74,7 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len) ...@@ -71,7 +74,7 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
uint8_t status; uint8_t status;
#if defined (RF24_LINUX) #if defined (RF24_LINUX)
csn(LOW); //In this case, calling csn(LOW) configures the spi settings csn(LOW); //In this case, calling csn(LOW) configures the spi settings for RPi
uint8_t * prx = spi_rxbuff; uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff; uint8_t * ptx = spi_txbuff;
uint8_t size = len + 1; // Add register value to transmit buffer uint8_t size = len + 1; // Add register value to transmit buffer
...@@ -86,7 +89,7 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len) ...@@ -86,7 +89,7 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
// decrement before to skip status byte // decrement before to skip status byte
while ( --size ){ *buf++ = *prx++; } while ( --size ){ *buf++ = *prx++; }
csn(HIGH);
#elif defined (RF24_DUE) #elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE ); status = _SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE );
while ( len-- > 1 ){ while ( len-- > 1 ){
...@@ -124,7 +127,7 @@ uint8_t RF24::read_register(uint8_t reg) ...@@ -124,7 +127,7 @@ uint8_t RF24::read_register(uint8_t reg)
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
result = *++prx; // result is 2nd byte of receive buffer result = *++prx; // result is 2nd byte of receive buffer
csn(HIGH);
#elif defined (RF24_DUE) #elif defined (RF24_DUE)
_SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ) , SPI_CONTINUE); _SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ) , SPI_CONTINUE);
result = _SPI.transfer(csn_pin,0xff); result = _SPI.transfer(csn_pin,0xff);
...@@ -158,7 +161,7 @@ uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len) ...@@ -158,7 +161,7 @@ uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer status = *prx; // status is 1st byte of receive buffer
csn(HIGH);
#elif defined (RF24_DUE) #elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE ); status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE );
while ( --len){ while ( --len){
...@@ -196,7 +199,7 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value) ...@@ -196,7 +199,7 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value)
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
status = *prx++; // status is 1st byte of receive buffer status = *prx++; // status is 1st byte of receive buffer
csn(HIGH);
#elif defined (RF24_DUE) #elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE); status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE);
_SPI.transfer(csn_pin,value); _SPI.transfer(csn_pin,value);
...@@ -240,7 +243,7 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri ...@@ -240,7 +243,7 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer status = *prx; // status is 1st byte of receive buffer
csn(HIGH);
#elif defined (RF24_DUE) #elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, writeType , SPI_CONTINUE); status = _SPI.transfer(csn_pin, writeType , SPI_CONTINUE);
...@@ -312,7 +315,7 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len) ...@@ -312,7 +315,7 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len)
*current++ = *prx++; *current++ = *prx++;
*current = *prx; *current = *prx;
csn(HIGH);
#elif defined (RF24_DUE) #elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, R_RX_PAYLOAD, SPI_CONTINUE ); status = _SPI.transfer(csn_pin, R_RX_PAYLOAD, SPI_CONTINUE );
...@@ -372,7 +375,6 @@ uint8_t RF24::spiTrans(uint8_t cmd){ ...@@ -372,7 +375,6 @@ uint8_t RF24::spiTrans(uint8_t cmd){
#if defined (RF24_LINUX) #if defined (RF24_LINUX)
csn(LOW); csn(LOW);
status = _SPI.transfer( cmd ); status = _SPI.transfer( cmd );
csn(HIGH);
#elif defined (RF24_DUE) #elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, cmd ); status = _SPI.transfer(csn_pin, cmd );
#else #else
...@@ -578,7 +580,7 @@ void RF24::printDetails(void) ...@@ -578,7 +580,7 @@ void RF24::printDetails(void)
case BCM2835_SPI_SPEED_32KHZ : printf("32 KHz"); break ; case BCM2835_SPI_SPEED_32KHZ : printf("32 KHz"); break ;
case BCM2835_SPI_SPEED_16KHZ : printf("16 KHz"); break ; case BCM2835_SPI_SPEED_16KHZ : printf("16 KHz"); break ;
case BCM2835_SPI_SPEED_8KHZ : printf("8 KHz"); break ; case BCM2835_SPI_SPEED_8KHZ : printf("8 KHz"); break ;
default : printf("Probably Bad !!!"); break ; default : printf("8 Mhz"); break ;
} }
printf("\n================ NRF Configuration ================\n"); printf("\n================ NRF Configuration ================\n");
...@@ -631,6 +633,7 @@ void RF24::begin(void) ...@@ -631,6 +633,7 @@ void RF24::begin(void)
switch(csn_pin){ //Ensure valid hardware CS pin switch(csn_pin){ //Ensure valid hardware CS pin
case 0: break; case 0: break;
case 1: break; case 1: break;
// Allow BCM2835 enums for RPi
case 8: csn_pin = 0; break; case 8: csn_pin = 0; break;
case 7: csn_pin = 1; break; case 7: csn_pin = 1; break;
default: csn_pin = 0; break; default: csn_pin = 0; break;
...@@ -657,7 +660,6 @@ void RF24::begin(void) ...@@ -657,7 +660,6 @@ void RF24::begin(void)
_SPI.setBitOrder(csn_pin,MSBFIRST); // Set the bit order and mode specific to this device _SPI.setBitOrder(csn_pin,MSBFIRST); // Set the bit order and mode specific to this device
_SPI.setDataMode(csn_pin,SPI_MODE0); _SPI.setDataMode(csn_pin,SPI_MODE0);
ce(LOW); ce(LOW);
//csn(HIGH);
#else #else
#if ! defined(LITTLEWIRE) #if ! defined(LITTLEWIRE)
if (ce_pin != csn_pin) if (ce_pin != csn_pin)
......
...@@ -102,7 +102,7 @@ public: ...@@ -102,7 +102,7 @@ public:
//#if defined (RF24_LINUX) //#if defined (RF24_LINUX)
/** /**
* Raspberry Pi Constructor * Optional Raspberry Pi Constructor
* *
* Creates a new instance of this driver. Before using, you create an instance * Creates a new instance of this driver. Before using, you create an instance
* and send in the unique pins that this chip is connected to. * and send in the unique pins that this chip is connected to.
...@@ -115,7 +115,9 @@ public: ...@@ -115,7 +115,9 @@ public:
RF24(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed ); RF24(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed );
//#endif //#endif
#if !defined (RF24_TINY)
virtual ~RF24() {}; virtual ~RF24() {};
#endif
/** /**
* Begin operation of the chip * Begin operation of the chip
...@@ -1307,8 +1309,10 @@ private: ...@@ -1307,8 +1309,10 @@ private:
* @section News News * @section News News
* *
* **Feb 2015**<br> * **Feb 2015**<br>
* - MRAA supported added ( Galileo, Edison, etc) * - <a href="MRAA.html">MRAA</a> support added ( Galileo, Edison, etc)
* - BBB/Linux support via spidev & MRAA * - <a href="BBB.html">BBB/Linux </a> support via spidev & MRAA
* - Break out requirements for individual platforms to RF24/arch
* - Documentation cleanup & update
* *
* <b>Dec 2014 </b><br> * <b>Dec 2014 </b><br>
* - New: Intel Galileo now supported * - New: Intel Galileo now supported
...@@ -1515,13 +1519,10 @@ private: ...@@ -1515,13 +1519,10 @@ private:
* *
* @page BBB BeagleBone Black * @page BBB BeagleBone Black
* *
* BeagleBone Black is supported via MRAA or SPIDEV, with MRAA being the preferred choice due to performance. * BeagleBone Black is supported via MRAA or SPIDEV.
*
* @warning At the time of this writing MRAA support for BBB is not yet completed. RF24 support for MRAA via BBB is included
* but is untested.
* *
* @note With SPIDEV, Users may need to edit the RF24/arch/BBB/spi.cpp file to configure the spi device. (Default: * @note The SPIDEV option should to work with most Linux systems supporting SPIDEV. <br>
* "/dev/spidev1.0";; ) * Users may need to edit the RF24/arch/BBB/spi.cpp file to configure the spi device. (Defaults: "/dev/spidev1.0"; or "/dev/spidev1.1"; )
* *
* <br> * <br>
* @section AutoInstall Automated Install * @section AutoInstall Automated Install
...@@ -1556,8 +1557,8 @@ private: ...@@ -1556,8 +1557,8 @@ private:
* 3. Change to the new RF24 directory * 3. Change to the new RF24 directory
* @code cd RF24 @endcode * @code cd RF24 @endcode
* 4. Build the library, and run an example file: * 4. Build the library, and run an example file:
* @code sudo make install OR sudo make install RF24_MRAA=1 @endcode
* **Note:** See the <a href="http://iotdk.intel.com/docs/master/mraa/index.html">MRAA </a> documentation for more info on installing MRAA * **Note:** See the <a href="http://iotdk.intel.com/docs/master/mraa/index.html">MRAA </a> documentation for more info on installing MRAA
* @code sudo make install OR sudo make install RF24_MRAA=1 @endcode
* @code * @code
* cd examples_RPi * cd examples_RPi
* @endcode * @endcode
...@@ -1571,10 +1572,12 @@ private: ...@@ -1571,10 +1572,12 @@ private:
* *
* @page MRAA MRAA * @page MRAA MRAA
* *
* MRAA is a Low Level Skeleton Library for Communication on GNU/Linux platforms * MRAA is a Low Level Skeleton Library for Communication on GNU/Linux platforms <br>
*
* See http://iotdk.intel.com/docs/master/mraa/index.html for more information * See http://iotdk.intel.com/docs/master/mraa/index.html for more information
* *
* RF24 supports all MRAA supported platforms, but might not be tested on each individual platform due to the wide range of hardware support:<br>
* <a href="https://github.com/TMRh20/RF24/issues">Report an RF24 bug or issue </a>
*
* @section Setup Setup * @section Setup Setup
* 1. Install the MRAA lib * 1. Install the MRAA lib
* 2. As per your device, SPI may need to be enabled * 2. As per your device, SPI may need to be enabled
...@@ -1618,8 +1621,8 @@ private: ...@@ -1618,8 +1621,8 @@ private:
* *
* If SPI is not already enabled, load it on boot: * If SPI is not already enabled, load it on boot:
* @code sudo raspi-config @endcode * @code sudo raspi-config @endcode
* A. Update the tool via the menu as required * A. Update the tool via the menu as required<br>
* B. Select Advanced and enable the SPI kernel module * B. Select **Advanced** and **enable the SPI kernel module** <br>
* C. Update other software and libraries: * C. Update other software and libraries:
* @code sudo apt-get update @endcode * @code sudo apt-get update @endcode
* @code sudo apt-get upgrade @endcode * @code sudo apt-get upgrade @endcode
...@@ -1709,6 +1712,12 @@ private: ...@@ -1709,6 +1712,12 @@ private:
* RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ); * RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
* or * or
* RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_26, BCM2835_SPI_SPEED_8MHZ); * RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_26, BCM2835_SPI_SPEED_8MHZ);
*
* General:
* RF24 radio(22,0);
* or
* RF24 radio(22,1);
*
* @endcode * @endcode
* See the gettingstarted example for an example of pin configuration * See the gettingstarted example for an example of pin configuration
* *
...@@ -1716,13 +1725,13 @@ private: ...@@ -1716,13 +1725,13 @@ private:
* <br><br> * <br><br>
* **MRAA Constructor:** * **MRAA Constructor:**
* *
* @code RF24 radio(15,24); @endcode * @code RF24 radio(15,0); @endcode
* *
* See http://iotdk.intel.com/docs/master/mraa/rasppi.html * See http://iotdk.intel.com/docs/master/mraa/rasppi.html
* <br><br> * <br><br>
* **SPI_DEV Constructor** * **SPI_DEV Constructor**
* *
* @code RF24 radio(22,8); @endcode * @code RF24 radio(22,0); @endcode
* *
* See http://pi.gadgetoid.com/pinout * See http://pi.gadgetoid.com/pinout
* *
......
...@@ -14,6 +14,9 @@ ...@@ -14,6 +14,9 @@
#include "bcm2835.h" #include "bcm2835.h"
#include "spi.h" #include "spi.h"
#define _SPI spi #define _SPI spi
#define RF24_BIT_ORDER BCM2835_SPI_BIT_ORDER_MSBFIRST
#define RF24_DATA_MODE BCM2835_SPI_MODE0
#define RF24_CLOCK_DIVIDER BCM2835_SPI_SPEED_8MHZ
// GCC a Arduino Missing // GCC a Arduino Missing
#define _BV(x) (1<<(x)) #define _BV(x) (1<<(x))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment