Commit 2eb660f6 authored by Anton Sabadash's avatar Anton Sabadash

Memory handling with MRAA device contexts, minor optimizations

parent e78893bf
......@@ -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) {
......@@ -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
......
......@@ -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"
......
......@@ -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>
......@@ -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
......@@ -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
#include "compatibility.h"
static struct timeval start, end;
//static long mtime, seconds, useconds;
/**********************************************************************/
/**
* This function is added in order to simulate arduino delay() function
......
......@@ -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();
......
......@@ -3,9 +3,9 @@
#define __RF24_INCLUDES_H__
#ifndef MRAA
#define MRAA
#define MRAA
#endif
#include "MRAA/RF24_arch_config.h"
#endif
\ No newline at end of file
#endif
#include "spi.h"
#include "mraa.h"
SPI::SPI() {
mspi = NULL;
......@@ -8,21 +9,15 @@ SPI::SPI() {
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() {
......@@ -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){
......
......@@ -22,8 +22,6 @@ public:
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);
......
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