I have small question about how bytes move around during SPI reading. I have a flash memory chip and microcontroller. Suppose I want to know jedec data from the chip(although it can be any data, this is just example), I found how to do it and it works fine, but I don't get the part when we assign jedec parameters to variables.
From the C code below I can see the possible logic like this:
we send "retrieve jedec" command to chip, then for some reason we start to send zeros to assign values to variables, probably it reads the value by sending dummy-bytes. Question is - isn't it supposed to overwrite bytes we are reading?
I mean - you shift something to the register you want to read therefore you get the value back by replacing it with what you have sent.
Maybe I express my question blurry, ask more info if you are confused, but to sum up everything I have two questions:
1. Why we are sending zeros during value assignments like *b1 = SPI_MasterTransmit(0);
2. If we do it to shift the value from slave device to master - why don't we erase the value from the slave by doing this?
void get_jedec_id(uint8_t *b1, uint8_t *b2, uint8_t *b3) {
PORTB |= 1<<PINB2; // turn high ss pin(/cs)
PORTB &= ~1<<PINB2; // turn low ss pin(/cs)
SPI_MasterTransmit(0x9F); // transmit get_jedec command of this chip
*b1 = SPI_MasterTransmit(0); // manufacturer id
*b2 = SPI_MasterTransmit(0); // memory type
*b3 = SPI_MasterTransmit(0); // capacity
PORTB |= 1<<PINB2;
not_busy();
}
SPI_MasterTransmit code:
unsigned char SPI_MasterTransmit(uint8_t outData)
{
// Transmission start
SPDR = outData;
// Waiting for transmission to end
while(!(SPSR & (1<<SPIF))) ;
return SPDR; // return received byte
}