Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jscrane committed Aug 23, 2014
0 parents commit 5213b4b
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 0 deletions.
187 changes: 187 additions & 0 deletions SpiRAM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* SpiRAM.cpp - Library for driving a 23k256 SPI attached SRAM chip
*
* Phil Stewart, 18/10/2009
*
* Copyright (c) 2009, Phil Stewart
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* Updated to Arduino 1.0.5 and newer version of SPI
* "2010 by Cristian Maglie"
* by Fred Jan Kraan, fjkraan@xs4all.nl, 2014-02-28
*/

#include <SPI.h>
#include <SpiRAM.h>

// Constructor

SpiRAM::SpiRAM(SPIClass &spi, byte ssPin): _spi(spi)
{
_ssPin = ssPin;

// Set the RAM operarion mode flag according to the chip default
_current_mode = BYTE_MODE;
}

// Enable and disable helper functions

void SpiRAM::enable()
{
digitalWrite(_ssPin, LOW);
}

void SpiRAM::disable()
{
digitalWrite(_ssPin, HIGH);
}

// Byte transfer functions

char SpiRAM::read_byte(int address)
{
char read_byte;

// Set byte mode
_set_mode(BYTE_MODE);

// Write address, read data
enable();
_spi.transfer(READ);
_spi.transfer((char)(address >> 8));
_spi.transfer((char)address);
read_byte = _spi.transfer(0xFF);
disable();

return read_byte;
}

char SpiRAM::write_byte(int address, char data_byte)
{
// Set byte mode
_set_mode(BYTE_MODE);

// Write address, read data
enable();
_spi.transfer(WRITE);
_spi.transfer((char)(address >> 8));
_spi.transfer((char)address);
_spi.transfer(data_byte);
disable();

return data_byte;
}

// Page transfer functions. Bound to current page. Passing the boundary
// will wrap to the beginning
void SpiRAM::read_page(int address, char *buffer)
{
int i;

// Set byte mode
_set_mode(PAGE_MODE);

// Write address, read data
enable();
_spi.transfer(READ);
_spi.transfer((char)(address >> 8));
_spi.transfer((char)address);
for (i = 0; i < 32; i++) {
buffer[i] = _spi.transfer(0xFF);
}
disable();
}

void SpiRAM::write_page(int address, char *buffer)
{
int i;

// Set byte mode
_set_mode(PAGE_MODE);

// Write address, read data
enable();
_spi.transfer(WRITE);
_spi.transfer((char)(address >> 8));
_spi.transfer((char)address);
for (i = 0; i < 32; i++) {
_spi.transfer(buffer[i]);
}
disable();
}

// Stream transfer functions. Ignores page boundaries.
void SpiRAM::read_stream(int address, char *buffer, int length)
{
int i;

// Set byte mode
_set_mode(STREAM_MODE);

// Write address, read data
enable();
_spi.transfer(READ);
_spi.transfer((char)(address >> 8));
_spi.transfer((char)address);
for (i = 0; i < length; i++) {
buffer[i] = _spi.transfer(0xFF);
}
disable();
}

void SpiRAM::write_stream(int address, char *buffer, int length)
{
int i;

// Set byte mode
_set_mode(STREAM_MODE);

// Write address, read data
enable();
_spi.transfer(WRITE);
_spi.transfer((char)(address >> 8));
_spi.transfer((char)address);
for (i = 0; i < length; i++) {
_spi.transfer(buffer[i]);
}
disable();
}


// Mode handling
void SpiRAM::_set_mode(char mode)
{
if (mode != _current_mode)
{
enable();
_spi.transfer(WRSR);
_spi.transfer(mode);
disable();
_current_mode = mode;
}
}
82 changes: 82 additions & 0 deletions SpiRAM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* SpiRAM.h - Library for driving a 23k256 SPI attached SRAM chip
*
* Phil Stewart, 18/10/2009
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#ifndef SpiRAM_h
#define SpiRAM_h

