Commit e78893bf authored by Anton Sabadash's avatar Anton Sabadash

Memory handling with MRAA device contexts

parent c0848cb1
......@@ -114,6 +114,9 @@ public:
RF24(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed );
//#endif
virtual ~RF24() {};
/**
* Begin operation of the chip
*
......
......@@ -4,7 +4,8 @@
#include "mraa.h"
#include "spi.h"
#include "gpio.h"
#include "compatibility.h"
//#include "compatibility.h"
#include <UtilTime.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
......@@ -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"
......@@ -44,9 +46,9 @@
#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()
//#define delay(milisec) __msleep(milisec)
//#define delayMicroseconds(usec) __usleep(usec)
//#define millis() __millis()
#define INPUT mraa::DIR_IN
#define OUTPUT mraa::DIR_OUT
......
......@@ -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,21 +3,32 @@
#include "spi.h"
SPI::SPI() {
mspi = NULL;
}
void SPI::begin( ) {
void SPI::begin(void) {
mspi = new mraa::Spi(0);
mspi->mode(mraa::SPI_MODE0);
mspi->bitPerWord(8);
mspi->frequency(8000000);
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);
}
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) {
......@@ -37,5 +48,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,9 @@ public:
inline void transfernb(char* tbuf, char* rbuf, uint32_t len);
inline void transfern(char* buf, uint32_t len);
void begin();
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