2014-03-22 12:23 AM
I am doing an SPI write sending out dummy data to clock in the data from my SPI flash.
// Dummy data to read status register out SPI_SendData8(SPI1, 0x05); // Wait until transmit buffer is empty than continue while( !(SPI1->SR & 0x02) ); At this point I know that the command has been sent out. The problem is that I am having a hard time determining when to read the data. For example when I do received[0] = SPI1->DR; I don't get the correct value and I seem to be a ''cycle'' off. When I look in my debugger I can see the value but that is not the one that I have read in. Should I poll in a similar way as I did in the transmit to know when to read the the receive data register? I can check the RXNE (receive buffer not empty bit). 0 = rx buffer empty 1: rx buffer not empty. // Wait until receive buffer is not empty then read data register while( !(SPI1->SR & 0x01) ); received[0] = SPI1->DR; I tried the above but I am still missing this value. You can see the attached screen shot of my debugger. There are two DR's showing. The top one (0x00000002) is that value that I am looking for but what I am grabbing from the received[0] = SPI1->DR; command ix 0xFF. Thanks for any tips. -Mike2014-03-22 12:55 AM
Maybe before the dummy write you need to read ->DR to make sure RX reg is empty.
I'm doing such thing in my code, i'm not sure if it's necessary but it's harmless.2014-03-24 11:19 AM
You can see below from my comments that there is a discrepancy between my first two reads and my last two reads of the data register for each commands sent out. It seems a bit cheesy for me to just throw is some extra reads and cross my fingers.
Maybe I should just poll the RXNE bit (1 = data in the receive buffer) and do a single read once data is available.SPI_Cmd(SPI1, ENABLE);
SPI_SendData8(SPI1, 0x05);
// Wait until transmit buffer is empty than continue
while( !(SPI1->SR & 0x02) );
// Here is where I need to check to be sure that the read-> is empty.
received[0] = SPI1->DR; // Reading back 0x00 but 0xFF on the scope.
received[1] = SPI1->DR; // Reading back 0x00 but 0xFF on the scope.
received[2] = SPI1->DR; // Reading back 0xFF but 0xFF on the scope.
received[3] = SPI1->DR; // Reading back 0xFF but 0xFF on the scope.
// Dummy data to read status register out
SPI_SendData8(SPI1, 0xAA);
// Wait until transmit buffer is empty than continue
while( !(SPI1->SR & 0x02) );
received[4] = SPI1->DR; // Reading back 0xFF but 0x02 on the scope.
received[5] = SPI1->DR; // Reading back 0xFF but 0x02 on the scope.
received[6] = SPI1->DR; // Reading back 0x02 but 0x02 on the scope.
received[7] = SPI1->DR; // Reading back 0x02 but 0x02 on the scope.
2014-03-24 11:29 AM
Not what I expected.
After I send out my command I get stuck in the last loop checking to see what was received. 0xFF should be the value clocked in. SPI_Cmd(SPI1, ENABLE); SPI_SendData8(SPI1, 0x05); // Wait until transmit buffer is empty than continue while( !(SPI1->SR & 0x02) ); // Wait until there is data in the receiver buffer and continue. while( !(SPI1->SR & 0x01) );