cancel
Showing results for 
Search instead for 
Did you mean: 

Incorrect value stored in SPI DR register on STM32F407

GTS.1
Associate III

Hi,

I'm using a STM32F4 Discovery board which has a STM32F407VG with a LIS3DSH accelerometer which is connected by SPI. I'm trying to read a register from the accelerometer and I tried writing the address of the register to SPI->DR. But SPI->DR shows an incorrect value in the System Viewer window and in the Memory window. I have attached a picture with the corresponding portions highlighted in yellow.

But this code still gets the correct value from the accelerometer.

0693W000001cEAKQA2.png

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

SPI->DR is special. When you write to it, it sends data to be transmitted, and when you read from it, it reads incoming data.

You should not expect the following to work:

SPIx->DR = 0x67;
assert(SPIx->DR == 0x67); // <-- will likely fail

From the reference manual:

The data register is split into 2 buffers - one for writing (Transmit Buffer) and another one for

reading (Receive buffer). A write to the data register will write into the Tx buffer and a read

from the data register will return the value held in the Rx buffer.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
S.Ma
Principal

Beware that the debugger needs to actually read the DR to show it to you, so it does steal the user code's job if you use live watch or when stopping at a breakpoint. As long as you know what is happening in the chip and on the debug tool, you'll be clear head to debug.

Now, some MEMs boot in SPI 3 wire mode. check them.

SPI full duplex mode is the one I mostly prefer as you have SPI SK generation under full SW control.

Either write first the MEMs control register to switch to 4 wire mode so you can read correct data, or implement 3 wire by shorting MOSI and MISO outside the MCU.

If you do so, remember to disable MOSI when reading and activate to write... it's simple play with GPIO MODER

The MEMs have basic C drivers on STMicro github, try to use it. even implement SPI by SW Bitbang to move forward and validate the MEMs is ok first, then implement the HW SPI as next step.

TDK
Guru

SPI->DR is special. When you write to it, it sends data to be transmitted, and when you read from it, it reads incoming data.

You should not expect the following to work:

SPIx->DR = 0x67;
assert(SPIx->DR == 0x67); // <-- will likely fail

From the reference manual:

The data register is split into 2 buffers - one for writing (Transmit Buffer) and another one for

reading (Receive buffer). A write to the data register will write into the Tx buffer and a read

from the data register will return the value held in the Rx buffer.

If you feel a post has answered your question, please click "Accept as Solution".