Skip to content

Commit

Permalink
Makefile and project structure improvements
Browse files Browse the repository at this point in the history
- BBB renamed to SPIDEV
- SPIDEV - support for spidev device specification by RF24 constructor
- LittleWire support unified with other supported linux devices
- configure script introduced for auto detection/configuration
- support for cross compilation including library upload/installation to a remote host
- examples_RPi renamed to examples_linux
- Python wrapper - preparation for cross compiling support
  • Loading branch information
martin-mat committed Mar 24, 2016
1 parent 970309d commit 3b13827
Show file tree
Hide file tree
Showing 32 changed files with 394 additions and 129 deletions.
127 changes: 49 additions & 78 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,124 +1,95 @@
#############################################################################
#
# Makefile for librf24-bcm on Raspberry Pi
# Makefile for librf24
#
# License: GPL (General Public License)
# Author: Charles-Henri Hallard
# Date: 2013/03/13
#
# Description:
# ------------
# use make all and mak install to install the library
# You can change the install directory by editing the LIBDIR line
# use make all and make install to install the library
#
PREFIX=/usr/local

# Library parameters
# where to put the lib
LIBDIR=$(PREFIX)/lib
# lib name
LIB=librf24-bcm
# shared library name
LIBNAME=$(LIB).so.1.0
ifeq ($(wildcard Makefile.inc), )
$(error Configuration not found. Run ./configure first)
endif

# Where to put the header files
HEADER_DIR=${PREFIX}/include/RF24
include Makefile.inc

# The base location of support files for different devices
ARCH_DIR=utility

ARCH=armv6zk
ifeq "$(shell uname -m)" "armv7l"
ARCH=armv7-a
endif

# The default objects to compile
OBJECTS=RF24.o spi.o

SHARED_LINKER_FLAGS=-shared -Wl,-soname,$@.so.1



# Detect the Raspberry Pi from cpuinfo
# Allow users to override the use of BCM2835 driver and force use of SPIDEV by specifying " sudo make install -B RF24_SPIDEV=1 "
ifeq "$(RF24_SPIDEV)" "1"
RPI=0
else
#Count the matches for BCM2708 or BCM2709 in cpuinfo
RPI=$(shell cat /proc/cpuinfo | grep Hardware | grep -c BCM2708)
ifneq "${RPI}" "1"
RPI=$(shell cat /proc/cpuinfo | grep Hardware | grep -c BCM2709)
endif
# Objects to compile
OBJECTS=RF24.o
ifeq ($(DRIVER), MRAA)
OBJECTS+=spi.o gpio.o compatibility.o
else ifeq ($(DRIVER), RPi)
OBJECTS+=spi.o bcm2835.o interrupt.o
else ifeq ($(DRIVER), SPIDEV)
OBJECTS+=spi.o gpio.o compatibility.o
endif

ifeq "$(RF24_MRAA)" "1"
SHARED_LINKER_FLAGS+=-lmraa
DRIVER_DIR=$(ARCH_DIR)/MRAA
OBJECTS+=gpio.o compatibility.o

else ifeq "$(RPI)" "1"
DRIVER_DIR=$(ARCH_DIR)/RPi
OBJECTS+=bcm2835.o
OBJECTS+=interrupt.o
SHARED_LINKER_FLAGS+=-pthread
# The recommended compiler flags for the Raspberry Pi
CCFLAGS=-Ofast -mfpu=vfp -mfloat-abi=hard -march=$(ARCH) -mtune=arm1176jzf-s

else
DRIVER_DIR=$(ARCH_DIR)/BBB
OBJECTS+=gpio.o compatibility.o interrupt.o
SHARED_LINKER_FLAGS+=-pthread
endif


# make all
# reinstall the library after each recompilation
all: test librf24-bcm
all: $(LIBNAME)

test:
cp ${DRIVER_DIR}/includes.h $(ARCH_DIR)/includes.h
# Make the library
librf24-bcm: $(OBJECTS)
g++ ${SHARED_LINKER_FLAGS} ${CCFLAGS} -o ${LIBNAME} $^

$(LIBNAME): $(OBJECTS)
@echo "[Linking]"
${CC} ${SHARED_LINKER_FLAGS} ${CFLAGS} -o ${LIBNAME} $^

# Library parts
RF24.o: RF24.cpp
g++ -Wall -fPIC ${CCFLAGS} -c $^
${CXX} -Wall -fPIC ${CFLAGS} -c $^

bcm2835.o: $(DRIVER_DIR)/bcm2835.c
gcc -Wall -fPIC ${CCFLAGS} -c $^
${CC} -Wall -fPIC ${CFLAGS} -c $^

spi.o: $(DRIVER_DIR)/spi.cpp
g++ -Wall -fPIC ${CCFLAGS} -c $^
${CXX} -Wall -fPIC ${CFLAGS} -c $^

