Skip to content

Commit

Permalink
Initial commit RPI-SPI-Transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oitzu committed Dec 20, 2015
1 parent bf06db9 commit e1fe438
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 73 deletions.
68 changes: 26 additions & 42 deletions RF24.cpp
Expand Up @@ -43,13 +43,6 @@ void RF24::csn(bool mode)
_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)
Expand All @@ -73,6 +66,9 @@ void RF24::ce(bool level)
#if defined (RF24_SPI_TRANSACTIONS)
_SPI.beginTransaction(SPISettings(RF_SPI_SPEED, MSBFIRST, SPI_MODE0));
#endif
#if defined (RF24_LINUX)
_SPI.beginTransaction(spi_speed ? spi_speed : RF24_CLOCK_DIVIDER, csn_pin);
#endif
csn(LOW);
}

Expand All @@ -83,6 +79,9 @@ void RF24::ce(bool level)
#if defined (RF24_SPI_TRANSACTIONS)
_SPI.endTransaction();
#endif
#if defined (RF24_LINUX)
_SPI.endTransaction();
#endif
}

/****************************************************************************/
Expand All @@ -92,7 +91,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 for RPi
beginTransaction(); //configures the spi settings for RPi, locks mutex and setting csn low
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
uint8_t size = len + 1; // Add register value to transmit buffer
Expand All @@ -107,6 +106,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++; }
endTransaction(); //unlocks mutex and setting csn high

#else

Expand All @@ -130,16 +130,17 @@ uint8_t RF24::read_register(uint8_t reg)

#if defined (RF24_LINUX)

csn(LOW);
beginTransaction();

uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
*ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) );
*ptx++ = NOP ; // Dummy operation, just for reading

_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

endTransaction();
#else

beginTransaction();
Expand All @@ -159,8 +160,7 @@ uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
uint8_t status;

#if defined (RF24_LINUX)

csn(LOW);
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
uint8_t size = len + 1; // Add register value to transmit buffer
Expand All @@ -171,7 +171,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

endTransaction();
#else

beginTransaction();
Expand All @@ -194,15 +194,15 @@ 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 (RF24_LINUX)
csn(LOW);
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
*ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) );
*ptx = value ;

_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
status = *prx++; // status is 1st byte of receive buffer

endTransaction();
#else

beginTransaction();
Expand All @@ -229,7 +229,7 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri
IF_SERIAL_DEBUG( printf("[Writing %u bytes %u blanks]\n",data_len,blank_len); );

#if defined (RF24_LINUX)
csn(LOW);
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
uint8_t size;
Expand All @@ -243,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

endTransaction();

#else

Expand Down Expand Up @@ -277,7 +277,7 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len)
IF_SERIAL_DEBUG( printf("[Reading %u bytes %u blanks]\n",data_len,blank_len); );

#if defined (RF24_LINUX)
csn(LOW);
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
uint8_t size;
Expand All @@ -297,7 +297,7 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len)
*current++ = *prx++;

*current = *prx;

endTransaction();
#else

beginTransaction();
Expand Down Expand Up @@ -334,18 +334,11 @@ uint8_t RF24::flush_tx(void)
uint8_t RF24::spiTrans(uint8_t cmd){

uint8_t status;
#if defined (RF24_LINUX)
csn(LOW);
status = _SPI.transfer( cmd );

#else


beginTransaction();
status = _SPI.transfer( cmd );
endTransaction();

#endif


return status;
}

Expand Down Expand Up @@ -1038,10 +1031,10 @@ uint8_t RF24::getDynamicPayloadSize(void)
#if defined (RF24_LINUX)
spi_txbuff[0] = R_RX_PL_WID;
spi_rxbuff[1] = 0xff;
csn(LOW);
beginTransaction();
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
result = spi_rxbuff[1];
csn(HIGH);
endTransaction();
#else
beginTransaction();
_SPI.transfer( R_RX_PL_WID );
Expand Down Expand Up @@ -1222,19 +1215,10 @@ void RF24::closeReadingPipe( uint8_t pipe )

void RF24::toggle_features(void)
{

#if defined (RF24_LINUX)
csn(LOW);
_SPI.transfer( ACTIVATE );
_SPI.transfer( 0x73 );
csn(HIGH);
#else
beginTransaction();
_SPI.transfer( ACTIVATE );
_SPI.transfer( 0x73 );
endTransaction();
#endif

}