#define SPIRAM_LIB_VERSION "0.3.00"

#include <Arduino.h>
#include <SPI.h>

// SRAM opcodes
// #define WREN 6
// #define WRDI 4
#define RDSR 5
#define WRSR 1
#define READ 3
#define WRITE 2

// SRAM Hold line override
#define HOLD 1

// SRAM modes
#define BYTE_MODE (0x00 | HOLD)
#define PAGE_MODE (0x80 | HOLD)
#define STREAM_MODE (0x40 | HOLD)

// Clock speeds
#define RAMCLK4M 0
#define RAMCLK1M 1
#define RAMCLK250K 2
#define RAMCLK125K 3

class SpiRAM
{
public:
SpiRAM(SPIClass &spi, byte ssPin);
void enable();
void disable();
char read_byte(int address);
char write_byte(int address, char data_byte);
void read_page(int address, char *buffer);
void write_page(int address, char *buffer);
void read_stream(int address, char *buffer, int length);
void write_stream(int address, char *buffer, int length);
private:
char _current_mode;
byte _ssPin;
void _set_mode(char mode);
SPIClass &_spi;
};

extern SpiRAM SpiRam;

#endif
32 changes: 32 additions & 0 deletions example/SpiRAM_Example/SpiRAM_Example.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <SPI.h>
#include <SpiRAM.h>

#define SS_PIN 10

byte clock = 0;
SpiRAM SpiRam(0, SS_PIN);

void setup() {
Serial.begin(9600);
}

void loop()
{
char data_to_chip[17] = "Testing 90123456";
char data_from_chip[17] = " ";
int i = 0;

// Write some data to RAM
SpiRam.write_stream(0, data_to_chip, 16);
delay(100);

// Read it back to a different buffer
SpiRam.read_stream(0, data_from_chip, 16);

// Write it to the serial port
for (i = 0; i < 16; i++) {
Serial.print(data_from_chip[i]);
}
Serial.print("\n");
delay(1000); // wait for a second
}
20 changes: 20 additions & 0 deletions example/spiRamTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#include <SPI.h>
#include <SpiRAM.h>

#define SS_PIN 10

byte clock = 0;
SpiRAM spiRam(0, SS_PIN);

void setup() {

Serial.begin(115200); // writing a byte is ~ 86 uS at 115200 Baud
delay(1000); // wait for a second
Serial.println("Versie 1");
}

void loop() {
unsigned long address = 0;

}
44 changes: 44 additions & 0 deletions example/spiRamTest/spiRamTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include <SPI.h>
#include <SpiRAM.h>

#define SS_PIN 10

byte clock = 0;
SpiRAM spiRam(0, SS_PIN);

void setup() {
digitalWrite(A5, HIGH);
Serial.begin(115200);
delay(1000); // wait for a second
Serial.println("Version 1");
}

// The RAM, initially having random contents, will fail the test. If A5 is
// pulled to ground, the correct value will be written before test, and the
// RAM location written to, will pass the test. Keeping A5 low for the full
// cycle from 0 to 7FFF will make the test completely happy. Until the next
// next power cycle.
void loop() {
unsigned int address = 0;
unsigned int i;
unsigned int maxRam = 32768;
byte wValue, rValue;
for (i=0; i < maxRam; i++) {
wValue = (byte) (i & 0xFF);
if (digitalRead(A5) == LOW) {
spiRam.write_byte(i, wValue);
}
rValue = (byte)spiRam.read_byte(i);
if (wValue != rValue) {
Serial.print("RAM error at ");
Serial.print(i, HEX);
Serial.print(" should be ");
Serial.print(wValue, HEX);
Serial.print(" is ");
Serial.println(rValue, HEX);
} else {
Serial.println(i, HEX);
}
}
}
Loading

0 comments on commit 5213b4b

Please sign in to comment.