compatibility.o: $(DRIVER_DIR)/compatibility.c
gcc -Wall -fPIC ${CCFLAGS} -c $(DRIVER_DIR)/compatibility.c
${CC} -Wall -fPIC ${CFLAGS} -c $(DRIVER_DIR)/compatibility.c

gpio.o: $(DRIVER_DIR)/gpio.cpp
g++ -Wall -fPIC ${CCFLAGS} -c $(DRIVER_DIR)/gpio.cpp
${CXX} -Wall -fPIC ${CFLAGS} -c $(DRIVER_DIR)/gpio.cpp

interrupt.o: $(DRIVER_DIR)/interrupt.c
g++ -Wall -fPIC ${CCFLAGS} -c $(DRIVER_DIR)/interrupt.c
${CXX} -Wall -fPIC ${CFLAGS} -c $(DRIVER_DIR)/interrupt.c

# clear build files
clean:
rm -rf *.o ${LIB}.*
@echo "[Cleaning]"
rm -rf *.o ${LIBNAME}.*

install: all install-libs install-headers
upload: all upload-libs upload-headers

# Install the library to LIBPATH
install-libs:
install-libs:
@echo "[Installing Libs]"
@if ( test ! -d $(PREFIX)/lib ) ; then mkdir -p $(PREFIX)/lib ; fi
@install -m 0755 ${LIBNAME} ${LIBDIR}
@ln -sf ${LIBDIR}/${LIBNAME} ${LIBDIR}/${LIB}.so.1
@ln -sf ${LIBDIR}/${LIBNAME} ${LIBDIR}/${LIB}.so
@ldconfig
@install -m 0755 ${LIBNAME} ${LIB_DIR}
@ln -sf ${LIB_DIR}/${LIBNAME} ${LIB_DIR}/${LIBNAME}.1
# @${LDCONFIG}

upload-libs:
@echo "[Uploading Libs to ${REMOTE}]"
@ssh -q -t -p ${REMOTE_PORT} ${REMOTE} "mkdir -p ${REMOTE_LIB_DIR}"
@scp -q -P ${REMOTE_PORT} ${LIBNAME} ${REMOTE}:/tmp
@ssh -q -t -p ${REMOTE_PORT} ${REMOTE} "sudo install -m 0755 /tmp/${LIBNAME} ${REMOTE_LIB_DIR} && rm /tmp/${LIBNAME} && sudo ln -sf ${REMOTE_LIB_DIR}/${LIBNAME} ${REMOTE_LIB_DIR}/${LIBNAME}.1 && sudo ldconfig"

install-headers:
@echo "[Installing Headers]"
@if ( test ! -d ${HEADER_DIR} ) ; then mkdir -p ${HEADER_DIR} ; fi
@mkdir -p ${HEADER_DIR}/${DRIVER_DIR}
@install -m 0644 *.h ${HEADER_DIR}
@if ( test ! -d ${HEADER_DIR}/${DRIVER_DIR} ) ; then mkdir -p ${HEADER_DIR}/${DRIVER_DIR} ; fi
@install -m 0644 ${DRIVER_DIR}/*.h ${HEADER_DIR}/${DRIVER_DIR}
@install -m 0644 ${ARCH_DIR}/*.h ${HEADER_DIR}/${ARCH_DIR}

upload-headers:
@echo "[Uploading Headers to ${REMOTE}]"
@ssh -q -t -p ${REMOTE_PORT} ${REMOTE} "sudo mkdir -p ${REMOTE_HEADER_DIR}/${DRIVER_DIR}"
@ssh -q -t -p ${REMOTE_PORT} ${REMOTE} "mkdir -p /tmp/RF24 && rm -rf /tmp/RF24/*"
@rsync -a --include="*.h" --include="*/" --exclude="*" -e "ssh -p ${REMOTE_PORT}" . ${REMOTE}:/tmp/RF24
@ssh -q -t -p ${REMOTE_PORT} ${REMOTE} "sudo install -m 0644 /tmp/RF24/*.h ${REMOTE_HEADER_DIR}"
@ssh -q -t -p ${REMOTE_PORT} ${REMOTE} "sudo install -m 0644 /tmp/RF24/${DRIVER_DIR}/*.h ${REMOTE_HEADER_DIR}/${DRIVER_DIR}"
@ssh -q -t -p ${REMOTE_PORT} ${REMOTE} "sudo install -m 0644 /tmp/RF24/${ARCH_DIR}/*.h ${REMOTE_HEADER_DIR}/${ARCH_DIR}"
@ssh -q -t -p ${REMOTE_PORT} ${REMOTE} "rm -rf /tmp/RF24"
21 changes: 11 additions & 10 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,16 @@ bool RF24::begin(void)
gpio.begin(ce_pin,csn_pin);
#endif


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;
}
#ifdef RF24_RPi
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;
}
#endif

_SPI.begin(csn_pin);

