cancel
Showing results for 
Search instead for 
Did you mean: 

How to init SPI properly

Tommino
Senior

Hi,

I am trying to init SPI1 (to just receive data qwith MISO) on nucleo G474RE without using HAL libraries.

Can you help me to assess why my code does not work ?

I can't see the clock eventhough SPI1 is enabled


_legacyfs_online_stmicro_images_0693W00000QLmdH.png
_legacyfs_online_stmicro_images_0693W00000QLmdR.png 

void SPI1_init(){
	RCC-> APB2ENR|=(1 AHB2ENR |= (1MODER &= ~(1 AFR[0] |= (1 AFR[0] |= (1 AFR[0] |= (1 CR1 |= (1 CR1 |= (1 CR1 |= (1 CR2 |=(1CR2 |= (1CR1|= (1CR1|= (1CR1&= ~(1

 

20 REPLIES 20

So have you set the RXONLY bit?

Tommino
Senior

Thanks @Andrew Neil​ 

It was probably the RXONLY bit causing the issue. Now it is fine.

@Tommino​ "Now it is fine"

Good to hear - now please mark the solution:

0693W000008y9fZQAQ.png

> Again, I would suggest starting with HAL to see how that does it, then base your bare-metal on that ...

I would recommend completely forget about existence of Cube/HAL, first. Then read the fine manuals, thoroughly.

The Rx-only mode is being matter of grief, especially for those who click, as it requires to know what one is doing.

JW

Tommino
Senior

Hi @Community member​ ,

Thanks for your post in the blog.

What I understood is that you recommend to not connect the MOSI pin through AF if not needed. But then who controls when the clock is output on SCK? the SPI enable?

SCK does not depend on MOSI.

SPI in its most basic form are two shift registers - one for output one for input - and some control logic to generate clocks.

If SPI is set as master and enabled, and then you write to DR, the control logic generates 8 or 16 (or whatever you set) clocks on SPI, and using those clocks it shifts out data from output shift register to MOSI and shifts in data into input shift register from MISO.

If you don't assign any pin to MOSI in GPIO, well simply the internal MOSI output from SPI module is connected nowhere and the shifted out data are lost. If you don't assign any pin to MISO in GPIO, the internal MISO input to SPI module is set to 0, so you will simply receive all zeros.

The mode which is set by RXONLY bit is already an extension to this basic model - if you set that bit, as soon as SPI is enabled, the control logic starts to generate clocks and it won't stop until SPI is disabled.

Tommino
Senior

Ok I see

But basically in this way you have to write to DR eventhough you have just to receive data. Is this what you recommend?

Can you write the code?

> But basically in this way you have to write to DR eventhough you have just to receive data. Is this what you recommend?

Yes. What's wrong with this?

> Can you write the code?

Read N Bytes:
for N bytes
  write byte to SPI data register
  wait until RXNE is set in status register
  read byte from SPI data register
end for
  

If you want to work with bytes rather than halfwords, you may need to think about data packing (depends on STM32 model, read RM).

JW

@Tommino​ "in this way you have to write to DR even though you have just to receive data"

That's the way you'd do it "in general" - for microcontrollers that don't have this special 'read only' mode.

ok thanks. I tried it and it works.

I was afraid of writing to SPI DR before reading it beacuse in some way I thought I could have readback what I wrote. How is this prevented?