Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ennui2342 committed Nov 28, 2013
1 parent 930f808 commit ec326d4
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,29 @@ arduino-sram
============

Arduino library for interfacing with 23K256 and 23LC1024 SRAM chips from Arduino

Implements Stream for convenience so you get readBytes() etc. But be aware this is not the fastest way to do it because of that (setting the address for every byte transfered)

## Installation

Clone as SRAM and put in your arduino libraries folder

## Notes

* Untested with 23K256
* Really only SRAM_SEQN_MODE is properly supported (please contribute if you use other modes)

## Usage

Use seek() to set the address before reading and writing

Example:

SRAM sram(SRAM_CS_PIN, SRAM_1024);
char test [] = "hello, world";
sram.seek(1);
sram.write((byte *) test, sizeof test);
char buf[100];
sram.seek(1);
sram.readBytes((char *) buf, sizeof buf);
Serial.println(buf);
79 changes: 79 additions & 0 deletions SRAM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <SPI.h>
#include "SRAM.h"

// Should be good for 23K256 and 23LC1024 but untested with the former
//
// Implements Stream for convenience so you get readBytes() etc. Not the fastest way
// to do it because of that (setting the address for every byte transfered)
//
// Really only SRAM_SEQN_MODE is properly supported (please contribute)
// Use seek() to set the address before reading and writing

/*
Example usage:
SRAM sram(SRAM_CS_PIN, SRAM_1024);
char test [] = "hello, world";
sram.seek(1);
sram.write((byte *) test, sizeof test);
char buf[100];
sram.seek(1);
sram.readBytes((char *) buf, sizeof buf);
Serial.println(buf);
*/

// setup SRAM device by turning into sequential mode
SRAM::SRAM(uint8_t cs_pin, uint8_t size, uint8_t mode) {
this->address = 1;
this->cs_pin = cs_pin;
this->mode = mode;
this->size = size;
// Need to support 256k SRAM as well
}

SRAM::SRAM(uint8_t cs_pin, uint8_t size) {
SRAM(cs_pin, size, SRAM_SEQN_MODE);
}

void SRAM::begin() {
SPI.begin();
pinMode(this->cs_pin, OUTPUT);
digitalWrite(this->cs_pin, LOW);
SPI.transfer(SRAM_WRSR);
SPI.transfer(this->mode);
digitalWrite(this->cs_pin, HIGH);
}

void SRAM::seek(uint32_t address) {
this->address = address;
}

size_t SRAM::write(uint8_t data) {
digitalWrite(this->cs_pin, LOW);
SPI.transfer(SRAM_WRITE);
if(this->size == SRAM_1024)
SPI.transfer((this->address >> 16) & 0xFF);

SPI.transfer((this->address >> 8) & 0xFF);
SPI.transfer(this->address & 0xFF);
SPI.transfer(data);
this->address++;
digitalWrite(this->cs_pin, HIGH);

return 1;
}

int SRAM::read() {
digitalWrite(this->cs_pin, LOW);
SPI.transfer(SRAM_READ);
if(this->size == SRAM_1024)
SPI.transfer((this->address >> 16) & 0xFF);

SPI.transfer((this->address >> 8) & 0xFF);
SPI.transfer(this->address & 0xFF);
uint8_t data = SPI.transfer(0);
this->address++;
digitalWrite(this->cs_pin, HIGH);

return data;
}
43 changes: 43 additions & 0 deletions SRAM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <SPI.h>

#define SRAM_READ 0b011 // Read data from memory array beginning at selected address
#define SRAM_WRITE 0b010 // Write data to memory array beginning at selected address
#define SRAM_RDSR 0b101 // Read STATUS register
#define SRAM_WRSR 0b001 // Write STATUS register

// operation modes (status register)

#define SRAM_BYTE_MODE 0b00000000 // Byte mode (default operation)
#define SRAM_PAGE_MODE 0b10000000 // Page mode
#define SRAM_SEQN_MODE 0b01000000 // Sequential mode
#define SRAM_RSVD_MODE 0b11000000 // Reserved
#define SRAM_HOLD_MODE 0b00000001 // Set this bit to DISABLE hold mode

#define SRAM_256 0b0
#define SRAM_1024 0b1

class SRAM : public Stream
{
private:
uint8_t cs_pin;
uint32_t address;
uint8_t size;
uint8_t mode;

public:
SRAM(uint8_t, uint8_t);
SRAM(uint8_t, uint8_t, uint8_t);
void begin();
void seek(uint32_t);

// Print::write
size_t write(uint8_t);

// Stream
int read();
int available() { return -1; }
void flush() {}
int peek() { return -1; }

using Print::write;
};

0 comments on commit ec326d4

Please sign in to comment.