Skip to content

Instantly share code, notes, and snippets.

@monsonite
Created June 11, 2011 17:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monsonite/1020771 to your computer and use it in GitHub Desktop.
Save monsonite/1020771 to your computer and use it in GitHub Desktop.
Some code to test 23K256 SPI SRAM on Nanode
// SPI SRAM/EPROM Speed Test
// Written by Ken Boak for Nanode SRAM tests May 22 2011
// Simple test of the 23K256 32Kx8 SRAM on the Nanode 5 board
// Remember to write 0 to the status register to put it into byte mode (not page or streaming mode)
// Before each write take the select line low and then high again after each write!
// Writes 32K bytes in about 2.68 seconds - with digital write 12,226 per second
// These tests are for random access mode
// For sreaming mode the whole 32K may be read in 116mS !!
// See Arduino Playground here for more details of streaming mode
// and a library which supports the 23K256 SPI SRAM
// http://www.arduino.cc/playground/Main/SpiRAM
// With SLAVESELECT being written to direct to port pin 32kbytes write is 2.4 seconds
// and read spi is reduced from 15.04uS to 10.8uS
// With address incrementation this comes to 13.28uS or 26.6 uS to read a 16 bit integer - say 27uS
// These figures can be improved for paged or streaming mode for multiple bytes
// Now tested with direct write
/*
Pin 9 = Port B1 Slave Select
Pin 10 = Port B2
Pin 11 = Port B3 MOSI
Pin 12 = Port B4 MISO
Pin 13 = SCK
DDRB = DDRB | B11101111; // Sets pins 12 as input, 8-15 output
// DDRD = DDRD | B11111100; // Sets pins 2 to 7 as outputs
pinMode(6,OUTPUT);
PORTD |= (1<<PD7) ; // PORTD. 7 = 1
// dlihb() ; // dly halfbit through instructions
PORTD &= ~(1<<PD7) ; // PORTD. 7 = 0
// dlihb() ; // dly halfbit through instructions
*/
#include <avr/io.h> // include the Port pin definitions
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 9//ss
#define LED 6 // LED on pin 6 - when pulled LOW!
//opcodes
#define WREN 6
#define WRDI 4
#define RDSR 5
#define WRSR 1
#define READ 3
#define WRITE 2
#define PD7 PORTD7 // digital 7
#define PB1 PORTB1 // digital 9
byte eeprom_output_data;
byte eeprom_input_data=0;
byte clr;
int address=0;
long start_time;
long stop_time;
long duration;
//data buffer
char buffer [128];
void fill_buffer()
{
for (int I=0;I<128;I++)
{
buffer[I]=I;
}
}
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}
void setup()
{
Serial.begin(9600);
DDRB = DDRB | B11101111; // Sets pins 12 as input, 8-15 output
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED,HIGH); //LED Off
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);
//fill buffer with data
fill_buffer(); //fill eeprom w/ buffer
digitalWrite(SLAVESELECT,LOW);
spi_transfer(WRSR); //write to status register
spi_transfer(0); // byte mode
digitalWrite(SLAVESELECT,HIGH);
digitalWrite(LED,LOW);
start_time = millis();
for (int I=0;I<32767;I++) // Write the 128 byte buffer to the RAM
{
// digitalWrite(SLAVESELECT,LOW);
PORTB &= ~(1<<PB1) ; // SLAVESELECT = 0
spi_transfer(WRITE); //write instruction
address=I;
spi_transfer((char)(address>>8)); //send MSByte address first
spi_transfer((char)(address)); //send LSByte address
// spi_transfer(buffer[I]); //write data byte
spi_transfer(I/4);
PORTB |= (1<<PB1) ; // SLAVESELECT = 1
// digitalWrite(SLAVESELECT,HIGH); //release chip
}
stop_time = millis();
duration = stop_time-start_time;
delay(2000);
Serial.println(start_time);
Serial.println(stop_time);
Serial.println(duration);
digitalWrite(LED,HIGH);
//wait for eeprom to finish writing
delay(3000);
Serial.print('h',BYTE);
Serial.print('i',BYTE);
Serial.print('\n',BYTE);//debug
delay(1000);
}
byte read_eeprom(int EEPROM_address)
{
//READ EEPROM
int data;
// digitalWrite(SLAVESELECT,LOW);
PORTB &= ~(1<<PB1) ; // SLAVESELECT = 0
spi_transfer(READ); //transmit read opcode
spi_transfer((char)(EEPROM_address>>8)); //send MSByte address first
spi_transfer((char)(EEPROM_address)); //send LSByte address
data = spi_transfer(0xFF); //get data byte
PORTB |= (1<<PB1) ; // SLAVESELECT = 1
// digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
return data;
}
void loop()
{
eeprom_output_data = read_eeprom(address);
// Serial.print(eeprom_output_data,DEC);
// Serial.print("'");
// Serial.print('\n',BYTE);
address++;
if (address == 32767)
address = 0;
// delay(500); //pause for readability
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment