Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Updates'
Browse files Browse the repository at this point in the history
  • Loading branch information
TMRh20 committed Oct 6, 2014
2 parents 63b672c + 3e6ca45 commit f8791b1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 67 deletions.
128 changes: 65 additions & 63 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ void RF24::csn(bool mode)
// divider of 4 is the minimum we want.
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
#ifdef ARDUINO
#if ( !defined(RF24_TINY) && !defined (__arm__) ) || defined (CORE_TEENSY)
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV2);
#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

Expand All @@ -38,7 +38,7 @@ void RF24::csn(bool mode)
}
else {
PORTB &= ~(1<<PINB2); // SCK->CSN LOW
delayMicroseconds(20); // allow csn to settle
delayMicroseconds(11); // allow csn to settle
}
}
#else if !defined (__arm__) || defined (CORE_TEENSY)
Expand All @@ -62,17 +62,17 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
uint8_t status;

#if defined (__arm__) && !defined ( CORE_TEENSY )
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 ){
*buf++ = SPI.transfer(csn_pin,0xff, SPI_CONTINUE);
*buf++ = _SPI.transfer(csn_pin,0xff, SPI_CONTINUE);
}
*buf++ = SPI.transfer(csn_pin,0xff);
*buf++ = _SPI.transfer(csn_pin,0xff);

#else
csn(LOW);
status = SPI.transfer( R_REGISTER | ( REGISTER_MASK & reg ) );
status = _SPI.transfer( R_REGISTER | ( REGISTER_MASK & reg ) );
while ( len-- ){
*buf++ = SPI.transfer(0xff);
*buf++ = _SPI.transfer(0xff);
}
csn(HIGH);

Expand All @@ -87,12 +87,12 @@ uint8_t RF24::read_register(uint8_t reg)
{

#if defined (__arm__) && !defined ( CORE_TEENSY )
SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ) , SPI_CONTINUE);
uint8_t result = SPI.transfer(csn_pin,0xff);
_SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ) , SPI_CONTINUE);
uint8_t result = _SPI.transfer(csn_pin,0xff);
#else
csn(LOW);
SPI.transfer( R_REGISTER | ( REGISTER_MASK & reg ) );
uint8_t result = SPI.transfer(0xff);
_SPI.transfer( R_REGISTER | ( REGISTER_MASK & reg ) );
uint8_t result = _SPI.transfer(0xff);

csn(HIGH);
#endif
Expand All @@ -107,17 +107,17 @@ uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
uint8_t status;

#if defined (__arm__) && !defined ( CORE_TEENSY )
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){
SPI.transfer(csn_pin,*buf++, SPI_CONTINUE);
_SPI.transfer(csn_pin,*buf++, SPI_CONTINUE);
}
SPI.transfer(csn_pin,*buf++);
_SPI.transfer(csn_pin,*buf++);
#else

csn(LOW);
status = SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
status = _SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
while ( len-- )
SPI.transfer(*buf++);
_SPI.transfer(*buf++);

csn(HIGH);

Expand All @@ -135,13 +135,13 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value)
IF_SERIAL_DEBUG(printf_P(PSTR("write_register(%02x,%02x)\r\n"),reg,value));

#if defined (__arm__) && !defined ( CORE_TEENSY )
status = SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE);
SPI.transfer(csn_pin,value);
status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE);
_SPI.transfer(csn_pin,value);
#else

csn(LOW);
status = SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
SPI.transfer(value);
status = _SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
_SPI.transfer(value);
csn(HIGH);

#endif
Expand All @@ -163,32 +163,32 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri

#if defined (__arm__) && !defined ( CORE_TEENSY )

status = SPI.transfer(csn_pin, writeType , SPI_CONTINUE);
status = _SPI.transfer(csn_pin, writeType , SPI_CONTINUE);

if(blank_len){
while ( data_len--){
SPI.transfer(csn_pin,*current++, SPI_CONTINUE);
_SPI.transfer(csn_pin,*current++, SPI_CONTINUE);
}
while ( --blank_len ){
SPI.transfer(csn_pin,0, SPI_CONTINUE);
_SPI.transfer(csn_pin,0, SPI_CONTINUE);
}
SPI.transfer(csn_pin,0);
_SPI.transfer(csn_pin,0);
}else{
while( --data_len ){
SPI.transfer(csn_pin,*current++, SPI_CONTINUE);
_SPI.transfer(csn_pin,*current++, SPI_CONTINUE);
}
SPI.transfer(csn_pin,*current);
_SPI.transfer(csn_pin,*current);
}

#else

