virtmem
virtual memory library for Arduino
virtmem::SerialVAllocP< IOStream, Properties > Class Template Reference

Virtual memory allocator that uses external memory via a serial connection as memory pool. More...

#include <serial_alloc.h>

Inheritance diagram for virtmem::SerialVAllocP< IOStream, Properties >:
virtmem::VAlloc< Properties, SerialVAllocP< IOStream, Properties > > virtmem::BaseVAlloc

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 VAllocgetInstance (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...
 

Detailed Description

template<typename IOStream = typeof(Serial), typename Properties = DefaultAllocProperties>
class virtmem::SerialVAllocP< IOStream, Properties >

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:

SerialVAllocP<> valloc;
SerialVAlloc valloc2; // same as above, both use the default serial port (Serial)

If another serial port is desired, both the type and (a pointer to the) variable must be specified:

// Uses Serial1 with a default pool size and baudrate
SerialVAllocP<typeof(Serial1)> valloc(VIRTMEM_DEFAULT_POOLSIZE, 115200, &Serial1);

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:

SoftwareSerial sserial(10, 11);
SerialVAllocP<SoftwareSerial> valloc(SERIALRAM_POOLSIZE, 115200, &sserial);

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:

1 serial_host.py # uses default serial port and baudrate
2 python serial_host.py -p COM2 # uses COM2 (Windows) as serial port, with default baudrate
3 serial_host.py -p /dev/ttyACM2 -b 9600 # uses /dev/ttyACM2 (Linux) as serial port, with 9600 baudrate
4 serial_host.py -l /dev/pts/1 # use default serial settings and pass all traffic through /dev/pts/1

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:

SerialVAlloc valloc;
// ...
if (valloc.input.available())
{
char c = valloc.input.read();
// ...
}

Note that in the above example it is more efficient to use the availableAtLeast function. For more information see the serram_utils::SerialInput class.

Template Parameters
IOStreamThe type of serial class to use for communication. Default is the type of the Serial class.
PropertiesAllocator properties, see DefaultAllocProperties
See also
Using virtual memory (tutorial), SerialVAlloc
Examples:
serial_input.ino, and serial_simple.ino.

Constructor & Destructor Documentation

template<typename IOStream = typeof(Serial), typename Properties = DefaultAllocProperties>
virtmem::SerialVAllocP< IOStream, Properties >::SerialVAllocP ( VPtrSize  ps = VIRTMEM_DEFAULT_POOLSIZE,
uint32_t  baud = 115200,
IOStream *  s = &Serial 
)
inline

Constructs (but not initializes) the serial allocator.

Parameters
psThe size of the virtual memory pool.
baudThe baudrate.
sThe instance variable of the serial class that should be used (e.g. Serial, Serial1)
See also
setBaudRate and setPoolSize

Member Function Documentation

template<typename IOStream = typeof(Serial), typename Properties = DefaultAllocProperties>
uint32_t virtmem::SerialVAllocP< IOStream, Properties >::ping ( void  ) const
inline

Send a 'ping' to retrieve a response time. Useful for debugging.

Returns
Response time of serial script connected over serial, in microseconds.
template<typename IOStream = typeof(Serial), typename Properties = DefaultAllocProperties>
void virtmem::SerialVAllocP< IOStream, Properties >::setBaudRate ( uint32_t  baud)
inline

Sets the baud rate.

Parameters
baudThe baudrate for the serial connection.
Note
Only call this function when the allocator is not yet initialized (i.e. before calling start)

Member Data Documentation

template<typename IOStream = typeof(Serial), typename Properties = DefaultAllocProperties>
serram_utils::SerialInput<IOStream> virtmem::SerialVAllocP< IOStream, Properties >::input

Handles input of shared serial connections.

See also
serram_utils::SerialInput, SerialVAllocP

The documentation for this class was generated from the following file: