-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |