SPI RAM Library

Post Reply
User avatar
majenko
Site Admin
Posts: 2188
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

SPI RAM Library

Post by majenko » Thu Sep 26, 2013 1:44 am

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:

Code: Select all

#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:

Code: Select all

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:

Code: Select all

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:

Code: Select all

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.

Code: Select all

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).
Attachments
SPIRAM.zip
(2.46 KiB) Downloaded 93 times
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

Post Reply