Commit 225052a3 authored by TMRh20's avatar TMRh20

Merge pull request #76 from prophet-roshak/mraa

MRAA: Updated memory usage
parents 30ef8dc5 2eb660f6
......@@ -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
......
......@@ -114,6 +114,9 @@ public:
RF24(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed );
//#endif
virtual ~RF24() {};
/**
* Begin operation of the chip
*
......
......@@ -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"
......
......@@ -5,6 +5,7 @@
#include "spi.h"
#include "gpio.h"
#include "compatibility.h"
#include <stdint.h>
#include <stdio.h>
#include <time.h>
......@@ -15,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
......@@ -30,6 +31,7 @@
//typedef uint16_t prog_uint16_t;
#define PSTR(x) (x)
#define printf_P printf
#define sprintf_P sprintf
#define strlen_P strlen
#define PROGMEM
#define PRIPSTR "%s"
......@@ -40,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 <time.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();
......
......@@ -6,9 +6,17 @@
#include "gpio.h"
GPIO::GPIO() {
// Prophet: basic members initialization
gpio_ce_pin = -1;
gpio_cs_pin = -1;
gpio_0 = NULL;
gpio_1 = NULL;
}
GPIO::~GPIO() {
// Prophet: this should free memory, and unexport pins when RF24 and/or GPIO gets deleted or goes out of scope
this->close(gpio_ce_pin);
this->close(gpio_cs_pin);
}
void GPIO::begin(uint8_t ce_pin, uint8_t cs_pin)
......@@ -16,9 +24,10 @@ void GPIO::begin(uint8_t ce_pin, uint8_t cs_pin)
gpio_ce_pin = ce_pin;
gpio_cs_pin = cs_pin;
gpio_0 = new mraa::Gpio(ce_pin,0);
gpio_1 = new mraa::Gpio(cs_pin,0);
// Prophet: owner can be set here, because we use our pins exclusively, and are making mraa:Gpio context persistent
// so pins will be unexported only if close is called, or on destruction
gpio_0 = new mraa::Gpio(ce_pin/*,0*/);
gpio_1 = new mraa::Gpio(cs_pin/*,0*/);
}
void GPIO::open(int port, int DDR)
{
......@@ -36,8 +45,20 @@ void GPIO::open(int port, int DDR)
void GPIO::close(int port)
{
delete gpio_0;
delete gpio_1;
// Prophet: using same theme of working with port numbers as with GPIO::open,
// checking for mraa::Gpio context existence to be sure, that GPIO::begin was called
if(port == gpio_ce_pin)
{
if (gpio_0 != NULL) {
delete gpio_0;
}
}
if(port == gpio_cs_pin) {
if (gpio_1 != NULL) {
delete gpio_1;
}
}
}
int GPIO::read(int port)
......
......@@ -3,8 +3,8 @@
*
*/
#ifndef H
#define H
#ifndef RF24_ARCH_GPIO_H
#define RF24_ARCH_GPIO_H
#include <cstdio>
#include <stdio.h>
......@@ -16,7 +16,8 @@ public:
/* Constants */
GPIO();
GPIO();
virtual ~GPIO();
/**
* Sets up GPIO on the CE & CS pins
......@@ -48,9 +49,7 @@ public:
* @param value
*/
void write(int port,int value);
virtual ~GPIO();
private:
int gpio_ce_pin; /** ce_pin value of the RF24 device **/
int gpio_cs_pin; /** cs_pin value of the RF24 device **/
......@@ -58,5 +57,5 @@ private:
mraa::Gpio* gpio_1; /** gpio object for cs_pin **/
};
#endif /* H */
#endif /* RF24_ARCH_GPIO_H */
......@@ -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;
}
void SPI::begin( ) {
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(8000000);
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() {
delete mspi;
// Prophet: we should check for existence of mspi before deleting it
if (mspi != NULL)
delete mspi;
}
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){
......@@ -37,5 +46,6 @@ void SPI::chipSelect(int csn_pin){
}
SPI::~SPI() {
}
\ No newline at end of file
// Prophet: we should call end here to free used memory and unexport SPI interface
this->end();
}
......@@ -21,7 +21,7 @@ public:
inline void transfernb(char* tbuf, char* rbuf, uint32_t len);
inline void transfern(char* buf, uint32_t len);
void begin();
void begin(void);
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