Expand Down Expand Up @@ -936,7 +937,7 @@ void RF24::startWrite( const void* buf, uint8_t len, const bool multicast ){
//write_payload( buf, len );
write_payload( buf, len,multicast? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD ) ;
ce(HIGH);
#if defined(CORE_TEENSY) || !defined(ARDUINO) || defined (RF24_BBB) || defined (RF24_DUE)
#if defined(CORE_TEENSY) || !defined(ARDUINO) || defined (RF24_SPIDEV) || defined (RF24_DUE)
delayMicroseconds(10);
#endif
ce(LOW);
Expand Down
4 changes: 1 addition & 3 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

#include "RF24_config.h"

#if defined (RF24_LINUX)
#if defined (RF24_LINUX) || defined (LITTLEWIRE)
#include "utility/includes.h"
#elif LITTLEWIRE
#include <LittleWireSPI/LittleWireSPI.h>
#elif defined SOFTSPI
#include <DigitalIO.h>
#endif
Expand Down
8 changes: 2 additions & 6 deletions RF24_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
#endif

//Generic Linux/ARM and //http://iotdk.intel.com/docs/master/mraa/
#if ( defined (__linux) || defined (LINUX) ) && defined( __arm__ ) || defined(MRAA) // BeagleBone Black running GNU/Linux or any other ARM-based linux device
//#if ( defined (__linux) || defined (LINUX) ) && defined( __arm__ ) || defined(MRAA) // BeagleBone Black running GNU/Linux or any other ARM-based linux device
#if ( defined (__linux) || defined (LINUX) ) || defined(LITTLEWIRE) || defined(MRAA) // BeagleBone Black running GNU/Linux or any other ARM-based linux device

// The Makefile checks for bcm2835 (RPi) and copies the correct includes.h file to /utility/includes.h (Default is spidev config)
// This behavior can be overridden by calling 'make RF24_SPIDEV=1' or 'make RF24_MRAA=1'
Expand All @@ -42,11 +43,6 @@
#define RF24_TINY
#include "utility/ATTiny/RF24_arch_config.h"

//LittleWire
#elif defined(LITTLEWIRE)

#include "utility/LittleWire/RF24_arch_config.h"

//Teensy
#elif defined (TEENSYDUINO)

Expand Down
38 changes: 38 additions & 0 deletions RPi/pyRF24/crossunixccompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys
from distutils import unixccompiler
from distutils import ccompiler


def register():
sys.modules['distutils.crossunixccompiler'] = sys.modules[__name__]
ccompiler.compiler_class['crossunix'] = (__name__,
'CrossUnixCCompiler',
'UNIX-style compiler for cross compilation')


def try_remove_all(lst, starts):
lst[:] = [x for x in lst if not x.startswith(starts)]


class CrossUnixCCompiler(unixccompiler.UnixCCompiler):
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
try_remove_all(self.compiler_so, ('-m64', '-fstack-protector-strong', '-mtune=generic'))
try_remove_all(cc_args, '-I/usr')
try_remove_all(pp_opts, '-I/usr')
return unixccompiler.UnixCCompiler._compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)

def link(self, target_desc, objects,
output_filename, output_dir=None, libraries=None,
library_dirs=None, runtime_library_dirs=None,
export_symbols=None, debug=0, extra_preargs=None,
extra_postargs=None, build_temp=None, target_lang=None):
try_remove_all(self.library_dirs, ('/usr'))
return unixccompiler.UnixCCompiler.link(self, target_desc, objects, output_filename, output_dir, libraries,
library_dirs, runtime_library_dirs, export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang)

def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
self.__class__ = unixccompiler.UnixCCompiler
ret = unixccompiler.UnixCCompiler._fix_lib_args(self, libraries, library_dirs, runtime_library_dirs)
self.__class__ = CrossUnixCCompiler
return ret
35 changes: 27 additions & 8 deletions RPi/pyRF24/setup.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
#!/usr/bin/env python

from distutils.core import setup, Extension
import os
import sys
# import distutils.core
import setuptools
import crossunixccompiler


def process_configparams():
with open('../Makefile.inc') as f:
config_lines = f.readlines()

for line in config_lines:
identifier, value = line.split('=', 1)
os.environ[identifier] = value


if sys.version_info >= (3,):
BOOST_LIB = 'boost_python3'
else:
BOOST_LIB = 'boost_python'

module_RF24 = Extension('RF24',
libraries = ['rf24-bcm', BOOST_LIB],
sources = ['pyRF24.cpp'])
process_configparams()
crossunixccompiler.register()

# module_RF24 = distutils.core.Extension('RF24',
module_RF24 = setuptools.Extension('RF24',
libraries=['rf24', BOOST_LIB],
sources=['pyRF24.cpp']
)

setup(name='RF24',
version='1.1',
ext_modules=[module_RF24]
)
# distutils.core.setup(name='RF24',
setuptools.setup(name='RF24',
version='1.1',
ext_modules=[module_RF24]
)
Loading

0 comments on commit 3b13827

Please sign in to comment.