DataFlash AT45DB041D - SPI

Hi, All!
I am trying to interface with Atmel 4-megabit DataFlash AT45DB041D over the SPI. Already 2 days I’m trying to write a byte to the first available address, and then read it from there. I tried the dataFlash library, don`t work. What am I doing wrong?

#include <SPI.h>

#define miso 12
#define mosi 11
#define sck 13
#define cs 10

void setup() 
{
  SPI.begin() ;
  Serial.begin(115200);
  pinMode(mosi, OUTPUT);
  pinMode(miso, INPUT);
  pinMode(sck,OUTPUT);
  pinMode(cs,OUTPUT);
  digitalWrite(cs,HIGH);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  delay(1000); 
}

void loop() 
{
  BuWrite();
  delay(100);
  BufferToPage();
  delay(100);
  mainMemoryPageRead();
  delay(100);
}

void BuWrite()
{
  boolean t;
  digitalWrite(cs,LOW);
  SPI.transfer(0x84);
  
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);

  if (t==true) SPI.transfer(0xFF);
  if (t==false) SPI.transfer(0xAA);
  digitalWrite(cs,HIGH);
  t=!t;
}

void BufferToPage()
{
  digitalWrite(cs,LOW);
  
  SPI.transfer(0x83);
  
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  
  digitalWrite(cs,HIGH);
}

void mainMemoryPageRead()
{
  int res;
  digitalWrite(cs,LOW);
  
  SPI.transfer(0xD2);
  
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  res=SPI.transfer(0x00);
  
  digitalWrite(cs,HIGH);
  Serial.println(res);
}

Get rid of these, SPI.begin takes over the pins:

#define miso 12
#define mosi 11
#define sck 13

pinMode(mosi, OUTPUT);
pinMode(miso, INPUT);
pinMode(sck,OUTPUT);

Move this out of here:

void BuWrite()
{
boolean t; <<< this effectively resets to 0 every time you enter this function

See if these make a difference.

Thank`s! I corrected the code, but for some reason it still does not work. What is interesting here is that I get :astonished:

0
0
0
0
0
0
0
0
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
0
0
0
0
0
0
0
0
0

Here`s the code:

#include <SPI.h>

#define cs 10
boolean t=true;

void setup() 
{
  SPI.begin() ;
  Serial.begin(115200);
  pinMode(cs,OUTPUT);
  digitalWrite(cs,HIGH);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE3);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  delay(100); 
}

void loop() 
{
  BufferWrite();
  delay(100);
  BufferToPage();
  delay(100);
  mainMemoryPageRead();
  delay(100);
}

void BufferWrite()
{
  digitalWrite(cs,LOW);
  SPI.transfer(0x84);
  
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);

  if (t==true) SPI.transfer(0xFF);
  if (t==false) SPI.transfer(0xAA);
  digitalWrite(cs,HIGH);
  t=!t;
}


void BufferToPage()
{
  digitalWrite(cs,LOW);
  
  SPI.transfer(0x83);
  
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  
  digitalWrite(cs,HIGH);
}

void mainMemoryPageRead()
{
  int res;
  digitalWrite(cs,LOW);
  
  SPI.transfer(0xD2);
  
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  res=SPI.transfer(0x00);
  
  digitalWrite(cs,HIGH);
  Serial.println(res);
}

So why is this code looks so ugly. I tried 2 libraries - serialFlash and Chinese one. Then i tried to combine them beacause serialFlash works with AT45DB041B but not AT45DB041D. Chinese seems to be ok but it`s not arduino library. When I joined them I still got the wrong result. What else can I try?

Post a link to the datasheet. Need to see what's required for setup. You have three functions with 0x82, 0x83, 0xD2, No comments at all so hard to tell what the chip wants from all that.

http://atmel.com/dyn/resources/prod_documents/doc3595.pdf Link to the AT45DB041D Datasheet