chipKIT® Development Platform

Inspired by Arduino™

SPI RAM Library

Created Thu, 26 Sep 2013 00:44:00 +0000 by majenko


majenko

Thu, 26 Sep 2013 00:44:00 +0000

Here is a little library I have written for accessing SPI SRAM Devices. It's a pretty simplistic interface, but then interfacing to these chip is pretty simple anyway.

There's only really 3 methods, plus the constructor, but there are a couple of nice overload tricks there too.

Construct an SPIRAM object passing it a pointer to a DSPI object, a chip-select pin number, and the size of the RAM chip:

#include <DSPI.h>
#include <SPIRAM.h>

DSPI0 spi;
SPIRAM ram(&spi, 10, 131072);

Start it all off with the good ol' begin() function - this sets up the SPI too:

ram.begin();

Read and write the ram with the read and write functions. There is a straight byte-wise access, and it's also overloaded with block transfers:

ram.write(address, byte);
ram.write(address, byte *, length);
byte = ram.read(address);
ram.read(address, byte *, length);

Now the nice overloading trick... I have overloaded the [] operator. It's one-way at the moment, but you can read the memory as if it were a simple byte array:

byte = ram[address];

There are ways of making it bi-directional, but it gets kind of complex, with wrapper utility classes and such.

There's also a little memory testing sketch included in the examples so you can see it all in action.

Walking bit... 1... 2... 4... 8... 16... 32... 64... 128...pass
Checkerboard... 0x55AA... 0xAA55...pass
Random Values...pass
Write time (individual bytes)... 956ms, 137104.61B/s
Write time (1K blocks)... 113ms, 1159929.25B/s
Read time (individual bytes)... 942ms, 139142.25B/s
Read time (1K blocks)... 114ms, 1149754.37B/s

Not bad speed that - roughly 1MBps when doing block transfers of 1K. It'd be very slightly faster with bigger blocks - blocks are limited only by host MCU memory size. (By the way, this is an MCP25LV1024 I am working with here - 1Mbit / 128KByte chip).