Foreword: This question is about a project that I am working on with a professor at my university. This is NOT for the class, but my reputation with this professor matters. Therefore, although my success in this project is very important to me, I do not consider it unfair to seek help from Stack Overflow.
However, the high level of my project is presented here . I have an ATmega328 microcontroller. I have a Microchip SST 64 Mbit memory card. ATmega has a hardware implementation of SPI. Flash memory has a hardware implementation of SPI.
My goal is to read data and write data to a flash chip using the ATmega master mode in SPI. The memory is organized in a multilayer structure, which is good for erasing, but for my purposes it is only 32,768 pages of 256 bytes each.
To write data, the main idea is to send an instruction byte, then a start address, then data. For reading data, the main idea is that I send an instruction byte, then a start address, then a dummy byte, and then it starts sending me data.
Here are the data:
Microcontroller: http://www.atmel.com/dyn/resources/prod_documents/doc8271.pdf
Flash: http://www.sst.com/dotAsset/40498.pdf
The code:
#include <SPI.h>
#include <Peggy2.h>
#define SS_PIN 16
Peggy2 frame1;
byte toDisp = 0;
byte checker = 0;
void setup()
{
frame1.HardwareInit();
pinMode(SS_PIN,OUTPUT);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.begin();
}
void loop()
{
if(!checker){
enableProgramming();
programData();
toDisp = receiveByte(0);
checker = 1;
frame1.WriteRow(0,toDisp);
}
frame1.RefreshAll(2);
}
byte receiveByte(unsigned long startAddress)
{
digitalWrite(SS_PIN,LOW);
SPI.transfer(0x0B);
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0xFF);
SPI.transfer(0xFF);
digitalWrite(SS_PIN,HIGH);
return SPDR;
}
void receiveBytes(int numOfBytes, unsigned long startAddress)
{
digitalWrite(SS_PIN,LOW);
SPI.transfer(0x0B);
}
void programData(){
digitalWrite(SS_PIN,LOW);
SPI.transfer(0x60);
digitalWrite(SS_PIN,HIGH);
delay(50);
digitalWrite(SS_PIN,LOW);
SPI.transfer(0x06);
digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW);
SPI.transfer(0x02);
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0xAA);
digitalWrite(SS_PIN,HIGH);
delayMicroseconds(3000);
}
void enableProgramming(){
digitalWrite(SS_PIN,LOW);
SPI.transfer(0x50);
digitalWrite(SS_PIN,HIGH);
delay(10);
digitalWrite(SS_PIN,LOW);
SPI.transfer(0x01);
SPI.transfer(0x00);
digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW);
SPI.transfer(0x06);
digitalWrite(SS_PIN,HIGH);
}
, , 1 -, . , : http://evilmadscience.com/tinykitlist/157
, , , , 8 . , -, factory 1s. , , - , , , , .
, SPI Arduinos . frame1.WriteRow(toDisp), .
, , , .
EDIT:. :
, SPI. . , SCK . , MOSI, , . , . AKA... , SPI.transfer() - ?