BlockoS / arduino-dataflash

Support for Atmel Dataflash for the Arduino
http://blockos.github.com/arduino-dataflash
23 stars 14 forks source link

Wrong status read from device and subsequent pageRead failure #14

Open rogierschouten opened 10 years ago

rogierschouten commented 10 years ago

I'm using a Teensy 3.1 and a AT45DB161D. For some reason, the very first status code read from the chip always returns 0. This causes the dataflash library to misinitialize its member variables and subsequently, most functions don't work as expected. I've narrowed it down to the SPI mode: with SPI mode 0, it works. With SPI mode 3, the first status code is 0 and subsequent status codes are good.

Therefore two suggestions for dataflash: 1) maybe change to SPI mode 0 2) prevent buffer overrun in Dataflash::setup() due to deviceIndex out-of-range (e.g. choose a default chip type)

Below is a small example program to reproduce (only shows the first status code):

include

define MY_SCS 10

uint8_t st;

void setup() { Serial.begin(9600);

// initialize SPI pinMode(MY_SCS, OUTPUT); digitalWrite(MY_SCS, HIGH); SPI.begin();
SPI.setDataMode(SPI_MODE3); // NO SPI.setBitOrder(MSBFIRST); SPI.setClockDivider(SPI_CLOCK_DIV2);

// enable chip digitalWrite(MY_SCS, LOW);

// status SPI.transfer(0xd7); st = SPI.transfer(0);

// disable chip digitalWrite(MY_SCS, HIGH);

}

void loop() { Serial.println(st, HEX); delay(1000); }

BlockoS commented 10 years ago

Thanks for report. I checked the datasheet. The device density bits are provided for backward compatibility. Maybe it will be safer to use the first byte of the device Id. But that won't solve the problem. A test may be to do something like: status = SPI.transfer(0xff)

Unfortunately I won't be able to test it before Monday.

rogierschouten commented 10 years ago
Thank you, and don't rush. In the
  meantime, I made this workaround: I added a digitalWrite to SCK
  after setting Mode 3, and I prevented buffer overflow:... in DataFlash::setup() ...    SPI.setDataMode(SPI_MODE3);        SPI.setBitOrder(MSBFIRST);    SPI.setClockDivider(SPI_CLOCK_DIV2);        // DIGITAL WRITE HERE TO ALLOW DETECTION OF MODE 3    digitalWrite(SCK, HIGH);    ... code omitted ...        /* Bit 3 of status register is ignored as it's always
    1. Note that it is     * equal to 0 on the obsolete chip with density
    higher than 64 MB. */    uint8_t deviceIndex = ((stat & 0x38) >> 3)
    - 1;     if (deviceIndex >= 7) deviceIndex = 4; // DEFAULT
    TO AT45DB161D in case of faulty status code    m_bufferSize = m_infos.bufferSize[deviceIndex];    m_pageSize   = m_infos.pageSize[deviceIndex];      m_sectorSize = m_infos.sectorSize[deviceIndex];
  BlockoS schreef op 19-4-2014 22:33:

  Thanks for report.
    I checked the datasheet. The device density bits are provided
    for backward compatibility. 
    Maybe it will be safer to use the first byte of the device Id.
    But that won't solve the problem.
    A test may be to do something like:
    status = SPI.transfer(0xff)
  Unfortunately I won't be able to test it before Monday.
  —
    Reply to this email directly or view
      it on GitHub.
tshivamiitk7 commented 6 years ago

I am trying to write on AT45DB041D using Arduino uno. But at present i am not able to read the device ID & manufacturer ID using your page test program. Can you suggest me what can be the possible settings or issues i am missing. @BlockoS @rogierschouten