Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/Interrupts'
Browse files Browse the repository at this point in the history
  • Loading branch information
TMRh20 committed Jan 10, 2016
2 parents bf06db9 + d37d030 commit 3e8147a
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 90 deletions.
67 changes: 24 additions & 43 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ 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);
if(!mode)
_SPI.chipSelect(csn_pin);
#endif

#if !defined (RF24_LINUX)
Expand All @@ -71,7 +67,7 @@ void RF24::ce(bool level)

inline void RF24::beginTransaction() {
#if defined (RF24_SPI_TRANSACTIONS)
_SPI.beginTransaction(SPISettings(RF_SPI_SPEED, MSBFIRST, SPI_MODE0));
_SPI.beginTransaction(SPISettings(RF24_SPI_SPEED, MSBFIRST, SPI_MODE0));
#endif
csn(LOW);
}
Expand All @@ -92,7 +88,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 +103,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 +127,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 +157,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 +168,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 +191,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 +226,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 +240,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 +274,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 +294,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 +331,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 @@ -444,7 +434,7 @@ RF24::RF24(uint8_t _cepin, uint8_t _cspin, uint32_t _spi_speed):

void RF24::setChannel(uint8_t channel)
{
const uint8_t max_channel = 127;
const uint8_t max_channel = 125;
write_register(RF_CH,rf24_min(channel,max_channel));
}

Expand Down Expand Up @@ -1038,10 +1028,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 +1212,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 +1283,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 +1292,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
4 changes: 2 additions & 2 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class RF24

uint8_t ce_pin; /**< "Chip Enable" pin, activates the RX or TX role */
uint8_t csn_pin; /**< SPI Chip select */
#if defined (RF24_LINUX)
uint16_t spi_speed; /**< SPI Bus Speed */
#if defined (RF24_LINUX)
uint8_t spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes)
uint8_t spi_txbuff[32+1] ; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)
#endif
Expand Down Expand Up @@ -708,7 +708,7 @@ s *
/**
* Set RF communication channel
*
* @param channel Which RF channel to communicate on, 0-127
* @param channel Which RF channel to communicate on, 0-125
*/
void setChannel(uint8_t channel);

Expand Down
2 changes: 1 addition & 1 deletion RF24_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#include <Arduino.h>

// RF modules support 10 Mhz SPI bus speed
const uint32_t RF_SPI_SPEED = 10000000;
const uint32_t RF24_SPI_SPEED = 10000000;

#if defined (ARDUINO) && !defined (__arm__) && !defined (__ARDUINO_X86__)
#if defined SPI_UART
Expand Down
2 changes: 1 addition & 1 deletion examples/scanner/scanner.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ RF24 radio(7,8);
// Channel info
//

const uint8_t num_channels = 128;
const uint8_t num_channels = 126;
uint8_t values[num_channels];

//
Expand Down
3 changes: 1 addition & 2 deletions examples_RPi/extra/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
//
// Channel info
//
//const uint8_t num_channels = 128;
const uint8_t num_channels = 120;
const uint8_t num_channels = 126;
uint8_t values[num_channels];


Expand Down
8 changes: 4 additions & 4 deletions utility/RPi/RF24_arch_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +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
#if defined SPI_HAS_TRANSACTION && !defined SPI_UART && !defined SOFTSPI
#define RF24_SPI_TRANSACTIONS
#endif
// GCC a Arduino Missing
#define _BV(x) (1<<(x))
#define pgm_read_word(p) (*(p))
Expand Down
67 changes: 51 additions & 16 deletions utility/RPi/interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ 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 ;

pthread_t threadId [64];

// sysFds:
// Map a file descriptor from the /sys/class/gpio/gpioX/value

static int sysFds [64] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
Expand Down Expand Up @@ -97,6 +98,7 @@ void *interruptHandler (void *arg)
pthread_mutex_lock (&pinMutex) ;
isrFunctions [myPin] () ;
pthread_mutex_unlock (&pinMutex) ;
pthread_testcancel(); //Cancel at this point if we have an cancellation request.
}

return NULL ;
Expand All @@ -105,7 +107,6 @@ void *interruptHandler (void *arg)

int attachInterrupt (int pin, int mode, void (*function)(void))
{
pthread_t threadId ;
const char *modeS ;
char fName [64] ;
char pinS [8] ;
Expand Down Expand Up @@ -151,7 +152,7 @@ int attachInterrupt (int pin, int mode, void (*function)(void))

if (sysFds [bcmGpioPin] == -1)
{
sprintf (fName, "/sys/class/gpio/gpio%d/value",bcmGpioPin) ; //WRONG
sprintf (fName, "/sys/class/gpio/gpio%d/value",bcmGpioPin);
if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
return printf ("wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
}
Expand All @@ -164,28 +165,62 @@ int attachInterrupt (int pin, int mode, void (*function)(void))

pthread_mutex_lock (&pinMutex) ;
pinPass = pin ;
pthread_create (&threadId, NULL, interruptHandler, NULL) ;
pthread_create (&threadId[bcmGpioPin], NULL, interruptHandler, NULL) ;
while (pinPass != -1)
delay (1) ;
pthread_mutex_unlock (&pinMutex) ;

return 0 ;
}

void rfNoInterrupts(){
pthread_mutex_lock (&pinMutex) ;
}
int detachInterrupt (int pin)
{
char pinS [8];
const char *modeS = "none";
pid_t pid ;

if (!pthread_cancel(threadId[pin])) //Cancel the thread
{
return 0;
}

if (!close(sysFds[pin])) //Close filehandle
{
return 0;
}

/* Set wiringPi to 'none' interrupt mode */

sprintf (pinS, "%d", pin) ;

void rfInterrupts(){
pthread_mutex_unlock (&pinMutex) ;
}
if ((pid = fork ()) < 0) // Fail
return printf("wiringPiISR: fork failed: %s\n", strerror (errno)) ;

void spiNoInterrupts(){
pthread_mutex_lock (&spiMutex) ;
if (pid == 0) // Child, exec
{
/**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
{
execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
return printf ("wiringPiISR: execl failed: %s\n", strerror (errno)) ;
}
else if (access ("/usr/bin/gpio", X_OK) == 0)
{
execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
return printf ("wiringPiISR: execl failed: %s\n", strerror (errno)) ;
}
else
return printf ("wiringPiISR: Can't find gpio program\n") ;
}
else // Parent, wait
wait (NULL) ;

return 1;
}

void spiInterrupts(){
pthread_mutex_unlock (&spiMutex) ;
void rfNoInterrupts(){
pthread_mutex_lock (&pinMutex) ;
}


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

1 comment on commit 3e8147a

@TMRh20
Copy link
Member Author

@TMRh20 TMRh20 commented on 3e8147a Jan 10, 2016

Choose a reason for hiding this comment

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

Merge interrupt branch after testing re #172 #173

Please sign in to comment.