/****************************************************************************/
Expand Down Expand Up @@ -1302,7 +1286,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
uint8_t data_len = rf24_min(len,32);

#if defined (RF24_LINUX)
csn(LOW);
beginTransaction();
uint8_t * ptx = spi_txbuff;
uint8_t size = data_len + 1 ; // Add register value to transmit buffer
*ptx++ = W_ACK_PAYLOAD | ( pipe & 0b111 );
Expand All @@ -1311,7 +1295,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
}

_SPI.transfern( (char *) spi_txbuff, size);
csn(HIGH);
endTransaction();
#else
beginTransaction();
_SPI.transfer(W_ACK_PAYLOAD | ( pipe & 0b111 ) );
Expand Down
13 changes: 1 addition & 12 deletions utility/RPi/interrupt.c
Expand Up @@ -24,7 +24,6 @@ see <http://www.gnu.org/licenses/>
#define delay(x) bcm2835_delay(x)

static pthread_mutex_t pinMutex ;
static pthread_mutex_t spiMutex ;
static volatile int pinPass = -1 ;
// sysFds:
// Map a file descriptor from the /sys/class/gpio/gpioX/value
Expand Down Expand Up @@ -178,14 +177,4 @@ void rfNoInterrupts(){

void rfInterrupts(){
pthread_mutex_unlock (&pinMutex) ;
}

void spiNoInterrupts(){
pthread_mutex_lock (&spiMutex) ;
}

void spiInterrupts(){
pthread_mutex_unlock (&spiMutex) ;
}


}
27 changes: 13 additions & 14 deletions utility/RPi/spi.cpp
@@ -1,49 +1,48 @@


#include "spi.h"
#include <pthread.h>

static pthread_mutex_t spiMutex;

SPI::SPI() {

}


void SPI::begin( int busNo ) {
spiNoInterrupts();
if (!bcm2835_init()){
spiInterrupts();
return;
}

bcm2835_spi_begin();
spiInterrupts();
}

void SPI::end() {
void SPI::beginTransaction(int clock_divider, uint8_t csn_pin) {
pthread_mutex_lock (&spiMutex);
setBitOrder(RF24_BIT_ORDER);
setDataMode(RF24_DATA_MODE);
setClockDivider(clock_divider);
chipSelect(csn_pin);
delayMicroseconds(5);
}

void SPI::endTransaction() {
pthread_mutex_unlock (&spiMutex);
}

void SPI::setBitOrder(uint8_t bit_order) {
spiNoInterrupts();
bcm2835_spi_setBitOrder(bit_order);
spiInterrupts();
}

void SPI::setDataMode(uint8_t data_mode) {
spiNoInterrupts();
bcm2835_spi_setDataMode(data_mode);
spiInterrupts();
}

void SPI::setClockDivider(uint16_t spi_speed) {
spiNoInterrupts();
bcm2835_spi_setClockDivider(spi_speed);
spiInterrupts();
}

void SPI::chipSelect(int csn_pin){
spiNoInterrupts();
bcm2835_spi_chipSelect(csn_pin);
spiInterrupts();
}

SPI::~SPI() {
Expand Down
9 changes: 4 additions & 5 deletions utility/RPi/spi.h
Expand Up @@ -32,20 +32,19 @@ class SPI {
static void setDataMode(uint8_t data_mode);
static void setClockDivider(uint16_t spi_speed);
static void chipSelect(int csn_pin);

static void beginTransaction(int clock_divider, uint8_t csn_pin);
static void endTransaction();
};


uint8_t SPI::transfer(uint8_t _data) {
spiNoInterrupts();
uint8_t data = bcm2835_spi_transfer(_data);
spiInterrupts();
uint8_t data = bcm2835_spi_transfer(_data);
return data;
}

void SPI::transfernb(char* tbuf, char* rbuf, uint32_t len){
spiNoInterrupts();
bcm2835_spi_transfernb( tbuf, rbuf, len);
spiInterrupts();
}

void SPI::transfern(char* buf, uint32_t len)
Expand Down

0 comments on commit e1fe438

Please sign in to comment.