cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F051 SPI1 Strange behaviour

ArturasV
Senior
Posted on November 02, 2015 at 22:52

My problem is that i am not able to write to SPI1->DR. I have a LIS2DH accelerometer on SPI1 and i am able to read �Who_Am_I'' register and it returns correct value. However when i try to read any other register value, i always read 0xFF. I noticed, that whatever i am trying to write to SPI1->DR register, result is always 0xFF in DR. Simple code writing to SPI1->DR :

if ((SPI1->SR & SPI_SR_TXE) == SPI_SR_TXE) /* Test Tx empty */

{

/* Will inititiate 8-bit transmission if TXE */

*(uint8_t *)&(SPI1->DR) = 0x20;

}

After this line of code goes through, i am seeing 0xFF in SPI1->DR. Does anyone can help me to solve this out??

My SPI Configuration: (Yes, clock is enabled elsewhere.)

void spi1_config(void)

{

SPI_I2S_DeInit(SPI1); // DeInit SPI to the reset state.

/* Configure SPI driver */

spi1_struct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // Use full duplex (receive and transmit)

spi1_struct.SPI_Mode = SPI_Mode_Master; // MCU is a master

spi1_struct.SPI_DataSize = SPI_DataSize_8b; // Use 8 bit size data

spi1_struct.SPI_CPOL = SPI_CPOL_High;

spi1_struct.SPI_CPHA = SPI_CPHA_2Edge;

spi1_struct.SPI_NSS = SPI_NSS_Soft; // Control Chip select pin (CS) from software

spi1_struct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // SPI Clock Psc

spi1_struct.SPI_FirstBit = SPI_FirstBit_MSB; // First bit send is MSB (required by LIS2DH)

spi1_struct.SPI_CRCPolynomial = 7; // Not Relevant parameter

/* Initialize SPI1 peripheral */

SPI_Init(SPI1, &spi1_struct);

/* Enable SPI1 peripheral */

SPI_Cmd(SPI1, ENABLE);

}

#spi1 #problem #stm32f051
4 REPLIES 4
ArturasV
Senior
Posted on November 02, 2015 at 23:14

I guess i did something bad with configuration, because i just tried SPI2 and it is acting exactly the same. Any write to SPI2->DR results in SPI2->DR = 0xFF.

Posted on November 02, 2015 at 23:26

The DR in SPI (and USART) is NOT a single register - two registers are located at the same address, one is the transmit register and it is write only, and the other is receive register which is read only.

Thus, you cannot ''read back'' what you've written into it.

JW

ArturasV
Senior
Posted on November 02, 2015 at 23:40

You want to say i cann'ot see in SPI->DR what i am writing to this register? The RM states: ''A write access to the SPIx_DR stores the written data in the TXFIFO at the end of a send queue.''. It looks strange to me if immediately after writing to SPI->DR i cannot see the written value in it. If it is true, whats the common way to check that i am sending out the data correclty? Oscilloscope or logic analyzer? Or is there a software way?

Thanks for your answer, Regards.

Posted on November 03, 2015 at 03:08

You shouldn't think of peripheral registers as memory, you are pushing data into the logic of the machine, there's no reason for it to give it back to you, that would just take a lot more unnecessary logic and transistors.

Think of it like sticking a letter in the mail box at the post office, you don't get to see it again, but it gets to where it's going. You could ask the staff to dig it out and give it back to you, but they will likely scowl at you and say no.

If you want to monitor the data on the wire, you'd use a scope or analyzer, or wire/loop it back to some other port.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..