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

 

1 ACCEPTED SOLUTION

Accepted Solutions

> 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

View solution in original post

20 REPLIES 20

SPI1_SCK is PA5 and its MODER is set to 0b00 i.e. Input instead of 0b10 for AF.

Did not look further.

JW

Tommino
Senior

Hi @Community member​ 

thanks again.

I modified PA5 MODER to AF5 but there should be still something that blocks the SPI1..

I doublechecked the GPIO and SPI clock, the GPIOsettings and SPI1 settings but I cannot find what is still wrong. Can you give a look to the registers again?

The code is just about enabling this SPI1 in order to see the SPI1 clock coming out from my NUCLEOG474RE

GPIOA has clock enalbled0693W00000QLrrsQAD.pngSPI1 clock enabled0693W00000QLrytQAD.pngPA4 PA5 and PA6 all alternate function AF50693W00000QLruIQAT.pngSPI registers

0693W00000QLrx7QAD.png

So you see still no clock on PA5 when you write into SPI1->DR, e.g. in debugger? How do the SPI registers change after that write?

I'm not sure what's the behaviour of the internal NSS node if you set SSOE as you've done. Try setting both SSM and SSI.

JW

Tommino
Senior

I found that the only way to see the SCK line activated is to write

while (1){

SPI1->DR= something;

}

if I write directly the registers while debugging the status register is affected (RXNE bit) but I cannot see the SCK outputs (in nucleo G474RE PA5 is connected to LED 2 so it is easy to see)

0693W00000QLsVJQA1.png 

Maybe this is the right behaviour of the SPI interface?

I thought that after enabling it I would have seen the SCK line activated even if I am not receving data

@Tommino​ "I thought that after enabling it I would have seen the SCK line activated even if I am not receving data"

No: the clock is generated by sending stuff out on MOSI - this is inherent to the way SPI works.

 EDIT:

Correction - See below: this chip has a special 'receive-only' mode

@Tommino​ "without using HAL libraries"

I would suggest that you get it working with the HAL first - then you have a working example on which to base your bare-metal implementation.

Tommino
Senior

Hi @Andrew Neil​ 

But what if the uC needs only to receive data on MISO and also be the master of the communication? The uC shall generate the clock...

First step with HAL done

Ah, you're right - this one has a special 'receive-only' mode:

0693W00000QLsxmQAD.png 

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