This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

at250x0

I looked the example of the work with at250x0 and has not understood one thing. Why when sensing from memory in she is written 0xff?
This program possible to find on request SPI here.
Here is this fragment:
while (length--)
{
SPDR = 0xFF;
while ((SPSR & 0x80) == 0);
*buf++ = SPDR;
}
About this in documentation on at250x0 is not written. This is some particularity?

  • You may want to read the article "How SPI Works" to gain a better understanding of SPI:

    http://www.embedded.com/story/OEG20020124S0116

    According to this article:

    "Devices communicate using a master/slave relationship, in which the master initiates the data frame. When the master generates a clock and selects a slave device, data may be transferred in either or both directions simultaneously. In fact, as far as SPI is concerned, data are always transferred in both directions. It is up to the master and slave devices to know whether a received byte is meaningful or not."

    If you include the parts of the code that were left out, it makes more sense:

    void read_at250x0 (
      unsigned address,
      unsigned char *buf,
      unsigned length)
    {
    AT250X0_CS = 0;
    
    SPDR = AT250X0_READ_OPCODE(address);
    while ((SPSR & 0x80) == 0);

    The above code enables the chip select of the AT25xxx memory and sends the READ opcode along with a part of the address. Note that the AT25xxx also returns a byte, but it's not meaningful so it is ignored. The while SPSR waits until the SPI transfer is complete.

    PDR = AT250X0_ADDRESS_LSB(address);
    while ((SPSR & 0x80) == 0);

    The above code sends the remainder of the address to read.

    while (length--)
      {
      SPDR = 0xFF;
      while ((SPSR & 0x80) == 0);
      *buf++ = SPDR;
      }

    OK. Now the program is ready to read length bytes from the AT25xxx -- that's what the while loop iterates. The SPDR = 0xFF simply initiates an SPI transfer. Only the master can do that. We could have used SPDR=ANYTHING. The value doesn't matter because the AT25xxx knows that what it receives is meaningless and is ignored. At the same time the MCU is sending garbage, the AT25xxx is returning the data from the memory. Again, the while SPSR waits for the SPI transfer to complete. Then, the *buf++ = SPDR reads the byte that the AT25xxx transferred back to the MCU. This loop repeats until length is 0.

    AT250X0_CS = 1;
    }

    Finally, release the chip select for the AT25xxx.

    Hope this helps.

    Jon