2
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ yes that should work. use this for reference mbed.org/projects/libraries/api/mbed/trunk/SPI#SPI.Functions \$\endgroup\$ – jsolarski Mar 14 '11 at 4:56
  • \$\begingroup\$ Why does the figure show an EFh for the manufacturer ID? Am I supposed to send that instead of 0x00? \$\endgroup\$ – 200ok404notfound Mar 14 '11 at 5:02
  • \$\begingroup\$ No you should not send EFh , that is the manufacture ID that is should return \$\endgroup\$ – jsolarski Mar 14 '11 at 5:05
  • \$\begingroup\$ the comms flow should be - send 9F -> memory(chip) should send back EF ->then ID15-ID0 should also be sent to your MBED \$\endgroup\$ – jsolarski Mar 14 '11 at 5:20
  • \$\begingroup\$ I'm just reading 0s. I updated my question to show the code. \$\endgroup\$ – 200ok404notfound Mar 14 '11 at 5:29
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.