0
\$\begingroup\$

I am a little confused regarding the order of bytes that are transferred through SPI to a slave device.

The next time diagram depicts the sequence of bytes to be sent when we want to write data to a a flash memory chip

Byte sequence diagram for writing data to flash memory

After I send the command byte (first 8 byte to the slave device (Flash Memory) I must send the address bytes and then the data I want to write.

So my question is this: Which byte do I send first, after the command byte? The High Byte or the Low byte of the address? Although it doesn't make sense to me why the High Byte should be sent first it is obvious in the diagram that the High Byte must be sent first and then the Low Byte. So, I wanted to ask in case I am thinking it the wrong way.

Thank you for your time

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

Even if a clear specification doesn't make sense to you, you should adhere it.

If it doesn't work the way it is specified, you can start to question it.

Byte order is often different between the microcontroller and external peripherals, so be prepared to face this in the future at other points as well.

And this is nothing inherent to SPI, it's the protocol defined by the manufacturer of the chip. SPI will not determine the byte order.

\$\endgroup\$
2
  • \$\begingroup\$ What I do not understand is that the communication between master and slave device occurs with fifo registers. So in order for the low byte( transmitter side) to move to the low byte space of the slave fifo it has to be transferred first. Right? \$\endgroup\$
    – Sparky
    Commented Jul 18, 2018 at 9:38
  • 3
    \$\begingroup\$ @Lefteris no. The diagram tells you specifically what is happening on the bus. It tells you that the high byte has to be on the bus first. So you push it out of your microcontroller first. And why do you assume that a fifo is implemented in the slave and why should it work like you said? Sometimes it helps to not think so much - the people at the other side already did that part. You are programming against a (hardware) interface, do not try to exploit internal workings which are not specified and might change in the future. \$\endgroup\$
    – Arsenal
    Commented Jul 18, 2018 at 11:10
2
\$\begingroup\$

Bit 12 has an MSB annotation, so you have to send the high byte first.

It doesn't have to "make obvious sense". For me if you have address 0x12 it makes sense to send 1 and then 2. It could also be little-endian, right adjusted. It is just a convention.

There are more wacky schemes, for example filter bits scattered all around in MSCAN peripheral from Freescale. Just look at 32.3.23 of the manual. It probably made the job of the chip designer easier at the cost of the firmware developer later on :)

\$\endgroup\$
1
  • 2
    \$\begingroup\$ "It probably made the job of the chip designer easier" That is why as chip designer I also wrote the low level drivers. Not only for testing the HW but also to find out how to make things easier. Also our SW guys where encouraged to come talk to us. One came to me asking if I could put 32 bits each on a separate address instead of all in one register. That saved a lot of effort as he did not need to use critical sections for access. \$\endgroup\$
    – Oldfart
    Commented Jul 18, 2018 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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