Electronic – How to read the device ID of a dataflash memory

deviceflashmbedmemoryspi

I am trying to read the device ID of a serial flash memory by using the SPI connections on an mbed. In the datasheet for the serial flash memory W25Q80 there is this figure.

The following is the code I'm using to try to read the device ID.

#include "mbed.h"

SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs(p8);

Serial pc(p9, p10); // tx, rx

int main() {
    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    spi.format(8,0);
    spi.frequency(1000000);

    // Select the device by seting chip select low
    cs = 0;

    wait(1.0);

    pc.printf("response from sending 90h: %d\r\n", spi.write(0x90));
    pc.printf("response from sending 00h: %d\r\n", spi.write(0x00));
    pc.printf("response from sending 00h: %d\r\n", spi.write(0x00));

    cs = 1;
}

All I read are 0s. Sometimes it reads 128 or 255. That's wrong, right?

Best Answer

The datasheet for the part you listed states that a 24-bit address should be written after the 0x90 code. Then the 2-byte Manufacturer and Device codes can be read. So insert 3 write instructions and it should work.

pc.printf("response from sending 90h: %d\r\n", spi.write(0x90));
pc.printf("response from sending 00h: %d\r\n", spi.write(0x00)); //Address 23..16
pc.printf("response from sending 00h: %d\r\n", spi.write(0x00)); //Address 15..8
pc.printf("response from sending 00h: %d\r\n", spi.write(0x00)); //Address 7..0
pc.printf("response from sending 00h: %d\r\n", spi.write(0x00)); //Manufacturer ID
pc.printf("response from sending 00h: %d\r\n", spi.write(0x00)); //Device ID

If you get inconsistent results, you may want to do your writes in a loop, then print when finished. I'm not sure how the printf IO affects the mbed SPI implementation timing, but if you get gaps between byte writes, that may affect how the flash part responds.

unsigned char command[6] = {0x90, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char response[6];

for(int i=0; i<6; i++)
{
    response[i] = spi.write(command[i]);
}

pc.printf("Manufacturer ID: %d\r\n", command[4]);
pc.printf("Device ID: %d:\r\n", command[5]);

You also have instruction 90h in your source, but your diagram is for instruction 9Fh. Just changing the instruction may allow your original code to work.

Related Topic