cancel
Showing results for 
Search instead for 
Did you mean: 

SPI receiver register buffer question.

mikemillernow
Associate II
Posted on March 22, 2014 at 08:23

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.

-Mike

3 REPLIES 3
zzdz2
Associate II
Posted on March 22, 2014 at 08:55

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.

mikemillernow
Associate II
Posted on March 24, 2014 at 19:19

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.

mikemillernow
Associate II
Posted on March 24, 2014 at 19:29

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)  );