Skip to content

Commit

Permalink
Memory handling with MRAA device contexts, minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
prophet-roshak committed Feb 18, 2015
1 parent e78893b commit 2eb660f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 44 deletions.
30 changes: 15 additions & 15 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@

void RF24::csn(bool mode)
{
// Minimum ideal SPI bus speed is 2x data rate
// If we assume 2Mbs data rate and 16Mhz clock, a
// divider of 4 is the minimum we want.
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
#ifdef ARDUINO
// Minimum ideal SPI bus speed is 2x data rate
// If we assume 2Mbs data rate and 16Mhz clock, a
// divider of 4 is the minimum we want.
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
#if defined(ARDUINO)
#if ( !defined(RF24_TINY) && !defined (__arm__) && !defined (SOFTSPI)) || defined (CORE_TEENSY)
_SPI.setBitOrder(MSBFIRST);
_SPI.setDataMode(SPI_MODE0);
_SPI.setClockDivider(SPI_CLOCK_DIV2);
_SPI.setBitOrder(MSBFIRST);
_SPI.setDataMode(SPI_MODE0);
_SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif
#endif

#if defined (RF24_RPi)
if(!mode){
if(!mode){

_SPI.setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
_SPI.setDataMode(BCM2835_SPI_MODE0);
_SPI.setClockDivider(spi_speed);
_SPI.chipSelect(csn_pin);
delayMicroseconds(5);
_SPI.setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
_SPI.setDataMode(BCM2835_SPI_MODE0);
_SPI.setClockDivider(spi_speed);
_SPI.chipSelect(csn_pin);
delayMicroseconds(5);
}
#elif defined (RF24_TINY)
if (ce_pin != csn_pin) {
Expand All @@ -52,7 +52,7 @@ void RF24::csn(bool mode)
#elif !defined (ARDUINO_SAM_DUE)
digitalWrite(csn_pin,mode);
#if !defined (RF24_BBB)
delayMicroseconds(5);
delayMicroseconds(5);
#endif
#endif

Expand Down
4 changes: 2 additions & 2 deletions RF24_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@


//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(RF24_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 /arch/includes.h (Default is spidev config)
// This behaviour can be overridden by calling 'make RF24_SPIDEV=1' or 'make RF24_MRAA=1'
// This behavior can be overridden by calling 'make RF24_SPIDEV=1' or 'make RF24_MRAA=1'
// The includes.h file defines either RF24_RPi, MRAA or RF24_BBB and includes the correct RF24_arch_config.h file
#include "arch/includes.h"

Expand Down
22 changes: 15 additions & 7 deletions arch/MRAA/RF24_arch_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "mraa.h"
#include "spi.h"
#include "gpio.h"
//#include "compatibility.h"
#include <UtilTime.h>
#include "compatibility.h"

#include <stdint.h>
#include <stdio.h>
#include <time.h>
Expand All @@ -16,7 +16,7 @@
#include <unistd.h>
#include <stdlib.h>

// #include <UtilTime.h> // Precompiled arduino x86 based utiltime for timing functions
//#include <UtilTime.h> // Precompiled arduino x86 based utiltime for timing functions

// GCC a Arduino Missing
#define HIGH 1
Expand All @@ -42,15 +42,23 @@
#define IF_SERIAL_DEBUG(x)
#endif

//#define digitalWrite(pin, value) mraa_gpio_write((mraa_gpio_context)pin, value)
#define digitalWrite(pin, value) gpio.write(pin, value)
#define digitalRead(pin) GPIO::read(pin)
#define pinMode(pin, direction) gpio.open(pin, direction)
//#define delay(milisec) __msleep(milisec)
//#define delayMicroseconds(usec) __usleep(usec)
//#define millis() __millis()

#ifndef __TIME_H__
// Prophet: Redefine time functions only if precompiled arduino time is not included
#define delay(milisec) __msleep(milisec)
#define delayMicroseconds(usec) __usleep(usec)
#define millis() __millis()
#endif

#define INPUT mraa::DIR_IN
#define OUTPUT mraa::DIR_OUT

// SPI defines for ARDUINO API
#define MSBFIRST 1
#define SPI_MODE0 mraa::SPI_MODE0
#define SPI_CLOCK_DIV2 8000000

#endif
4 changes: 4 additions & 0 deletions arch/MRAA/compatibility.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

#include "compatibility.h"

static struct timeval start, end;
//static long mtime, seconds, useconds;

/**********************************************************************/
/**
* This function is added in order to simulate arduino delay() function
Expand Down
3 changes: 0 additions & 3 deletions arch/MRAA/compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ extern "C" {
#include <unistd.h>
#include <sys/time.h>

static struct timeval start, end;
//static long mtime, seconds, useconds;

void __msleep(int milisec);
void __usleep(int milisec);
void __start_timer();
Expand Down
4 changes: 2 additions & 2 deletions arch/MRAA/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#define __RF24_INCLUDES_H__

#ifndef MRAA
#define MRAA
#define MRAA
#endif
#include "MRAA/RF24_arch_config.h"


#endif
#endif
24 changes: 11 additions & 13 deletions arch/MRAA/spi.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@


#include "spi.h"
#include "mraa.h"

SPI::SPI() {
mspi = NULL;
}


void SPI::begin(void) {
// Prophet: this is only a suggestion, but can update begin with SPI bus number for devices with multiple SPI ports,
// and then #define _SPI_BUS_NUMBER in config, so for non MRAA platforms it will state as SPI.beign(),
// while for MRAA ones it will go SPI.begin(0) or any other valid bus number

mspi = new mraa::Spi(0);

mspi->mode(mraa::SPI_MODE0);
mspi->bitPerWord(8);
mspi->frequency(4000000);
}

// Prophet: this is only a suggestion, but can be useful for devices with multiple SPI ports
void SPI::begin(int bus, int frequency) {
mspi = new mraa::Spi(bus);

mspi->mode(mraa::SPI_MODE0);
mspi->bitPerWord(8);
mspi->frequency(frequency);
mspi->frequency(8000000); // Prophet: this will try to set 8MHz, however MRAA will reset to max platform speed and syslog a message of it
}

void SPI::end() {
Expand All @@ -32,15 +27,18 @@ void SPI::end() {
}

void SPI::setBitOrder(uint8_t bit_order) {
mspi->bitPerWord(bit_order);
if (mspi != NULL)
mspi->lsbmode((mraa_boolean_t)bit_order); // Prophet: bit_order
}

void SPI::setDataMode(uint8_t data_mode) {
mspi->mode((mraa::Spi_Mode)data_mode);
if (mspi != NULL)
mspi->mode((mraa::Spi_Mode)data_mode);
}

void SPI::setClockDivider(uint32_t spi_speed) {
mspi->frequency(spi_speed);
if (mspi != NULL)
mspi->frequency(spi_speed);
}

void SPI::chipSelect(int csn_pin){
Expand Down
2 changes: 0 additions & 2 deletions arch/MRAA/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class SPI {
inline void transfern(char* buf, uint32_t len);

void begin(void);
// Prophet: A customized SPI::begin can be used with parameters defined as macro in RF24_arch_config.h
void begin(int bus, int frequency);
void end();

void setBitOrder(uint8_t bit_order);
Expand Down

0 comments on commit 2eb660f

Please sign in to comment.