Min Code to Read/write 2 bytes to AT45DB321

Hello everyone, I need some help with this one, Flash memory is something completely new for me and I'm having trouble figuring out how to achieve a simple read and write to this adesto flash chip. I've been able to figure out how to get the device id, but thats as far as i can understand. I am using and arduino due, and just simple SPI.transfer. I just need to read and write to 2 bytes. I do need to learn how to do this for a separate in an fpga, using arduino is the simplest to start with having the serial console.

heres the device ID read.

this returns 1F, 27, 01,01, 0.

#include <SPI.h>
#define SERIAL_BAUD 9600
#define MEMORY 4 // Chip select pin
void setup()
{
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
Serial.begin(SERIAL_BAUD);
Serial.println("Starting");

SPI.begin(MEMORY);
SPI.setClockDivider(MEMORY, 21);
SPI.setDataMode(MEMORY, SPI_MODE0);

// Get manufacturer id
SPI.transfer(MEMORY, 9F, SPI_CONTINUE);
byte response1 = SPI.transfer(MEMORY, 0x00, SPI_CONTINUE);
byte response2 = SPI.transfer(MEMORY, 0x00, SPI_CONTINUE);
byte response3 = SPI.transfer(MEMORY, 0x00, SPI_CONTINUE);
byte response4 = SPI.transfer(MEMORY, 0x00, SPI_CONTINUE);
byte response5 = SPI.transfer(MEMORY, 0x00);

Serial.print("Manufacturer ID: ");
Serial.println(response1,HEX);
Serial.print(", ");
Serial.println(response2,HEX);
Serial.print(", ");
Serial.println(response3,HEX);
Serial.print(", ");
Serial.println(response4,HEX);
Serial.print(", ");
Serial.println(response5,HEX);
}
void loop()
{
}

ChrisChris:
Hello everyone, I need some help with this one, Flash memory is something completely new for me and I'm having trouble figuring out how to achieve a simple read and write to this adesto flash chip. I've been able to figure out how to get the device id, but thats as far as i can understand. I am using and arduino due, and just simple SPI.transfer. I just need to read and write to 2 bytes. I do need to learn how to do this for a separate in an fpga, using arduino is the simplest to start with having the serial console.

heres the device ID read.

this returns 1F, 27, 01,01, 0.

#include <SPI.h>

#define SERIAL_BAUD 9600
#define MEMORY 4 // Chip select pin
void setup()
{
 pinMode(2,OUTPUT);
 pinMode(3,OUTPUT);
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 Serial.begin(SERIAL_BAUD);
 Serial.println("Starting");

SPI.begin(MEMORY);
 SPI.setClockDivider(MEMORY, 21);
 SPI.setDataMode(MEMORY, SPI_MODE0);

// Get manufacturer id
 SPI.transfer(MEMORY, 9F, SPI_CONTINUE);
  byte response1 = SPI.transfer(MEMORY, 0x00, SPI_CONTINUE);
  byte response2 = SPI.transfer(MEMORY, 0x00, SPI_CONTINUE);
  byte response3 = SPI.transfer(MEMORY, 0x00, SPI_CONTINUE);
  byte response4 = SPI.transfer(MEMORY, 0x00, SPI_CONTINUE);
  byte response5 = SPI.transfer(MEMORY, 0x00);

Serial.print("Manufacturer ID: ");
Serial.println(response1,HEX);
Serial.print(", ");
Serial.println(response2,HEX);
Serial.print(", ");
 Serial.println(response3,HEX);
Serial.print(", ");
Serial.println(response4,HEX);
Serial.print(", ");
 Serial.println(response5,HEX);
}
void loop()
{  
}

You'll have to read the DataSheet, but page 6 of the data sheet lays it out pretty simply.

As a flash device a 'page' has to be erased to change a bit from '0' to '1', but if the bit is a '1' you can just write it.

so, as the DataSheet says, you have do to steps:

  • Transfer the new data to one of the two internal 512 byte buffers
  • Then you apply this data to the actual Flash. Either selecting
    automatic erase (The chip erases a the selected 512byte page then programs the data you previously loaded in to the buffer. If you did not 'fill' the buffer with 512 bytes of data, the unwritten bytes are erases '0xff'.
    The second option does not automatically erase the Flash Page, so if the data you transfered to the buffer in step 1. tries to overwrite a bit that was '0' back to a '1' it will not succeed. It is only able to change '1' bits to '0' using the Write commands. To go from '0' to '1' you have to use one of the ERASE commands.

This page write/page erase can be accommodated by first reading the 512byte Flash page, applying your modifications, the uploading the whole 512 bytes using option 1.

An other method to increment you storage location each time you write.

read the whole 512 byte page. search from the front for the last two bytes you previously wrote. (0xFF,0xFF) marks the first unused cell, Write your new value to the 'found' offset.

Chuck.

With some help I figured it out, 15 micro second reads, not bad, going to shoot for 50mhz on the fpga.

#include <SPI.h>
void setup()
{
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
Serial.begin(9600);
SPI.begin(4);
SPI.setClockDivider(4, 3);
SPI.setDataMode(4, SPI_MODE3);

SPI.transfer(4, 0X82, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
SPI.transfer(4, 0x07, SPI_CONTINUE);
SPI.transfer(4, 0x20);
}
void loop()
{
SPI.begin(4);
SPI.setClockDivider(4, 3);
int start=0;
int finish=0;
start = micros();
SPI.transfer(4, 0XD2, SPI_CONTINUE);
SPI.transfer(4, 0X00, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
SPI.transfer(4, 0x00, SPI_CONTINUE);
byte response1 = SPI.transfer(4, 0x00, SPI_CONTINUE);
byte response2 = SPI.transfer(4, 0x00);
SPI.end(4);

finish = micros();

Serial.print(response1,HEX);
Serial.print(", ");
Serial.println(response2,HEX);
Serial.println(finish-start,DEC);
delay(1000);

}