csn(LOW);
status = SPI.transfer( writeType );
status = _SPI.transfer( writeType );
while ( data_len-- ) {
SPI.transfer(*current++);
_SPI.transfer(*current++);
}
while ( blank_len-- ) {
SPI.transfer(0);
_SPI.transfer(0);
}
csn(HIGH);

Expand All @@ -212,33 +212,33 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len)

#if defined (__arm__) && !defined ( CORE_TEENSY )

status = SPI.transfer(csn_pin, R_RX_PAYLOAD, SPI_CONTINUE );
status = _SPI.transfer(csn_pin, R_RX_PAYLOAD, SPI_CONTINUE );

if( blank_len ){
while ( data_len-- ){
*current++ = SPI.transfer(csn_pin,0xFF, SPI_CONTINUE);
*current++ = _SPI.transfer(csn_pin,0xFF, SPI_CONTINUE);
}

while ( --blank_len ){
SPI.transfer(csn_pin,0xFF, SPI_CONTINUE);
_SPI.transfer(csn_pin,0xFF, SPI_CONTINUE);
}
SPI.transfer(csn_pin,0xFF);
_SPI.transfer(csn_pin,0xFF);
}else{
while ( --data_len ){
*current++ = SPI.transfer(csn_pin,0xFF, SPI_CONTINUE);
*current++ = _SPI.transfer(csn_pin,0xFF, SPI_CONTINUE);
}
*current = SPI.transfer(csn_pin,0xFF);
*current = _SPI.transfer(csn_pin,0xFF);
}

#else

csn(LOW);
status = SPI.transfer( R_RX_PAYLOAD );
status = _SPI.transfer( R_RX_PAYLOAD );
while ( data_len-- ) {
*current++ = SPI.transfer(0xFF);
*current++ = _SPI.transfer(0xFF);
}
while ( blank_len-- ) {
SPI.transfer(0xff);
_SPI.transfer(0xff);
}
csn(HIGH);

Expand Down Expand Up @@ -267,11 +267,11 @@ uint8_t RF24::spiTrans(uint8_t cmd){

uint8_t status;
#if defined (__arm__) && !defined ( CORE_TEENSY )
status = SPI.transfer(csn_pin, cmd );
status = _SPI.transfer(csn_pin, cmd );
#else

csn(LOW);
status = SPI.transfer( cmd );
status = _SPI.transfer( cmd );
csn(HIGH);
#endif
return status;
Expand Down Expand Up @@ -448,15 +448,15 @@ void RF24::begin(void)
if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT);

#if defined(__arm__) && ! defined( CORE_TEENSY )
SPI.begin(csn_pin); // Using the extended SPI features of the DUE
SPI.setClockDivider(csn_pin, 9); // Set the bus speed to 8.4mhz on Due
SPI.setBitOrder(csn_pin,MSBFIRST); // Set the bit order and mode specific to this device
SPI.setDataMode(csn_pin,SPI_MODE0);
_SPI.begin(csn_pin); // Using the extended SPI features of the DUE
_SPI.setClockDivider(csn_pin, 9); // Set the bus speed to 8.4mhz on Due
_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 (ce_pin != csn_pin) pinMode(csn_pin,OUTPUT);
SPI.begin();
_SPI.begin();
ce(LOW);
csn(HIGH);
#endif
Expand Down Expand Up @@ -836,12 +836,12 @@ uint8_t RF24::getDynamicPayloadSize(void)
uint8_t result = 0;

#if defined (__arm__) && ! defined( CORE_TEENSY )
SPI.transfer(csn_pin, R_RX_PL_WID, SPI_CONTINUE );
result = SPI.transfer(csn_pin,0xff);
_SPI.transfer(csn_pin, R_RX_PL_WID, SPI_CONTINUE );
result = _SPI.transfer(csn_pin,0xff);
#else
csn(LOW);
SPI.transfer( R_RX_PL_WID );
result = SPI.transfer(0xff);
_SPI.transfer( R_RX_PL_WID );
result = _SPI.transfer(0xff);
csn(HIGH);

#endif
Expand Down Expand Up @@ -1027,12 +1027,12 @@ void RF24::toggle_features(void)
{

#if defined (__arm__) && ! defined( CORE_TEENSY )
SPI.transfer(csn_pin, ACTIVATE, SPI_CONTINUE );
SPI.transfer(csn_pin, 0x73 );
_SPI.transfer(csn_pin, ACTIVATE, SPI_CONTINUE );
_SPI.transfer(csn_pin, 0x73 );
#else
csn(LOW);
SPI.transfer( ACTIVATE );
SPI.transfer( 0x73 );
_SPI.transfer( ACTIVATE );
_SPI.transfer( 0x73 );
csn(HIGH);
#endif
}
Expand Down Expand Up @@ -1102,18 +1102,18 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
uint8_t data_len = min(len,32);

