Commit e78893bf authored by Anton Sabadash's avatar Anton Sabadash

Memory handling with MRAA device contexts

parent c0848cb1
...@@ -114,6 +114,9 @@ public: ...@@ -114,6 +114,9 @@ public:
RF24(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed ); RF24(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed );
//#endif //#endif
virtual ~RF24() {};
/** /**
* Begin operation of the chip * Begin operation of the chip
* *
......
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
#include "mraa.h" #include "mraa.h"
#include "spi.h" #include "spi.h"
#include "gpio.h" #include "gpio.h"
#include "compatibility.h" //#include "compatibility.h"
#include <UtilTime.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
...@@ -30,6 +31,7 @@ ...@@ -30,6 +31,7 @@
//typedef uint16_t prog_uint16_t; //typedef uint16_t prog_uint16_t;
#define PSTR(x) (x) #define PSTR(x) (x)
#define printf_P printf #define printf_P printf
#define sprintf_P sprintf
#define strlen_P strlen #define strlen_P strlen
#define PROGMEM #define PROGMEM
#define PRIPSTR "%s" #define PRIPSTR "%s"
...@@ -44,9 +46,9 @@ ...@@ -44,9 +46,9 @@
#define digitalWrite(pin, value) gpio.write(pin, value) #define digitalWrite(pin, value) gpio.write(pin, value)
#define digitalRead(pin) GPIO::read(pin) #define digitalRead(pin) GPIO::read(pin)
#define pinMode(pin, direction) gpio.open(pin, direction) #define pinMode(pin, direction) gpio.open(pin, direction)
#define delay(milisec) __msleep(milisec) //#define delay(milisec) __msleep(milisec)
#define delayMicroseconds(usec) __usleep(usec) //#define delayMicroseconds(usec) __usleep(usec)
#define millis() __millis() //#define millis() __millis()
#define INPUT mraa::DIR_IN #define INPUT mraa::DIR_IN
#define OUTPUT mraa::DIR_OUT #define OUTPUT mraa::DIR_OUT
......
...@@ -6,9 +6,17 @@ ...@@ -6,9 +6,17 @@
#include "gpio.h" #include "gpio.h"
GPIO::GPIO() { GPIO::GPIO() {
// Prophet: basic members initialization
gpio_ce_pin = -1;
gpio_cs_pin = -1;
gpio_0 = NULL;
gpio_1 = NULL;
} }
GPIO::~GPIO() { 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) 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) ...@@ -16,9 +24,10 @@ void GPIO::begin(uint8_t ce_pin, uint8_t cs_pin)
gpio_ce_pin = ce_pin; gpio_ce_pin = ce_pin;
gpio_cs_pin = cs_pin; gpio_cs_pin = cs_pin;
gpio_0 = new mraa::Gpio(ce_pin,0); // Prophet: owner can be set here, because we use our pins exclusively, and are making mraa:Gpio context persistent
gpio_1 = new mraa::Gpio(cs_pin,0); // 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) void GPIO::open(int port, int DDR)
{ {
...@@ -36,8 +45,20 @@ void GPIO::open(int port, int DDR) ...@@ -36,8 +45,20 @@ void GPIO::open(int port, int DDR)
void GPIO::close(int port) void GPIO::close(int port)
{ {
// 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; delete gpio_0;
}
}
if(port == gpio_cs_pin) {
if (gpio_1 != NULL) {
delete gpio_1; delete gpio_1;
}
}
} }
int GPIO::read(int port) int GPIO::read(int port)
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
* *
*/ */
#ifndef H #ifndef RF24_ARCH_GPIO_H
#define H #define RF24_ARCH_GPIO_H
#include <cstdio> #include <cstdio>
#include <stdio.h> #include <stdio.h>
...@@ -17,6 +17,7 @@ public: ...@@ -17,6 +17,7 @@ public:
/* Constants */ /* Constants */
GPIO(); GPIO();
virtual ~GPIO();
/** /**
* Sets up GPIO on the CE & CS pins * Sets up GPIO on the CE & CS pins
...@@ -49,8 +50,6 @@ public: ...@@ -49,8 +50,6 @@ public:
*/ */
void write(int port,int value); void write(int port,int value);
virtual ~GPIO();
private: private:
int gpio_ce_pin; /** ce_pin value of the RF24 device **/ int gpio_ce_pin; /** ce_pin value of the RF24 device **/
int gpio_cs_pin; /** cs_pin value of the RF24 device **/ int gpio_cs_pin; /** cs_pin value of the RF24 device **/
...@@ -58,5 +57,5 @@ private: ...@@ -58,5 +57,5 @@ private:
mraa::Gpio* gpio_1; /** gpio object for cs_pin **/ mraa::Gpio* gpio_1; /** gpio object for cs_pin **/
}; };
#endif /* H */ #endif /* RF24_ARCH_GPIO_H */
...@@ -3,20 +3,31 @@ ...@@ -3,20 +3,31 @@
#include "spi.h" #include "spi.h"
SPI::SPI() { SPI::SPI() {
mspi = NULL;
} }
void SPI::begin( ) { void SPI::begin(void) {
mspi = new mraa::Spi(0); mspi = new mraa::Spi(0);
mspi->mode(mraa::SPI_MODE0); mspi->mode(mraa::SPI_MODE0);
mspi->bitPerWord(8); 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() { void SPI::end() {
// Prophet: we should check for existence of mspi before deleting it
if (mspi != NULL)
delete mspi; delete mspi;
} }
...@@ -37,5 +48,6 @@ void SPI::chipSelect(int csn_pin){ ...@@ -37,5 +48,6 @@ void SPI::chipSelect(int csn_pin){
} }
SPI::~SPI() { SPI::~SPI() {
// Prophet: we should call end here to free used memory and unexport SPI interface
this->end();
} }
...@@ -21,7 +21,9 @@ public: ...@@ -21,7 +21,9 @@ public:
inline void transfernb(char* tbuf, char* rbuf, uint32_t len); inline void transfernb(char* tbuf, char* rbuf, uint32_t len);
inline void transfern(char* buf, 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 end();
void setBitOrder(uint8_t bit_order); 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