Hello all,
virtmem is an Arduino (and Teensyduino!) library that makes it easy to extend the limited RAM available on microcontrollers with 'virtual memory'. To do so, virtmem provides a simple interface to use external memory sources such as an SD card or SPI RAM chip which resembles working with 'regular' memory.
Some of its main features are:
- Extend the available memory with kilobytes, megabytes or even gigabytes
- Supports SPI RAM (23LC series from Microchip), SD cards and RAM from a computer connected through serial
- Easy C++ interface that closely resembles regular data access
- Memory page system to speed up access to virtual memory
- New memory interfaces can be added easily
And a small demonstration that uses an SD card as memory source:
Code:
#include <Arduino.h>
#include <SdFat.h>
#include <virtmem.h>
#include <alloc/sd_alloc.h>
// Simplify virtmem usage
using namespace virtmem;
// Create virtual a memory allocator that uses SD card (with FAT filesystem) as virtual memory pool
// The default memory pool size (1 MB) is used.
SDVAlloc valloc;
SdFat sd;
struct MyStruct { int x, y; };
void setup()
{
// Initialize SdFatlib
if (!sd.begin(9, SPI_FULL_SPEED))
sd.initErrorHalt();
valloc.start(); // Always call this to initialize the allocator before using it
// Allocate a char buffer of 10000 bytes in virtual memory and store the address to a virtual pointer
VPtr<char, SDVAlloc> str = valloc.alloc<char>(10000);
// Set the first 1000 bytes to 'A'
memset(str, 'A', 1000);
// array access
str[1] = 'B';
// Own types (structs/classes) also work.
VPtr<MyStruct, SDVAlloc> ms = valloc.alloc<MyStruct>(); // alloc call without parameters: use automatic size deduction
ms->x = 5;
ms->y = 15;
}
void loop()
{
// ...
}
The project is hosted on github: https://github.com/rhelmus/virtmem
A detailed manual with more info can be found here: http://rhelmus.github.io/virtmem/index.html
Most of the development was done on Teensy 3.X boards. This is the first release, but already pretty feature complete. Nevertheless, any feedback (questions, suggestions, bugs etc) is welcome!