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 @@
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);
_SPI.setDataMode(BCM2835_SPI_MODE0);
_SPI.setClockDivider(spi_speed);
_SPI.chipSelect(csn_pin);
delayMicroseconds(5);
}
#elif defined (RF24_TINY)
#if defined (RF24_TINY)
if (ce_pin != csn_pin) {
digitalWrite(csn_pin,mode);
}
......@@ -48,8 +28,31 @@ void RF24::csn(bool mode)
PORTB &= ~(1<<PINB2); // SCK->CSN LOW
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);
delayMicroseconds(5);
#endif
......@@ -71,7 +74,7 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
uint8_t status;
#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 * ptx = spi_txbuff;
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)
// decrement before to skip status byte
while ( --size ){ *buf++ = *prx++; }
csn(HIGH);
#elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE );
while ( len-- > 1 ){
......@@ -124,7 +127,7 @@ uint8_t RF24::read_register(uint8_t reg)
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
result = *++prx; // result is 2nd byte of receive buffer
csn(HIGH);
#elif defined (RF24_DUE)
_SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ) , SPI_CONTINUE);
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)
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer
csn(HIGH);
#elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE );
while ( --len){
......@@ -196,7 +199,7 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value)
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
status = *prx++; // status is 1st byte of receive buffer
csn(HIGH);
#elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE);
_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
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer
csn(HIGH);
#elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, writeType , SPI_CONTINUE);
......@@ -312,7 +315,7 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len)
*current++ = *prx++;
*current = *prx;
csn(HIGH);
#elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, R_RX_PAYLOAD, SPI_CONTINUE );
......@@ -372,7 +375,6 @@ uint8_t RF24::spiTrans(uint8_t cmd){
#if defined (RF24_LINUX)
csn(LOW);
status = _SPI.transfer( cmd );
csn(HIGH);
#elif defined (RF24_DUE)
status = _SPI.transfer(csn_pin, cmd );
#else
......@@ -578,7 +580,7 @@ void RF24::printDetails(void)
case BCM2835_SPI_SPEED_32KHZ : printf("32 KHz"); break ;
case BCM2835_SPI_SPEED_16KHZ : printf("16 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");
......@@ -631,6 +633,7 @@ void RF24::begin(void)
switch(csn_pin){ //Ensure valid hardware CS pin
case 0: break;
case 1: break;
// Allow BCM2835 enums for RPi
case 8: csn_pin = 0; break;
case 7: csn_pin = 1; break;
default: csn_pin = 0; break;
......@@ -657,7 +660,6 @@ void RF24::begin(void)
_SPI.setBitOrder(csn_pin,MSBFIRST); // Set the bit order and mode specific to this device
_SPI.setDataMode(csn_pin,SPI_MODE0);
ce(LOW);
//csn(HIGH);
#else
#if ! defined(LITTLEWIRE)
if (ce_pin != csn_pin)
......
......@@ -102,7 +102,7 @@ public:
//#if defined (RF24_LINUX)
/**
* Raspberry Pi Constructor
* Optional Raspberry Pi Constructor
*
* 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.
......@@ -115,7 +115,9 @@ public:
RF24(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed );
//#endif
#if !defined (RF24_TINY)
virtual ~RF24() {};
#endif
/**
* Begin operation of the chip
......@@ -1307,8 +1309,10 @@ private:
* @section News News
*
* **Feb 2015**<br>
* - MRAA supported added ( Galileo, Edison, etc)
* - BBB/Linux support via spidev & MRAA
* - <a href="MRAA.html">MRAA</a> support added ( Galileo, Edison, etc)
* - <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>
* - New: Intel Galileo now supported
......@@ -1515,13 +1519,10 @@ private:
*
* @page BBB BeagleBone Black
*
* BeagleBone Black is supported via MRAA or SPIDEV, with MRAA being the preferred choice due to performance.
*
* @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.
* BeagleBone Black is supported via MRAA or SPIDEV.
*
* @note With SPIDEV, Users may need to edit the RF24/arch/BBB/spi.cpp file to configure the spi device. (Default:
* "/dev/spidev1.0";; )
* @note The SPIDEV option should to work with most Linux systems supporting SPIDEV. <br>
* 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>
* @section AutoInstall Automated Install
......@@ -1556,25 +1557,27 @@ private:
* 3. Change to the new RF24 directory
* @code cd RF24 @endcode
* 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
* @code sudo make install OR sudo make install RF24_MRAA=1 @endcode
* @code
* cd examples_RPi
* @endcode
* Edit the gettingstarted example, to set your pin configuration
* @code nano gettingstarted.cpp
* make
* sudo ./gettingstarted
* make
* sudo ./gettingstarted
* @endcode
*
* <br><br>
*
* @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
*
* 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
* 1. Install the MRAA lib
* 2. As per your device, SPI may need to be enabled
......@@ -1618,8 +1621,8 @@ private:
*
* If SPI is not already enabled, load it on boot:
* @code sudo raspi-config @endcode
* A. Update the tool via the menu as required
* B. Select Advanced and enable the SPI kernel module
* A. Update the tool via the menu as required<br>
* B. Select **Advanced** and **enable the SPI kernel module** <br>
* C. Update other software and libraries:
* @code sudo apt-get update @endcode
* @code sudo apt-get upgrade @endcode
......@@ -1705,10 +1708,16 @@ private:
* or
* RF24 radio(RPI_V2_GPIO_P1_15,BCM2835_SPI_CS1, BCM2835_SPI_SPEED_8MHZ);
*
* RPi B+:
* RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
* or
* RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_26, BCM2835_SPI_SPEED_8MHZ);
* RPi B+:
* RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
* or
* 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
* See the gettingstarted example for an example of pin configuration
*
......@@ -1716,13 +1725,13 @@ private:
* <br><br>
* **MRAA Constructor:**
*
* @code RF24 radio(15,24); @endcode
* @code RF24 radio(15,0); @endcode
*
* See http://iotdk.intel.com/docs/master/mraa/rasppi.html
* <br><br>
* **SPI_DEV Constructor**
*
* @code RF24 radio(22,8); @endcode
* @code RF24 radio(22,0); @endcode
*
* See http://pi.gadgetoid.com/pinout
*
......
......@@ -14,7 +14,10 @@
#include "bcm2835.h"
#include "spi.h"
#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
#define _BV(x) (1<<(x))
#define pgm_read_word(p) (*(p))
......
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