virtmem
virtual memory library for Arduino
|
Virtual memory allocator that uses external memory via a serial connection as memory pool. More...
#include <serial_alloc.h>
Public Member Functions | |
SerialVAllocP (VPtrSize ps=VIRTMEM_DEFAULT_POOLSIZE, uint32_t baud=115200, IOStream *s=&Serial) | |
Constructs (but not initializes) the serial allocator. More... | |
void | setBaudRate (uint32_t baud) |
Sets the baud rate. More... | |
uint32_t | ping (void) const |
Send a 'ping' to retrieve a response time. Useful for debugging. More... | |
Public Member Functions inherited from virtmem::VAlloc< Properties, SerialVAllocP< IOStream, Properties > > | |
VPtr< T, SerialVAllocP< IOStream, Properties > > | alloc (VPtrSize size=sizeof(T)) |
Allocates memory from the linked virtual memory allocator. More... | |
void | free (VPtr< T, SerialVAllocP< IOStream, Properties > > &p) |
Frees a block of virtual memory. More... | |
VPtr< T, SerialVAllocP< IOStream, Properties > > | newClass (VPtrSize size=sizeof(T)) |
Allocates memory and constructs data type. More... | |
void | deleteClass (VPtr< T, SerialVAllocP< IOStream, Properties > > &p) |
Destructs data type and frees memory. More... | |
VPtr< T, SerialVAllocP< IOStream, Properties > > | newArray (VPtrSize elements) |
Allocates and constructs an array of objects. More... | |
void | deleteArray (VPtr< T, SerialVAllocP< IOStream, Properties > > &p) |
Destructs and frees an array of objects. More... | |
Public Member Functions inherited from virtmem::BaseVAlloc | |
void | start (void) |
Starts the allocator. More... | |
void | stop (void) |
Deinitializes the allocator. More... | |
void | setPoolSize (VPtrSize ps) |
Sets the total size of the memory pool. More... | |
VPtrNum | allocRaw (VPtrSize size) |
Allocates a piece of raw (virtual) memory. More... | |
void | freeRaw (VPtrNum ptr) |
Frees a memory block for re-usage. More... | |
void * | read (VPtrNum p, VPtrSize size) |
Reads a raw block of (virtual) memory. More... | |
void | write (VPtrNum p, const void *d, VPtrSize size) |
Writes a piece of raw data to (virtual) memory. More... | |
void | flush (void) |
Synchronizes all big memory pages. More... | |
void | clearPages (void) |
Synchronizes and clears all big memory pages. More... | |
uint8_t | getFreeBigPages (void) const |
uint8_t | getUnlockedSmallPages (void) const |
Returns amount of small pages which are not locked. | |
uint8_t | getUnlockedMediumPages (void) const |
Returns amount of medium pages which are not locked. | |
uint8_t | getUnlockedBigPages (void) const |
Returns amount of big pages which are not locked. | |
uint8_t | getSmallPageCount (void) const |
Returns total amount of small pages. | |
uint8_t | getMediumPageCount (void) const |
Returns total amount of medium pages. | |
uint8_t | getBigPageCount (void) const |
Returns total amount of big pages. | |
VirtPageSize | getSmallPageSize (void) const |
Returns the size of a small page. | |
VirtPageSize | getMediumPageSize (void) const |
Returns the size of a medium page. | |
VirtPageSize | getBigPageSize (void) const |
Returns the size of a big page. | |
VPtrSize | getPoolSize (void) const |
Returns the size the memory pool. More... | |
VPtrSize | getMemUsed (void) const |
Returns total memory used. | |
VPtrSize | getMaxMemUsed (void) const |
Returns the maximum memory used so far. | |
uint32_t | getBigPageReads (void) const |
Returns the times big pages were read (swapped). | |
uint32_t | getBigPageWrites (void) const |
Returns the times big pages written (synchronized). | |
uint32_t | getBytesRead (void) const |
Returns the amount of bytes read as a result of page swaps. | |
uint32_t | getBytesWritten (void) const |
Returns the amount of bytes written as a results of page swaps. | |
void | resetStats (void) |
Reset all statistics. Called by start() | |
Public Attributes | |
serram_utils::SerialInput< IOStream > | input |
Handles input of shared serial connections. More... | |
Additional Inherited Members | |
Public Types inherited from virtmem::VAlloc< Properties, SerialVAllocP< IOStream, Properties > > | |
using | VPtr = virtmem::VPtr< T, SerialVAllocP< IOStream, Properties > > |
Static Public Member Functions inherited from virtmem::VAlloc< Properties, SerialVAllocP< IOStream, Properties > > | |
static VAlloc * | getInstance (void) |
Returns a pointer to the instance of the class. More... | |
Protected Member Functions inherited from virtmem::BaseVAlloc | |
void | writeZeros (VPtrNum start, VPtrSize n) |
Writes zeros to raw virtual memory. Can be used to initialize the memory pool. More... | |
Virtual memory allocator that uses external memory via a serial connection as memory pool.
This class utilizes RAM from an external device (e.g. a PC or ARM board) that is connected with a serial port. This allocator is particularly useful when a permanant serial connection with such a device exists, for instance on an Arduino Yun or Udoo ARM board. Since this allocator requires no extra hardware (besides a serial connection), it is easy to test and a good starting point and generally useful for debugging. The device that provides the RAM (the RAM host) should run a script (serial_host.py
) that is responsible for communicating with virtmem
and memory management.
Choosing a serial port
By default, the allocator uses the default serial port (i.e. the Serial
class). In this case, the SerialVAlloc shortcut can also be used:
If another serial port is desired, both the type and (a pointer to the) variable must be specified:
The typeof
expression used in the above example may raise some questions. We need to know the type of the serial class to be used (in this case the class that defines Serial1). Since this not well standardized, we simply use the typeof
expression to retrieve it. Note that this is an extension of gcc
(the compiler used by Arduino and many other platforms), and that decltype
could also be used on C++11 systems. Note that this approach is quite generic, and as long as a class defines similar functions as the SerialX
classes, other types such as SoftwareSerial
can also be used:
Communication with the RAM host
A special Python script needs to be run on the RAM host (PC, ARM board etc.) that communicates with the Arduino code. This script allocates a buffer that is sufficient in size to act as RAM for the code running virtmem
. Both this script and SerialVAlloc::start will block and wait until a connection is established between the two.
The serial script is stored in virtmem/extras/serial_host.py
, and requires Python 3 as well as the pyserial module.
Running the script with the -h
(or --help
) option will display the available commandline parameters for configuration:
usage: serial_host.py [-h] [-p PORT] [-b BAUD] [-l PASSDEV] [-r PASSBAUD] optional arguments: -h, --help show this help message and exit -p PORT, --port PORT serial device connected to the arduino. Default: COM3 (Windows) or /dev/ttyACM0 (other OS) -b BAUD, --baud BAUD Serial baudrate. Default: 115200 -l PASSDEV, --pass PASSDEV serial pass through device -r PASSBAUD, --passbaud PASSBAUD baud rate of serial port pass through device. Default: 115200
The port (-p
or --port
option) should be set to the serial port connected to the MCU running virtmem
(e.g. /dev/ttyACM0, COM3 etc). The baudrate (-b
or --baud
option) should match the configured baudrate of the allocator (see SerialVAlloc::SerialVAllocP and SerialVAlloc::setBaudRate).
The serial_host.py
script allows to act as a serial bridge and can 'pass through' all communication to and from the MCU to another (virtual) serial port. By setting up a 'virtual serial port' (e.g. via socat or com0com) and connecting serial_host.py
to it, other software is still able to communicate with your MCU. The (virtual) serial port and its baudrate is set by the -l
(or --pass
) and -r
(or --passbaud
) options, respectively.
Some examples:
Once the script has started it will keep monitoring the serial port until exited manually (e.g. by pressing ctrl+C). Sending text can be done by simply writing the text and pressing enter.
Sharing serial ports with other code
Sometimes it may be desired to use the serial port used by SerialVAllocP
for other purposes. Even when a port is 'shared', simply writing to it is no problem and dealt with by serial_host.py
. For instance, when Serial.print()
is called (and Serial is bound to SerialVAllocP), the text is simply printed to the terminal running serial_host.py
.
Dealing with user input, however, is more tricky. Any text that is typed in the terminal running serial_host.py
is still sent to the MCU running virtmem
. To retrieve it, the SerialVAllocP::input class must be used:
Note that in the above example it is more efficient to use the availableAtLeast function. For more information see the serram_utils::SerialInput class.
IOStream | The type of serial class to use for communication. Default is the type of the Serial class. |
Properties | Allocator properties, see DefaultAllocProperties |
|
inline |
Constructs (but not initializes) the serial allocator.
ps | The size of the virtual memory pool. |
baud | The baudrate. |
s | The instance variable of the serial class that should be used (e.g. Serial, Serial1) |
|
inline |
Send a 'ping' to retrieve a response time. Useful for debugging.
|
inline |
Sets the baud rate.
baud | The baudrate for the serial connection. |
serram_utils::SerialInput<IOStream> virtmem::SerialVAllocP< IOStream, Properties >::input |
Handles input of shared serial connections.