#if defined (__arm__) && ! defined( CORE_TEENSY )
SPI.transfer(csn_pin, W_ACK_PAYLOAD | ( pipe & B111 ), SPI_CONTINUE);
_SPI.transfer(csn_pin, W_ACK_PAYLOAD | ( pipe & B111 ), SPI_CONTINUE);
while ( data_len-- > 1 ){
SPI.transfer(csn_pin,*current++, SPI_CONTINUE);
_SPI.transfer(csn_pin,*current++, SPI_CONTINUE);
}
SPI.transfer(csn_pin,*current++);
_SPI.transfer(csn_pin,*current++);

#else
csn(LOW);
SPI.transfer(W_ACK_PAYLOAD | ( pipe & B111 ) );
_SPI.transfer(W_ACK_PAYLOAD | ( pipe & B111 ) );

while ( data_len-- )
SPI.transfer(*current++);
_SPI.transfer(*current++);

csn(HIGH);

Expand Down Expand Up @@ -1292,9 +1292,11 @@ void RF24::setCRCLength(rf24_crclength_e length)
rf24_crclength_e RF24::getCRCLength(void)
{
rf24_crclength_e result = RF24_CRC_DISABLED;

uint8_t config = read_register(CONFIG) & ( _BV(CRCO) | _BV(EN_CRC)) ;

if ( config & _BV(EN_CRC ) )
uint8_t AA = read_register(EN_AA);

if ( config & _BV(EN_CRC ) || AA)
{
if ( config & _BV(CRCO) )
result = RF24_CRC_16;
Expand Down
12 changes: 10 additions & 2 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#define __RF24_H__

#include <RF24_config.h>

#if defined SOFTSPI
#include <DigitalIO.h>
#endif
/**
* Power Amplifier level.
*
Expand Down Expand Up @@ -45,6 +47,12 @@ typedef enum { RF24_CRC_DISABLED = 0, RF24_CRC_8, RF24_CRC_16 } rf24_crclength_e
class RF24
{
private:
#ifdef SOFTSPI
SoftSPI<SOFT_SPI_MISO_PIN, SOFT_SPI_MOSI_PIN, SOFT_SPI_SCK_PIN, SPI_MODE> spi;
#elif defined (SPI_UART)
SPIUARTClass uspi;
#endif

uint8_t ce_pin; /**< "Chip Enable" pin, activates the RX or TX role */
uint8_t csn_pin; /**< SPI Chip select */
bool p_variant; /* False for RF24L01 and true for RF24L01P */
Expand Down Expand Up @@ -223,7 +231,7 @@ class RF24
* @@return True if all three 32-byte radio buffers are full
*/
bool rxFifoFull();

/**
* Enter low-power mode
*
Expand Down
18 changes: 18 additions & 0 deletions RF24_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,32 @@
//#define FAILURE_HANDLING
//#define SERIAL_DEBUG
//#define MINIMAL
//#define SPI_UART
//#define SOFTSPI
/**********************/

// Define _BV for non-Arduino platforms and for Arduino DUE
#if defined (ARDUINO) && !defined (__arm__)
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
#define RF24_TINY
#define _SPI SPI
#else
#if defined SPI_UART
#include <SPI_UART.h>
#define _SPI uspi
#elif defined SOFTSPI
// change these pins to your liking
//
const uint8_t SOFT_SPI_MISO_PIN = 16;
const uint8_t SOFT_SPI_MOSI_PIN = 15;
const uint8_t SOFT_SPI_SCK_PIN = 14;
const uint8_t SPI_MODE = 0;
#define _SPI spi

#else
#include <SPI.h>
#define _SPI SPI
#endif
#endif
#else

Expand Down
5 changes: 3 additions & 2 deletions RPi/RF24/RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,9 @@ rf24_crclength_e RF24::getCRCLength(void)
{
rf24_crclength_e result = RF24_CRC_DISABLED;
uint8_t config = read_register(CONFIG) & ( _BV(CRCO) | _BV(EN_CRC)) ;

if ( config & _BV(EN_CRC ) )
uint8_t AA = read_register(EN_AA);

if ( config & _BV(EN_CRC ) || AA)
{
if ( config & _BV(CRCO) )
result = RF24_CRC_16;
Expand Down

1 comment on commit f8791b1

@TMRh20
Copy link
Member Author

@TMRh20 TMRh20 commented on f8791b1 Oct 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.