Skip to main content
Tommino
Associate III
July 21, 2022
Solved

How to init SPI properly

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

 

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
July 22, 2022

> 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

20 replies

Tommino
TomminoAuthor
Associate III
July 22, 2022

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?

Andrew Neil
Super User
July 22, 2022

@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.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
waclawek.jan
Super User
July 22, 2022

> 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
TomminoAuthor
Associate III
July 25, 2022

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?

Andrew Neil
Super User
July 25, 2022

@Tommino​ "How is this prevented?"

There'll be a status bit that tells you when a transfer has completed.

0693W00000QM3UqQAL.png 

On this chip, you also have read & write FIFOs

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.