Commit 67d0285f authored by Oitzu's avatar Oitzu

Initial commit for interrupts based on wiringPi.

parent 13c069ca
...@@ -53,7 +53,8 @@ OBJECTS+=gpio.o compatibility.o ...@@ -53,7 +53,8 @@ OBJECTS+=gpio.o compatibility.o
else ifeq "$(RPI)" "1" else ifeq "$(RPI)" "1"
DRIVER_DIR=$(ARCH_DIR)/RPi DRIVER_DIR=$(ARCH_DIR)/RPi
OBJECTS+=bcm2835.o OBJECTS+=bcm2835.o
OBJECTS+=interrupt.o
# The recommended compiler flags for the Raspberry Pi # The recommended compiler flags for the Raspberry Pi
CCFLAGS=-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s CCFLAGS=-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s
...@@ -88,6 +89,9 @@ compatibility.o: $(DRIVER_DIR)/compatibility.c ...@@ -88,6 +89,9 @@ compatibility.o: $(DRIVER_DIR)/compatibility.c
gpio.o: $(DRIVER_DIR)/gpio.cpp gpio.o: $(DRIVER_DIR)/gpio.cpp
g++ -Wall -fPIC ${CCFLAGS} -c $(DRIVER_DIR)/gpio.cpp g++ -Wall -fPIC ${CCFLAGS} -c $(DRIVER_DIR)/gpio.cpp
interrupt.o: $(DRIVER_DIR)/interrupt.c
g++ -Wall -fPIC ${CCFLAGS} -pthread -c $(DRIVER_DIR)/interrupt.c
# clear build files # clear build files
clean: clean:
......
...@@ -23,13 +23,13 @@ endif ...@@ -23,13 +23,13 @@ endif
# define all programs # define all programs
#PROGRAMS = scanner pingtest gettingstarted #PROGRAMS = scanner pingtest gettingstarted
PROGRAMS = gettingstarted gettingstarted_call_response transfer pingpair_dyn PROGRAMS = gettingstarted gettingstarted_call_response transfer pingpair_dyn transfer_interrupt
SOURCES = ${PROGRAMS:=.cpp} SOURCES = ${PROGRAMS:=.cpp}
all: ${PROGRAMS} all: ${PROGRAMS}
${PROGRAMS}: ${SOURCES} ${PROGRAMS}: ${SOURCES}
g++ ${CCFLAGS} -Wall -I../ -lrf24-bcm $@.cpp -o $@ g++ ${CCFLAGS} -Wall -pthread -I../ -lrf24-bcm $@.cpp -o $@
clean: clean:
rm -rf $(PROGRAMS) rm -rf $(PROGRAMS)
......
/*
TMRh20 2014
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/** General Data Transfer Rate Test
* This example demonstrates basic data transfer functionality with the
updated library. This example will display the transfer rates acheived using
the slower form of high-speed transfer using blocking-writes.
*/
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>
#include <unistd.h>
using namespace std;
//
// Hardware configuration
//
/****************** Raspberry Pi ***********************/
// Radio CE Pin, CSN Pin, SPI Speed
// See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
// NEW: Setup for RPi B+
//RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
// Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
/*** RPi Alternate ***/
//Note: Specify SPI BUS 0 or 1 instead of CS pin number.
// See http://tmrh20.github.io/RF24/RPi.html for more information on usage
//RPi Alternate, with MRAA
//RF24 radio(15,0);
//RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
//RF24 radio(22,0);
/****************** Linux (BBB,x86,etc) ***********************/
// See http://tmrh20.github.io/RF24/pages.html for more information on usage
// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
// Setup for ARM(Linux) devices like BBB using spidev (default is "/dev/spidev1.0" )
//RF24 radio(115,0);
//BBB Alternate, with mraa
// CE pin = (Header P9, Pin 13) = 59 = 13 + 46
//Note: Specify SPI BUS 0 or 1 instead of CS pin number.
//RF24 radio(59,0);
/**************************************************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t addresses[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
uint8_t data[32];
unsigned long startTime, stopTime, counter, rxTimer=0;
void intHandler()
{
//Read as long data is available
//Single interrupts may be lost if a lot of data comes in.
while(radio.available())
{
radio.read(&data,32);
counter++;
}
}
int main(int argc, char** argv){
bool role_ping_out = 1, role_pong_back = 0;
bool role = 0;
// Print preamble:
cout << "RF24/examples/Transfer/\n";
radio.begin(); // Setup and configure rf radio
radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS);
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8); // Use 8-bit CRC for performance
radio.printDetails();
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit)\n>";
getline(cin,input);
attachInterrupt(23, INT_EDGE_FALLING, intHandler); //Attach interrupt to bcm pin 23
if(input.length() == 1) {
myChar = input[0];
if(myChar == '0'){
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
}else{ cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
if ( role == role_ping_out ) {
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
radio.stopListening();
} else {
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.startListening();
}
for(int i=0; i<32; i++){
data[i] = rand() % 255; //Load the buffer with random data
}
// forever loop
while (1)
{
if (role == role_ping_out){
sleep(2);
printf("Initiating Basic Data Transfer\n\r");
long int cycles = 10000; //Change this to a higher or lower number.
// unsigned long pauseTime = millis(); //Uncomment if autoAck == 1 ( NOACK )
startTime = millis();
for(int i=0; i<cycles; i++){ //Loop through a number of cycles
data[0] = i; //Change the first byte of the payload for identification
if(!radio.writeFast(&data,32)){ //Write to the FIFO buffers
counter++; //Keep count of failed payloads
}
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
/* if(millis() - pauseTime > 3){ // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
pauseTime = millis();
radio.txStandBy(); // This gives the PLL time to sync back up
}
*/
}
stopTime = millis();
if(!radio.txStandBy()){ counter+=3; }
float numBytes = cycles*32;
float rate = numBytes / (stopTime - startTime);
printf("Transfer complete at %.2f KB/s \n\r",rate);
printf("%lu of %lu Packets Failed to Send\n\r",counter,cycles);
counter = 0;
}
if(role == role_pong_back)
{
counter = 0;
delay(10000);
rxTimer = millis();
printf("Rate: ");
float numBytes = counter*32;
printf("%.2f KB/s \n\r",numBytes/100);
printf("Payload Count: %lu \n\r", counter);
}
} // loop
} // main
...@@ -5,6 +5,6 @@ ...@@ -5,6 +5,6 @@
#define RF24_RPi #define RF24_RPi
#include "RPi/bcm2835.h" #include "RPi/bcm2835.h"
#include "RPi/RF24_arch_config.h" #include "RPi/RF24_arch_config.h"
#include "RPi/interrupt.h"
#endif #endif
\ No newline at end of file
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <poll.h>
#include <sys/stat.h>
#include "interrupt.h"
#include <pthread.h>
#define delay(x) bcm2835_delay(x)
static pthread_mutex_t pinMutex ;
static volatile int pinPass = -1 ;
// 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,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
} ;
// ISR Data
static void (*isrFunctions [64])(void) ;
int waitForInterrupt (int pin, int mS)
{
int fd, x ;
uint8_t c ;
struct pollfd polls ;
if ((fd = sysFds [pin]) == -1)
return -2 ;
// Setup poll structure
polls.fd = fd ;
polls.events = POLLPRI ; // Urgent data!
// Wait for it ...
x = poll (&polls, 1, mS) ;
// Do a dummy read to clear the interrupt
// A one character read appars to be enough.
// Followed by a seek to reset it.
(void)read (fd, &c, 1) ;
lseek (fd, 0, SEEK_SET) ;
return x ;
}
int piHiPri (const int pri)
{
struct sched_param sched ;
memset (&sched, 0, sizeof(sched)) ;
if (pri > sched_get_priority_max (SCHED_RR))
sched.sched_priority = sched_get_priority_max (SCHED_RR) ;
else
sched.sched_priority = pri ;
return sched_setscheduler (0, SCHED_RR, &sched) ;
}
static void *interruptHandler (void *arg)
{
int myPin ;
(void)piHiPri (55) ; // Only effective if we run as root
myPin = pinPass ;
pinPass = -1 ;
for (;;)
if (waitForInterrupt (myPin, -1) > 0)
isrFunctions [myPin] () ;
return NULL ;
}
int attachInterrupt (int pin, int mode, void (*function)(void))
{
pthread_t threadId ;
const char *modeS ;
char fName [64] ;
char pinS [8] ;
pid_t pid ;
int count, i ;
char c ;
int bcmGpioPin ;
bcmGpioPin = pin ;
if (mode != INT_EDGE_SETUP)
{
/**/ if (mode == INT_EDGE_FALLING)
modeS = "falling" ;
else if (mode == INT_EDGE_RISING)
modeS = "rising" ;
else
modeS = "both" ;
sprintf (pinS, "%d", bcmGpioPin) ;
if ((pid = fork ()) < 0) // Fail
return printf("wiringPiISR: fork failed: %s\n", strerror (errno)) ;
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) ;
}
if (sysFds [bcmGpioPin] == -1)
{
sprintf (fName, "/sys/class/gpio/gpio%d/value",bcmGpioPin) ; //WRONG
if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
return printf ("wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
}
ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
for (i = 0 ; i < count ; ++i)
read (sysFds [bcmGpioPin], &c, 1) ;
isrFunctions [pin] = function ;
pthread_mutex_lock (&pinMutex) ;
pinPass = pin ;
pthread_create (&threadId, NULL, interruptHandler, NULL) ;
while (pinPass != -1)
delay (1) ;
pthread_mutex_unlock (&pinMutex) ;
return 0 ;
}
/*
Interrupts functions extruded from wiringPi library by Oitzu.
*/
#include "RF24_arch_config.h"
#define INT_EDGE_SETUP 0
#define INT_EDGE_FALLING 1
#define INT_EDGE_RISING 2
#define INT_EDGE_BOTH 3
static void *interruptHandler (void *arg);
#ifdef __cplusplus
extern "C" {
#endif
extern int waitForInterrupt (int pin, int mS);
extern int piHiPri (const int pri);
extern int attachInterrupt (int pin, int mode, void (*function)(void));
#ifdef __cplusplus
}
#endif
\ No newline at end of file
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