Hello!
I want to a quick question about SPI.
Just thinking you might have long questions in the future scares me.
And I've seen some videos and info that SPI usually have 16 bit registers and not 8 bit.
Be careful with what you see on a random video on the net, be it technical, news or
whatever else. What was the context? "Usually"? How frequently?
I would say that usually if you design silicium, you don't setup more registers than
necessary. And usually SPI sensors, memory chips, etc, work in 8 bit SPI.
So the 16 bit register must have an algorythm how to manage the 16 bit register to work
in 8 bit format. I've tried to give some of my conclusion above :>
STM32 has obviously at least 16 bit registers because you can setup 16 bit
SPI. What does it change? If the SPI setup is 8 bit, then you will use (i.e. transmit
or receive) only the 8 first bits of this register, be it lsb or msb. I don't know any other
processor working with up to 16 bit SPI. And I don't understand why they did it.
But that's only a physical register in which you can put whatever you want.
Just imagine you have a company shipping goods, be it water mellons, water guns or
water pumps. You have a parking area with 32 trucks. For some order, you need
8 trucks. Will you move all your 32 trucks or just use 8 of them?
So this will be the last time before I give up: if you need to send a byte, you just
send a byte. If you need to send 3 bytes, you send 3 bytes, not 4.
What I meant by 4th byte was that if : the SPI was 16 bit format
If the SPI is in 16 bit mode, it means that YOU set it that way. We're not
responsible of your choices. In this case, obviously, you will have to send and
receive 2 16-bit words and ignore half of the second one.
If you want to be compatible with existing hardware, configure it in 8 bit mode even
if you need only 4 bits.
Second question is that whenever Master or Slave want's to transmit information the
other side automatically fulls it's register with dummy byte ?
It doesn't have to. Dummy bytes are just ignored.
Have you ever boarded a cable car? That's exactly what SPI does. On both sides you have
a departure and an arrival platforms. Early in the morning, the descending car is
empty. Do you think the guys at the top station fill the descending car with
absent people? Do you think the guys at the base station will spend time to NOT control
the tickets of the absent people of the empty seats?
how it does that the Slave doesn't read the dummy bytes, how he ignores them
The slave does the same way as the clerk at the base station to NOT control
the empty seats' absent people tickets. It simply ignores the dummy value, in other
words, it does as if the dummy value didn't even exist.
how does the device know that it is a dummy byte
It doesn't know anything. YOU know it. It's designed that way, YOU read the documentation
and YOU design the software to be compliant with the SPI device YOU have chosen.
Example: In a SPI memory context, you send an opcode and an address.
C = opcode byte
D = dummy
A = address
V = value
You read this in the datasheet that reading a byte is done as follows : CAAAD
So this is a memory containing between 128k and 16M of memory (17 to 24 bit, therefore
3 bytes address).
Therefore in the MISO, buffer you will receive DDDDV and you can retrieve the value
by val = misobuf[4]. (i.e. you don't even look at the dummy bytes). Note that it's a
5-byte buffer, for which you have to exchange 5 bytes, not 6, not 8.
Once you have sent the opcode, the memory knows you want to read.
Once you have sent the address, the memory knows you want the value at address AAA
and immediately sets it to the output buffer.
Then the master dummy byte will be exchanged with the value that was just fetched from address
AAA.
-> at this point, the address is usually automatically incremented, so that if you
send CAAADVVVV, it uses 4 consecutive address bytes to write an integer or whatever else
fitting in 4 bytes.
OK, as I said earlier, It's my last mail on this issue.
I'm not sure exactly how you are trying to learn about SPI. It looks like you are reading
a book and trying to figure out what happens.
What I would advice is that you find a cheap device which is programmable, SPI
capable, etc. Arduino or anything else. You find a cheap oscilloscope. For SPI, even a
cheap device will work. a few 100 dollars.
I would advice a cheap scope but digital, with a SPI decoder so that you don't spend
too much time counting edges. And play with SPI. Modify your program, verify
that you can send your 3 bytes without caring about the 4th. Etc.
Good luck!
Dora